businessGoodsEdit.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. const uploadJS = require('../../mixin/upload.js')
  2. const { getProductCategoryList, getProductBrandList } = 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': ''
  55. }, // 分类ID
  56. 'product_brand_id': {
  57. 'brand_name': '',
  58. 'id': ''
  59. }, // 品牌ID
  60. 'product_spec': '', // 规格
  61. 'product_unit': '', // 单位
  62. 'product_all_price': '', // 批发价
  63. 'product_price': '', // 零售价
  64. 'product_count': '', // 库存
  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. await this.fetchProductCategoryList()
  91. await this.fetchProductBrandList()
  92. },
  93. /**
  94. * 生命周期函数--监听页面初次渲染完成
  95. */
  96. onReady() {
  97. },
  98. /**
  99. * 生命周期函数--监听页面显示
  100. */
  101. onShow() {
  102. },
  103. /**
  104. * 生命周期函数--监听页面隐藏
  105. */
  106. onHide() {
  107. },
  108. /**
  109. * 生命周期函数--监听页面卸载
  110. */
  111. onUnload() {
  112. },
  113. /**
  114. * 页面相关事件处理函数--监听用户下拉动作
  115. */
  116. onPullDownRefresh() {
  117. },
  118. /**
  119. * 页面上拉触底事件的处理函数
  120. */
  121. onReachBottom() {
  122. },
  123. /**
  124. * 用户点击右上角分享
  125. */
  126. onShareAppMessage() {
  127. },
  128. async fetchProductCategoryList() {
  129. try {
  130. const { status, data, msg } = await getProductCategoryList()
  131. if (status) {
  132. this.setData({
  133. categoryList: data.map(item => {
  134. return {
  135. ...item,
  136. text: item.category_name
  137. }
  138. })
  139. })
  140. } else {
  141. wx.showToast({
  142. title: msg,
  143. icon: 'none'
  144. })
  145. }
  146. } catch (err) {}
  147. },
  148. async fetchProductBrandList() {
  149. const { status, data, msg } = await getProductBrandList()
  150. if (status) {
  151. this.setData({
  152. brandList: data.map(item => {
  153. return {
  154. ...item,
  155. text: item.brand_name
  156. }
  157. })
  158. })
  159. } else {
  160. wx.showToast({
  161. title: msg,
  162. icon: 'none'
  163. })
  164. }
  165. },
  166. ...uploadJS,
  167. uploadCallBack(res) {
  168. const temp = res.map(item => {
  169. return {
  170. 'url': item.url,
  171. 'formkey': item.formkey
  172. }
  173. })
  174. let tempForm = {}
  175. let formkey = ''
  176. if (temp.length > 0) {
  177. formkey = temp[0].formkey
  178. }
  179. switch (formkey) {
  180. case 'product_img_url':
  181. tempForm[`form.${formkey}`] = temp
  182. break
  183. case 'product_rotation_img_list':
  184. case 'product_detail_img_list':
  185. tempForm[`form.${formkey}`] = this.data.form[formkey].concat(...temp)
  186. break
  187. default:
  188. }
  189. if (Object.keys(tempForm).length > 0) {
  190. this.setData(tempForm)
  191. }
  192. },
  193. categoryShow() {
  194. this.setData({
  195. booCategory: true
  196. })
  197. },
  198. categoryHide() {
  199. if (this.data.categoryList.length > 0) {
  200. this.selectComponent('#picker-category').setIndexes([this.data.categoryInDefaultIndex])
  201. }
  202. this.setData({
  203. booCategory: false
  204. })
  205. },
  206. categoryConfirm(event) {
  207. const { value, index } = event.detail
  208. this.setData({
  209. 'form.product_category_id': value,
  210. categoryInDefaultIndex: index
  211. })
  212. this.categoryHide()
  213. },
  214. brandShow() {
  215. this.setData({
  216. booBrand: true
  217. })
  218. },
  219. brandHide() {
  220. if (this.data.brandList.length > 0) {
  221. this.selectComponent('#picker-brand').setIndexes([this.data.brandInDefaultIndex])
  222. }
  223. this.setData({
  224. booBrand: false
  225. })
  226. },
  227. brandConfirm(event) {
  228. const { value, index } = event.detail
  229. this.setData({
  230. 'form.product_brand_id': value,
  231. brandInDefaultIndex: index
  232. })
  233. this.brandHide()
  234. },
  235. unitShow() {
  236. this.setData({
  237. booUnit: true
  238. })
  239. },
  240. unitHide() {
  241. if (this.data.unitInColumns.length > 0) {
  242. this.selectComponent('#picker-unit').setIndexes([this.data.unitInDefaultIndex])
  243. }
  244. this.setData({
  245. booUnit: false
  246. })
  247. },
  248. unitConfirm(event) {
  249. const { value, index } = event.detail
  250. this.setData({
  251. 'form.product_unit': value,
  252. unitInDefaultIndex: index
  253. })
  254. this.unitHide()
  255. },
  256. setFormValue(event) {
  257. const { value } = event.detail
  258. const { formkey } = event.target.dataset
  259. let tempForm = {}
  260. switch (formkey) {
  261. case 'product_title':
  262. case 'product_desc':
  263. case 'product_spec':
  264. tempForm[`form.${formkey}`] = value
  265. break
  266. case 'product_all_price':
  267. case 'product_price':
  268. tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0').replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
  269. break
  270. case 'product_count':
  271. tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0')
  272. break
  273. default:
  274. }
  275. this.setData(tempForm)
  276. },
  277. saleAtTypeShow() {
  278. this.setData({ booSaleAtType: true })
  279. },
  280. saleAtTypeHide() {
  281. let tempForm = {}
  282. tempForm['booSaleAtType'] = false
  283. if (this.data.saleAtType === '0') {
  284. tempForm['form.product_sale_at'] = '预售'
  285. } else {
  286. tempForm['form.product_sale_at'] = ''
  287. tempForm['booSaleAt'] = true
  288. }
  289. this.setData(tempForm)
  290. },
  291. handleSaleAtType(e) {
  292. const { type } = e.currentTarget.dataset
  293. this.setData({
  294. saleAtType: type
  295. }, () => {
  296. this.saleAtTypeHide()
  297. })
  298. },
  299. saleAtHide() {
  300. this.setData({
  301. booSaleAt: false
  302. })
  303. },
  304. saleAtConfirm(event) {
  305. const { YYYY, MM, DD, HH, mm } = formatTs(event.detail)
  306. this.setData({
  307. currentDate: event.detail,
  308. 'form.product_sale_at': `${YYYY}年${MM}月${DD}日 ${HH}:${mm}`
  309. })
  310. this.saleAtHide()
  311. },
  312. getForm() {
  313. const {
  314. product_img_url,
  315. product_rotation_img_list,
  316. product_detail_img_list,
  317. product_title,
  318. product_desc,
  319. product_category_id,
  320. product_brand_id,
  321. product_spec,
  322. product_unit,
  323. product_all_price,
  324. product_price,
  325. product_count,
  326. product_sale_at
  327. } = this.data.form
  328. return {
  329. product_img_url: product_img_url.map(item => item.url).join(''),
  330. product_rotation_img_list: product_rotation_img_list.map(item => item.url),
  331. product_detail_img_list: product_detail_img_list.map(item => item.url),
  332. product_title,
  333. product_desc,
  334. product_category_id: product_category_id.id,
  335. product_brand_id: product_brand_id.id,
  336. product_spec,
  337. product_unit,
  338. product_all_price: yuan2Fen(product_all_price),
  339. product_price: yuan2Fen(product_price),
  340. product_count,
  341. product_sale_at
  342. }
  343. },
  344. setForm() {},
  345. verify() {
  346. let errorList = []
  347. const {
  348. product_img_url,
  349. product_rotation_img_list,
  350. product_detail_img_list,
  351. product_title,
  352. product_desc,
  353. product_category_id,
  354. product_brand_id,
  355. product_spec,
  356. product_unit,
  357. product_all_price,
  358. product_price,
  359. product_count,
  360. product_sale_at
  361. } = this.getForm()
  362. if (!product_img_url) {
  363. errorList.push('请上传商品主图')
  364. }
  365. if (!product_rotation_img_list.length) {
  366. errorList.push('请上传商品轮播图')
  367. }
  368. if (!product_detail_img_list.length) {
  369. errorList.push('请上传商品详情图')
  370. }
  371. if (!product_title) {
  372. errorList.push('请输入商品标题')
  373. }
  374. if (!product_desc) {
  375. errorList.push('请输入商品简介')
  376. }
  377. if (!product_category_id) {
  378. errorList.push('请选择商品分类')
  379. }
  380. if (!product_brand_id) {
  381. errorList.push('请选择商品品牌')
  382. }
  383. if (!product_spec) {
  384. errorList.push('请输入商品规格')
  385. }
  386. if (!product_unit) {
  387. errorList.push('请选择商品单位')
  388. }
  389. if (!(this.data.form.product_all_price)) {
  390. errorList.push('请输入批发价')
  391. }
  392. if (!(this.data.form.product_price)) {
  393. errorList.push('请输入零售价')
  394. }
  395. if (!product_count) {
  396. errorList.push('请输入库存')
  397. }
  398. if (!product_sale_at) {
  399. errorList.push('请选择发售时间')
  400. }
  401. return errorList
  402. },
  403. onSubmit(e) {
  404. const verifyList = this.verify()
  405. if (verifyList.length) {
  406. wx.showToast({
  407. title: verifyList[0],
  408. icon: 'none'
  409. })
  410. }
  411. }
  412. })