Ver código fonte

始宁农业小程序:代码优化

panyong 1 ano atrás
pai
commit
b784e1ea6e

+ 4 - 3
htmldev/shiningWxMini/app.js

@@ -10,14 +10,14 @@ App({
     // 胶囊按钮位置信息
     const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
     // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度
-    that.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 +
-      menuButtonInfo.height + systemInfo.statusBarHeight
+    that.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight
     that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right
     that.globalData.menuBottom = menuButtonInfo.top - systemInfo.statusBarHeight
     that.globalData.menuHeight = menuButtonInfo.height
     that.fetchSystemConfig()
     that.fetchUserData()
     checkUpdateVersion()
+    wx.removeStorage({ key: this.globalData.businessGoodsEditsStorageKey })
   },
   globalData: {
     wxMiniversion: 'v0.0.0',
@@ -69,7 +69,8 @@ App({
         name: '家庭农场',
         value: '2'
       }
-    ]
+    ],
+    businessGoodsEditsStorageKey: 'businessGoodsEdit'
   },
   async fetchUserData() {
     try {

+ 125 - 83
htmldev/shiningWxMini/pages/businessGoodsEdit/businessGoodsEdit.js

@@ -85,6 +85,13 @@ Page({
       wx.setNavigationBarTitle({
         title: '产品新增'
       })
+
+      try {
+        const response = await wx.getStorage({ key: app.globalData.businessGoodsEditsStorageKey })
+        if (response.errMsg === 'getStorage:ok' && /^{/.test(response.data)) {
+          this.setForm(JSON.parse(response.data))
+        }
+      } catch (err) {}
     }
     app.fetchSystemConfigCallback = () => {
       this.setUnitInColumns()
@@ -161,80 +168,10 @@ Page({
     } catch (err) {}
   },
   async fetchProductDetail() {
-    let temp = {}
     try {
       const { status, data, msg } = await getProductDetail(this.data.form.id)
       if (status) {
-        for (let key in data) {
-          let value = data[key]
-          if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
-
-            if (key === 'product_img_url') {
-              value = [
-                {
-                  'formkey': key,
-                  'url': value
-                }
-              ]
-            }
-            if (key === 'product_rotation_img_list' || key === 'product_detail_img_list') {
-              value = JSON.parse(value).map(item => {
-                return {
-                  'formkey': key,
-                  'url': item
-                }
-              })
-            }
-
-            if (key === 'product_category_id') {
-              const category = this.data.categoryList.filter(item => item.id === value)
-              if (category.length > 0) {
-                value = category[0]
-                temp['categoryInDefaultIndex'] = this.data.categoryList.findIndex(item => item.id === value)
-              }
-            }
-            if (key === 'product_brand_id') {
-              const brand = this.data.brandList.filter(item => item.id === value)
-              if (brand.length > 0) {
-                value = brand[0]
-                temp['brandInDefaultIndex'] = this.data.brandList.findIndex(item => item.id === value)
-              }
-            }
-
-            if (key === 'product_unit') {
-              const unit = this.data.unitInColumns.filter(item => item === value)
-              if (unit.length > 0) {
-                temp['unitInDefaultIndex'] = this.data.unitInColumns.findIndex(item => item === value)
-              }
-            }
-
-            if (key === 'product_all_price' || key === 'product_price') {
-              value = fen2Yuan(value)
-            }
-
-            if (key === 'product_sale_at') {
-              if (value === 0) {
-                value = '现货'
-              } else {
-                const { YYYY, MM, DD} = formatTs(value * 1000)
-
-                temp['saleAtType'] = '1'
-                temp['currentDate'] = value * 1000
-                value = `${YYYY}年${MM}月${DD}日`
-              }
-            }
-
-            if (key === 'product_check_status') {
-              temp['productCheckStatus'] = value
-            }
-
-            if (this.data.form.hasOwnProperty(key)) {
-              temp[`form.${key}`] = value
-            }
-          }
-        }
-
-        this.setData(temp)
+        this.setForm(data)
       } else {
         wx.showToast({
           title: msg,
@@ -245,6 +182,7 @@ Page({
   },
   ...uploadJS,
   uploadCallBack(res) {
+    const that = this
     const temp = res.map(item => {
       return {
         'url': item.url,
@@ -270,7 +208,9 @@ Page({
     }
 
     if (Object.keys(tempForm).length > 0) {
-      this.setData(tempForm)
+      this.setData(tempForm, () => {
+        that.updateBusinessGoodsEditsDataOfStorage(formkey, that.data.form[formkey].map(item => item.url))
+      })
     }
   },
   categoryShow() {
@@ -293,6 +233,8 @@ Page({
     this.setData({
       'form.product_category_id': value,
       categoryInDefaultIndex: index
+    }, () => {
+      this.updateBusinessGoodsEditsDataOfStorage('product_category_id', value.id)
     })
 
     this.categoryHide()
@@ -317,6 +259,8 @@ Page({
     this.setData({
       'form.product_brand_id': value,
       brandInDefaultIndex: index
+    }, () => {
+      this.updateBusinessGoodsEditsDataOfStorage('product_brand_id', value.id)
     })
 
     this.brandHide()
@@ -341,6 +285,8 @@ Page({
     this.setData({
       'form.product_unit': value,
       unitInDefaultIndex: index
+    }, () => {
+      this.updateBusinessGoodsEditsDataOfStorage('product_unit', value)
     })
 
     this.unitHide()
@@ -366,13 +312,8 @@ Page({
       default:
     }
 
-    this.setData(tempForm)
-  },
-  handleRadio(event) {
-    const { formkey, item } = event.currentTarget.dataset
-
-    this.setData({
-      [`form.${formkey}`]: item.value
+    this.setData(tempForm, () => {
+      this.updateBusinessGoodsEditsDataOfStorage(formkey, this.data.form[formkey])
     })
   },
   saleAtTypeShow() {
@@ -389,7 +330,9 @@ Page({
       tempForm['booSaleAt'] = true
     }
 
-    this.setData(tempForm)
+    this.setData(tempForm, () => {
+      this.updateBusinessGoodsEditsDataOfStorage('product_sale_at', 0)
+    })
   },
   handleSaleAtType(e) {
     const { type } = e.currentTarget.dataset
@@ -411,6 +354,8 @@ Page({
     this.setData({
       currentDate: event.detail,
       'form.product_sale_at': `${YYYY}年${MM}月${DD}日`
+    }, () => {
+      this.updateBusinessGoodsEditsDataOfStorage('product_sale_at', getTs(this.data.form.product_sale_at, 'YYYY年MM月DD日', 's'))
     })
 
     this.saleAtHide()
@@ -525,10 +470,10 @@ Page({
       } else {
         res = await postAddProduct(temp)
       }
-      const { status, data, msg } = res
+      const { status, msg } = res
       if (status) {
-        wx.redirectTo({
-          url: '/pages/businessGoodsManage/businessGoodsManage'
+        wx.navigateBack({
+          delta: 1
         })
       } else {
         wx.showToast({
@@ -544,5 +489,102 @@ Page({
     this.setData({
       unitInColumns: Array.isArray(product_unit_list) && product_unit_list.length > 0 ? product_unit_list : []
     })
+  },
+  setForm(data) {
+    let temp = {}
+    for (let key in data) {
+      let value = data[key]
+      if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
+        if (key === 'product_img_url') {
+          value = [
+            {
+              'formkey': key,
+              'url': Array.isArray(value) ? value.join('') : value
+            }
+          ]
+        }
+        if (key === 'product_rotation_img_list' || key === 'product_detail_img_list') {
+          value = (Array.isArray(value) ? value : JSON.parse(value)).map(item => {
+            return {
+              'formkey': key,
+              'url': item
+            }
+          })
+        }
+
+        if (key === 'product_category_id') {
+          const category = this.data.categoryList.filter(item => item.id === value)
+          if (category.length > 0) {
+            value = category[0]
+            temp['categoryInDefaultIndex'] = this.data.categoryList.findIndex(item => item.id === value.id)
+          }
+        }
+        if (key === 'product_brand_id') {
+          const brand = this.data.brandList.filter(item => item.id === value)
+          if (brand.length > 0) {
+            value = brand[0]
+            temp['brandInDefaultIndex'] = this.data.brandList.findIndex(item => item.id === value.id)
+          }
+        }
+
+        if (key === 'product_unit') {
+          const unit = this.data.unitInColumns.filter(item => item === value)
+          if (unit.length > 0) {
+            temp['unitInDefaultIndex'] = this.data.unitInColumns.findIndex(item => item === value)
+          }
+        }
+
+        if (key === 'product_all_price' || key === 'product_price') {
+          value = fen2Yuan(value)
+        }
+
+        if (key === 'product_sale_at') {
+          if (value === 0) {
+            value = '现货'
+          } else {
+            const { YYYY, MM, DD } = formatTs(value * 1000)
+
+            temp['saleAtType'] = '1'
+            temp['currentDate'] = value * 1000
+            value = `${YYYY}年${MM}月${DD}日`
+          }
+        }
+
+        if (key === 'product_check_status') {
+          temp['productCheckStatus'] = value
+        }
+
+        if (this.data.form.hasOwnProperty(key)) {
+          temp[`form.${key}`] = value
+        }
+      }
+    }
+
+    this.setData(temp)
+  },
+  async updateBusinessGoodsEditsDataOfStorage(formKey, value) {
+    const key = app.globalData.businessGoodsEditsStorageKey
+    let temp = {}
+
+    if (this.data.form.id) {
+      return
+    }
+
+    try {
+      const { data, errMsg } = await wx.getStorage({ key })
+      if (errMsg === 'getStorage:ok' && /^{/.test(data)) {
+        temp = JSON.parse(data)
+      }
+    } catch (e) {
+      temp = {}
+    }
+
+    wx.setStorage({
+      key,
+      data: JSON.stringify({
+        ...temp,
+        [formKey]: value
+      })
+    })
   }
 })