list.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <template>
  2. <view class="page-container">
  3. <!-- 导航栏 -->
  4. <u-navbar :autoBack="true" :placeholder="true" :border-bottom="false">
  5. <view class="navbar-title">团餐商品列表</view>
  6. <view slot="right" class="navbar-right">
  7. <text class="nav-btn" @click="addGoods">添加商品</text>
  8. <text class="nav-btn manage-btn" @click="toggleManageMode">{{ isManaging ? '退出管理' : '批量管理' }}</text>
  9. </view>
  10. </u-navbar>
  11. <!-- 搜索框 -->
  12. <view class="search-bar">
  13. <u-search bg-color="#F7F8FC" placeholder="请输入商品名称" search-icon-color="#999"
  14. :showAction="true" actionText="搜索"
  15. :actionStyle="{
  16. width: 'auto',
  17. color: '#333',
  18. padding: '0 20rpx',
  19. borderLeft: '1rpx solid #DDDDDD'
  20. }"
  21. ></u-search>
  22. </view>
  23. <!-- 一级分类Tabs -->
  24. <u-tabs :list="statusTabs"
  25. :current="currentStatusTab"
  26. inactive-color="#666666"
  27. :bar-style="{
  28. bottom: '20rpx',
  29. background: 'linear-gradient(90deg, #82BCFF 0%, rgba(130,188,255,0) 100%)'
  30. }"
  31. :active-item-style="{
  32. fontWeight: '500',
  33. fontSize: '32rpx',
  34. color: '#000000',
  35. }"
  36. @change="onTabChange"
  37. ></u-tabs>
  38. <!-- 主内容区 -->
  39. <view class="main-content">
  40. <!-- 左侧二级分类 -->
  41. <view class="category-list">
  42. <view v-for="(category, index) in categoryTabs" :key="index">
  43. <view
  44. :class="['category-item', currentCategoryTab === index ? 'active' : '']"
  45. @click="onCategoryTabChange(index)"
  46. >
  47. {{ category.name }}
  48. </view>
  49. </view>
  50. </view>
  51. <!-- 右侧商品列表 -->
  52. <scroll-view scroll-y class="goods-scroll-view"
  53. :style="{ paddingBottom: isManaging ? '120rpx' : '0' }"
  54. :scroll-into-view="scrollIntoViewId"
  55. :scroll-with-animation="true"
  56. @scroll="onScroll"
  57. >
  58. <view class="goods-category-section" :id="category.scrollId" v-for="category in filteredGoods" :key="category.id">
  59. <view class="category-title">{{ category.name }}</view>
  60. <u-checkbox-group v-model="selectedGoods" placement="column">
  61. <view class="goods-card" v-for="goods in category.items" :key="goods.id">
  62. <!-- <image class="goods-image" :src="goods.image" mode="aspectFill"></image> -->
  63. <u-image width="120rpx" height="120rpx" border-radius="12rpx" :src="goods.image" mode="aspectFill"></u-image>
  64. <view class="goods-info">
  65. <view class="goods-name">{{ goods.name }}</view>
  66. <view class="goods-desc">{{ goods.description }}</view>
  67. <view class="goods-sales">销量:{{ goods.sales }}</view>
  68. <view class="goods-price-line">
  69. <text class="price"><text class="unit">¥</text>{{ goods.price }}</text>
  70. <text class="original-price">¥{{ goods.originalPrice }}</text>
  71. </view>
  72. </view>
  73. <!-- 根据模式和状态显示不同操作 -->
  74. <view class="goods-actions">
  75. <u-button
  76. v-if="!isManaging && currentStatusTab === 2"
  77. type="primary" plain
  78. @click="editGoods(goods)"
  79. >编辑</u-button>
  80. <u-checkbox v-if="isManaging" :name="goods.id" shape="circle" v-model="goods.checked"></u-checkbox>
  81. </view>
  82. </view>
  83. </u-checkbox-group>
  84. </view>
  85. </scroll-view>
  86. </view>
  87. <!-- 批量管理底部操作栏 -->
  88. <view class="footer-bar" v-if="isManaging">
  89. <u-checkbox-group v-model="isAllSelected" placement="row">
  90. <u-checkbox name="all" shape="circle" v-model="isAllSelected" @change="selectAll">全选</u-checkbox>
  91. </u-checkbox-group>
  92. <view class="action-buttons">
  93. <template v-if="currentStatusTab === 0 || currentStatusTab === 1">
  94. <u-button type="primary" @click="handleBatchAction('unpublish')">下架</u-button>
  95. </template>
  96. <template v-if="currentStatusTab === 2">
  97. <u-button type="primary" @click="handleBatchAction('publish')">上架</u-button>
  98. <u-button type="error" @click="handleBatchAction('delete')">删除</u-button>
  99. </template>
  100. </view>
  101. </view>
  102. </view>
  103. </template>
  104. <script>
  105. export default {
  106. data() {
  107. return {
  108. isManaging: false, // 是否处于批量管理模式
  109. // 一级分类
  110. statusTabs: [{ name: '售卖中' }, { name: '审核中' }, { name: '未上架' }],
  111. currentStatusTab: 0,
  112. // 二级分类
  113. categoryTabs: [{ name: '早餐' }, { name: '午餐' }, { name: '晚餐' }],
  114. currentCategoryTab: 0,
  115. // 所有商品数据 (模拟)
  116. allGoods: [
  117. { id: 1, categoryId: 0, status: 0, name: '套餐名称套餐名称套餐名称套餐名称', sales: 1255, price: 10.8, originalPrice: 12, description: '米饭, 鱼香肉丝, 宫保鸡丁', image: '/static/demo/goods1.png' },
  118. { id: 2, categoryId: 0, status: 0, name: '套餐名称套餐名称', sales: 1255, price: 14.8, originalPrice: 16, description: '米饭, 红烧排骨', image: '/static/demo/goods2.png' },
  119. { id: 3, categoryId: 1, status: 0, name: '套餐名称套餐名称', sales: 1255, price: 13.1, originalPrice: 15.0, description: '米饭, 糖醋里脊', image: '/static/demo/goods3.png' },
  120. { id: 4, categoryId: 0, status: 1, name: '审核中套餐', sales: 0, price: 20, originalPrice: 25, description: '审核中商品描述', image: '/static/demo/goods1.png' },
  121. { id: 5, categoryId: 0, status: 2, name: '未上架早餐', sales: 0, price: 15, originalPrice: 18, description: '未上架商品描述', image: '/static/demo/goods2.png' },
  122. { id: 6, categoryId: 1, status: 2, name: '未上架午餐', sales: 0, price: 25, originalPrice: 30, description: '未上架商品描述', image: '/static/demo/goods3.png' },
  123. ],
  124. selectedGoods: [], // 已选中的商品ID
  125. isAllSelected: false, // 用于全选checkbox的状态绑定
  126. // 联动功能相关数据
  127. scrollIntoViewId: '', // 滚动到指定视图的 ID
  128. categoryOffsetTops: [], // 每个分类区域到 scroll-view 顶部的距离
  129. isScrolling: false, // 标记是否正在进行点击滚动
  130. };
  131. },
  132. computed: {
  133. // 根据一、二级分类筛选商品
  134. filteredGoods() {
  135. const filteredByCategory = this.allGoods.filter(goods => goods.status === this.currentStatusTab);
  136. const grouped = [];
  137. this.categoryTabs.forEach((category, index) => {
  138. const items = filteredByCategory.filter(goods => goods.categoryId === index);
  139. if (items.length > 0) {
  140. grouped.push({
  141. id: index,
  142. name: category.name,
  143. items: items,
  144. scrollId: `category_${index}` // 滚动目标 ID
  145. });
  146. }
  147. });
  148. // 每次 filteredGoods 数据变化后,重新计算 offset
  149. this.$nextTick(() => {
  150. // 确保在数据渲染完成后再计算
  151. this.calculateCategoryOffsets();
  152. });
  153. return grouped;
  154. }
  155. },
  156. methods: {
  157. addGoods() {
  158. uni.navigateTo({
  159. url: '/pages/groupMeal/goods/form-step1'
  160. });
  161. },
  162. toggleManageMode() {
  163. this.isManaging = !this.isManaging;
  164. this.selectedGoods = []; // 切换模式时清空选择
  165. this.isAllSelected = false;
  166. },
  167. onTabChange(e) {
  168. this.currentStatusTab = e;
  169. this.selectedGoods = []; // 切换Tab时清空选择
  170. this.isAllSelected = false;
  171. // 切换 Tab 后,重置左侧分类高亮到第一个
  172. // this.currentCategoryTab = 0;
  173. // this.$nextTick(() => {
  174. // // 确保在新数据渲染后,滚动到第一个分类
  175. // this.scrollIntoViewId = this.filteredGoods[0]?.scrollId || '';
  176. // });
  177. },
  178. onCategoryTabChange(index) {
  179. this.currentCategoryTab = index;
  180. // 这里可以添加滚动到对应锚点的逻辑
  181. },
  182. editGoods(goods) {
  183. console.log('编辑商品:', goods);
  184. uni.navigateTo({
  185. url: '/pages/groupMeal/goods/form-step1?id=' + goods.id
  186. });
  187. },
  188. selectAll(e) {
  189. if (e.value) {
  190. // 全选
  191. this.selectedGoods = this.filteredGoods.flatMap(category => category.items.map(item => item.id));
  192. } else {
  193. // 取消全选
  194. this.selectedGoods = [];
  195. }
  196. this.filteredGoods.flatMap(category => category.items.forEach(item => {
  197. item.checked = e.value;
  198. }));
  199. },
  200. handleBatchAction(action) {
  201. if (this.selectedGoods.length === 0) {
  202. uni.showToast({ title: '请先选择商品', icon: 'none' });
  203. return;
  204. }
  205. console.log(`执行批量操作: ${action}`, this.selectedGoods);
  206. uni.showToast({ title: '操作成功', icon: 'success' });
  207. },
  208. // 核心功能 1: 点击左侧二级分类,右侧商品列表滚动到对应位置
  209. onCategoryTabChange(index) {
  210. this.currentCategoryTab = index;
  211. // 1. 设置标记,在滚动动画期间忽略 onGoodsScroll,防止快速切换闪烁
  212. this.isScrolling = true;
  213. // 2. 找到对应的 scrollId 并设置给 scroll-view
  214. const targetCategory = this.filteredGoods.find(c => c.id === index);
  215. if (targetCategory) {
  216. this.scrollIntoViewId = targetCategory.scrollId;
  217. }
  218. // 3. 延迟清除标记,等待滚动动画结束 (通常 300ms)
  219. setTimeout(() => {
  220. this.isScrolling = false;
  221. }, 300);
  222. },
  223. // 核心功能 2: 滚动商品列表时,左侧二级分类高亮联动
  224. onGoodsScroll(e) {
  225. // 如果是点击左侧分类触发的滚动,则忽略联动
  226. if (this.isScrolling) {
  227. return;
  228. }
  229. const scrollTop = e.detail.scrollTop;
  230. const threshold = 10; // 切换阈值,避免刚好在边缘时闪烁
  231. // 遍历所有分类的 offset,找到当前位于视图顶部的分类
  232. for (let i = 0; i < this.categoryOffsetTops.length; i++) {
  233. const current = this.categoryOffsetTops[i];
  234. const next = this.categoryOffsetTops[i + 1];
  235. // 检查条件:
  236. // 1. 当前分类的 top 已经小于或等于 scrollTop (即分类标题已经经过视图顶部)
  237. // 2. 并且 (如果存在下一个分类) 下一个分类的 top 还没有经过视图顶部
  238. if (current.top <= scrollTop + threshold && (!next || next.top > scrollTop + threshold)) {
  239. // 确保左侧分类的索引是正确的
  240. if (this.currentCategoryTab !== current.id) {
  241. this.currentCategoryTab = current.id;
  242. }
  243. break;
  244. }
  245. }
  246. },
  247. // 核心功能 3: 计算所有分类区块的顶部偏移量
  248. calculateCategoryOffsets() {
  249. if (this.filteredGoods.length === 0) {
  250. this.categoryOffsetTops = [];
  251. return;
  252. }
  253. // 必须使用 $nextTick 确保 DOM 已经渲染
  254. this.$nextTick(() => {
  255. const query = uni.createSelectorQuery().in(this);
  256. let scrollRectTop = 0;
  257. // 1. 获取 scroll-view 顶部到页面的距离 (作为基准点)
  258. query.select('.goods-scroll-view').boundingClientRect(rect => {
  259. scrollRectTop = rect.top;
  260. }).exec();
  261. // 2. 获取所有分类 section 的位置信息
  262. query.selectAll('.goods-category-section').boundingClientRect(data => {
  263. if (!data.length) return;
  264. this.categoryOffsetTops = data.map(item => ({
  265. id: parseInt(item.dataset.categoryIndex), // 从 data-category-index 获取 categoryId
  266. top: item.top - scrollRectTop // 计算相对于 scroll-view 顶部的距离 (即 scrollTop 的值)
  267. }));
  268. }).exec();
  269. });
  270. },
  271. }
  272. };
  273. </script>
  274. <style lang="scss" scoped>
  275. .page-container {
  276. height: 100vh;
  277. display: flex;
  278. flex-direction: column;
  279. .navbar-title {
  280. font-weight: 500;
  281. font-size: 36rpx;
  282. color: #333333;
  283. }
  284. .navbar-right {
  285. padding: 0 24rpx;
  286. display: flex;
  287. align-items: center;
  288. .nav-btn {
  289. font-weight: 500;
  290. font-size: 28rpx;
  291. color: #333333;
  292. }
  293. .manage-btn {
  294. margin-left: 24rpx;
  295. }
  296. }
  297. .search-bar {
  298. padding: 12rpx 32rpx;
  299. background-color: #fff;
  300. .u-search {
  301. border-radius: 8rpx;
  302. background-color: #F7F8FC;
  303. }
  304. }
  305. }
  306. .main-content {
  307. flex: 1;
  308. display: flex;
  309. overflow: hidden;
  310. .category-list {
  311. width: 160rpx;
  312. height: 100%;
  313. background-color: #F7F8FC;
  314. .category-item {
  315. font-weight: 400;
  316. font-size: 24rpx;
  317. color: #333333;
  318. text-align: left;
  319. padding: 24rpx 32rpx;
  320. &.active {
  321. background-color: #fff;
  322. font-weight: 500;
  323. color: #2983EC;
  324. }
  325. }
  326. }
  327. .goods-scroll-view {
  328. flex: 1;
  329. height: 100%;
  330. background-color: #fff;
  331. padding: 20rpx 24rpx;
  332. .category-title {
  333. font-weight: 500;
  334. font-size: 26rpx;
  335. color: #000000;
  336. }
  337. .u-checkbox-group {
  338. width: 100%;
  339. }
  340. }
  341. .goods-card {
  342. width: 100%;
  343. padding: 20rpx 0;
  344. border-bottom: 1rpx solid #f0f0f0;
  345. display: flex;
  346. align-items: flex-start;
  347. position: relative;
  348. .goods-image {
  349. width: 120rpx;
  350. height: 120rpx;
  351. border-radius: 12rpx;
  352. flex-shrink: 0;
  353. background-color: #eee;
  354. }
  355. .goods-info {
  356. flex: 1;
  357. margin-left: 20rpx;
  358. .goods-name {
  359. font-size: 28rpx;
  360. font-weight: 500;
  361. color: #333333;
  362. display: -webkit-box;
  363. -webkit-line-clamp: 2;
  364. -webkit-box-orient: vertical;
  365. overflow: hidden;
  366. }
  367. .goods-sales, .goods-desc {
  368. font-size: 22rpx;
  369. font-weight: 400;
  370. color: #8A91A8;
  371. margin-top: 4rpx;
  372. display: -webkit-box;
  373. -webkit-line-clamp: 1;
  374. -webkit-box-orient: vertical;
  375. overflow: hidden;
  376. }
  377. .goods-price-line {
  378. margin-top: 8rpx;
  379. .price {
  380. font-size: 32rpx;
  381. font-weight: 500;
  382. color: #FD4912;
  383. .unit {
  384. font-size: 22rpx;
  385. margin-right: 2rpx;
  386. }
  387. }
  388. .original-price {
  389. font-size: 22rpx;
  390. font-weight: 400;
  391. color: #999999;
  392. text-decoration: line-through;
  393. margin-left: 10rpx;
  394. }
  395. }
  396. }
  397. .goods-actions {
  398. position: absolute;
  399. right: 0;
  400. bottom: 20rpx;
  401. ::v-deep .u-btn {
  402. height: 46rpx;
  403. font-weight: 400;
  404. font-size: 26rpx;
  405. color: #2D88F4;
  406. padding: 0 24rpx;
  407. // background: rgba(45,136,244,0.05);
  408. border-radius: 61rpx;
  409. border: 1rpx solid #2D88F4;
  410. &::after {
  411. display: none;
  412. }
  413. }
  414. }
  415. }
  416. }
  417. .footer-bar {
  418. .action-buttons {
  419. display: flex;
  420. gap: 20rpx;
  421. ::v-deep .u-btn {
  422. height: 69rpx;
  423. border-radius: 49rpx;
  424. }
  425. }
  426. }
  427. </style>