瀏覽代碼

增加兑换时的条件验证

Joe 4 年之前
父節點
當前提交
380bf38beb
共有 5 個文件被更改,包括 57 次插入43 次删除
  1. 22 8
      pages/point/detail/_id.vue
  2. 0 7
      pages/point/index.vue
  3. 2 1
      pages/point/list.vue
  4. 33 26
      pages/point/pay/_id.vue
  5. 0 1
      store/authen.js

+ 22 - 8
pages/point/detail/_id.vue

@@ -20,13 +20,13 @@
       <div class="text">- 图文详情 -</div>
     </div> -->
     <div class="buy">
-      <nuxt-link :to="'/point/pay/' + this.id" tag="button">立刻兑换</nuxt-link>
+      <button @click="exchange">立刻兑换</button>
     </div>
   </div>
 </template>
 
 <script>
-import { mapActions, mapGetters } from 'vuex'
+import { mapActions, mapState, mapGetters } from "vuex";
 export default {
   validate({ params }) {
     // 必须是number类型
@@ -39,8 +39,8 @@ export default {
   },
   data() {
     return {
-      ware:{},
-      id:this.$route.params.id
+      ware: {},
+      id: this.$route.params.id,
     };
   },
   mounted() {
@@ -48,17 +48,31 @@ export default {
     this.init();
   },
   computed: {
+    ...mapState({
+      pointInfo:state=>state.point.pointInfo
+    }),
     ...mapGetters({
       findWare: "point/findWare",
     }),
   },
   methods: {
     ...mapActions({
-      getPointInfo:"point/getPointInfo"
+      getPointInfo: "point/getPointInfo",
     }),
-    init(){
-      this.ware = this.findWare(this.id)
-    }
+    init() {
+      this.ware = this.findWare(this.id);
+    },
+    exchange() {
+      if (this.ware.waresCount <= 0) {
+        alert("该商品库存不足");
+      } else if (this.ware.saleIntegral > this.pointInfo.points) {
+        alert("您的积分不足");
+      } else {
+        this.$router.push({
+          path: "/point/pay/" + this.id,
+        });
+      }
+    },
   },
 };
 </script>

+ 0 - 7
pages/point/index.vue

@@ -60,16 +60,9 @@ export default {
       pointInfo:state=>state.point.pointInfo
     })
   },
-  beforeCreate() {},
-  created() {},
-  beforeMount() {},
   mounted() {
     this.getPointInfo();
   },
-  beforeUpdate() {},
-  updated() {},
-  beforeDestroy() {},
-  destroyed() {},
   methods: {
     ...mapActions({
       getPointInfo:"point/getPointInfo"

+ 2 - 1
pages/point/list.vue

@@ -57,7 +57,8 @@
         </div>
       </div>
     </div>
-  </div> -->
+    -->
+  </div> 
 </template>
 
 <script>

+ 33 - 26
pages/point/pay/_id.vue

@@ -5,19 +5,22 @@
         <img :src="ware.waresPic" alt="" />
       </div>
       <div class="content">
-        <div class="title">{{ware.waresName}}</div>
+        <div class="title">{{ ware.waresName }}</div>
         <div class="amount">
-          <span class="text">积分 {{ware.saleIntegral}}</span> <span class="num">x 1</span>
+          <span class="text">积分 {{ ware.saleIntegral }}</span>
+          <span class="num">x 1</span>
         </div>
       </div>
     </div>
     <div class="have">
       <span class="icon1"></span>
-      <span class="text">当前可用积分余额 {{pointInfo.points}}</span>
+      <span class="text">当前可用积分余额 {{ pointInfo.points }}</span>
       <span class="icon2"></span>
     </div>
     <div class="total">
-      <div class="text">合计:<span class="num">{{ware.saleIntegral}}积分</span></div>
+      <div class="text">
+        合计:<span class="num">{{ ware.saleIntegral }}积分</span>
+      </div>
       <button @click="pay">提交订单</button>
     </div>
   </div>
@@ -50,42 +53,46 @@ export default {
   computed: {
     ...mapState({
       pointInfo: (state) => state.point.pointInfo,
-      userInfo:(state) => state.authen.userInfo
+      userInfo: (state) => state.authen.userInfo,
     }),
     ...mapGetters({
       userInfo: "authen/userInfo",
       findWare: "point/findWare",
     }),
     ware() {
-      console.log("ware");
-      return this.findWare(this.id)
-    }
+      return this.findWare(this.id);
+    },
   },
   components: {
     ProductList,
   },
   mounted() {
-    console.log(this);
+    
   },
   methods: {
-    pay(){
-      this.$axios.$post("insertIntegralOrder",{
-        "waresId":this.id,
-        "waresName":this.ware.waresName, 
-        "openId":this.openId,
-        "unionId":this.unionId,
-        "customerName":this.userInfo.nickname,
-        "exchangeNum":1,
-        "integral":this.ware.saleIntegral,
-        "stationId":this.stationId
-      }).then((res)=>{
-        this.$router.push({
-          path:"/point/success"
+    pay() {
+      this.$axios
+        .$post("insertIntegralOrder", {
+          waresId: this.id,
+          waresName: this.ware.waresName,
+          openId: this.openId,
+          unionId: this.unionId,
+          customerName: this.userInfo.nickname,
+          exchangeNum: 1,
+          integral: this.ware.saleIntegral,
+          stationId: this.stationId,
         })
-      })
-      
-    }
-
+        .then((res) => {
+          console.log(res);
+          if (res.retCode === 0) {
+            this.$router.push({
+              path: "/point/success",
+            });
+          }else{
+            alert("兑换失败")
+          }
+        });
+    },
   },
 };
 </script>

+ 0 - 1
store/authen.js

@@ -47,7 +47,6 @@ export const mutations = {
     state.unionIdOrigin = unionId
   },
   setUserInfo(state, userInfo) {
-    console.log("setUserInfo",userInfo);
     state.userInfoOrigin = userInfo
   }
 }