businessGoodsEdit.js 15 KB

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