Browse Source

fix(shop): 购物车总价适配iOS安全区+首页/商城双入口常驻显眼

Agnes Agent 2 hours ago
parent
commit
1e8f2081f5

+ 231 - 0
cfc-frontend/__tests__/pages/shop/cartLayout.contract.test.js

@@ -0,0 +1,231 @@
+/**
+ * 契约级单测:购物车总价安全区适配 + 商城/首页双入口常驻显眼
+ * 设计正本:docs/design/2026-11-26-cart-spec.md §6.1 / §6.2 / §6.3
+ *
+ * 沿用项目既有 SFC 源码抽取式约定(见 __tests__/pages/health/healthSurvey.memberPicker.test.js):
+ * jest 无 vue-jest/jsdom,直接读取真实 .vue 原文断言样式/模板,
+ * <script> 抽取时把 import 替换为桩,再真实调用 computeQuickApps。
+ */
+import fs from 'fs'
+import path from 'path'
+
+const ROOT = path.join(__dirname, '../../..')
+const CART_PATH = path.join(ROOT, 'pages/shop/cart/cart.vue')
+const SHOP_INDEX_PATH = path.join(ROOT, 'pages/shop/index/index.vue')
+const HOME_INDEX_PATH = path.join(ROOT, 'pages/index-home/index.vue')
+
+function read(file) {
+  return fs.readFileSync(file, 'utf8')
+}
+
+function styleOf(src) {
+  const m = src.match(/<style[^>]*>([\s\S]*?)<\/style>/)
+  return m ? m[1] : ''
+}
+
+function templateOf(src) {
+  const m = src.match(/<template>([\s\S]*?)<\/template>/)
+  return m ? m[1] : ''
+}
+
+/** 提取指定 class 的样式块内容 */
+function ruleOf(style, selector) {
+  const re = new RegExp(selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\s*\\{([\\s\\S]*?)\\}')
+  const m = style.match(re)
+  return m ? m[1] : ''
+}
+
+/** 把 import 语句替换为全局桩读取(与 healthSurvey.memberPicker.test.js 同约定) */
+function stubImports(script) {
+  return script.replace(/import\s+([^;]+?)\s+from\s+['"]([^'"]+)['"];?/g, function (m, clause, id) {
+    var names = []
+    var c = clause.trim()
+    var braceIdx = c.indexOf('{')
+    if (braceIdx >= 0) {
+      var def = c.slice(0, braceIdx).replace(/,\s*$/, '').trim()
+      if (def) names.push(def)
+      var named = c.slice(braceIdx + 1, c.lastIndexOf('}'))
+      named.split(',').forEach(function (part) {
+        var p = part.trim()
+        if (!p) return
+        var parts = p.split(/\s+as\s+/)
+        var local = parts.length > 1 ? parts[1].trim() : parts[0].trim()
+        if (local) names.push(local)
+      })
+    } else if (c.indexOf('*') === 0) {
+      var nsMatch = c.match(/\*\s+as\s+(\w+)/)
+      if (nsMatch) names.push(nsMatch[1])
+    } else {
+      names.push(c)
+    }
+    return names.map(function (n) {
+      // 惰性取桩:loadSfcScript 在 describe 定义期执行,此时 beforeEach 尚未设置逐例桩
+      return 'var ' + n + ' = function() { var s = (globalThis.__cartStubs[' + JSON.stringify(id) +
+        '] || {})[' + JSON.stringify(n) + ']; return typeof s === "function" ? s.apply(this, arguments) : s; };'
+    }).join('\n')
+  })
+}
+
+function loadSfcScript(file) {
+  if (!globalThis.__cartStubs) globalThis.__cartStubs = {}
+  var src = read(file)
+  var scriptMatch = src.match(/<script>([\s\S]*?)<\/script>/)
+  if (!scriptMatch) throw new Error('无法定位 <script>: ' + file)
+  var script = stubImports(scriptMatch[1])
+  script = script.replace(/export default\s*\{/, 'module.exports = {')
+  var mod = { exports: {} }
+  new Function('module', 'exports', script)(mod, mod.exports)
+  return mod.exports
+}
+
+beforeEach(function () {
+  globalThis.__cartStubs = {}
+})
+
+// ---------- bug① cart.vue 底部结算栏 ----------
+describe('cart.vue 底部结算栏:总价完整显示 + iOS 安全区', function () {
+  const style = styleOf(read(CART_PATH))
+
+  test('bottom-bar 用 min-height:112rpx 替代定高 120rpx', function () {
+    expect(style).toContain('min-height: 112rpx')
+    expect(style).not.toContain('height: 120rpx')
+  })
+
+  test('bottom-bar 双行安全区兜底(constant 旧 iOS + env 新 iOS)', function () {
+    expect(style).toContain('padding-bottom: calc(20rpx + constant(safe-area-inset-bottom))')
+    expect(style).toContain('padding-bottom: calc(20rpx + env(safe-area-inset-bottom, 0px))')
+    expect(style).toContain('padding: 20rpx $uni-space-page')
+  })
+
+  test('bottom-bar 层级降到 10,不压过弹窗 30', function () {
+    const bar = ruleOf(style, '.bottom-bar')
+    expect(bar).toContain('z-index: 10')
+    expect(bar).not.toContain('z-index: 100')
+  })
+
+  test('价格数字 nowrap、左右块 flex-shrink:0、right 块 min-width:0 + margin-left:12rpx', function () {
+    expect(style).toContain('white-space: nowrap')
+    expect(ruleOf(style, '.bottom-left')).toContain('flex-shrink: 0')
+    const right = ruleOf(style, '.bottom-right')
+    expect(right).toContain('flex-shrink: 0')
+    expect(right).toContain('min-width: 0')
+    expect(right).toContain('margin-left: 12rpx')
+    expect(ruleOf(style, '.total-amount')).toContain('white-space: nowrap')
+    expect(ruleOf(style, '.price-estimate')).toContain('white-space: nowrap')
+    expect(ruleOf(style, '.price-original')).toContain('white-space: nowrap')
+  })
+
+  test('会员态双行价格:price-compare 基线对齐不折行,标签 28rpx', function () {
+    expect(ruleOf(style, '.price-compare')).toContain('display: flex')
+    expect(ruleOf(style, '.price-compare')).toContain('align-items: baseline')
+    expect(ruleOf(style, '.price-compare')).toContain('gap: 8rpx')
+    expect(ruleOf(style, '.price-compare')).toContain('white-space: nowrap')
+    expect(style).toMatch(/price-original-label\s*,\s*\.price-estimate-label\s*\{/)
+    expect(style).toContain('calc(28rpx * var(--font-scale, 1))')
+  })
+})
+
+// ---------- bug② 之一:商城页购物车入口 ----------
+describe('shop/index.vue 购物车入口:88rpx + 品牌浅底 + 非 emoji 图标', function () {
+  const src = read(SHOP_INDEX_PATH)
+  const style = styleOf(src)
+  const tpl = templateOf(src)
+
+  test('cart-btn 尺寸 88rpx、品牌浅底、品牌描边', function () {
+    const btn = ruleOf(style, '.cart-btn')
+    expect(btn).toContain('width: 88rpx')
+    expect(btn).toContain('height: 88rpx')
+    expect(btn).toContain('background: $uni-primary-pale')
+    expect(btn).toContain('border: 1rpx solid $brand-primary-light-2')
+    expect(btn).toContain('flex-shrink: 0')
+  })
+
+  test('模板与样式中不得出现 emoji 🛒', function () {
+    expect(tpl).not.toContain('🛒')
+    expect(style).not.toContain('🛒')
+  })
+
+  test('图标为纯 CSS 购物车轮廓(伪元素 + border),角标在 88rpx 容器内 top/right -4rpx', function () {
+    expect(style).toContain('.cart-icon::before')
+    expect(style).toContain('border: 3rpx solid $brand-primary')
+    const badge = ruleOf(style, '.cart-badge')
+    expect(badge).toContain('top: -4rpx')
+    expect(badge).toContain('right: -4rpx')
+    expect(badge).toContain('min-width: 32rpx')
+    expect(badge).toContain('height: 32rpx')
+    expect(tpl).toContain("cartCount > 99 ? '99+' : cartCount")
+  })
+})
+
+// ---------- bug② 之二:首页快捷区 cart 常驻 ----------
+describe('index-home/index.vue computeQuickApps:cart 常驻保障', function () {
+  const homeSrc = read(HOME_INDEX_PATH)
+  const component = loadSfcScript(HOME_INDEX_PATH)
+  const compute = component.methods.computeQuickApps
+  const vm = { memberActive: true }
+
+  function appKeys() {
+    return ['task', 'interaction', 'tiandi', 'members', 'circle', 'shop', 'growth', 'wish']
+  }
+
+  test('源码含 cart 常驻保障(appKey 判定 + 追加/替换 + 全锁定尊重)', function () {
+    const body = homeSrc.slice(
+      homeSrc.indexOf('computeQuickApps: function'),
+      homeSrc.indexOf('getDefaultQuickApps: function')
+    )
+    expect(body).toContain("item.appKey === 'cart'")
+    expect(body).toContain("result.push({ appKey: 'cart', locked: 0, sortOrder: 99 })")
+    expect(body).toContain("result[i].locked !== 1")
+  })
+
+  test('未满 6 格且无 cart:追加 cart 到末位', function () {
+    globalThis.__cartStubs['../../utils/app-library.js'] = {
+      getShortcutableApps: function () {
+        return appKeys().slice(0, 3).map(function (k) { return { key: k, name: k } })
+      }
+    }
+    const shortcuts = appKeys().slice(0, 6).map(function (k, i) {
+      return { appKey: k, locked: 0, sortOrder: i }
+    })
+    const result = compute.call(vm, shortcuts, {})
+    expect(result.length).toBe(4)
+    expect(result.some(function (x) { return x.appKey === 'cart' })).toBe(true)
+    expect(result[result.length - 1].appKey).toBe('cart')
+  })
+
+  test('满 6 格且无 cart:替换最后一个非锁定项,cart 常驻且总数仍为 6', function () {
+    globalThis.__cartStubs['../../utils/app-library.js'] = {
+      getShortcutableApps: function () {
+        return appKeys().map(function (k) { return { key: k, name: k } })
+      }
+    }
+    const shortcuts = appKeys().slice(0, 6).map(function (k, i) {
+      return { appKey: k, locked: 0, sortOrder: i }
+    })
+    const result = compute.call(vm, shortcuts, {})
+    expect(result.length).toBe(6)
+    expect(result.some(function (x) { return x.appKey === 'cart' })).toBe(true)
+  })
+
+  test('6 个全锁定且无 cart:尊重用户锁定选择,不追加也不替换', function () {
+    globalThis.__cartStubs['../../utils/app-library.js'] = {
+      getShortcutableApps: function () {
+        return appKeys().map(function (k) { return { key: k, name: k } })
+      }
+    }
+    const shortcuts = appKeys().slice(0, 6).map(function (k, i) {
+      return { appKey: k, locked: 1, sortOrder: i }
+    })
+    const result = compute.call(vm, shortcuts, {})
+    expect(result.length).toBe(6)
+    expect(result.every(function (x) { return x.locked === 1 })).toBe(true)
+    expect(result.some(function (x) { return x.appKey === 'cart' })).toBe(false)
+  })
+
+  test('cart 项模板有条件 class + 品牌浅底描边样式', function () {
+    expect(homeSrc).toContain("'quick-item--cart': app.key === 'cart'")
+    expect(styleOf(homeSrc)).toContain('.quick-item--cart .quick-icon')
+    expect(styleOf(homeSrc)).toContain('background: $uni-primary-pale !important')
+    expect(styleOf(homeSrc)).toContain('border: 1rpx solid $brand-primary-light-2')
+  })
+})

+ 30 - 2
cfc-frontend/pages/index-home/index.vue

@@ -135,7 +135,7 @@
         <text class="section-more" @click="goAppLibrary">应用库 ›</text>
         <text class="section-more" @click="goAppLibrary">应用库 ›</text>
       </view>
       </view>
       <view class="quick-bar">
       <view class="quick-bar">
-        <view class="quick-item" v-for="(app, idx) in quickApps" :key="app.key" @click="onQuickTap(app)">
+        <view class="quick-item" :class="{ 'quick-item--cart': app.key === 'cart' }" v-for="(app, idx) in quickApps" :key="app.key" @click="onQuickTap(app)">
           <view class="quick-icon" :style="getQuickIconStyle(idx)"><text>{{ app.icon }}</text></view>
           <view class="quick-icon" :style="getQuickIconStyle(idx)"><text>{{ app.icon }}</text></view>
           <text class="quick-label">{{ app.name }}</text>
           <text class="quick-label">{{ app.name }}</text>
         </view>
         </view>
@@ -944,7 +944,30 @@ membershipDays: 0,
         var cb = (usage && usage[b.key]) || 0
         var cb = (usage && usage[b.key]) || 0
         return cb - ca
         return cb - ca
       })
       })
-      return lockedApps.concat(candidates.slice(0, remaining))
+      var result = lockedApps.concat(candidates.slice(0, remaining))
+      // cart 常驻:用户自定义快捷方式后购物车不得被挤出(产品 A3 决策)
+      // result 中锁定项为 shortcut 对象(appKey),频度候选项为应用库对象(key);
+      // loadQuickApps 按 appKey 取应用,故这里以 appKey 判定并补 appKey 形态的 cart。
+      var hasCart = result.some(function(item) { return item.appKey === 'cart' })
+      if (!hasCart) {
+        if (result.length < 6) {
+          result.push({ appKey: 'cart', locked: 0, sortOrder: 99 })
+        } else {
+          // 满 6 格:从后往前替换最后一个非锁定且非 cart 的项,绝不替换 locked=1 的项
+          var replaceIdx = -1
+          for (var i = result.length - 1; i >= 0; i--) {
+            if (result[i].locked !== 1 && result[i].appKey !== 'cart') {
+              replaceIdx = i
+              break
+            }
+          }
+          // 6 个全锁定:尊重用户锁定选择,不追加也不替换
+          if (replaceIdx >= 0) {
+            result[replaceIdx] = { appKey: 'cart', locked: 0, sortOrder: 99 }
+          }
+        }
+      }
+      return result
     },
     },
     // 默认 6 个快捷入口(未配置自定义快捷方式时使用)
     // 默认 6 个快捷入口(未配置自定义快捷方式时使用)
     getDefaultQuickApps: function() {
     getDefaultQuickApps: function() {
@@ -3086,6 +3109,11 @@ $c-warm-tint:     #FFF3E7;
 .quick-icon text {
 .quick-icon text {
   font-size: calc(36rpx * var(--font-scale, 1));
   font-size: calc(36rpx * var(--font-scale, 1));
 }
 }
+/* 购物车常驻入口:品牌浅底 + 描边强调(覆盖 getQuickIconStyle 的索引调色板) */
+.quick-item--cart .quick-icon {
+  background: $uni-primary-pale !important;
+  border: 1rpx solid $brand-primary-light-2;
+}
 
 
 .quick-item .quick-label {
 .quick-item .quick-label {
   font-size: calc(26rpx * var(--font-scale, 1));
   font-size: calc(26rpx * var(--font-scale, 1));

+ 29 - 3
cfc-frontend/pages/shop/cart/cart.vue

@@ -666,18 +666,26 @@ export default {
   bottom: 0;
   bottom: 0;
   left: 0;
   left: 0;
   right: 0;
   right: 0;
-  height: 120rpx;
+  min-height: 112rpx;
   background: $app-surface;
   background: $app-surface;
   border-top: 1rpx solid $facade-hairline-2;
   border-top: 1rpx solid $facade-hairline-2;
   display: flex;
   display: flex;
   align-items: center;
   align-items: center;
   justify-content: space-between;
   justify-content: space-between;
-  padding: 0 $uni-space-page;
-  z-index: 100;
+  padding: 20rpx $uni-space-page;
+  padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
+  padding-bottom: calc(20rpx + env(safe-area-inset-bottom, 0px));
+  z-index: 10;
 }
 }
 .bottom-left {
 .bottom-left {
   display: flex;
   display: flex;
   align-items: baseline;
   align-items: baseline;
+  flex-shrink: 0;
+}
+.bottom-right {
+  flex-shrink: 0;
+  min-width: 0;
+  margin-left: 12rpx;
 }
 }
 .selected-count {
 .selected-count {
   font-size: calc(28rpx * var(--font-scale, 1));
   font-size: calc(28rpx * var(--font-scale, 1));
@@ -692,6 +700,7 @@ export default {
   font-size: calc(42rpx * var(--font-scale, 1));
   font-size: calc(42rpx * var(--font-scale, 1));
   color: $brand-primary;
   color: $brand-primary;
   font-weight: bold;
   font-weight: bold;
+  white-space: nowrap;
 }
 }
 .checkout-btn {
 .checkout-btn {
   background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
   background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
@@ -738,15 +747,32 @@ export default {
   color: $brand-primary;
   color: $brand-primary;
   font-weight: 500;
   font-weight: 500;
 }
 }
+/* 会员态双行价格:标签+数字同行不折行,两行之间留 8rpx 呼吸 */
+.price-compare {
+  display: flex;
+  align-items: baseline;
+  gap: 8rpx;
+  white-space: nowrap;
+}
+.price-compare + .price-compare {
+  margin-top: 8rpx;
+}
+.price-original-label,
+.price-estimate-label {
+  font-size: calc(28rpx * var(--font-scale, 1));
+  white-space: nowrap;
+}
 .price-original {
 .price-original {
   font-size: calc(22rpx * var(--font-scale, 1));
   font-size: calc(22rpx * var(--font-scale, 1));
   color: $app-text-subtle;
   color: $app-text-subtle;
   text-decoration: line-through;
   text-decoration: line-through;
+  white-space: nowrap;
 }
 }
 .price-estimate {
 .price-estimate {
   font-size: calc(40rpx * var(--font-scale, 1));
   font-size: calc(40rpx * var(--font-scale, 1));
   font-weight: bold;
   font-weight: bold;
   color: $brand-primary;
   color: $brand-primary;
+  white-space: nowrap;
 }
 }
 /* Gift badge */
 /* Gift badge */
 .gift-badge {
 .gift-badge {

+ 46 - 7
cfc-frontend/pages/shop/index/index.vue

@@ -21,7 +21,10 @@
         <text class="search-placeholder">搜索商品</text>
         <text class="search-placeholder">搜索商品</text>
       </view>
       </view>
       <view class="cart-btn" @click="goCart">
       <view class="cart-btn" @click="goCart">
-        <text class="cart-icon">🛒</text>
+        <view class="cart-icon">
+          <view class="cart-icon-wheel cart-icon-wheel--left"></view>
+          <view class="cart-icon-wheel cart-icon-wheel--right"></view>
+        </view>
         <text v-if="cartCount > 0" class="cart-badge">{{ cartCount > 99 ? '99+' : cartCount }}</text>
         <text v-if="cartCount > 0" class="cart-badge">{{ cartCount > 99 ? '99+' : cartCount }}</text>
       </view>
       </view>
     </view>
     </view>
@@ -445,23 +448,59 @@ export default {
 }
 }
 .cart-btn {
 .cart-btn {
   position: relative;
   position: relative;
-  width: 72rpx;
-  height: 72rpx;
+  width: 88rpx;
+  height: 88rpx;
   border-radius: 50%;
   border-radius: 50%;
-  background: $app-surface;
-  border: 1rpx solid $facade-hairline-2;
+  background: $uni-primary-pale;
+  border: 1rpx solid $brand-primary-light-2;
   display: flex;
   display: flex;
   align-items: center;
   align-items: center;
   justify-content: center;
   justify-content: center;
   flex-shrink: 0;
   flex-shrink: 0;
 }
 }
+/* 纯 CSS 购物车轮廓(无 emoji):车体 + 把手 + 双轮 */
 .cart-icon {
 .cart-icon {
+  position: relative;
+  width: 40rpx;
+  height: 28rpx;
+  margin-top: 8rpx;
+  border: 3rpx solid $brand-primary;
+  border-top: 0;
+  border-radius: 0 0 8rpx 8rpx;
   font-size: calc(36rpx * var(--font-scale, 1));
   font-size: calc(36rpx * var(--font-scale, 1));
 }
 }
+.cart-icon::before {
+  content: '';
+  position: absolute;
+  left: 2rpx;
+  top: -12rpx;
+  width: 16rpx;
+  height: 12rpx;
+  border-left: 3rpx solid $brand-primary;
+  border-top: 3rpx solid $brand-primary;
+  border-top-left-radius: 8rpx;
+  transform: rotate(-18deg);
+  transform-origin: left bottom;
+}
+.cart-icon-wheel {
+  position: absolute;
+  bottom: -11rpx;
+  width: 8rpx;
+  height: 8rpx;
+  border: 3rpx solid $brand-primary;
+  border-radius: 50%;
+  background: $uni-primary-pale;
+}
+.cart-icon-wheel--left {
+  left: 4rpx;
+}
+.cart-icon-wheel--right {
+  right: 4rpx;
+}
 .cart-badge {
 .cart-badge {
   position: absolute;
   position: absolute;
-  top: -6rpx;
-  right: -6rpx;
+  top: -4rpx;
+  right: -4rpx;
   min-width: 32rpx;
   min-width: 32rpx;
   height: 32rpx;
   height: 32rpx;
   line-height: 32rpx;
   line-height: 32rpx;