businessGoodsEdit.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. const uploadJS = require('../../mixin/upload.js')
  2. const { postAddProduct, postModifyProduct } = require('./api/index')
  3. const { getProductCategoryList, getProductBrandList } = require('../../api/common')
  4. const { formatTs, yuan2Fen } = require('../../utils/util')
  5. const minDate = new Date().getTime()
  6. const objMinDate = formatTs(minDate)
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. // * 商品品牌:下拉选择
  13. // * 单位:下拉选择
  14. // * 商品货号:系统自动生成(只读)
  15. // * 批发价
  16. // * 零售价
  17. // * 库存
  18. // * 状态:现售产品有上架、下架状态
  19. // * 发售时间:不能低于当前创建时间
  20. form: {
  21. 'product_img_url': [
  22. {
  23. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe57fa.jpg',
  24. 'formkey': 'product_rotation_img_list'
  25. }
  26. ], // 商品主图
  27. 'product_rotation_img_list': [
  28. {
  29. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe57fa.jpg',
  30. 'formkey': 'product_rotation_img_list'
  31. },
  32. {
  33. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe51c4.jpg',
  34. 'formkey': 'product_rotation_img_list'
  35. },
  36. {
  37. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffefa54.jpg',
  38. 'formkey': 'product_rotation_img_list'
  39. },
  40. {
  41. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi6323230067e26.png',
  42. 'formkey': 'product_rotation_img_list'
  43. }
  44. ], // 商品轮播图
  45. 'product_detail_img_list': [
  46. {
  47. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe57fa.jpg',
  48. 'formkey': 'product_rotation_img_list'
  49. }
  50. ], // 详情图
  51. 'product_title': '商品标题', // 商品标题
  52. 'product_desc': '商品简介', // 商品简介
  53. 'product_category_id': {
  54. 'category_name': '',
  55. 'id': '1'
  56. }, // 分类ID
  57. 'product_brand_id': {
  58. 'brand_name': '',
  59. 'id': '1'
  60. }, // 品牌ID
  61. 'product_spec': '10个没件', // 规格
  62. 'product_unit': '个', // 单位
  63. 'product_all_price': '100', // 批发价
  64. 'product_price': '88', // 零售价
  65. 'product_count': '100', // 库存
  66. 'product_sale_at': '预售' // 预售时间(0代表预售)
  67. },
  68. product_img_url_max: 1,
  69. product_rotation_img_list_max: 5,
  70. product_detail_img_list_max: 5,
  71. booCategory: false,
  72. categoryInDefaultIndex: 0,
  73. categoryList: [],
  74. booBrand: false,
  75. brandInDefaultIndex: 0,
  76. brandList: [],
  77. booUnit: false,
  78. unitInDefaultIndex: 0,
  79. unitInColumns: ['件', '个'],
  80. booSaleAtType: false,
  81. saleAtType: '0',
  82. booSaleAt: false,
  83. minDate: minDate,
  84. maxDate: new Date(objMinDate.YYYY + 5, 11, 31).getTime(),
  85. currentDate: new Date().getTime()
  86. },
  87. /**
  88. * 生命周期函数--监听页面加载
  89. */
  90. async onLoad(options) {
  91. const { id } = options
  92. await this.fetchProductCategoryList()
  93. await this.fetchProductBrandList()
  94. if (id) {
  95. this.setData({
  96. 'form.id': id
  97. }, () => {
  98. })
  99. }
  100. },
  101. /**
  102. * 生命周期函数--监听页面初次渲染完成
  103. */
  104. onReady() {
  105. },
  106. /**
  107. * 生命周期函数--监听页面显示
  108. */
  109. onShow() {
  110. },
  111. /**
  112. * 生命周期函数--监听页面隐藏
  113. */
  114. onHide() {
  115. },
  116. /**
  117. * 生命周期函数--监听页面卸载
  118. */
  119. onUnload() {
  120. },
  121. /**
  122. * 页面相关事件处理函数--监听用户下拉动作
  123. */
  124. onPullDownRefresh() {
  125. },
  126. /**
  127. * 页面上拉触底事件的处理函数
  128. */
  129. onReachBottom() {
  130. },
  131. /**
  132. * 用户点击右上角分享
  133. */
  134. onShareAppMessage() {
  135. },
  136. async fetchProductCategoryList() {
  137. try {
  138. const { status, data, msg } = await getProductCategoryList()
  139. if (status) {
  140. this.setData({
  141. categoryList: data.map(item => {
  142. return {
  143. ...item,
  144. text: item.category_name
  145. }
  146. })
  147. })
  148. } else {
  149. wx.showToast({
  150. title: msg,
  151. icon: 'none'
  152. })
  153. }
  154. } catch (err) {}
  155. },
  156. async fetchProductBrandList() {
  157. const { status, data, msg } = await getProductBrandList()
  158. if (status) {
  159. this.setData({
  160. brandList: data.map(item => {
  161. return {
  162. ...item,
  163. text: item.brand_name
  164. }
  165. })
  166. })
  167. } else {
  168. wx.showToast({
  169. title: msg,
  170. icon: 'none'
  171. })
  172. }
  173. },
  174. ...uploadJS,
  175. uploadCallBack(res) {
  176. const temp = res.map(item => {
  177. return {
  178. 'url': item.url,
  179. 'formkey': item.formkey
  180. }
  181. })
  182. let tempForm = {}
  183. let formkey = ''
  184. if (temp.length > 0) {
  185. formkey = temp[0].formkey
  186. }
  187. switch (formkey) {
  188. case 'product_img_url':
  189. tempForm[`form.${formkey}`] = temp
  190. break
  191. case 'product_rotation_img_list':
  192. case 'product_detail_img_list':
  193. tempForm[`form.${formkey}`] = this.data.form[formkey].concat(...temp)
  194. break
  195. default:
  196. }
  197. if (Object.keys(tempForm).length > 0) {
  198. this.setData(tempForm)
  199. }
  200. },
  201. categoryShow() {
  202. this.setData({
  203. booCategory: true
  204. })
  205. },
  206. categoryHide() {
  207. if (this.data.categoryList.length > 0) {
  208. this.selectComponent('#picker-category').setIndexes([this.data.categoryInDefaultIndex])
  209. }
  210. this.setData({
  211. booCategory: false
  212. })
  213. },
  214. categoryConfirm(event) {
  215. const { value, index } = event.detail
  216. this.setData({
  217. 'form.product_category_id': value,
  218. categoryInDefaultIndex: index
  219. })
  220. this.categoryHide()
  221. },
  222. brandShow() {
  223. this.setData({
  224. booBrand: true
  225. })
  226. },
  227. brandHide() {
  228. if (this.data.brandList.length > 0) {
  229. this.selectComponent('#picker-brand').setIndexes([this.data.brandInDefaultIndex])
  230. }
  231. this.setData({
  232. booBrand: false
  233. })
  234. },
  235. brandConfirm(event) {
  236. const { value, index } = event.detail
  237. this.setData({
  238. 'form.product_brand_id': value,
  239. brandInDefaultIndex: index
  240. })
  241. this.brandHide()
  242. },
  243. unitShow() {
  244. this.setData({
  245. booUnit: true
  246. })
  247. },
  248. unitHide() {
  249. if (this.data.unitInColumns.length > 0) {
  250. this.selectComponent('#picker-unit').setIndexes([this.data.unitInDefaultIndex])
  251. }
  252. this.setData({
  253. booUnit: false
  254. })
  255. },
  256. unitConfirm(event) {
  257. const { value, index } = event.detail
  258. this.setData({
  259. 'form.product_unit': value,
  260. unitInDefaultIndex: index
  261. })
  262. this.unitHide()
  263. },
  264. setFormValue(event) {
  265. const { value } = event.detail
  266. const { formkey } = event.target.dataset
  267. let tempForm = {}
  268. switch (formkey) {
  269. case 'product_title':
  270. case 'product_desc':
  271. case 'product_spec':
  272. tempForm[`form.${formkey}`] = value
  273. break
  274. case 'product_all_price':
  275. case 'product_price':
  276. tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0').replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
  277. break
  278. case 'product_count':
  279. tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0')
  280. break
  281. default:
  282. }
  283. this.setData(tempForm)
  284. },
  285. saleAtTypeShow() {
  286. this.setData({ booSaleAtType: true })
  287. },
  288. saleAtTypeHide() {
  289. let tempForm = {}
  290. tempForm['booSaleAtType'] = false
  291. if (this.data.saleAtType === '0') {
  292. tempForm['form.product_sale_at'] = '预售'
  293. } else {
  294. tempForm['form.product_sale_at'] = ''
  295. tempForm['booSaleAt'] = true
  296. }
  297. this.setData(tempForm)
  298. },
  299. handleSaleAtType(e) {
  300. const { type } = e.currentTarget.dataset
  301. this.setData({
  302. saleAtType: type
  303. }, () => {
  304. this.saleAtTypeHide()
  305. })
  306. },
  307. saleAtHide() {
  308. this.setData({
  309. booSaleAt: false
  310. })
  311. },
  312. saleAtConfirm(event) {
  313. const { YYYY, MM, DD, HH, mm } = formatTs(event.detail)
  314. this.setData({
  315. currentDate: event.detail,
  316. 'form.product_sale_at': `${YYYY}年${MM}月${DD}日 ${HH}:${mm}`
  317. })
  318. this.saleAtHide()
  319. },
  320. getForm() {
  321. const {
  322. product_img_url,
  323. product_rotation_img_list,
  324. product_detail_img_list,
  325. product_title,
  326. product_desc,
  327. product_category_id,
  328. product_brand_id,
  329. product_spec,
  330. product_unit,
  331. product_all_price,
  332. product_price,
  333. product_count,
  334. product_sale_at
  335. } = this.data.form
  336. return {
  337. product_img_url: product_img_url.map(item => item.url).join(''),
  338. product_rotation_img_list: product_rotation_img_list.map(item => item.url),
  339. product_detail_img_list: product_detail_img_list.map(item => item.url),
  340. product_title,
  341. product_desc,
  342. product_category_id: product_category_id.id,
  343. product_brand_id: product_brand_id.id,
  344. product_spec,
  345. product_unit,
  346. product_all_price: yuan2Fen(product_all_price),
  347. product_price: yuan2Fen(product_price),
  348. product_count,
  349. product_sale_at: product_sale_at === '预售' ? 0 : this.data.currentDate
  350. }
  351. },
  352. setForm() {},
  353. verify() {
  354. let errorList = []
  355. const {
  356. product_img_url,
  357. product_rotation_img_list,
  358. product_detail_img_list,
  359. product_title,
  360. product_desc,
  361. product_category_id,
  362. product_brand_id,
  363. product_spec,
  364. product_unit,
  365. product_count,
  366. product_sale_at
  367. } = this.getForm()
  368. if (!product_img_url) {
  369. errorList.push('请上传商品主图')
  370. }
  371. if (!product_rotation_img_list.length) {
  372. errorList.push('请上传商品轮播图')
  373. }
  374. if (!product_detail_img_list.length) {
  375. errorList.push('请上传商品详情图')
  376. }
  377. if (!product_title) {
  378. errorList.push('请输入商品标题')
  379. }
  380. if (!product_desc) {
  381. errorList.push('请输入商品简介')
  382. }
  383. if (!product_category_id) {
  384. errorList.push('请选择商品分类')
  385. }
  386. if (!product_brand_id) {
  387. errorList.push('请选择商品品牌')
  388. }
  389. if (!product_spec) {
  390. errorList.push('请输入商品规格')
  391. }
  392. if (!product_unit) {
  393. errorList.push('请选择商品单位')
  394. }
  395. if (!(this.data.form.product_all_price)) {
  396. errorList.push('请输入批发价')
  397. }
  398. if (!(this.data.form.product_price)) {
  399. errorList.push('请输入零售价')
  400. }
  401. if (!product_count) {
  402. errorList.push('请输入库存')
  403. }
  404. if (product_sale_at !== 0 && !product_sale_at) {
  405. errorList.push('请选择发售时间')
  406. }
  407. return errorList
  408. },
  409. async onSubmit(e) {
  410. const temp = this.getForm()
  411. const verifyList = this.verify()
  412. if (verifyList.length) {
  413. wx.showToast({
  414. title: verifyList[0],
  415. icon: 'none'
  416. })
  417. return
  418. }
  419. try {
  420. let res = await postAddProduct(temp)
  421. if (this.data.form.id) {
  422. res = await postModifyProduct({ ...temp, id: this.data.form.id })
  423. }
  424. const { status, data, msg } = res
  425. } catch (err) {}
  426. }
  427. })