addEdit.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 v-if="ruleForm.long === '1'" label="授权时间" style="margin: 0">
  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">
  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. </el-col>
  76. </el-form-item>
  77. <el-col :span="17">
  78. <el-form-item
  79. label="授权图片"
  80. prop="brand_book"
  81. :disabled="id == '007'"
  82. class="activity-upload"
  83. >
  84. <div class="btnupload" style="position: relative">
  85. <img
  86. v-if="ruleForm.brand_book"
  87. :src="ruleForm.brand_book"
  88. class="avatar"
  89. />
  90. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  91. <file-upload
  92. class="Upload"
  93. :disabled="type === 'view'"
  94. :accept="'.jpg,.png,.jpeg'"
  95. :multiple="true"
  96. :uploadcondition="beforeAvatarUpload"
  97. @UploadErrorEvent="UploadErrorEvent"
  98. @UploadSuccessEvent="UploadSuccessEvent"
  99. ></file-upload>
  100. </div>
  101. </el-form-item>
  102. </el-col>
  103. <el-col :span="7" style="text-align: right; padding: 72px 0 0 0">
  104. <el-button
  105. v-if="type !== 'view'"
  106. :size="'small'"
  107. type="primary"
  108. @click="submitForm"
  109. >保 存
  110. </el-button>
  111. <el-button @click="showModelThis = false" :size="'small'">{{
  112. type === "view" ? "关 闭" : "取 消"
  113. }}</el-button>
  114. </el-col>
  115. </el-form>
  116. </el-col>
  117. </el-row>
  118. </el-card>
  119. </el-dialog>
  120. </template>
  121. <script>
  122. import asyncRequest from "@/apis/service/serviceParam/supplierBrand";
  123. import resToken from "@/mixins/resToken";
  124. export default {
  125. name: "brand",
  126. props: ["showModel", "id", "type", "sitem"],
  127. mixins: [resToken],
  128. data() {
  129. return {
  130. loading: false,
  131. title: "添加支持品牌",
  132. showModelThis: this.showModel,
  133. brand_name: "",
  134. ruleForm: {},
  135. rulesThis: this.rules,
  136. pickerOptions1: {
  137. disabledDate: (time) => {
  138. if (this.ruleForm.endtime != null && this.ruleForm.endtime != "" && time) {
  139. return time.getTime() > new Date(this.ruleForm.endtime).valueOf();
  140. }
  141. },
  142. },
  143. pickerOptions2: {
  144. disabledDate: (time) => {
  145. if (this.ruleForm.starttime != null && this.ruleForm.starttime != "" && time) {
  146. return time.getTime() < new Date(this.ruleForm.starttime).valueOf();
  147. }
  148. },
  149. },
  150. rules: {
  151. gyscode: [
  152. {
  153. required: true,
  154. message: "供应商不能为空",
  155. trigger: "change",
  156. },
  157. ],
  158. brand_id: [
  159. {
  160. type: "array",
  161. required: true,
  162. message: "请选择支持品牌",
  163. trigger: "blur",
  164. },
  165. ],
  166. long: [{ required: false, message: "请选择授权类型", trigger: "change" }],
  167. starttime: [{ required: false, message: "请选择授权开始日期", trigger: "blur" }],
  168. brand_book: [{ required: false, message: "授权图片不能为空", trigger: "change" }],
  169. },
  170. };
  171. },
  172. watch: {
  173. showModel: function (val) {
  174. this.showModelThis = val;
  175. if (val) {
  176. this.initForm();
  177. }
  178. },
  179. showModelThis(val) {
  180. if (!val) {
  181. this.$emit("cancel");
  182. }
  183. },
  184. },
  185. methods: {
  186. async initForm() {
  187. this.loading = true;
  188. this.brand_name = "";
  189. this.resetFormData();
  190. if (this.type === "add") {
  191. this.rulesThis = this.rules;
  192. this.title = "添加支持品牌";
  193. await this.resetForm();
  194. } else if (this.type === "edit") {
  195. this.rulesThis = this.rules;
  196. this.title = "修改支持品牌";
  197. await this.resetForm();
  198. await this.initData();
  199. } else {
  200. this.rulesThis = {};
  201. this.title = "查看支持品牌";
  202. await this.resetForm();
  203. await this.initData();
  204. }
  205. this.loading = false;
  206. },
  207. async resetForm() {
  208. // 重置
  209. await this.$nextTick(() => {
  210. if (this.$refs.ruleForm) {
  211. this.$refs.ruleForm.resetFields();
  212. this.$refs.ruleForm.clearValidate();
  213. this.resetFormData();
  214. }
  215. });
  216. },
  217. timeChange() {
  218. if (
  219. this.ruleForm.starttime !== "" &&
  220. this.ruleForm.starttime !== null &&
  221. this.ruleForm.endtime !== "" &&
  222. this.ruleForm.endtime !== null
  223. ) {
  224. if (
  225. new Date(this.ruleForm.endtime).valueOf() <
  226. new Date(this.ruleForm.starttime).valueOf()
  227. ) {
  228. this.$message.warning("结束时间不大于开始时间!");
  229. this.ruleForm.endtime = "";
  230. this.ruleForm.starttime = "";
  231. this.$refs.ruleForm.validateField("starttime");
  232. this.$refs.ruleForm.validateField("endtime");
  233. } else {
  234. this.$refs.ruleForm.validateField("starttime");
  235. this.$refs.ruleForm.validateField("endtime");
  236. }
  237. } else {
  238. this.$refs.ruleForm.validateField("starttime");
  239. this.$refs.ruleForm.validateField("endtime");
  240. }
  241. },
  242. resetFormData() {
  243. this.ruleForm = {
  244. id: "",
  245. gyscode: this.sitem,
  246. brand_id: [],
  247. long: "1",
  248. starttime: "",
  249. endtime: "",
  250. brand_book: "",
  251. };
  252. console.log(this.sitem);
  253. },
  254. async initData() {
  255. this.loading = true;
  256. const { code, message, data } = await asyncRequest.detail({
  257. id: this.id,
  258. });
  259. this.loading = false;
  260. if (code === 0) {
  261. const {
  262. id,
  263. brand_book,
  264. brand_id,
  265. brand_name,
  266. long,
  267. starttime,
  268. endtime,
  269. gyscode,
  270. } = data;
  271. this.brand_name = brand_name;
  272. this.ruleForm = {
  273. id: id || "",
  274. gyscode: gyscode,
  275. brand_id: brand_id ? [brand_id] : [],
  276. long: long || "",
  277. starttime: starttime || "",
  278. endtime: endtime || "",
  279. brand_book: brand_book || "",
  280. };
  281. } else if (code >= 100 && code <= 104) {
  282. await this.logout();
  283. } else {
  284. this.$message.warning(message);
  285. }
  286. },
  287. async submitForm() {
  288. await this.$refs.ruleForm.validate(async (valid) => {
  289. if (valid) {
  290. if (this.loading) return;
  291. this.loading = true;
  292. let model = JSON.parse(JSON.stringify(this.ruleForm));
  293. model.brand_id = model.brand_id.toString();
  294. // model.gyscode = model.gyscode.toString();
  295. let res = {};
  296. if (this.type === "add") {
  297. delete model["id"];
  298. res = await asyncRequest.add(model);
  299. } else {
  300. res = await asyncRequest.update(model);
  301. }
  302. this.loading = false;
  303. const { code, data, message } = res;
  304. if (code === 0) {
  305. const title = this.type === "add" ? "添加成功!" : "修改成功!";
  306. this.$notify.success({
  307. title,
  308. message: "",
  309. });
  310. this.showModelThis = false;
  311. // 刷新
  312. this.$emit("refresh");
  313. } else if (code >= 100 && code <= 104) {
  314. await this.logout();
  315. } else {
  316. this.$message.warning(message);
  317. }
  318. } else {
  319. console.log("error submit!!");
  320. return false;
  321. }
  322. });
  323. },
  324. //商品品牌选择
  325. brandidsearchChange(e) {
  326. const { id } = e;
  327. this.ruleForm.brand_id = id ? [id] : [];
  328. this.$refs.ruleForm.validateField("brand_id");
  329. },
  330. //判断图片规格
  331. beforeAvatarUpload(file) {
  332. let isJPG = false,
  333. isLt2M = false;
  334. if (file) {
  335. if (
  336. file.type === "image/jpg" ||
  337. file.type === "image/png" ||
  338. file.type === "image/jpeg"
  339. ) {
  340. isJPG = true;
  341. }
  342. isLt2M = file.size / 1024 / 1024 < 1;
  343. if (!isJPG) {
  344. this.$message.error("图片格式不正确!");
  345. }
  346. if (!isLt2M) {
  347. this.$message.error("图片大小不能超过 1MB!");
  348. }
  349. }
  350. return isJPG && isLt2M;
  351. },
  352. //图片上传失败
  353. UploadErrorEvent(res) {
  354. if (res !== "break") {
  355. this.$message.error("图片上传失败!");
  356. this.ruleForm.brand_book = "";
  357. this.$refs.ruleForm.validateField("brand_book");
  358. }
  359. },
  360. //图片上传成功
  361. async UploadSuccessEvent(data) {
  362. const { url } = data;
  363. if (url === "noToken") {
  364. await this.logout();
  365. } else {
  366. this.ruleForm.brand_book = url;
  367. this.$message.success("图片上传成功!");
  368. this.$refs.ruleForm.validateField("brand_book");
  369. }
  370. },
  371. },
  372. };
  373. </script>
  374. <style lang="scss" scoped>
  375. .brand {
  376. }
  377. </style>