businessGoodsEdit.js 13 KB

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