addEdit.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <template>
  2. <el-dialog
  3. v-loading="loading"
  4. :title="title"
  5. :center="true"
  6. align="left"
  7. top="5vh"
  8. width="600px"
  9. append-to-body
  10. :close-on-click-modal="false"
  11. :visible.sync="showModelThis"
  12. element-loading-text="拼命加载中"
  13. element-loading-spinner="el-icon-loading"
  14. element-loading-background="rgba(0, 0, 0, 0.8)"
  15. @close="showModelThis = false"
  16. >
  17. <el-card style="margin: -20px 0 0 0">
  18. <el-row :gutter="10">
  19. <el-col :span="24">
  20. <el-form
  21. ref="ruleForm"
  22. :model="ruleForm"
  23. status-icon
  24. :rules="rulesThis"
  25. :size="'mini'"
  26. label-width="90px"
  27. class="demo-ruleForm"
  28. >
  29. <el-form-item label="支持品牌" prop="brand_id">
  30. <search-brand
  31. :value="ruleForm.brand_id"
  32. :size="'mini'"
  33. :disabled="type === 'view'"
  34. :isDetail="type !== 'add'"
  35. :names="brand_name"
  36. :placeholder="'商品品牌'"
  37. @searchChange="brandidsearchChange"
  38. />
  39. </el-form-item>
  40. <el-form-item label="授权类型" prop="long">
  41. <el-radio-group :disabled="type === 'view'" v-model="ruleForm.long">
  42. <el-radio :label="'0'">长期</el-radio>
  43. <el-radio :label="'1'">非长期</el-radio>
  44. </el-radio-group>
  45. </el-form-item>
  46. <el-form-item label="授权时间" style="margin: 0" required="true">
  47. <el-col :span="11">
  48. <el-form-item prop="starttime">
  49. <el-date-picker
  50. type="date"
  51. :disabled="type === 'view'"
  52. placeholder="开始日期"
  53. v-model="ruleForm.starttime"
  54. :picker-options="pickerOptions1"
  55. style="width: 100%"
  56. value-format="yyyy-MM-dd"
  57. @change="timeChange"
  58. ></el-date-picker>
  59. </el-form-item>
  60. </el-col>
  61. <el-col class="line tc" :span="2">-</el-col>
  62. <el-col :span="11">
  63. <el-form-item prop="endtime" v-if="ruleForm.long === '1'">
  64. <el-date-picker
  65. type="date"
  66. :disabled="type === 'view'"
  67. placeholder="结束日期"
  68. value-format="yyyy-MM-dd"
  69. v-model="ruleForm.endtime"
  70. :picker-options="pickerOptions2"
  71. style="width: 100%"
  72. @change="timeChange"
  73. ></el-date-picker>
  74. </el-form-item>
  75. <p style="font-size:16px;font-weight:700" v-else>长期有效</p>
  76. </el-col>
  77. </el-form-item>
  78. <el-col :span="24">
  79. <el-form-item
  80. label="授权图片"
  81. prop="brand_book"
  82. :disabled="id == '007'"
  83. class="activity-upload"
  84. >
  85. <template v-if="ruleForm.brand_book && ruleForm.brand_book.length > 0">
  86. <div
  87. class="btnupload"
  88. style="position: relative;"
  89. v-for="(url,index) in ruleForm.brand_book"
  90. :key="url"
  91. >
  92. <img :src="url" class="avatar" />
  93. <i v-if="type !== 'view'" class="el-icon-close" @click="ruleForm.brand_book.splice(index,1)" />
  94. </div>
  95. </template>
  96. <div class="btnupload" style="position: relative;" v-if="type !== 'view'">
  97. <i class="el-icon-plus avatar-uploader-icon" />
  98. <file-upload
  99. class="Upload"
  100. :multiple="true"
  101. :accept="'.jpg,.png,.jpeg'"
  102. :disabled="type === 'view'"
  103. @UploadErrorEvent="UploadErrorEvent"
  104. :uploadcondition="beforeAvatarUpload"
  105. @UploadSuccessEvent="UploadSuccessEvent"
  106. ></file-upload>
  107. </div>
  108. </el-form-item>
  109. </el-col>
  110. <el-col :span="24">
  111. <el-form-item label="备注" prop="remark">
  112. <el-input :disabled="type === 'view'" type="textarea" v-model="ruleForm.remark" placeholder="备注" />
  113. </el-form-item>
  114. </el-col>
  115. <el-col :span="24" style="text-align: right;">
  116. <el-button
  117. v-if="type !== 'view'"
  118. :size="'small'"
  119. type="primary"
  120. @click="submitForm"
  121. >保 存</el-button>
  122. <el-button @click="showModelThis = false" :size="'small'">
  123. {{
  124. type === "view" ? "关 闭" : "取 消"
  125. }}
  126. </el-button>
  127. </el-col>
  128. </el-form>
  129. </el-col>
  130. </el-row>
  131. </el-card>
  132. </el-dialog>
  133. </template>
  134. <script>
  135. import asyncRequest from "@/apis/service/serviceParam/supplierBrand";
  136. import { hasSpace } from "@/utils/validate";
  137. import resToken from "@/mixins/resToken";
  138. import dayjs from 'dayjs';
  139. export default {
  140. name: "brand",
  141. props: ["showModel", "id", "type", "sitem"],
  142. mixins: [resToken],
  143. data() {
  144. return {
  145. loading: false,
  146. title: "添加支持品牌",
  147. showModelThis: this.showModel,
  148. brand_name: "",
  149. ruleForm: {},
  150. rulesThis: this.rules,
  151. pickerOptions1: {
  152. disabledDate: time => {
  153. if (
  154. this.ruleForm.endtime != null &&
  155. this.ruleForm.endtime != "" &&
  156. time
  157. ) {
  158. return time.getTime() > new Date(this.ruleForm.endtime).valueOf();
  159. }
  160. }
  161. },
  162. pickerOptions2: {
  163. disabledDate: time => {
  164. if (
  165. this.ruleForm.starttime != null &&
  166. this.ruleForm.starttime != "" &&
  167. time
  168. ) {
  169. return time.getTime() < new Date(this.ruleForm.starttime).valueOf();
  170. }
  171. }
  172. },
  173. rules: {
  174. gyscode: [
  175. {
  176. required: true,
  177. message: "供应商不能为空",
  178. trigger: "change"
  179. }
  180. ],
  181. brand_id: [
  182. {
  183. type: "array",
  184. required: true,
  185. message: "请选择支持品牌",
  186. trigger: "blur"
  187. }
  188. ],
  189. long: [
  190. { required: true, message: "请选择授权类型", trigger: "change" }
  191. ],
  192. starttime: [
  193. { required: true, message: "请选择授权开始日期", trigger: "blur" }
  194. ],
  195. endtime: [
  196. { required: true, message: "请选择授权结果日期", trigger: "blur" }
  197. ]
  198. }
  199. };
  200. },
  201. watch: {
  202. // "ruleForm.long": function(val) {
  203. // this.rulesThis.long[0].required = Number(val) !== 0;
  204. // },
  205. showModel: function(val) {
  206. this.showModelThis = val;
  207. if (val) {
  208. this.initForm();
  209. }
  210. },
  211. showModelThis(val) {
  212. if (!val) {
  213. this.$emit("cancel");
  214. }
  215. }
  216. },
  217. methods: {
  218. async initForm() {
  219. this.loading = true;
  220. this.brand_name = "";
  221. this.resetFormData();
  222. if (this.type === "add") {
  223. this.rulesThis = this.rules;
  224. this.title = "添加支持品牌";
  225. await this.resetForm();
  226. } else if (this.type === "edit") {
  227. this.rulesThis = this.rules;
  228. this.title = "修改支持品牌";
  229. await this.resetForm();
  230. await this.initData();
  231. } else {
  232. this.rulesThis = {};
  233. this.title = "查看支持品牌";
  234. await this.resetForm();
  235. await this.initData();
  236. }
  237. this.loading = false;
  238. },
  239. async resetForm() {
  240. // 重置
  241. await this.$nextTick(() => {
  242. if (this.$refs.ruleForm) {
  243. this.$refs.ruleForm.resetFields();
  244. this.$refs.ruleForm.clearValidate();
  245. this.resetFormData();
  246. }
  247. });
  248. },
  249. timeChange() {
  250. if (
  251. this.ruleForm.starttime !== "" &&
  252. this.ruleForm.starttime !== null &&
  253. this.ruleForm.endtime !== "" &&
  254. this.ruleForm.endtime !== null
  255. ) {
  256. if (
  257. new Date(this.ruleForm.endtime).valueOf() <
  258. new Date(this.ruleForm.starttime).valueOf()
  259. ) {
  260. this.$message.warning("结束时间不大于开始时间!");
  261. this.ruleForm.endtime = "";
  262. this.ruleForm.starttime = "";
  263. this.$refs.ruleForm.validateField("starttime");
  264. this.$refs.ruleForm.validateField("endtime");
  265. } else {
  266. this.$refs.ruleForm.validateField("starttime");
  267. this.$refs.ruleForm.validateField("endtime");
  268. }
  269. } else {
  270. this.$refs.ruleForm.validateField("starttime");
  271. this.$refs.ruleForm.validateField("endtime");
  272. }
  273. },
  274. resetFormData() {
  275. this.ruleForm = {
  276. id: "",
  277. remark:"",
  278. gyscode: this.sitem,
  279. brand_id: [],
  280. long: "1",
  281. starttime: "",
  282. endtime: "",
  283. brand_book: []
  284. };
  285. },
  286. async initData() {
  287. this.loading = true;
  288. const { code, message, data } = await asyncRequest.detail({
  289. id: this.id
  290. });
  291. this.loading = false;
  292. if (code === 0) {
  293. const {
  294. id,
  295. brand_book,
  296. brand_id,
  297. brand_name,
  298. long,
  299. starttime,
  300. endtime,
  301. gyscode,
  302. remark
  303. } = data;
  304. this.brand_name = brand_name;
  305. this.ruleForm = {
  306. id: id || "",
  307. gyscode: gyscode,
  308. brand_id: brand_id ? [brand_id] : [],
  309. long: long || "",
  310. starttime: starttime || "",
  311. endtime: endtime || "",
  312. brand_book: brand_book ? brand_book.split(",") : [],
  313. remark: remark || ""
  314. };
  315. } else if (code >= 100 && code <= 104) {
  316. await this.logout();
  317. } else {
  318. this.$message.warning(message);
  319. }
  320. },
  321. async submitForm() {
  322. await this.$refs.ruleForm.validate(async valid => {
  323. if (valid) {
  324. if (this.loading) return;
  325. this.loading = true;
  326. let model = JSON.parse(JSON.stringify(this.ruleForm));
  327. model.brand_id = model.brand_id.toString();
  328. model.brand_book = model.brand_book.join(",");
  329. model.status = "1";
  330. if(model.starttime){
  331. model.starttime = dayjs(model.starttime).format("YYYY-MM-DD HH:mm:ss");
  332. }
  333. if(model.endtime){
  334. model.endtime = dayjs(model.endtime).format("YYYY-MM-DD HH:mm:ss");
  335. }
  336. if(String(model.long) === "0"){
  337. delete model.endtime
  338. }
  339. let res = {};
  340. if (this.type === "add") {
  341. delete model["id"];
  342. res = await asyncRequest.add(model);
  343. } else {
  344. res = await asyncRequest.update(model);
  345. }
  346. this.loading = false;
  347. const { code, data, message } = res;
  348. if (code === 0) {
  349. const title = this.type === "add" ? "添加成功!" : "修改成功!";
  350. this.$notify.success({
  351. title,
  352. message: ""
  353. });
  354. this.showModelThis = false;
  355. // 刷新
  356. this.$emit("refresh");
  357. } else if (code >= 100 && code <= 104) {
  358. await this.logout();
  359. } else {
  360. this.$message.warning(message);
  361. }
  362. } else {
  363. console.log("error submit!!");
  364. return false;
  365. }
  366. });
  367. },
  368. //商品品牌选择
  369. brandidsearchChange(e) {
  370. const { id } = e;
  371. this.ruleForm.brand_id = id ? [id] : [];
  372. this.$refs.ruleForm.validateField("brand_id");
  373. },
  374. //判断图片规格
  375. beforeAvatarUpload(file) {
  376. let isJPG = false,
  377. isLt2M = false;
  378. if (file) {
  379. if (
  380. file.type === "image/jpg" ||
  381. file.type === "image/png" ||
  382. file.type === "image/jpeg"
  383. ) {
  384. isJPG = true;
  385. }
  386. isLt2M = file.size / 1024 / 1024 < 1;
  387. if (!isJPG) {
  388. this.$message.error("图片格式不正确!");
  389. }
  390. if (!isLt2M) {
  391. this.$message.error("图片大小不能超过 1MB!");
  392. }
  393. }
  394. return isJPG && isLt2M;
  395. },
  396. //图片上传失败
  397. UploadErrorEvent(res) {
  398. if (res !== "break") {
  399. this.$message.error("图片上传失败!");
  400. this.ruleForm.brand_book = [];
  401. this.$refs.ruleForm.validateField("brand_book");
  402. }
  403. },
  404. //图片上传成功
  405. async UploadSuccessEvent(data) {
  406. const { url } = data;
  407. if (url === "noToken") {
  408. await this.logout();
  409. } else {
  410. console.log(this.ruleForm.brand_book);
  411. this.ruleForm.brand_book.push(url);
  412. this.$message.success("图片上传成功!");
  413. this.$refs.ruleForm.validateField("brand_book");
  414. }
  415. }
  416. }
  417. };
  418. </script>
  419. <style lang="scss" scoped>
  420. .btnupload {
  421. margin-left: 5px !important;
  422. margin-bottom: 5px !important;
  423. .el-icon-close {
  424. position: absolute;
  425. top: 5px;
  426. right: 5px;
  427. cursor: pointer;
  428. &:hover {
  429. color: #409eff;
  430. }
  431. }
  432. }
  433. </style>