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

fix(home): 修复首页无效状态展示

- 清理 hdApp H5 开发环境过期凭证并兜底字典请求异常
- 过滤 mallApp 首页普通及活动专区的无库存商品
shizhongqi 14 часов назад
Родитель
Сommit
2458d7e911

+ 33 - 14
hdApp/src/App.vue

@@ -25,23 +25,42 @@ export default {
     // 推送消息监听改在登录成功后注册,见 utils/appDevice.js setupAppPushMessageListener
 
     // #ifdef H5
-    // H5开发环境无法微信授权,使用固定的用户 shish 2020.4.30
+    // H5 开发环境无微信授权:仅补齐 account;不再写入过期硬编码 token(会覆盖登录态并触发 /console/init 500)
+    // 有本地 token 时拉取字典;必须 catch,否则 reject 的普通对象会被 webpack overlay 显示成 [object Object]
     if (process.env.NODE_ENV == "development") {
-      uni.setStorageSync("token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjBfNTIifQ.eyJpc3MiOiJodHRwczpcL1wvYXBpLnNob3AuaHpnaGQuY29tIiwiYXVkIjoiaHR0cHM6XC9cL2FwaS5zaG9wLmh6Z2hkLmNvbSIsImp0aSI6IjBfNTIiLCJpYXQiOjE2MTYxNTc2NjQsIm5iZiI6MTYxNjE1NzY2NCwiZXhwIjoxNjI0Nzk3NjY0LCJ1bmlxdWVJZCI6IjUyIiwic291cmNlSWQiOjB9.VzSekbSwSczMWsfdPVdrm3gZ-v9o3p01NbMVNT4nGZ4")
-      uni.setStorageSync("account", "0")
-      getDictionaries({ token: uni.getStorageSync('token') }).then(res => {
-        if (util.isEmpty(res)) {
-          return false
+      if (!uni.getStorageSync('account')) {
+        uni.setStorageSync("account", "0")
+      }
+      let h5Token = uni.getStorageSync('token')
+      // 清理历史硬编码/已过期 JWT,避免每次启动都打出 500 并干扰登录态
+      if (h5Token) {
+        try {
+          const payload = JSON.parse(atob(h5Token.split('.')[1] || ''))
+          if (payload.exp && payload.exp * 1000 < Date.now()) {
+            uni.removeStorageSync('token')
+            h5Token = ''
+          }
+        } catch (e) {
+          // 非 JWT 结构时保留原 token,交由接口鉴权
         }
-        store.commit('setDictionariesInfo', {
-          ...res.data.dict,
-          ...res.data
+      }
+      if (h5Token) {
+        getDictionaries({ token: h5Token }).then(res => {
+          if (util.isEmpty(res)) {
+            return false
+          }
+          store.commit('setDictionariesInfo', {
+            ...res.data.dict,
+            ...res.data
+          })
+          store.commit('setMyShopInfo', {
+            ...res.data.shop,
+            ...res.data
+          })
+        }).catch(err => {
+          console.error('H5 拉取字典失败(token 可能失效,请重新登录)', err && (err.errMsg || err.msg || err.statusCode) || err)
         })
-		store.commit('setMyShopInfo', {
-			...res.data.shop,
-			...res.data
-		})
-      })
+      }
     }
     // #endif
 

+ 11 - 3
mallApp/src/components/home/activitySection.vue

@@ -5,7 +5,7 @@
   秒杀点击跳商品详情;团购点击跳团购详情页(开团/参团入口)
 -->
 <template>
-  <view v-if="isActive && data && data.enabled == 1 && data.goods && data.goods.length" class="home-activity">
+  <view v-if="isActive && data && data.enabled == 1 && displayGoods.length" class="home-activity">
     <view class="activity-head">
       <view class="activity-head-row">
         <view class="activity-title-row">
@@ -30,7 +30,7 @@
 
     <!-- 1排1列:左图右文,底部价格与按钮同行 -->
     <view v-if="cols === 1" class="list-1">
-      <view v-for="g in data.goods" :key="g.id" class="row-card" @click="goDetail(g)">
+      <view v-for="g in displayGoods" :key="g.id" class="row-card" @click="goDetail(g)">
         <image class="row-cover" :src="g.coverUrl" mode="aspectFill" />
         <view class="row-content">
           <text class="row-name">{{ g.name }}</text>
@@ -48,7 +48,7 @@
 
     <!-- 1排2列 / 1排3列:网格 -->
     <view v-else class="goods-grid" :class="'cols-' + cols">
-      <view v-for="g in data.goods" :key="g.id" class="grid-card" @click="goDetail(g)">
+      <view v-for="g in displayGoods" :key="g.id" class="grid-card" @click="goDetail(g)">
         <view class="grid-cover-wrap">
           <image class="grid-cover" :src="g.coverUrl" mode="aspectFill" />
           <view v-if="isGroupBuy" class="group-badge">{{ g.groupSize }}人团</view>
@@ -106,6 +106,14 @@ export default {
       const c = parseInt(this.data && this.data.layoutCols, 10)
       return [1, 2, 3].includes(c) ? c : 3
     },
+    /**
+     * 仅展示有库存活动商品:stock <= 0 视为售罄,专区不展示
+     * @returns {Array}
+     */
+    displayGoods() {
+      const list = (this.data && this.data.goods) || []
+      return list.filter(g => Number(g && g.stock) > 0)
+    },
     /** 展示数量小于商品总数时显示「更多」 */
     showMore() {
       return Number(this.data && this.data.goodsTotal || 0) > ((this.data && this.data.goods) || []).length

+ 11 - 3
mallApp/src/components/home/goodsSection.vue

@@ -4,7 +4,7 @@
   点击商品跳转详情;点击加号/购物车:单规格直接加购,多规格跳转详情
 -->
 <template>
-  <view v-if="data && data.enabled == 1 && data.goods && data.goods.length" class="home-goods-section">
+  <view v-if="data && data.enabled == 1 && displayGoods.length" class="home-goods-section">
     <view class="section-head">
       <text class="section-title">{{ data.name }}</text>
       <text v-if="showMore" class="section-more" @click="goMore">更多 ></text>
@@ -12,7 +12,7 @@
 
     <!-- 1排1列:横向列表 -->
     <view v-if="cols === 1" class="list-1">
-      <view v-for="g in data.goods" :key="g.id" class="row-card" @click="goDetail(g)">
+      <view v-for="g in displayGoods" :key="g.id" class="row-card" @click="goDetail(g)">
         <image class="row-cover" :src="g.coverUrl" mode="aspectFill" />
         <view class="row-body">
           <text class="row-name">{{ g.name }}</text>
@@ -25,7 +25,7 @@
 
     <!-- 1排2列 / 1排3列:网格 -->
     <view v-else class="goods-grid" :class="'cols-' + cols">
-      <view v-for="g in data.goods" :key="g.id" class="grid-card" @click="goDetail(g)">
+      <view v-for="g in displayGoods" :key="g.id" class="grid-card" @click="goDetail(g)">
         <view class="grid-cover-wrap">
           <image class="grid-cover" :src="g.coverUrl" mode="aspectFill" />
         </view>
@@ -83,6 +83,14 @@ export default {
       const c = parseInt(this.data && this.data.layoutCols, 10)
       return [1, 2, 3].includes(c) ? c : 3
     },
+    /**
+     * 仅展示有库存商品:stock <= 0 视为售罄,首页不展示避免用户点进无货详情
+     * @returns {Array}
+     */
+    displayGoods() {
+      const list = (this.data && this.data.goods) || []
+      return list.filter(g => Number(g && g.stock) > 0)
+    },
     showMore() {
       return Number(this.data && this.data.goodsTotal || 0) > ((this.data && this.data.goods) || []).length
     }