businessGoodsEdit.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. const uploadJS = require('../../mixin/upload.js')
  2. const { getProductCategoryList, getProductBrandList } = require('./api/index')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. // * 商品品牌:下拉选择
  9. // * 单位:下拉选择
  10. // * 商品货号:系统自动生成(只读)
  11. // * 批发价
  12. // * 零售价
  13. // * 库存
  14. // * 状态:现售产品有上架、下架状态
  15. // * 发售时间:不能低于当前创建时间
  16. form: {
  17. 'product_img_url': [], // 商品主图
  18. 'product_rotation_img_list': [
  19. {
  20. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe57fa.jpg',
  21. 'formkey': 'product_rotation_img_list'
  22. },
  23. {
  24. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe51c4.jpg',
  25. 'formkey': 'product_rotation_img_list'
  26. },
  27. {
  28. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffefa54.jpg',
  29. 'formkey': 'product_rotation_img_list'
  30. },
  31. {
  32. 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi6323230067e26.png',
  33. 'formkey': 'product_rotation_img_list'
  34. }
  35. ], // 商品轮播图
  36. 'product_detail_img_list': [], // 详情图
  37. 'product_title': '', // 商品标题
  38. 'product_desc': '', // 商品简介
  39. 'product_category_id': {
  40. 'category_name': '',
  41. 'id': ''
  42. }, // 分类ID
  43. 'product_brand_id': {
  44. 'brand_name': '',
  45. 'id': ''
  46. }, // 品牌ID
  47. 'product_spec': '', // 规格
  48. 'product_unit': '', // 单位
  49. 'product_all_price': '', // 批发价
  50. 'product_price': '', // 零售价
  51. 'product_count': '', // 库存
  52. 'product_sale_at': '' // 预售时间(0代表预售)
  53. },
  54. product_img_url_max: 1,
  55. product_rotation_img_list_max: 5,
  56. product_detail_img_list_max: 5,
  57. booCategory: false,
  58. categoryInDefaultIndex: 0,
  59. categoryList: [],
  60. booBrand: false,
  61. brandInDefaultIndex: 0,
  62. brandList: [],
  63. booUnit: false,
  64. unitInDefaultIndex: 0,
  65. unitInColumns: ['件', '个']
  66. },
  67. /**
  68. * 生命周期函数--监听页面加载
  69. */
  70. async onLoad(options) {
  71. await this.fetchProductCategoryList()
  72. await this.fetchProductBrandList()
  73. },
  74. /**
  75. * 生命周期函数--监听页面初次渲染完成
  76. */
  77. onReady() {
  78. },
  79. /**
  80. * 生命周期函数--监听页面显示
  81. */
  82. onShow() {
  83. },
  84. /**
  85. * 生命周期函数--监听页面隐藏
  86. */
  87. onHide() {
  88. },
  89. /**
  90. * 生命周期函数--监听页面卸载
  91. */
  92. onUnload() {
  93. },
  94. /**
  95. * 页面相关事件处理函数--监听用户下拉动作
  96. */
  97. onPullDownRefresh() {
  98. },
  99. /**
  100. * 页面上拉触底事件的处理函数
  101. */
  102. onReachBottom() {
  103. },
  104. /**
  105. * 用户点击右上角分享
  106. */
  107. onShareAppMessage() {
  108. },
  109. async fetchProductCategoryList() {
  110. try {
  111. const { status, data, msg } = await getProductCategoryList()
  112. if (status) {
  113. this.setData({
  114. categoryList: data.map(item => {
  115. return {
  116. ...item,
  117. text: item.category_name
  118. }
  119. })
  120. })
  121. } else {
  122. wx.showToast({
  123. title: msg,
  124. icon: 'none'
  125. })
  126. }
  127. } catch (err) {}
  128. },
  129. async fetchProductBrandList() {
  130. const { status, data, msg } = await getProductBrandList()
  131. if (status) {
  132. this.setData({
  133. brandList: data.map(item => {
  134. return {
  135. ...item,
  136. text: item.brand_name
  137. }
  138. })
  139. })
  140. } else {
  141. wx.showToast({
  142. title: msg,
  143. icon: 'none'
  144. })
  145. }
  146. },
  147. ...uploadJS,
  148. uploadCallBack(res) {
  149. const temp = res.map(item => {
  150. return {
  151. 'url': item.url,
  152. 'formkey': item.formkey
  153. }
  154. })
  155. let tempForm = {}
  156. let formkey = ''
  157. if (temp.length > 0) {
  158. formkey = temp[0].formkey
  159. }
  160. switch (formkey) {
  161. case 'product_img_url':
  162. tempForm[`form.${formkey}`] = temp
  163. break
  164. case 'product_rotation_img_list':
  165. case 'product_detail_img_list':
  166. tempForm[`form.${formkey}`] = this.data.form[formkey].concat(...temp)
  167. break
  168. default:
  169. }
  170. if (Object.keys(tempForm).length > 0) {
  171. this.setData(tempForm)
  172. }
  173. },
  174. categoryShow() {
  175. this.setData({
  176. booCategory: true
  177. })
  178. },
  179. categoryHide() {
  180. if (this.data.categoryList.length > 0) {
  181. this.selectComponent('#picker-category').setIndexes([this.data.categoryInDefaultIndex])
  182. }
  183. this.setData({
  184. booCategory: false
  185. })
  186. },
  187. categoryConfirm(event) {
  188. const { value, index } = event.detail
  189. this.setData({
  190. 'form.product_category_id': value,
  191. categoryInDefaultIndex: index
  192. })
  193. this.categoryHide()
  194. },
  195. brandShow() {
  196. this.setData({
  197. booBrand: true
  198. })
  199. },
  200. brandHide() {
  201. if (this.data.brandList.length > 0) {
  202. this.selectComponent('#picker-brand').setIndexes([this.data.brandInDefaultIndex])
  203. }
  204. this.setData({
  205. booBrand: false
  206. })
  207. },
  208. brandConfirm(event) {
  209. const { value, index } = event.detail
  210. this.setData({
  211. 'form.product_brand_id': value,
  212. brandInDefaultIndex: index
  213. })
  214. this.brandHide()
  215. },
  216. unitShow() {
  217. this.setData({
  218. booUnit: true
  219. })
  220. },
  221. unitHide() {
  222. if (this.data.unitInColumns.length > 0) {
  223. this.selectComponent('#picker-unit').setIndexes([this.data.unitInDefaultIndex])
  224. }
  225. this.setData({
  226. booUnit: false
  227. })
  228. },
  229. unitConfirm(event) {
  230. const { value, index } = event.detail
  231. this.setData({
  232. 'form.product_unit': value,
  233. unitInDefaultIndex: index
  234. })
  235. this.unitHide()
  236. },
  237. setFormValue(event) {
  238. const { value } = event.detail
  239. const { formkey } = event.target.dataset
  240. let tempForm = {}
  241. switch (formkey) {
  242. case 'product_title':
  243. case 'product_desc':
  244. case 'product_spec':
  245. tempForm[`form.${formkey}`] = value
  246. break
  247. case 'product_all_price':
  248. case 'product_price':
  249. tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0').replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
  250. break
  251. case 'product_count':
  252. tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0')
  253. break
  254. default:
  255. }
  256. this.setData(tempForm)
  257. },
  258. onSubmit(e) {
  259. console.log(e.detail)
  260. }
  261. })