businessGoodsEdit.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. }
  85. app.fetchSystemConfigCallback = () => {
  86. this.setUnitInColumns()
  87. }
  88. },
  89. /**
  90. * 生命周期函数--监听页面初次渲染完成
  91. */
  92. onReady() {
  93. },
  94. /**
  95. * 生命周期函数--监听页面显示
  96. */
  97. onShow() {
  98. this.setUnitInColumns()
  99. },
  100. /**
  101. * 生命周期函数--监听页面隐藏
  102. */
  103. onHide() {
  104. },
  105. /**
  106. * 生命周期函数--监听页面卸载
  107. */
  108. onUnload() {
  109. },
  110. // 获取商品分类
  111. async fetchProductCategoryList() {
  112. try {
  113. const { status, data, msg } = await getProductCategoryList()
  114. if (status) {
  115. this.setData({
  116. categoryList: data.map(item => {
  117. return {
  118. ...item,
  119. text: item.category_name
  120. }
  121. })
  122. })
  123. } else {
  124. wx.showToast({
  125. title: msg,
  126. icon: 'none'
  127. })
  128. }
  129. } catch (err) {}
  130. },
  131. // 获取品牌
  132. async fetchProductBrandList() {
  133. try {
  134. const { status, data, msg } = await getProductBrandList()
  135. if (status) {
  136. this.setData({
  137. brandList: data.map(item => {
  138. return {
  139. ...item,
  140. text: item.brand_name
  141. }
  142. })
  143. })
  144. } else {
  145. wx.showToast({
  146. title: msg,
  147. icon: 'none'
  148. })
  149. }
  150. } catch (err) {}
  151. },
  152. async fetchProductDetail() {
  153. let temp = {}
  154. try {
  155. const { status, data, msg } = await getProductDetail(this.data.form.id)
  156. if (status) {
  157. for (let key in data) {
  158. let value = data[key]
  159. if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
  160. if (key === 'product_img_url') {
  161. value = [
  162. {
  163. 'formkey': key,
  164. 'url': value
  165. }
  166. ]
  167. }
  168. if (key === 'product_rotation_img_list' || key === 'product_detail_img_list') {
  169. value = JSON.parse(value).map(item => {
  170. return {
  171. 'formkey': key,
  172. 'url': item
  173. }
  174. })
  175. }
  176. if (key === 'product_category_id') {
  177. const category = this.data.categoryList.filter(item => item.id === value)
  178. if (category.length > 0) {
  179. value = category[0]
  180. temp['categoryInDefaultIndex'] = this.data.categoryList.findIndex(item => item.id === value)
  181. }
  182. }
  183. if (key === 'product_brand_id') {
  184. const brand = this.data.brandList.filter(item => item.id === value)
  185. if (brand.length > 0) {
  186. value = brand[0]
  187. temp['brandInDefaultIndex'] = this.data.brandList.findIndex(item => item.id === value)
  188. }
  189. }
  190. if (key === 'product_unit') {
  191. const unit = this.data.unitInColumns.filter(item => item === value)
  192. if (unit.length > 0) {
  193. temp['unitInDefaultIndex'] = this.data.unitInColumns.findIndex(item => item === value)
  194. }
  195. }
  196. if (key === 'product_all_price' || key === 'product_price') {
  197. value = fen2Yuan(value)
  198. }
  199. if (key === 'product_sale_at') {
  200. if (value === 0) {
  201. value = '现货'
  202. } else {
  203. const { YYYY, MM, DD} = formatTs(value * 1000)
  204. temp['saleAtType'] = '1'
  205. temp['currentDate'] = value * 1000
  206. value = `${YYYY}年${MM}月${DD}日`
  207. }
  208. }
  209. if (key === 'product_check_status') {
  210. temp['productCheckStatus'] = value
  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 } = formatTs(event.detail)
  373. this.setData({
  374. currentDate: event.detail,
  375. 'form.product_sale_at': `${YYYY}年${MM}月${DD}日`
  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_sale_at
  394. } = this.data.form
  395. return {
  396. product_img_url: product_img_url.map(item => item.url).join(''),
  397. product_rotation_img_list: product_rotation_img_list.map(item => item.url),
  398. product_detail_img_list: product_detail_img_list.map(item => item.url),
  399. product_title,
  400. product_desc,
  401. product_category_id: product_category_id.id,
  402. product_brand_id: product_brand_id.id,
  403. product_spec,
  404. product_unit,
  405. product_all_price: yuan2Fen(product_all_price),
  406. product_price: yuan2Fen(product_price),
  407. product_count,
  408. product_sale_at: product_sale_at === '现货' ? 0 : (product_sale_at ? getTs(product_sale_at, 'YYYY年MM月DD日', 's') : '')
  409. }
  410. },
  411. verify() {
  412. let errorList = []
  413. const {
  414. product_img_url,
  415. product_rotation_img_list,
  416. product_detail_img_list,
  417. product_title,
  418. product_desc,
  419. product_category_id,
  420. product_brand_id,
  421. product_spec,
  422. product_unit,
  423. product_count,
  424. product_sale_at
  425. } = this.getForm()
  426. if (!product_img_url) {
  427. errorList.push('请上传商品主图')
  428. }
  429. if (!product_rotation_img_list) {
  430. errorList.push('请上传商品轮播图')
  431. }
  432. if (!product_detail_img_list) {
  433. errorList.push('请上传商品详情图')
  434. }
  435. if (!product_title) {
  436. errorList.push('请输入商品标题')
  437. }
  438. if (!product_desc) {
  439. errorList.push('请输入商品简介')
  440. }
  441. if (!product_category_id) {
  442. errorList.push('请选择商品分类')
  443. }
  444. if (!product_brand_id) {
  445. errorList.push('请选择商品品牌')
  446. }
  447. if (!product_spec) {
  448. errorList.push('请输入商品规格')
  449. }
  450. if (!product_unit) {
  451. errorList.push('请选择商品单位')
  452. }
  453. if (!(this.data.form.product_all_price)) {
  454. errorList.push('请输入批发价')
  455. }
  456. if (!(this.data.form.product_price)) {
  457. errorList.push('请输入零售价')
  458. }
  459. if (!product_count) {
  460. errorList.push('请输入库存')
  461. }
  462. if (product_sale_at !== 0 && !product_sale_at) {
  463. errorList.push('请选择预售时间')
  464. }
  465. return errorList
  466. },
  467. async onSubmit(e) {
  468. const temp = this.getForm()
  469. const verifyList = this.verify()
  470. if (verifyList.length) {
  471. wx.showToast({
  472. title: verifyList[0],
  473. icon: 'none'
  474. })
  475. return
  476. }
  477. try {
  478. let res = {}
  479. if (this.data.form.id) {
  480. res = await postModifyProduct({ ...temp, id: this.data.form.id })
  481. } else {
  482. res = await postAddProduct(temp)
  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. setUnitInColumns() {
  498. const { product_unit_list } = app.globalData.objSystemConfig
  499. this.setData({
  500. unitInColumns: Array.isArray(product_unit_list) && product_unit_list.length > 0 ? product_unit_list : []
  501. })
  502. }
  503. })