list.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. @search="onSearch" @custom="onSearch"
  22. ></u-search>
  23. </view>
  24. <!-- 一级分类Tabs -->
  25. <u-tabs :list="statusTabs"
  26. :current="currentStatusTab"
  27. font-size="30"
  28. inactive-color="#666666"
  29. :bar-style="{
  30. bottom: '20rpx',
  31. background: 'linear-gradient(90deg, #82BCFF 0%, rgba(130,188,255,0) 100%)'
  32. }"
  33. :active-item-style="{
  34. fontWeight: '500',
  35. fontSize: '32rpx',
  36. color: '#000000',
  37. }"
  38. @change="onTabChange"
  39. ></u-tabs>
  40. <!-- 主内容区 -->
  41. <view class="main-content">
  42. <!-- 左侧二级分类 -->
  43. <view class="category-list">
  44. <view v-for="(category, index) in categoryTabs" :key="index">
  45. <view
  46. :class="['category-item', currentCategoryTab === index ? 'active' : '']"
  47. @click="onCategoryTabChange(index)"
  48. >
  49. {{ category.name }}
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 右侧商品列表 -->
  54. <scroll-view scroll-y class="goods-scroll-view"
  55. :style="{ paddingBottom: isManaging ? '120rpx' : '0' }"
  56. :scroll-into-view="scrollIntoViewId"
  57. :scroll-with-animation="true"
  58. @scroll="onScroll"
  59. >
  60. <view class="goods-category-section" :id="category.scrollId" v-for="category in filteredGoods" :key="category.id">
  61. <view class="category-title">{{ category.name }}</view>
  62. <u-checkbox-group v-model="selectedGoods" placement="column">
  63. <view class="goods-card" v-for="goods in category.items" :key="goods.id">
  64. <!-- <image class="goods-image" :src="goods.image" mode="aspectFill"></image> -->
  65. <u-image width="120rpx" height="120rpx" border-radius="12rpx" :src="goods.image" mode="aspectFill"></u-image>
  66. <view class="goods-info">
  67. <view class="goods-name">{{ goods.name }}</view>
  68. <view class="goods-desc">{{ goods.dishName || goods.description }}</view>
  69. <view class="goods-sales">销量:{{ goods.sales }}</view>
  70. <view class="goods-price-line">
  71. <text class="price"><text class="unit">¥</text>{{ goods.price }}</text>
  72. <text class="original-price">¥{{ goods.sellingPrice }}</text>
  73. </view>
  74. </view>
  75. <!-- 根据模式和状态显示不同操作 -->
  76. <view class="goods-actions">
  77. <u-button
  78. v-if="!isManaging && currentStatusTab === 2"
  79. type="primary" plain
  80. @click="editGoods(goods)"
  81. >编辑</u-button>
  82. <u-checkbox v-if="isManaging" :name="goods.id" shape="circle" v-model="goods.checked" @change="onSelect"></u-checkbox>
  83. </view>
  84. </view>
  85. </u-checkbox-group>
  86. </view>
  87. </scroll-view>
  88. </view>
  89. <!-- <ListScrollView ref="listScroll0"
  90. :api="api"
  91. :init="{
  92. ...initParams,
  93. }">
  94. <template v-slot:list="{ data }"></template>
  95. </ListScrollView> -->
  96. <!-- 批量管理底部操作栏 -->
  97. <view class="footer-bar" v-if="isManaging">
  98. <u-checkbox-group v-model="isAllSelected" placement="row">
  99. <u-checkbox name="all" shape="circle" v-model="isAllSelected" @change="onSelectAll">全选</u-checkbox>
  100. </u-checkbox-group>
  101. <view class="action-buttons">
  102. <template v-if="currentStatusTab === 0 || currentStatusTab === 1">
  103. <u-button type="primary" @click="handleBatchAction('unpublish')">下架</u-button>
  104. </template>
  105. <template v-if="currentStatusTab === 2">
  106. <u-button type="primary" @click="handleBatchAction('publish')">上架</u-button>
  107. <u-button type="error" @click="handleBatchAction('delete')">删除</u-button>
  108. </template>
  109. </view>
  110. </view>
  111. <Tabbar />
  112. <Modal ref="myModal"
  113. :title="modalText.title"
  114. :content="modalText.content"
  115. />
  116. </view>
  117. </template>
  118. <script>
  119. import Modal from '@/components/modal/index.vue';
  120. import Tabbar from '@/pages/groupMeal/tabbar/index.vue';
  121. import configService from '@/common/service/config.service';
  122. import ListScrollView from '@/components/list-scroll-view/index.vue';
  123. export default {
  124. components: {
  125. Modal,
  126. Tabbar,
  127. ListScrollView
  128. },
  129. data() {
  130. return {
  131. api: {
  132. url: '/groupmeal/gmMerchantPackage/pageList',
  133. method: 'POST',
  134. },
  135. // 是否处于批量管理模式
  136. isManaging: false,
  137. // 一级分类
  138. statusTabs: [{ name: '售卖中' }, { name: '审核中' }, { name: '未上架' }],
  139. currentStatusTab: 0,
  140. // 二级分类
  141. categoryTabs: [{ name: '早餐' }, { name: '午餐' }, { name: '晚餐' }],
  142. currentCategoryTab: 0,
  143. // 所有商品数据
  144. allGoods: [],
  145. selectedGoods: [], // 已选中的商品ID
  146. isAllSelected: false, // 用于全选checkbox的状态绑定
  147. // 联动功能相关数据
  148. scrollIntoViewId: '', // 滚动到指定视图的 ID
  149. categoryOffsetTops: [], // 每个分类区域到 scroll-view 顶部的距离
  150. isScrolling: false, // 标记是否正在进行点击滚动
  151. // 弹窗
  152. modalText: {
  153. title: '提示',
  154. btntext: '确定',
  155. // 所选商品有未通过审核商品无法批量操作上架
  156. content: '是否将所选商品上架?'
  157. },
  158. };
  159. },
  160. computed: {
  161. // 根据一、二级分类筛选商品
  162. filteredGoods() {
  163. const filteredByCategory = this.allGoods;
  164. const grouped = [];
  165. this.categoryTabs.forEach((category, index) => {
  166. const items = filteredByCategory.filter(goods => goods.category == (index + 1));
  167. if (items.length > 0) {
  168. grouped.push({
  169. id: index,
  170. name: category.name,
  171. items: items,
  172. scrollId: `category_${index}` // 滚动目标 ID
  173. });
  174. }
  175. });
  176. // 每次 filteredGoods 数据变化后,重新计算 offset
  177. this.$nextTick(() => {
  178. // 确保在数据渲染完成后再计算
  179. this.calculateCategoryOffsets();
  180. });
  181. return grouped;
  182. },
  183. // 数据参数
  184. initParams() {
  185. let params = {
  186. // 商品状态: 0 未上架, 1 已上架
  187. status: 0,
  188. // 审核状态: 0 审核中, 1 已审核
  189. // reviewStatus: 0,
  190. }
  191. // 售卖中
  192. if (this.currentStatusTab == 0) {
  193. params = {
  194. status: 1,
  195. reviewStatus: 1,
  196. }
  197. }
  198. // 审核中
  199. else if (this.currentStatusTab == 1) {
  200. params = {
  201. status: 0,
  202. reviewStatus: 0,
  203. }
  204. }
  205. // 未上架
  206. else if (this.currentStatusTab == 2) {
  207. params = {
  208. status: 0,
  209. }
  210. }
  211. return params;
  212. }
  213. },
  214. onLoad() {
  215. // 获取商品数据
  216. this.getData();
  217. // 获取餐品分类
  218. this.getClassData();
  219. },
  220. methods: {
  221. // 商品数据
  222. getData() {
  223. let params = {
  224. pageNo: 1,
  225. pageSize: 10,
  226. ...this.initParams
  227. };
  228. this.$http.post('/groupmeal/gmMerchantPackage/pageList', params).then(res => {
  229. if (res.data.code === 200) {
  230. this.allGoods = res.data.result.records.map(item => {
  231. return {
  232. ...item,
  233. image: configService.apiUrl + '/' + item.productImage.split(',')[0]
  234. }
  235. });
  236. }
  237. });
  238. },
  239. // 餐品分类
  240. getClassData() {
  241. this.$http.get('/groupmeal/gmMerchantPackage/getGroupMealCategory', {}).then(res => {
  242. if (res.data.code === 200) {}
  243. });
  244. },
  245. // 操作模式
  246. toggleManageMode() {
  247. this.isManaging = !this.isManaging;
  248. this.selectedGoods = []; // 切换模式时清空选择
  249. this.isAllSelected = false;
  250. if (!this.isManaging) {
  251. this.filteredGoods.flatMap(category => category.items.forEach(item => {
  252. item.checked = false;
  253. }));
  254. }
  255. },
  256. onSearch(value) {
  257. this.$refs[`listScroll`].reloadList({
  258. queryName: value,
  259. });
  260. },
  261. // 主分类事件
  262. onTabChange(e) {
  263. this.currentStatusTab = e;
  264. this.selectedGoods = []; // 切换Tab时清空选择
  265. this.isAllSelected = false;
  266. // 列表数据
  267. this.getData();
  268. // 切换 Tab 后,重置左侧分类高亮到第一个
  269. // this.currentCategoryTab = 0;
  270. // this.$nextTick(() => {
  271. // // 确保在新数据渲染后,滚动到第一个分类
  272. // this.scrollIntoViewId = this.filteredGoods[0]?.scrollId || '';
  273. // });
  274. },
  275. // 添加商品
  276. addGoods() {
  277. uni.navigateTo({
  278. url: '/pages/groupMeal/goods/form-step1'
  279. });
  280. },
  281. // 编辑商品
  282. editGoods(goods) {
  283. uni.navigateTo({
  284. url: '/pages/groupMeal/goods/form-step1?id=' + goods.id
  285. });
  286. },
  287. // 单选
  288. onSelect(e) {
  289. if (e.value) {
  290. this.selectedGoods.push(e.name);
  291. } else {
  292. let i = this.selectedGoods.indexOf(e.name);
  293. this.selectedGoods.splice(i, 1);
  294. }
  295. this.isAllSelected = this.selectedGoods.length === this.allGoods.length;
  296. },
  297. // 全选事件
  298. onSelectAll(e) {
  299. if (e.value) {
  300. // 全选
  301. this.selectedGoods = this.filteredGoods.flatMap(category => category.items.map(item => item.id));
  302. } else {
  303. // 取消全选
  304. this.selectedGoods = [];
  305. }
  306. this.filteredGoods.flatMap(category => category.items.forEach(item => {
  307. item.checked = e.value;
  308. }));
  309. },
  310. // 操作提交事件
  311. handleBatchAction(action) {
  312. if (this.selectedGoods.length === 0) {
  313. uni.showToast({ title: '请先选择商品', icon: 'none' });
  314. return;
  315. }
  316. let content = '';
  317. if (action === 'delete') {
  318. content = '是否将所选商品删除?';
  319. } else {
  320. content = `是否将所选商品${action === 'publish' ? '上架' : '下架'}?`;
  321. }
  322. this.modalText.content = content;
  323. this.$refs.myModal.onOpen(false, () => {
  324. this.onConfirm(action);
  325. });
  326. console.log(`执行批量操作: ${action}`, this.selectedGoods);
  327. },
  328. // 弹窗确认事件
  329. onConfirm(action) {
  330. if (action === 'delete') {
  331. // 单删接口
  332. // this.$http.get('/groupmeal/gmMerchantPackage/delete');
  333. // 多删接口
  334. this.$http.delete('/groupmeal/gmMerchantPackage/deleteBatch', {}, {
  335. params: { ids: this.selectedGoods.join() }
  336. }).then(res => {
  337. if (res.data.code === 200) {}
  338. this.$tip.toast(res.data.message);
  339. });
  340. } else {
  341. // 单个接口
  342. // this.$http.post('/groupmeal/gmMerchantPackage/launch');
  343. // 多选接口
  344. this.$http.post('/groupmeal/gmMerchantPackage/batchLaunch', {
  345. ids: this.selectedGoods,
  346. status: this.currentStatusTab == 2 ? 1 : 0
  347. }).then(res => {
  348. if (res.data.code === 200) {}
  349. this.$tip.toast(res.data.message);
  350. });
  351. }
  352. },
  353. // 核心功能 1: 点击左侧二级分类,右侧商品列表滚动到对应位置
  354. onCategoryTabChange(index) {
  355. this.currentCategoryTab = index;
  356. // 1. 设置标记,在滚动动画期间忽略 onGoodsScroll,防止快速切换闪烁
  357. this.isScrolling = true;
  358. // 2. 找到对应的 scrollId 并设置给 scroll-view
  359. const targetCategory = this.filteredGoods.find(c => c.id === index);
  360. if (targetCategory) {
  361. this.scrollIntoViewId = targetCategory.scrollId;
  362. }
  363. // 3. 延迟清除标记,等待滚动动画结束 (通常 300ms)
  364. setTimeout(() => {
  365. this.isScrolling = false;
  366. }, 300);
  367. },
  368. // 核心功能 2: 滚动商品列表时,左侧二级分类高亮联动
  369. onGoodsScroll(e) {
  370. // 如果是点击左侧分类触发的滚动,则忽略联动
  371. if (this.isScrolling) {
  372. return;
  373. }
  374. const scrollTop = e.detail.scrollTop;
  375. const threshold = 10; // 切换阈值,避免刚好在边缘时闪烁
  376. // 遍历所有分类的 offset,找到当前位于视图顶部的分类
  377. for (let i = 0; i < this.categoryOffsetTops.length; i++) {
  378. const current = this.categoryOffsetTops[i];
  379. const next = this.categoryOffsetTops[i + 1];
  380. // 检查条件:
  381. // 1. 当前分类的 top 已经小于或等于 scrollTop (即分类标题已经经过视图顶部)
  382. // 2. 并且 (如果存在下一个分类) 下一个分类的 top 还没有经过视图顶部
  383. if (current.top <= scrollTop + threshold && (!next || next.top > scrollTop + threshold)) {
  384. // 确保左侧分类的索引是正确的
  385. if (this.currentCategoryTab !== current.id) {
  386. this.currentCategoryTab = current.id;
  387. }
  388. break;
  389. }
  390. }
  391. },
  392. // 核心功能 3: 计算所有分类区块的顶部偏移量
  393. calculateCategoryOffsets() {
  394. if (this.filteredGoods.length === 0) {
  395. this.categoryOffsetTops = [];
  396. return;
  397. }
  398. // 必须使用 $nextTick 确保 DOM 已经渲染
  399. this.$nextTick(() => {
  400. const query = uni.createSelectorQuery().in(this);
  401. let scrollRectTop = 0;
  402. // 1. 获取 scroll-view 顶部到页面的距离 (作为基准点)
  403. query.select('.goods-scroll-view').boundingClientRect(rect => {
  404. scrollRectTop = rect.top;
  405. }).exec();
  406. // 2. 获取所有分类 section 的位置信息
  407. query.selectAll('.goods-category-section').boundingClientRect(data => {
  408. if (!data.length) return;
  409. this.categoryOffsetTops = data.map(item => ({
  410. id: parseInt(item.dataset.categoryIndex), // 从 data-category-index 获取 categoryId
  411. top: item.top - scrollRectTop // 计算相对于 scroll-view 顶部的距离 (即 scrollTop 的值)
  412. }));
  413. }).exec();
  414. });
  415. },
  416. }
  417. };
  418. </script>
  419. <style lang="scss" scoped>
  420. .page-container {
  421. height: 100vh;
  422. display: flex;
  423. flex-direction: column;
  424. .navbar-title {
  425. font-weight: 500;
  426. font-size: 36rpx;
  427. color: #333333;
  428. }
  429. .navbar-right {
  430. padding: 0 24rpx;
  431. display: flex;
  432. align-items: center;
  433. .nav-btn {
  434. font-weight: 500;
  435. font-size: 28rpx;
  436. color: #333333;
  437. }
  438. .manage-btn {
  439. margin-left: 24rpx;
  440. }
  441. }
  442. .search-bar {
  443. padding: 12rpx 32rpx;
  444. background-color: #fff;
  445. .u-search {
  446. border-radius: 8rpx;
  447. background-color: #F7F8FC;
  448. }
  449. }
  450. }
  451. .main-content {
  452. flex: 1;
  453. display: flex;
  454. overflow: hidden;
  455. .category-list {
  456. width: 160rpx;
  457. height: 100%;
  458. background-color: #F7F8FC;
  459. .category-item {
  460. font-weight: 400;
  461. font-size: 24rpx;
  462. color: #333333;
  463. text-align: left;
  464. padding: 24rpx 32rpx;
  465. &.active {
  466. background-color: #fff;
  467. font-weight: 500;
  468. color: #2983EC;
  469. }
  470. }
  471. }
  472. .goods-scroll-view {
  473. flex: 1;
  474. height: 100%;
  475. background-color: #fff;
  476. padding: 0 24rpx 20rpx;
  477. .category-title {
  478. font-weight: 500;
  479. font-size: 26rpx;
  480. color: #000000;
  481. padding-top: 20rpx;
  482. }
  483. .u-checkbox-group {
  484. width: 100%;
  485. }
  486. }
  487. .goods-card {
  488. width: 100%;
  489. padding: 20rpx 0;
  490. border-bottom: 1rpx solid #f0f0f0;
  491. display: flex;
  492. align-items: flex-start;
  493. position: relative;
  494. .goods-image {
  495. width: 120rpx;
  496. height: 120rpx;
  497. border-radius: 12rpx;
  498. flex-shrink: 0;
  499. background-color: #eee;
  500. }
  501. .goods-info {
  502. flex: 1;
  503. margin-left: 20rpx;
  504. .goods-name {
  505. font-size: 28rpx;
  506. font-weight: 500;
  507. color: #333333;
  508. display: -webkit-box;
  509. -webkit-line-clamp: 2;
  510. -webkit-box-orient: vertical;
  511. overflow: hidden;
  512. }
  513. .goods-sales, .goods-desc {
  514. font-size: 22rpx;
  515. font-weight: 400;
  516. color: #8A91A8;
  517. margin-top: 4rpx;
  518. display: -webkit-box;
  519. -webkit-line-clamp: 1;
  520. -webkit-box-orient: vertical;
  521. overflow: hidden;
  522. }
  523. .goods-price-line {
  524. margin-top: 8rpx;
  525. .price {
  526. font-size: 32rpx;
  527. font-weight: 500;
  528. color: #FD4912;
  529. .unit {
  530. font-size: 22rpx;
  531. margin-right: 2rpx;
  532. }
  533. }
  534. .original-price {
  535. font-size: 22rpx;
  536. font-weight: 400;
  537. color: #999999;
  538. text-decoration: line-through;
  539. margin-left: 10rpx;
  540. }
  541. }
  542. }
  543. .goods-actions {
  544. position: absolute;
  545. right: 0;
  546. bottom: 20rpx;
  547. ::v-deep .u-btn {
  548. height: 46rpx;
  549. font-weight: 400;
  550. font-size: 26rpx;
  551. color: #2D88F4;
  552. padding: 0 24rpx;
  553. // background: rgba(45,136,244,0.05);
  554. border-radius: 61rpx;
  555. border: 1rpx solid #2D88F4;
  556. &::after {
  557. display: none;
  558. }
  559. }
  560. }
  561. }
  562. }
  563. .footer-bar {
  564. bottom: 50px;
  565. padding-bottom: 20px;
  566. .action-buttons {
  567. display: flex;
  568. gap: 20rpx;
  569. ::v-deep .u-btn {
  570. height: 69rpx;
  571. border-radius: 49rpx;
  572. }
  573. }
  574. }
  575. </style>