businessGoodsEdit.js 11 KB

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