list.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. <Tabbar />
  103. <Modal ref="myModal"
  104. :title="modalText.title"
  105. :content="modalText.content"
  106. :showCancelButton="false"
  107. />
  108. </view>
  109. </template>
  110. <script>
  111. import Modal from '@/components/modal/index.vue';
  112. import Tabbar from '@/pages/groupMeal/tabbar/index.vue';
  113. export default {
  114. components: {
  115. Modal,
  116. Tabbar
  117. },
  118. data() {
  119. return {
  120. isManaging: false, // 是否处于批量管理模式
  121. // 一级分类
  122. statusTabs: [{ name: '售卖中' }, { name: '审核中' }, { name: '未上架' }],
  123. currentStatusTab: 0,
  124. // 二级分类
  125. categoryTabs: [{ name: '早餐' }, { name: '午餐' }, { name: '晚餐' }],
  126. currentCategoryTab: 0,
  127. // 所有商品数据 (模拟)
  128. allGoods: [
  129. { id: 1, categoryId: 0, status: 0, name: '套餐名称套餐名称套餐名称套餐名称', sales: 1255, price: 10.8, originalPrice: 12, description: '米饭, 鱼香肉丝, 宫保鸡丁', image: '/static/demo/goods1.png' },
  130. { id: 2, categoryId: 0, status: 0, name: '套餐名称套餐名称', sales: 1255, price: 14.8, originalPrice: 16, description: '米饭, 红烧排骨', image: '/static/demo/goods2.png' },
  131. { id: 3, categoryId: 1, status: 0, name: '套餐名称套餐名称', sales: 1255, price: 13.1, originalPrice: 15.0, description: '米饭, 糖醋里脊', image: '/static/demo/goods3.png' },
  132. { id: 4, categoryId: 0, status: 1, name: '审核中套餐', sales: 0, price: 20, originalPrice: 25, description: '审核中商品描述', image: '/static/demo/goods1.png' },
  133. { id: 5, categoryId: 0, status: 2, name: '未上架早餐', sales: 0, price: 15, originalPrice: 18, description: '未上架商品描述', image: '/static/demo/goods2.png' },
  134. { id: 6, categoryId: 1, status: 2, name: '未上架午餐', sales: 0, price: 25, originalPrice: 30, description: '未上架商品描述', image: '/static/demo/goods3.png' },
  135. ],
  136. selectedGoods: [], // 已选中的商品ID
  137. isAllSelected: false, // 用于全选checkbox的状态绑定
  138. // 联动功能相关数据
  139. scrollIntoViewId: '', // 滚动到指定视图的 ID
  140. categoryOffsetTops: [], // 每个分类区域到 scroll-view 顶部的距离
  141. isScrolling: false, // 标记是否正在进行点击滚动
  142. // 弹窗
  143. modalText: {
  144. title: '提示',
  145. btntext: '确定',
  146. // 是否将所选商品上架?
  147. // 所选商品有未通过审核商品无法批量操作上架
  148. // 是否将所选商品删除?删除后需重新添加品牌标配
  149. content: '报名成功,请等待平台审核'
  150. },
  151. };
  152. },
  153. computed: {
  154. // 根据一、二级分类筛选商品
  155. filteredGoods() {
  156. const filteredByCategory = this.allGoods.filter(goods => goods.status === this.currentStatusTab);
  157. const grouped = [];
  158. this.categoryTabs.forEach((category, index) => {
  159. const items = filteredByCategory.filter(goods => goods.categoryId === index);
  160. if (items.length > 0) {
  161. grouped.push({
  162. id: index,
  163. name: category.name,
  164. items: items,
  165. scrollId: `category_${index}` // 滚动目标 ID
  166. });
  167. }
  168. });
  169. // 每次 filteredGoods 数据变化后,重新计算 offset
  170. this.$nextTick(() => {
  171. // 确保在数据渲染完成后再计算
  172. this.calculateCategoryOffsets();
  173. });
  174. return grouped;
  175. }
  176. },
  177. onLoad() {
  178. // 获取商品数据
  179. this.getData();
  180. // 获取餐品分类
  181. this.getClassData();
  182. // this.$refs.myModal.onOpen();
  183. },
  184. methods: {
  185. // 商品数据
  186. getData() {
  187. let params = {
  188. status: 0,
  189. reviewStatus: 1,
  190. pageNo: 1,
  191. pageSize: 10
  192. };
  193. this.$http.post('/groupmeal/gmMerchantPackage/pageList', params).then(res => {
  194. if (res.data.code === 200) {}
  195. });
  196. },
  197. // 餐品分类
  198. getClassData() {
  199. this.$http.get('/groupmeal/gmMerchantPackage/getGroupMealCategory', {}).then(res => {
  200. if (res.data.code === 200) {}
  201. });
  202. },
  203. // 操作模式
  204. toggleManageMode() {
  205. this.isManaging = !this.isManaging;
  206. this.selectedGoods = []; // 切换模式时清空选择
  207. this.isAllSelected = false;
  208. },
  209. // 主分类事件
  210. onTabChange(e) {
  211. this.currentStatusTab = e;
  212. this.selectedGoods = []; // 切换Tab时清空选择
  213. this.isAllSelected = false;
  214. // 切换 Tab 后,重置左侧分类高亮到第一个
  215. // this.currentCategoryTab = 0;
  216. // this.$nextTick(() => {
  217. // // 确保在新数据渲染后,滚动到第一个分类
  218. // this.scrollIntoViewId = this.filteredGoods[0]?.scrollId || '';
  219. // });
  220. },
  221. // 添加商品
  222. addGoods() {
  223. uni.navigateTo({
  224. url: '/pages/groupMeal/goods/form-step1'
  225. });
  226. },
  227. // 编辑商品
  228. editGoods(goods) {
  229. console.log('编辑商品:', goods);
  230. uni.navigateTo({
  231. url: '/pages/groupMeal/goods/form-step1?id=' + goods.id
  232. });
  233. },
  234. // 全选事件
  235. selectAll(e) {
  236. if (e.value) {
  237. // 全选
  238. this.selectedGoods = this.filteredGoods.flatMap(category => category.items.map(item => item.id));
  239. } else {
  240. // 取消全选
  241. this.selectedGoods = [];
  242. }
  243. this.filteredGoods.flatMap(category => category.items.forEach(item => {
  244. item.checked = e.value;
  245. }));
  246. },
  247. // 操作提交事件
  248. handleBatchAction(action) {
  249. if (this.selectedGoods.length === 0) {
  250. uni.showToast({ title: '请先选择商品', icon: 'none' });
  251. return;
  252. }
  253. if (action === 'delete') {
  254. } else {
  255. this.$http.post('/groupmeal/gmMerchantPackage/launch', {
  256. id: '',
  257. status: action === 'publish' ? 1 : 0
  258. }).then(res => {
  259. if (res.data.code === 200) {}
  260. });
  261. }
  262. console.log(`执行批量操作: ${action}`, this.selectedGoods);
  263. uni.showToast({ title: '操作成功', icon: 'success' });
  264. },
  265. // 核心功能 1: 点击左侧二级分类,右侧商品列表滚动到对应位置
  266. onCategoryTabChange(index) {
  267. this.currentCategoryTab = index;
  268. // 1. 设置标记,在滚动动画期间忽略 onGoodsScroll,防止快速切换闪烁
  269. this.isScrolling = true;
  270. // 2. 找到对应的 scrollId 并设置给 scroll-view
  271. const targetCategory = this.filteredGoods.find(c => c.id === index);
  272. if (targetCategory) {
  273. this.scrollIntoViewId = targetCategory.scrollId;
  274. }
  275. // 3. 延迟清除标记,等待滚动动画结束 (通常 300ms)
  276. setTimeout(() => {
  277. this.isScrolling = false;
  278. }, 300);
  279. },
  280. // 核心功能 2: 滚动商品列表时,左侧二级分类高亮联动
  281. onGoodsScroll(e) {
  282. // 如果是点击左侧分类触发的滚动,则忽略联动
  283. if (this.isScrolling) {
  284. return;
  285. }
  286. const scrollTop = e.detail.scrollTop;
  287. const threshold = 10; // 切换阈值,避免刚好在边缘时闪烁
  288. // 遍历所有分类的 offset,找到当前位于视图顶部的分类
  289. for (let i = 0; i < this.categoryOffsetTops.length; i++) {
  290. const current = this.categoryOffsetTops[i];
  291. const next = this.categoryOffsetTops[i + 1];
  292. // 检查条件:
  293. // 1. 当前分类的 top 已经小于或等于 scrollTop (即分类标题已经经过视图顶部)
  294. // 2. 并且 (如果存在下一个分类) 下一个分类的 top 还没有经过视图顶部
  295. if (current.top <= scrollTop + threshold && (!next || next.top > scrollTop + threshold)) {
  296. // 确保左侧分类的索引是正确的
  297. if (this.currentCategoryTab !== current.id) {
  298. this.currentCategoryTab = current.id;
  299. }
  300. break;
  301. }
  302. }
  303. },
  304. // 核心功能 3: 计算所有分类区块的顶部偏移量
  305. calculateCategoryOffsets() {
  306. if (this.filteredGoods.length === 0) {
  307. this.categoryOffsetTops = [];
  308. return;
  309. }
  310. // 必须使用 $nextTick 确保 DOM 已经渲染
  311. this.$nextTick(() => {
  312. const query = uni.createSelectorQuery().in(this);
  313. let scrollRectTop = 0;
  314. // 1. 获取 scroll-view 顶部到页面的距离 (作为基准点)
  315. query.select('.goods-scroll-view').boundingClientRect(rect => {
  316. scrollRectTop = rect.top;
  317. }).exec();
  318. // 2. 获取所有分类 section 的位置信息
  319. query.selectAll('.goods-category-section').boundingClientRect(data => {
  320. if (!data.length) return;
  321. this.categoryOffsetTops = data.map(item => ({
  322. id: parseInt(item.dataset.categoryIndex), // 从 data-category-index 获取 categoryId
  323. top: item.top - scrollRectTop // 计算相对于 scroll-view 顶部的距离 (即 scrollTop 的值)
  324. }));
  325. }).exec();
  326. });
  327. },
  328. }
  329. };
  330. </script>
  331. <style lang="scss" scoped>
  332. .page-container {
  333. height: 100vh;
  334. display: flex;
  335. flex-direction: column;
  336. .navbar-title {
  337. font-weight: 500;
  338. font-size: 36rpx;
  339. color: #333333;
  340. }
  341. .navbar-right {
  342. padding: 0 24rpx;
  343. display: flex;
  344. align-items: center;
  345. .nav-btn {
  346. font-weight: 500;
  347. font-size: 28rpx;
  348. color: #333333;
  349. }
  350. .manage-btn {
  351. margin-left: 24rpx;
  352. }
  353. }
  354. .search-bar {
  355. padding: 12rpx 32rpx;
  356. background-color: #fff;
  357. .u-search {
  358. border-radius: 8rpx;
  359. background-color: #F7F8FC;
  360. }
  361. }
  362. }
  363. .main-content {
  364. flex: 1;
  365. display: flex;
  366. overflow: hidden;
  367. .category-list {
  368. width: 160rpx;
  369. height: 100%;
  370. background-color: #F7F8FC;
  371. .category-item {
  372. font-weight: 400;
  373. font-size: 24rpx;
  374. color: #333333;
  375. text-align: left;
  376. padding: 24rpx 32rpx;
  377. &.active {
  378. background-color: #fff;
  379. font-weight: 500;
  380. color: #2983EC;
  381. }
  382. }
  383. }
  384. .goods-scroll-view {
  385. flex: 1;
  386. height: 100%;
  387. background-color: #fff;
  388. padding: 20rpx 24rpx;
  389. .category-title {
  390. font-weight: 500;
  391. font-size: 26rpx;
  392. color: #000000;
  393. }
  394. .u-checkbox-group {
  395. width: 100%;
  396. }
  397. }
  398. .goods-card {
  399. width: 100%;
  400. padding: 20rpx 0;
  401. border-bottom: 1rpx solid #f0f0f0;
  402. display: flex;
  403. align-items: flex-start;
  404. position: relative;
  405. .goods-image {
  406. width: 120rpx;
  407. height: 120rpx;
  408. border-radius: 12rpx;
  409. flex-shrink: 0;
  410. background-color: #eee;
  411. }
  412. .goods-info {
  413. flex: 1;
  414. margin-left: 20rpx;
  415. .goods-name {
  416. font-size: 28rpx;
  417. font-weight: 500;
  418. color: #333333;
  419. display: -webkit-box;
  420. -webkit-line-clamp: 2;
  421. -webkit-box-orient: vertical;
  422. overflow: hidden;
  423. }
  424. .goods-sales, .goods-desc {
  425. font-size: 22rpx;
  426. font-weight: 400;
  427. color: #8A91A8;
  428. margin-top: 4rpx;
  429. display: -webkit-box;
  430. -webkit-line-clamp: 1;
  431. -webkit-box-orient: vertical;
  432. overflow: hidden;
  433. }
  434. .goods-price-line {
  435. margin-top: 8rpx;
  436. .price {
  437. font-size: 32rpx;
  438. font-weight: 500;
  439. color: #FD4912;
  440. .unit {
  441. font-size: 22rpx;
  442. margin-right: 2rpx;
  443. }
  444. }
  445. .original-price {
  446. font-size: 22rpx;
  447. font-weight: 400;
  448. color: #999999;
  449. text-decoration: line-through;
  450. margin-left: 10rpx;
  451. }
  452. }
  453. }
  454. .goods-actions {
  455. position: absolute;
  456. right: 0;
  457. bottom: 20rpx;
  458. ::v-deep .u-btn {
  459. height: 46rpx;
  460. font-weight: 400;
  461. font-size: 26rpx;
  462. color: #2D88F4;
  463. padding: 0 24rpx;
  464. // background: rgba(45,136,244,0.05);
  465. border-radius: 61rpx;
  466. border: 1rpx solid #2D88F4;
  467. &::after {
  468. display: none;
  469. }
  470. }
  471. }
  472. }
  473. }
  474. .footer-bar {
  475. bottom: 50px;
  476. padding-bottom: 20px;
  477. .action-buttons {
  478. display: flex;
  479. gap: 20rpx;
  480. ::v-deep .u-btn {
  481. height: 69rpx;
  482. border-radius: 49rpx;
  483. }
  484. }
  485. }
  486. </style>