businessGoodsEdit.js 14 KB

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