戴艳蓉 3 năm trước cách đây
mục cha
commit
4b361da138
4 tập tin đã thay đổi với 51 bổ sung11 xóa
  1. 1 0
      src/store/getters.js
  2. 16 1
      src/store/modules/user.js
  3. 13 0
      src/utils/auth.js
  4. 21 10
      src/views/product/index.vue

+ 1 - 0
src/store/getters.js

@@ -13,5 +13,6 @@ const getters = {
   roles: state => state.user.roles,
   ad: state => state.user.ad,
   goodsNum: state => state.user.goodsNum,
+  goodsUnit: state => state.user.goodsUnit,
 }
 export default getters

+ 16 - 1
src/store/modules/user.js

@@ -1,4 +1,4 @@
-import { login, logout, userinfo } from '@/apis/user'
+import { login } from '@/apis/user'
 import {
   getToken,
   setToken,
@@ -7,6 +7,8 @@ import {
   setAd,
   getGoodsNum,
   setGoodsNum,
+  getGoodsUnit,
+  setGoodsUnit
 
 } from '@/utils/auth'
 
@@ -17,6 +19,7 @@ const state = {
   roles: ['admin'],
   ad: getAd() ? JSON.parse(getAd()) : {},
   goodsNum: isNaN(getGoodsNum()) ? 0 : parseInt(getGoodsNum()),
+  goodsUnit: isNaN(getGoodsUnit()) ? 0 : parseInt(getGoodsUnit()),
 }
 
 const mutations = {
@@ -44,6 +47,9 @@ const mutations = {
   SET_GOODSNUM: (state, roles) => {
     state.goodsNum = roles
   },
+  SET_GOODSUNIT: (state, roles) => {
+    state.getGoodsUnit = roles
+  },
 
 }
 
@@ -112,6 +118,15 @@ const actions = {
       resolve({ code: 0, data: {}, message: 'ok' })
     })
   },
+  set_goodsUnit({ commit }, item) {
+    // console.log(item);
+    return new Promise((resolve, reject) => {
+      let key = item
+      setGoodsUnit(key)
+      commit('SET_GOODSUNIT', key)
+      resolve({ code: 0, data: {}, message: 'ok' })
+    })
+  },
 }
 
 

+ 13 - 0
src/utils/auth.js

@@ -4,6 +4,7 @@ const TokenKey = 'wuchang-h5-Token'
 const NoticeKey = "wuchang-h5-Notice"
 const AdKey = "wuchang-h5-Ad"
 const goodsNumKey = "wuchang-h5-goodsNum"
+const goodsUnitKey="wuchang-h5-goodsUnit"
 
 export function getToken() {
   return Cookies.get(TokenKey)
@@ -57,3 +58,15 @@ export function removeGoodsNum() {
   return Cookies.remove(goodsNumKey)
 }
 
+
+export function getGoodsUnit() {
+  return Cookies.get(goodsUnitKey)
+}
+
+export function setGoodsUnit(has) {
+  return Cookies.set(goodsUnitKey, has)
+}
+
+export function removeGoodsUnit() {
+  return Cookies.remove(goodsUnitKey)
+}

+ 21 - 10
src/views/product/index.vue

@@ -64,7 +64,7 @@ import asyncRequest from "@/apis/product/index";
 export default {
   mixins: [resToken],
   computed: {
-    ...mapGetters(["ad", "goodsNum"]),
+    ...mapGetters(["ad", "goodsNum", "goodsUnit"]),
   },
   data() {
     return {
@@ -94,8 +94,11 @@ export default {
       }
     },
     async getaddress() {
-      let res = await this.set_user_goodsNum(this.num || this.goodsNum);
-      if (res && res.code === 0) {
+      let numRes = await this.set_user_goodsNum(this.num || this.goodsNum);
+      let unitRes = await this.set_user_goodsUnit(
+        this.radioV || this.goodsUnit
+      );
+      if (numRes && numRes.code === 0 && unitRes && unitRes.code === 0) {
         window.vm.$router.push({
           path: "/address",
           query: {
@@ -144,8 +147,11 @@ export default {
             unit: v1.name,
           };
           this.productList.push(item);
-          this.changePitem(0);
         });
+        let index = this.productList.findIndex(v1 => {
+        return  v1.id == this.goodsUnit+"";
+        });
+        this.changePitem(index === -1 ? 0 : index);
       }
     },
     async initData(id) {
@@ -155,7 +161,6 @@ export default {
         await this.logout();
       } else {
         this.$store.dispatch("user/set_ad", {}).then((res) => {
-          console.log(res);
         });
       }
     },
@@ -185,8 +190,7 @@ export default {
             .then(async () => {
               await this.placeHttpOrder();
             })
-            .catch(() => {
-            });
+            .catch(() => {});
         } else {
           this.show_title("请选择收货地址!");
         }
@@ -197,7 +201,7 @@ export default {
       let model = {
         addrid: this.ad.id,
         num: this.num + "",
-        unit_id:this.radioV
+        unit_id: this.radioV,
       };
       let res = await asyncRequest.add(model);
       this.loading = false;
@@ -224,6 +228,13 @@ export default {
         });
       });
     },
+    async set_user_goodsUnit(unit) {
+      return new Promise((resolve, reject) => {
+        this.$store.dispatch("user/set_goodsUnit", unit).then((res) => {
+          resolve(res);
+        });
+      });
+    },
   },
 };
 </script>
@@ -364,9 +375,9 @@ export default {
       padding: 5px 0 0 16px;
     }
     button {
-      background: #F4D022 ;
+      background: #f4d022;
       color: #333;
-      border: 1px solid #F4D022 ;
+      border: 1px solid #f4d022;
       border-radius: 10px;
       overflow: hidden;
       margin: 0 16px 0 0;