Просмотр исходного кода

优化 mallApp/src/pages/order/buy.vue
有以下优化与清理:
- 删除未使用的导入与组件:移除 `vuex` 的 `mapGetters`、`AppTabs/AppCouponSel/AppTrademark/AlertModule`,剔除 `freight` 未使用导入。
- 新增 `onLoad(option)` 初始化:保存 `option`,并在页面加载时调用 `init()`,修复之前依赖 `this.option` 但未赋值的问题。
- 健壮化数据初始化:`init()` 中为 `buyGoodsDetil` 与 `option` 做空值与数值转换处理,避免 `NaN`。
- 切换配送逻辑优化:`changeSendType(2)` 时用 `$nextTick` 再取 `$refs`,避免未渲染完成导致的空引用。
- 地址与运费计算更安全:`changeAddress()` 中对 `$refs.appDelivery` 的存在与 `region` 做校验;返回数据做空值兼容,并同步 `form.sendDistance`。
- 提交时校验表单:当 `sendType==2` 时校验 `$refs.appDelivery.form` 的必填项;失败时直接 `return` 而非 `return false`;补充了 `catch` 中隐藏 loading。
- 模板合规:将底部应付款区域的 `span` 替换为 `text/view` 并删除空 `span`,符合 uni-app 组件规范。
- 移除未用的 `data` 字段:删除 `tabIndex/showAlert/discountArr/goodsNum/memberDiscount/couponDiscountAmount` 等未使用字段。
- 计算属性更健壮:`mayPayAmount` 对空值做默认处理,避免 `NaN`。

shizhongqi 11 месяцев назад
Родитель
Сommit
04db09fbf2
1 измененных файлов с 40 добавлено и 39 удалено
  1. 40 39
      mallApp/src/pages/order/buy.vue

+ 40 - 39
mallApp/src/pages/order/buy.vue

@@ -50,34 +50,24 @@
   </view>
 </template>
 <script>
-import { mapGetters } from "vuex";
-import AppTabs from "@/components/plugin/tabs";
 import TuiListCell from "@/components/plugin/list-cell";
 import AppDeliveryModule from "@/components/app-delivery";
-import AppTrademark from "@/components/module/app-trademark";
 import AppListModule from "@/components/module/app-order-list2";
-import AppCouponSel from "@/components/app-coupon-sel";
-import AlertModule from "@/components/plugin/alert";
 import { getShopUser } from "@/utils/auth";
 import { getOrderShop } from "@/utils/config";
-import { createOrder,freight } from "@/api/order";
+import { createOrder } from "@/api/order";
 import { getUserDistance, getHasMap } from "@/api/express";
 export default {
   name: "buy",
   components: {
-    AppTabs,
     AppDeliveryModule,
     TuiListCell,
-    AppListModule,
-    AppCouponSel,
-    AppTrademark,
-    AlertModule
+    AppListModule
   },
   data() {
     return {
+      option: {},
       goodsInfo: {},
-      tabIndex: 0,
-      showAlert: false,
       form: {
         sendDistance:"",
         goodsId: "",
@@ -88,85 +78,94 @@ export default {
         lat:'',
         long:''
       },
-      discountArr: false,
       freightPrice: 0,
-      goodsNum: 1,
       showDistance: 0,
       goodsTotalPrice: 0,
-      memberDiscount: 1,
-      couponDiscountAmount: 0,
       hasMap: 0 // 是否具备定位功能 0不具备 1具备
     };
   },
+  onLoad(option) {
+    this.option = option || {};
+    this.init();
+  },
   mounted() {},
   computed: {
-    ...mapGetters({ orderShop: "getOrderShop" }),
-    ...mapGetters({ shopUser: "getShopUser" }),
     mayPayAmount(){
-      let price = this.goodsInfo.price * this.goodsInfo.buyNum
+      let price = (this.goodsInfo.price || 0) * (this.goodsInfo.buyNum || 0)
       price = Number(this.freightPrice)+Number(price)
       return parseFloat(price.toFixed(2))
     }
   },
   methods: {
     init() {
-      let goodsInfo = uni.getStorageSync("buyGoodsDetil")
+      let goodsInfo = uni.getStorageSync("buyGoodsDetil") || {}
 
       getOrderShop()
       getShopUser();
 
-      goodsInfo.buyNum = this.option.goodsNum;
-      this.goodsInfo = goodsInfo;
+      const goodsNum = Number(this.option.goodsNum || goodsInfo.buyNum || 0)
+      goodsInfo.buyNum = goodsNum
+      this.goodsInfo = goodsInfo
 
-      this.form.goodsId = this.option.goodsId
-      this.form.goodsNum = this.option.goodsNum
+      this.form.goodsId = this.option.goodsId || ""
+      this.form.goodsNum = goodsNum
 
-      this.goodsTotalPrice = goodsInfo.price * this.option.goodsNum;
-      this.goodsTotalPrice = this.goodsTotalPrice.toFixed(2)
-      this.goodsTotalPrice = parseFloat(this.goodsTotalPrice)
+      const total = Number(goodsInfo.price || 0) * goodsNum
+      this.goodsTotalPrice = parseFloat(total.toFixed(2))
 
       getHasMap().then(res => {
-        this.hasMap = res.data.hasMap || 0
+        this.hasMap = (res.data && res.data.hasMap) ? res.data.hasMap : 0
       })
 
     },
 		changeSendType(num){
 			this.form.sendType = num
 			if(num == 2){
-        this.changeAddress()
+				this.$nextTick(()=>{
+					this.changeAddress()
+				})
 			}
 		},
     changeAddress() {
-      let lat = this.$refs.appDelivery.region.latitude;
-      let long = this.$refs.appDelivery.region.longitude;
+      const ref = this.$refs.appDelivery
+      if(!ref || !ref.region) return
+      let lat = ref.region.latitude;
+      let long = ref.region.longitude;
       this.form.lat = lat
       this.form.long = long
       getUserDistance({ lat: lat, lng: long }).then(res => {
-        this.freightPrice = res.data.fee?parseFloat(res.data.fee):0;
-        this.showDistance = res.data.showDistance;
+        const data = res.data || {}
+        this.freightPrice = data.fee?parseFloat(data.fee):0;
+        this.showDistance = data.showDistance || 0;
+        this.form.sendDistance = this.showDistance
       });
     },
-    formSubmit(e) {
+    formSubmit() {
       this.createOrderFn()
     },
     createOrderFn() {
       let form = {}
       if (this.form.sendType == 2) {
-        form = this.$refs.appDelivery.form
+        const deliveryRef = this.$refs.appDelivery
+        if(!deliveryRef || !deliveryRef.form){
+          this.$msg("请完善配送信息")
+          return
+        }
+        form = deliveryRef.form
         let receiveUserName = form.receiveUserName;
         if (this.$util.isEmpty(receiveUserName)) {
           this.$msg("请填写收花人姓名");
-          return false;
+          return;
         }
         let receiveMobile = form.receiveMobile;
         if (this.$util.isEmpty(receiveMobile)) {
           this.$msg("请填写收花人电话");
-          return false;
+          return;
         }
         let address = form.address;
         if (this.$util.isEmpty(address)) {
           this.$msg("请填写地址");
-          return false;
+          return;
         }
       }
       uni.showLoading({mask:true})
@@ -176,6 +175,8 @@ export default {
         uni.hideLoading()
         uni.removeStorageSync("buyGoodsDetil")
         this.$util.pageTo({ url: "/pages/callback/pay", type: 2, query: { pageStatus: 1, ...res.data,hdId:hdId,account:account } })
+      }).catch(()=>{
+        uni.hideLoading()
       })
     }
   }