businessGoodsEdit.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. const uploadJS = require('../../mixin/upload.js')
  2. const { postAddProduct, postModifyProduct, getProductDetail } = require('./api/index')
  3. const { getProductCategoryList, getProductBrandList } = require('../../api/common')
  4. const { formatTs, yuan2Fen, fen2Yuan } = 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. 'product_rotation_img_list': [], // 商品轮播图
  23. 'product_detail_img_list': [], // 详情图
  24. 'product_title': '', // 商品标题
  25. 'product_desc': '', // 商品简介
  26. 'product_category_id': {
  27. 'category_name': '',
  28. 'id': ''
  29. }, // 分类ID
  30. 'product_brand_id': {
  31. 'brand_name': '',
  32. 'id': ''
  33. }, // 品牌ID
  34. 'product_spec': '', // 规格
  35. 'product_unit': '', // 单位
  36. 'product_all_price': '', // 批发价
  37. 'product_price': '', // 零售价
  38. 'product_count': '', // 库存
  39. 'product_status': 0, // 状态
  40. 'product_sale_at': '预售' // 预售时间(0代表预售)
  41. },
  42. product_img_url_max: 1,
  43. product_rotation_img_list_max: 5,
  44. product_detail_img_list_max: 5,
  45. booCategory: false,
  46. categoryInDefaultIndex: 0,
  47. categoryList: [],
  48. booBrand: false,
  49. brandInDefaultIndex: 0,
  50. brandList: [],
  51. booUnit: false,
  52. unitInDefaultIndex: 0,
  53. unitInColumns: ['件', '个'],
  54. booSaleAtType: false,
  55. saleAtType: '0',
  56. booSaleAt: false,
  57. minDate: minDate,
  58. maxDate: new Date(objMinDate.YYYY + 5, 11, 31).getTime(),
  59. currentDate: new Date().getTime()
  60. },
  61. /**
  62. * 生命周期函数--监听页面加载
  63. */
  64. async onLoad(options) {
  65. const { product_id } = options
  66. await this.fetchProductCategoryList()
  67. await this.fetchProductBrandList()
  68. if (product_id) {
  69. this.setData({
  70. 'form.id': product_id
  71. }, () => {
  72. this.fetchProductDetail()
  73. })
  74. wx.setNavigationBarTitle({
  75. title: '产品编辑'
  76. })
  77. } else {
  78. wx.setNavigationBarTitle({
  79. title: '产品新增'
  80. })
  81. }
  82. },
  83. /**
  84. * 生命周期函数--监听页面初次渲染完成
  85. */
  86. onReady() {
  87. },
  88. /**
  89. * 生命周期函数--监听页面显示
  90. */
  91. onShow() {
  92. },
  93. /**
  94. * 生命周期函数--监听页面隐藏
  95. */
  96. onHide() {
  97. },
  98. /**
  99. * 生命周期函数--监听页面卸载
  100. */
  101. onUnload() {
  102. },
  103. // 获取商品分类
  104. async fetchProductCategoryList() {
  105. try {
  106. const { status, data, msg } = await getProductCategoryList()
  107. if (status) {
  108. this.setData({
  109. categoryList: data.map(item => {
  110. return {
  111. ...item,
  112. text: item.category_name
  113. }
  114. })
  115. })
  116. } else {
  117. wx.showToast({
  118. title: msg,
  119. icon: 'none'
  120. })
  121. }
  122. } catch (err) {}
  123. },
  124. // 获取品牌
  125. async fetchProductBrandList() {
  126. try {
  127. const { status, data, msg } = await getProductBrandList()
  128. if (status) {
  129. this.setData({
  130. brandList: data.map(item => {
  131. return {
  132. ...item,
  133. text: item.brand_name
  134. }
  135. })
  136. })
  137. } else {
  138. wx.showToast({
  139. title: msg,
  140. icon: 'none'
  141. })
  142. }
  143. } catch (err) {}
  144. },
  145. async fetchProductDetail() {
  146. let temp = {}
  147. try {
  148. const { status, data, msg } = await getProductDetail(this.data.form.id)
  149. if (status) {
  150. for (let key in data) {
  151. let value = data[key]
  152. if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
  153. if (key === 'product_img_url') {
  154. value = [
  155. {
  156. 'formkey': key,
  157. 'url': value
  158. }
  159. ]
  160. }
  161. if (key === 'product_rotation_img_list' || key === 'product_detail_img_list') {
  162. value = JSON.parse(value).map(item => {
  163. return {
  164. 'formkey': key,
  165. 'url': item
  166. }
  167. })
  168. }
  169. if (key === 'product_category_id') {
  170. const category = this.data.categoryList.filter(item => item.id === value)
  171. if (category.length > 0) {
  172. value = category[0]
  173. temp['categoryInDefaultIndex'] = this.data.categoryList.findIndex(item => item.id === value)
  174. }
  175. }
  176. if (key === 'product_brand_id') {
  177. const brand = this.data.brandList.filter(item => item.id === value)
  178. if (brand.length > 0) {
  179. value = brand[0]
  180. temp['brandInDefaultIndex'] = this.data.brandList.findIndex(item => item.id === value)
  181. }
  182. }
  183. if (key === 'product_unit') {
  184. const unit = this.data.unitInColumns.filter(item => item === value)
  185. if (unit.length > 0) {
  186. temp['unitInDefaultIndex'] = this.data.unitInColumns.findIndex(item => item === value)
  187. }
  188. }
  189. if (key === 'product_all_price' || key === 'product_price') {
  190. value = fen2Yuan(value)
  191. }
  192. if (key === 'product_sale_at') {
  193. if (value === 0) {
  194. value = '预售'
  195. } else {
  196. const { YYYY, MM, DD, HH, mm } = formatTs(value)
  197. value = `${YYYY}年${MM}月${DD}日 ${HH}:${mm}`
  198. temp['saleAtType'] = '1'
  199. temp['currentDate'] = value
  200. }
  201. }
  202. if (this.data.form.hasOwnProperty(key)) {
  203. temp[`form.${key}`] = value
  204. }
  205. }
  206. }
  207. this.setData(temp)
  208. } else {
  209. wx.showToast({
  210. title: msg,
  211. icon: 'none'
  212. })
  213. }
  214. } catch (err) {}
  215. },
  216. ...uploadJS,
  217. uploadCallBack(res) {
  218. const temp = res.map(item => {
  219. return {
  220. 'url': item.url,
  221. 'formkey': item.formkey
  222. }
  223. })
  224. let tempForm = {}
  225. let formkey = ''
  226. if (temp.length > 0) {
  227. formkey = temp[0].formkey
  228. }
  229. switch (formkey) {
  230. case 'product_img_url':
  231. tempForm[`form.${formkey}[0]`] = temp[0]
  232. break
  233. case 'product_rotation_img_list':
  234. case 'product_detail_img_list':
  235. const formkeyData = this.data.form[formkey]
  236. tempForm[`form.${formkey}`] = formkeyData.concat(...temp)
  237. break
  238. default:
  239. }
  240. if (Object.keys(tempForm).length > 0) {
  241. this.setData(tempForm)
  242. }
  243. },
  244. categoryShow() {
  245. this.setData({
  246. booCategory: true
  247. })
  248. },
  249. categoryHide() {
  250. if (this.data.categoryList.length > 0) {
  251. this.selectComponent('#picker-category').setIndexes([this.data.categoryInDefaultIndex])
  252. }
  253. this.setData({
  254. booCategory: false
  255. })
  256. },
  257. categoryConfirm(event) {
  258. const { value, index } = event.detail
  259. this.setData({
  260. 'form.product_category_id': value,
  261. categoryInDefaultIndex: index
  262. })
  263. this.categoryHide()
  264. },
  265. brandShow() {
  266. this.setData({
  267. booBrand: true
  268. })
  269. },
  270. brandHide() {
  271. if (this.data.brandList.length > 0) {
  272. this.selectComponent('#picker-brand').setIndexes([this.data.brandInDefaultIndex])
  273. }
  274. this.setData({
  275. booBrand: false
  276. })
  277. },
  278. brandConfirm(event) {
  279. const { value, index } = event.detail
  280. this.setData({
  281. 'form.product_brand_id': value,
  282. brandInDefaultIndex: index
  283. })
  284. this.brandHide()
  285. },
  286. unitShow() {
  287. this.setData({
  288. booUnit: true
  289. })
  290. },
  291. unitHide() {
  292. if (this.data.unitInColumns.length > 0) {
  293. this.selectComponent('#picker-unit').setIndexes([this.data.unitInDefaultIndex])
  294. }
  295. this.setData({
  296. booUnit: false
  297. })
  298. },
  299. unitConfirm(event) {
  300. const { value, index } = event.detail
  301. this.setData({
  302. 'form.product_unit': value,
  303. unitInDefaultIndex: index
  304. })
  305. this.unitHide()
  306. },
  307. setFormValue(event) {
  308. const { value } = event.detail
  309. const { formkey } = event.target.dataset
  310. let tempForm = {}
  311. switch (formkey) {
  312. case 'product_title':
  313. case 'product_desc':
  314. case 'product_spec':
  315. tempForm[`form.${formkey}`] = value
  316. break
  317. case 'product_all_price':
  318. case 'product_price':
  319. tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0').replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
  320. break
  321. case 'product_count':
  322. tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0')
  323. break
  324. default:
  325. }
  326. this.setData(tempForm)
  327. },
  328. saleAtTypeShow() {
  329. this.setData({ booSaleAtType: true })
  330. },
  331. saleAtTypeHide() {
  332. let tempForm = {}
  333. tempForm['booSaleAtType'] = false
  334. if (this.data.saleAtType === '0') {
  335. tempForm['form.product_sale_at'] = '预售'
  336. } else {
  337. tempForm['form.product_sale_at'] = ''
  338. tempForm['booSaleAt'] = true
  339. }
  340. this.setData(tempForm)
  341. },
  342. handleSaleAtType(e) {
  343. const { type } = e.currentTarget.dataset
  344. this.setData({
  345. saleAtType: type
  346. }, () => {
  347. this.saleAtTypeHide()
  348. })
  349. },
  350. saleAtHide() {
  351. this.setData({
  352. booSaleAt: false
  353. })
  354. },
  355. saleAtConfirm(event) {
  356. const { YYYY, MM, DD, HH, mm } = formatTs(event.detail)
  357. this.setData({
  358. currentDate: event.detail,
  359. 'form.product_sale_at': `${YYYY}年${MM}月${DD}日 ${HH}:${mm}`
  360. })
  361. this.saleAtHide()
  362. },
  363. getForm() {
  364. const {
  365. product_img_url,
  366. product_rotation_img_list,
  367. product_detail_img_list,
  368. product_title,
  369. product_desc,
  370. product_category_id,
  371. product_brand_id,
  372. product_spec,
  373. product_unit,
  374. product_all_price,
  375. product_price,
  376. product_count,
  377. product_sale_at
  378. } = this.data.form
  379. return {
  380. product_img_url: product_img_url.map(item => item.url).join(''),
  381. product_rotation_img_list: product_rotation_img_list.map(item => item.url),
  382. product_detail_img_list: product_detail_img_list.map(item => item.url),
  383. product_title,
  384. product_desc,
  385. product_category_id: product_category_id.id,
  386. product_brand_id: product_brand_id.id,
  387. product_spec,
  388. product_unit,
  389. product_all_price: yuan2Fen(product_all_price),
  390. product_price: yuan2Fen(product_price),
  391. product_count,
  392. product_sale_at: product_sale_at === '预售' ? 0 : this.data.currentDate
  393. }
  394. },
  395. verify() {
  396. let errorList = []
  397. const {
  398. product_img_url,
  399. product_rotation_img_list,
  400. product_detail_img_list,
  401. product_title,
  402. product_desc,
  403. product_category_id,
  404. product_brand_id,
  405. product_spec,
  406. product_unit,
  407. product_count,
  408. product_sale_at
  409. } = this.getForm()
  410. if (!product_img_url) {
  411. errorList.push('请上传商品主图')
  412. }
  413. if (!product_rotation_img_list) {
  414. errorList.push('请上传商品轮播图')
  415. }
  416. if (!product_detail_img_list) {
  417. errorList.push('请上传商品详情图')
  418. }
  419. if (!product_title) {
  420. errorList.push('请输入商品标题')
  421. }
  422. if (!product_desc) {
  423. errorList.push('请输入商品简介')
  424. }
  425. if (!product_category_id) {
  426. errorList.push('请选择商品分类')
  427. }
  428. if (!product_brand_id) {
  429. errorList.push('请选择商品品牌')
  430. }
  431. if (!product_spec) {
  432. errorList.push('请输入商品规格')
  433. }
  434. if (!product_unit) {
  435. errorList.push('请选择商品单位')
  436. }
  437. if (!(this.data.form.product_all_price)) {
  438. errorList.push('请输入批发价')
  439. }
  440. if (!(this.data.form.product_price)) {
  441. errorList.push('请输入零售价')
  442. }
  443. if (!product_count) {
  444. errorList.push('请输入库存')
  445. }
  446. if (product_sale_at !== 0 && !product_sale_at) {
  447. errorList.push('请选择发售时间')
  448. }
  449. return errorList
  450. },
  451. async onSubmit(e) {
  452. const temp = this.getForm()
  453. const verifyList = this.verify()
  454. if (verifyList.length) {
  455. wx.showToast({
  456. title: verifyList[0],
  457. icon: 'none'
  458. })
  459. return
  460. }
  461. try {
  462. let res = await postAddProduct(temp)
  463. if (this.data.form.id) {
  464. res = await postModifyProduct({ ...temp, id: this.data.form.id })
  465. }
  466. const { status, data, msg } = res
  467. if (status) {
  468. wx.redirectTo({
  469. url: '/pages/businessGoodsManage/businessGoodsManage'
  470. })
  471. } else {
  472. wx.showToast({
  473. title: msg,
  474. icon: 'none'
  475. })
  476. }
  477. } catch (err) {}
  478. }
  479. })