index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <div class="wrapper">
  3. <div class="title">{{title}}</div>
  4. <el-form class="form" ref="form" :model="form" label-width="80px" :rules="rules">
  5. <el-form-item class="single" label="商品名:" label-width="120px" prop="title">
  6. <el-input class="pr-50" placeholder="请输入" v-model="form.title" maxlength="60" :disabled="isdisable"
  7. show-word-limit>
  8. </el-input>
  9. </el-form-item>
  10. <el-form-item label="类目:" label-width="120px" prop="third_cat_id">
  11. <el-select v-model="form.third_cat_id" placeholder="请选择" :disabled="isdisable" clearable filterable>
  12. <el-option v-for="item in categoryList" :key="item.level3" :label="item.level3_name"
  13. :value="item.level3">
  14. </el-option>
  15. <el-option label="无需审核类目" value="-1"></el-option>
  16. </el-select>
  17. </el-form-item>
  18. <el-form-item label="商品ID:" label-width="100px" prop="out_product_id">
  19. <el-input placeholder="请输入" :disabled="isdisable||outproductidDisable" v-model="form.out_product_id" maxlength="9" show-word-limit>
  20. </el-input>
  21. </el-form-item>
  22. <el-form-item label="品牌:" label-width="120px" prop="brand_id">
  23. <el-select v-model="form.brand_id" :disabled="isdisable" placeholder="请选择" clearable filterable>
  24. <el-option v-for="item in brandList" :key="item.brand_info_id" :label="item.brand_wording"
  25. :value="item.brand_info_id">
  26. </el-option>
  27. <el-option label="无品牌" value="-1"></el-option>
  28. </el-select>
  29. </el-form-item>
  30. <el-form-item label="商品库存:" label-width="100px" prop="stock_num">
  31. <el-input placeholder="请输入" :disabled="isdisable" v-model="form.stock_num" maxlength="6" show-word-limit>
  32. </el-input>
  33. </el-form-item>
  34. <el-form-item label="售卖价格:" label-width="120px" prop="sale_price">
  35. <el-input placeholder="请输入" :disabled="isdisable" maxlength="9" show-word-limit
  36. v-model="form.sale_price">
  37. </el-input>
  38. </el-form-item>
  39. <el-form-item label="市场价格:" label-width="120px" prop="market_price">
  40. <el-input placeholder="请输入" :disabled="isdisable" maxlength="9" show-word-limit
  41. v-model="form.market_price">
  42. </el-input>
  43. </el-form-item>
  44. <el-form-item class="single" label="小程序链接:" label-width="120px" prop="path">
  45. <el-input class="pr-50" :disabled="isdisable" placeholder="请输入" v-model="form.path" maxlength="100" show-word-limit>
  46. </el-input>
  47. </el-form-item>
  48. <el-form-item label="三级类目:" v-if="form.third_cat_id=='-1'" label-width="120px"
  49. :rules="[{ required: form.third_cat_id=='-1', message: '请输入必填项', trigger: 'blur' }]">
  50. <el-input placeholder="请输入" :disabled="isdisable" type='number' oninput="if(value.length>8)value=value.slice(0,8)" v-model="form.third">
  51. </el-input>
  52. </el-form-item>
  53. <el-form-item class="single" label="商品图片:" label-width="120px" required>
  54. <upload v-if="updatePic" acceptType='.jpg, .jpeg, .png' :disabled="isdisable" ref="upload_goods" :file="goodsList" limitNum="1">
  55. </upload>
  56. </el-form-item>
  57. <el-form-item class="single" label="商品资质图片:" label-width="120px">
  58. <upload v-if="updatePic" acceptType='.jpg, .jpeg, .png' :disabled="isdisable" ref="upload_zizhi" :file="goodszizhiList" limitNum="10">
  59. </upload>
  60. </el-form-item>
  61. <el-form-item class="commit" v-if="!isdisable">
  62. <el-button class="blackbtn" :loading="loading" @click.native.prevent="commit">提交
  63. </el-button>
  64. </el-form-item>
  65. </el-form>
  66. </div>
  67. </template>
  68. <script>
  69. import store from '@/store/index'
  70. import upload from '@/components/upload'
  71. import {
  72. validnumber,
  73. validPrice
  74. } from '@/utils/validate'
  75. import {
  76. add,
  77. editDetail,
  78. edit,
  79. authDetail
  80. } from "@/api/goods";
  81. import {
  82. getCategoryList
  83. } from '@/api/category'
  84. import {
  85. getBrandList
  86. } from '@/api/brand'
  87. export default {
  88. name: 'addGoods',
  89. components: {
  90. upload
  91. },
  92. props: {},
  93. data() {
  94. return {
  95. title:'新增商品',
  96. outproductidDisable:false,
  97. categoryList: [],
  98. brandList: [],
  99. goodsList: [],
  100. goodszizhiList: [],
  101. outProductId: 0,
  102. updatePic: true,
  103. commodity_id: 0,
  104. isdisable: false,
  105. form: {
  106. title: '',
  107. third_cat_audit: '',
  108. out_product_id: '',
  109. has_brand: '',
  110. stock_num: '',
  111. sale_price: '',
  112. market_price: '',
  113. path: '',
  114. third_cat_id: '',
  115. third: '',
  116. brand_id: '',
  117. },
  118. loading: false,
  119. rules: {
  120. title: [{
  121. required: true,
  122. message: '请输入必填项',
  123. trigger: 'blur'
  124. }],
  125. third_cat_id: [{
  126. required: true,
  127. message: '请选择必填项',
  128. trigger: 'blur'
  129. }],
  130. out_product_id: [{
  131. required: true,
  132. validator: validnumber,
  133. trigger: 'blur'
  134. }],
  135. brand_id: [{
  136. required: true,
  137. message: '请选择必填项',
  138. trigger: 'blur'
  139. }],
  140. stock_num: [{
  141. required: true,
  142. validator: validnumber,
  143. trigger: 'blur'
  144. }],
  145. sale_price: [{
  146. required: true,
  147. validator: validPrice,
  148. trigger: 'blur'
  149. }],
  150. market_price: [{
  151. required: true,
  152. validator: validPrice,
  153. trigger: 'blur'
  154. }],
  155. path: [{
  156. required: true,
  157. message: '请输入必填项',
  158. trigger: 'blur'
  159. }],
  160. },
  161. };
  162. },
  163. watch: {},
  164. computed: {},
  165. methods: {
  166. commit() {
  167. this.$refs.form.validate(valid => {
  168. if (valid) {
  169. if (this.goodsList.length == 0) {
  170. this.$message.error('请选择至少一张商品图片');
  171. return
  172. }
  173. let zizhiarray = []
  174. this.goodszizhiList.forEach(element => {
  175. zizhiarray.push(element.url)
  176. });
  177. let params = {
  178. brand_id: this.form.brand_id == -1 ? 2100000000 : this.form.brand_id,
  179. has_brand: this.form.brand_id == -1 ? 0 : 1,
  180. market_price: this.form.market_price,
  181. out_product_id: this.form.out_product_id,
  182. path: this.form.path,
  183. qualificationImgs: zizhiarray.join(","),
  184. sale_price: this.form.sale_price,
  185. stock_num: this.form.stock_num,
  186. third_cat_audit: this.form.third_cat_id == -1 ? 0 : 1,
  187. third_cat_id: this.form.third_cat_id == -1 ? this.form.third : this.form
  188. .third_cat_id,
  189. thumb_img: this.goodsList[0].url,
  190. title: this.form.title
  191. }
  192. this.loading = true
  193. if (this.outProductId == 0) { //新增
  194. add(params).then(() => {
  195. this.$message.success('添加成功');
  196. store.commit('REMOVETAB', '商品信息')
  197. this.$router.push('/goods')
  198. }).catch(() => {
  199. this.loading = false
  200. });
  201. } else { //修改
  202. params.commodity_id = this.commodity_id
  203. edit(params).then(() => {
  204. this.$message.success('修改成功');
  205. store.commit('REMOVETAB', '商品信息')
  206. store.commit('REMOVETAB', '商品详情')
  207. this.$router.push('/goods')
  208. }).catch(() => {
  209. this.loading = false
  210. });
  211. }
  212. }
  213. })
  214. },
  215. getDetail() {
  216. if (this.outProductId != 0) {
  217. authDetail({
  218. outProductId: this.outProductId
  219. }).then(res => {
  220. this.form.title = res.data.title
  221. this.form.out_product_id = res.data.out_product_id
  222. this.form.stock_num = res.data.stock_num
  223. this.form.sale_price = res.data.sale_price
  224. this.form.market_price = res.data.market_price
  225. this.form.path = res.data.path
  226. this.form.brand_id=res.data.has_brand==0?'-1':res.data.has_brand
  227. this.form.third=res.data.third_cat_audit==0?res.data.third_cat_id:''
  228. this.form.third_cat_id =res.data.third_cat_audit==0?'-1':res.data.third_cat_id
  229. this.commodity_id = res.data.commodity_id
  230. this.goodsList = [{
  231. url: res.data.thumb_img
  232. }]
  233. this.goodszizhiList = []
  234. if (res.data?.qualificationImgs?.length > 0) {
  235. res.data.qualificationImgs.forEach(element => {
  236. this.goodszizhiList.push({
  237. url: element
  238. })
  239. });
  240. }
  241. //刷新子组件
  242. this.updatePic = false
  243. this.$nextTick(() => {
  244. this.updatePic = true
  245. })
  246. })
  247. }
  248. },
  249. getCategoryList() { //获取类目列表
  250. getCategoryList().then(res => {
  251. this.categoryList = res.data
  252. })
  253. },
  254. getBrandList() { //获取品牌列表
  255. getBrandList().then(res => {
  256. this.brandList = res.data
  257. })
  258. },
  259. initPage() {
  260. this.loading = false
  261. this.$refs.form.clearValidate()
  262. if (this.$route.params.outProductId) { //新增获取详情
  263. this.outProductId = this.$route.params.outProductId
  264. this.title='修改商品'
  265. this.isdisable = false
  266. this.getDetail()
  267. if (this.$route.params.isDisable) {
  268. this.title='商品详情'
  269. this.isdisable = true
  270. }
  271. }
  272. if (this.$route.params.isNew) { //新增清空
  273. this.title='新增商品'
  274. this.isdisable = false
  275. this.outproductidDisable=false
  276. if(this.$route.params.data){
  277. this.title='新的审核内容'
  278. this.outproductidDisable=true
  279. let data=JSON.parse(this.$route.params.data)
  280. this.outProductId=data.out_product_id
  281. this.form.title=data.title
  282. this.commodity_id=data.commodity_id
  283. this.form.out_product_id=data.out_product_id
  284. this.form.stock_num=data.stock_num
  285. this.form.sale_price=data.sale_price
  286. this.form.market_price=data.market_price
  287. this.form.path=data.path
  288. this.form.third_cat_id=data.third_cat_id
  289. this.form.third=data.third
  290. this.form.brand_id=data.brand_id
  291. this.goodsList=data.goodsList
  292. this.goodszizhiList=data.goodszizhiList
  293. }else{
  294. this.outProductId = 0
  295. this.form = {
  296. title: '',
  297. third_cat_audit: '',
  298. out_product_id: '',
  299. has_brand: '',
  300. stock_num: '',
  301. sale_price: '',
  302. market_price: '',
  303. path: '',
  304. third_cat_id: '',
  305. third: '',
  306. brand_id: '',
  307. }
  308. this.goodsList = []
  309. this.goodszizhiList = []
  310. }
  311. //刷新子组件
  312. this.updatePic = false
  313. this.$nextTick(() => {
  314. this.updatePic = true
  315. })
  316. }
  317. }
  318. },
  319. created() {},
  320. activated() {
  321. this.initPage()
  322. this.getCategoryList()
  323. this.getBrandList()
  324. },
  325. mounted() {}
  326. };
  327. </script>
  328. <style lang="less" scoped>
  329. .wrapper {
  330. display: flex;
  331. flex-direction: column;
  332. align-items: center;
  333. .title {
  334. font-weight: bold;
  335. width: 90%;
  336. text-align: left;
  337. margin-left: 100px;
  338. }
  339. .form {
  340. margin-top: 20px;
  341. width: 75%;
  342. display: flex;
  343. justify-content: space-between;
  344. flex-wrap: wrap;
  345. .el-form-item {
  346. color: #232323;
  347. font-weight: 450;
  348. }
  349. .el-select {
  350. width: 300px;
  351. }
  352. .el-input {
  353. width: 300px;
  354. }
  355. .single {
  356. width: 100%;
  357. .el-input {
  358. width: 855px;
  359. }
  360. .el-select {
  361. width: 855px;
  362. }
  363. }
  364. .commit {
  365. width: 100%;
  366. text-align: center;
  367. }
  368. }
  369. }
  370. </style>