businessGoodsEdit.js 13 KB

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