costFormAddEdit.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <el-dialog
  3. v-loading="loading"
  4. :title="title"
  5. :center="true"
  6. align="left"
  7. top="12vh"
  8. width="500px"
  9. :close-on-click-modal="false"
  10. :visible.sync="showModelThis"
  11. element-loading-text="拼命加载中"
  12. element-loading-spinner="el-icon-loading"
  13. element-loading-background="rgba(0, 0, 0, 0.8)"
  14. @close="showModelThis = false"
  15. >
  16. <el-card style="margin: -20px 0 0 0">
  17. <el-row :gutter="10">
  18. <el-col :span="24">
  19. <el-form
  20. ref="ruleForm"
  21. :model="ruleForm"
  22. status-icon
  23. :size="'mini'"
  24. :rules="rulesThis"
  25. label-width="80px"
  26. >
  27. <el-form-item label="预算单价" prop="budget_price">
  28. <digital-input
  29. :values="ruleForm.budget_price"
  30. :placeholder="'预算单价'"
  31. :min="0"
  32. :max="100000000000"
  33. :position="'right'"
  34. :precision="2"
  35. :size="'mini'"
  36. :controls="false"
  37. :append="'元'"
  38. @reschange="budget_price_change"
  39. />
  40. </el-form-item>
  41. <el-form-item label="购买数量" prop="num">
  42. <digital-input
  43. :values="ruleForm.num"
  44. :placeholder="'购买数量'"
  45. :min="0"
  46. :max="100000000000"
  47. :position="'right'"
  48. :precision="0"
  49. :size="'mini'"
  50. :controls="false"
  51. :append="''"
  52. @reschange="num_change"
  53. />
  54. </el-form-item>
  55. <el-form-item label="商品类型" prop="good_type">
  56. <el-select
  57. style="width: 100%"
  58. v-model="ruleForm.good_type"
  59. placeholder="商品类型"
  60. @change="good_type_change"
  61. >
  62. <el-option
  63. v-for="item in options"
  64. :key="item.value"
  65. :label="item.label"
  66. :value="item.value"
  67. >
  68. </el-option>
  69. </el-select>
  70. </el-form-item>
  71. <el-form-item label="商品分类" prop="cat_id">
  72. <good-class
  73. :value="ruleForm.cat_id"
  74. @handleChange="goods_class_change"
  75. :disabled="false"
  76. :size="'mini'"
  77. :isDetail="false"
  78. :placeholder="'商品分类'"
  79. />
  80. </el-form-item>
  81. <el-form-item label="商品名称" prop="good_name">
  82. <el-input
  83. placeholder="商品名称"
  84. v-model="ruleForm.good_name"
  85. maxlength="100"
  86. >
  87. </el-input>
  88. </el-form-item>
  89. <el-form-item label="商品图片" prop="good_img">
  90. <div v-if="ruleForm.good_img" class="clearfix">
  91. <img
  92. style="width: 84px; height: 84px"
  93. :src="ruleForm.good_img"
  94. class="avatar fl"
  95. />
  96. <el-link
  97. :underline="false"
  98. @click="deleteUrl()"
  99. type="warning"
  100. style="margin: 0 0 0 16px"
  101. >删除</el-link
  102. >
  103. </div>
  104. <div class="activity-upload" v-else>
  105. <div class="btnupload" style="position: relative">
  106. <i class="el-icon-plus avatar-uploader-icon"></i>
  107. <file-upload
  108. class="Upload"
  109. :disabled="type === 'view' || type === 'editCoin'"
  110. :accept="'.jpg,.png,.jpeg'"
  111. :multiple="false"
  112. :uploadcondition="beforeAvatarUpload"
  113. @UploadErrorEvent="UploadErrorEventproof_url"
  114. @UploadSuccessEvent="UploadSuccessEventproof_url"
  115. ></file-upload>
  116. </div>
  117. <div class="txt-tips fl">
  118. <p>建议大小:小于1MB</p>
  119. <p>文件格式:.jpg,.png,.jpeg</p>
  120. </div>
  121. </div>
  122. </el-form-item>
  123. </el-form>
  124. </el-col>
  125. <el-col :span="24" style="text-align: right">
  126. <el-button :size="'small'" type="primary" @click="submitForm"
  127. >保 存
  128. </el-button>
  129. <el-button @click="showModelThis = false" :size="'small'">{{
  130. "取 消"
  131. }}</el-button>
  132. </el-col>
  133. </el-row>
  134. </el-card>
  135. </el-dialog>
  136. </template>
  137. <script>
  138. import resToken from "@/mixins/resToken";
  139. import asyncRequest from "@/apis/service/sellOut/project";
  140. export default {
  141. name: "brand",
  142. props: ["showModel", "index", "sitem"],
  143. mixins: [resToken],
  144. data() {
  145. const validate_min_num = (rule, value, callback) => {
  146. if (value === "") {
  147. callback(new Error("购买数量不能为空!"));
  148. } else if (value === "0") {
  149. callback(new Error("购买数量不能为零!"));
  150. } else {
  151. callback();
  152. }
  153. };
  154. const validate_sale_price = (rule, value, callback) => {
  155. if (value === "") {
  156. callback(new Error("预算单价不能为空!"));
  157. } else if (
  158. value === "0" ||
  159. value === "0." ||
  160. value === "0.0" ||
  161. value === "0.00"
  162. ) {
  163. callback(new Error("预算单价不能为零!"));
  164. } else {
  165. callback();
  166. }
  167. };
  168. return {
  169. loading: false,
  170. title: "",
  171. options: [
  172. { value: "1", label: "竞品" },
  173. { value: "2", label: "竞聘" },
  174. ],
  175. showModelThis: this.showModel,
  176. ruleForm: {
  177. id: "",
  178. index: -1,
  179. good_type: "1",
  180. good_name: "",
  181. good_img: "",
  182. cat_id: [],
  183. budget_price: "",
  184. num: "",
  185. },
  186. rulesThis: this.rules,
  187. rules: {
  188. good_type: [
  189. { required: true, message: "请选择商品类型", trigger: "change" },
  190. ],
  191. good_name: [
  192. { required: false, message: "商品名称不能为空", trigger: "blur" },
  193. ],
  194. good_img: [
  195. { required: false, message: "商品图片不能为空", trigger: "change" },
  196. ],
  197. cat_id: [
  198. {
  199. type: "array",
  200. required: true,
  201. message: "请选择商品分类",
  202. trigger: "change",
  203. },
  204. ],
  205. budget_price: [
  206. { required: true, validator: validate_sale_price, trigger: "blur" },
  207. ],
  208. num: [{ required: true, validator: validate_min_num, trigger: "blur" }],
  209. },
  210. };
  211. },
  212. watch: {
  213. showModel: function (val) {
  214. this.showModelThis = val;
  215. if (val) {
  216. this.initForm();
  217. }
  218. },
  219. showModelThis(val) {
  220. if (!val) {
  221. this.$emit("cancel");
  222. }
  223. },
  224. },
  225. methods: {
  226. async initForm() {
  227. this.loading = true;
  228. this.rulesThis = this.rules;
  229. console.log(this.index);
  230. if (this.index + "" === "-1") {
  231. this.title = "添加商品要求";
  232. } else {
  233. this.title = "修改商品要求";
  234. }
  235. await this.resetForm();
  236. this.loading = false;
  237. },
  238. //商品分类选择
  239. goods_class_change(e) {
  240. this.ruleForm.cat_id = e;
  241. this.$refs.ruleForm.validateField("cat_id");
  242. },
  243. deleteUrl() {
  244. this.ruleForm.good_img = "";
  245. this.$refs.ruleForm.validateField("good_img");
  246. },
  247. good_type_change() {
  248. const { good_type } = this.ruleForm;
  249. if (good_type === "1") {
  250. this.rulesThis.cat_id[0].required = true;
  251. } else {
  252. this.rulesThis.cat_id[0].required = false;
  253. }
  254. },
  255. //图片上传成功
  256. async UploadSuccessEventproof_url(data) {
  257. const { url } = data;
  258. if (url === "noToken") {
  259. await this.logout();
  260. } else {
  261. this.ruleForm.good_img = url;
  262. this.$refs.ruleForm.validateField("good_img");
  263. this.$message.success("图片上传成功!");
  264. }
  265. },
  266. //图片上传失败
  267. UploadErrorEventproof_url(res) {
  268. if (res !== "break") {
  269. this.$refs.ruleForm.validatefield("good_img");
  270. this.$message.error("图片上传失败!");
  271. }
  272. },
  273. //判断图片规格
  274. beforeAvatarUpload(file) {
  275. let isJPG = false;
  276. if (
  277. file.type === "image/jpg" ||
  278. file.type === "image/png" ||
  279. file.type === "image/jpeg"
  280. ) {
  281. isJPG = true;
  282. }
  283. const isLt2M = file.size / 1024 / 1024 < 1;
  284. if (!isJPG) {
  285. this.$message.error("图片格式不正确!");
  286. }
  287. if (!isLt2M) {
  288. this.$message.error("图片大小不能超过 1MB!");
  289. }
  290. return isJPG && isLt2M;
  291. },
  292. budget_price_change(e) {
  293. this.ruleForm.budget_price = e + "";
  294. this.$refs.ruleForm.validateField("budget_price");
  295. },
  296. num_change(e) {
  297. this.ruleForm.num = e + "";
  298. this.$refs.ruleForm.validateField("num");
  299. },
  300. async resetForm() {
  301. // 重置
  302. await this.$nextTick(async () => {
  303. if (this.$refs.ruleForm) {
  304. this.$refs.ruleForm.resetFields();
  305. this.$refs.ruleForm.clearValidate();
  306. await this.resetFormData();
  307. }
  308. });
  309. },
  310. async resetFormData() {
  311. const { id, cat_id, good_type, good_name, good_img, budget_price, num } =
  312. this.sitem;
  313. this.ruleForm = {
  314. id: id || "",
  315. index: this.index,
  316. good_type: good_type || "1",
  317. good_name: good_name || "",
  318. good_img: good_img || "",
  319. cat_id: cat_id ? [cat_id] : [],
  320. budget_price: budget_price || "0.00",
  321. num: num || "0",
  322. };
  323. this.good_type_change();
  324. },
  325. async submitForm() {
  326. await this.$refs.ruleForm.validate(async (valid) => {
  327. if (valid) {
  328. this.loading = true;
  329. let model = JSON.parse(JSON.stringify(this.ruleForm));
  330. const { cat_id } = model;
  331. let cat_id_name = "";
  332. let length = cat_id.length;
  333. for (let i = 0; i < length; i++) {
  334. const { code, data, message } = await asyncRequest.catinfo({id:cat_id[i]});
  335. if (code === 0) {
  336. const { cat_name } = data;
  337. cat_id_name += i === 0 ? cat_name : `/${cat_name}`;
  338. } else if (code >= 100 && code <= 104) {
  339. await this.logout();
  340. } else {
  341. this.$message.warning(message);
  342. }
  343. }
  344. model.cat_name = cat_id_name;
  345. this.showModelThis = false;
  346. this.$emit("refresh", model);
  347. } else {
  348. console.log("error submit!!");
  349. return false;
  350. }
  351. });
  352. },
  353. },
  354. };
  355. </script>
  356. <style lang="scss" scoped>
  357. .brand {
  358. }
  359. </style>