businessGoodsEdit.js 14 KB

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