index.vue 14 KB

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