box-bind.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <view class="page">
  3. <u-navbar title="团餐商品列表" :autoBack="true" :placeholder="true" :border-bottom="true"></u-navbar>
  4. <view class="search">
  5. <u-search
  6. v-model="keyword"
  7. placeholder="请输入商品名称"
  8. :show-action="true"
  9. action-text="搜索"
  10. bg-color="#F5F5F5"
  11. @search="fetchPackages"
  12. @custom="fetchPackages"
  13. ></u-search>
  14. </view>
  15. <view class="body">
  16. <view class="cats">
  17. <view
  18. v-for="(c, i) in cats"
  19. :key="c"
  20. :class="['cat', catIndex === i ? 'on' : '']"
  21. @click="onCatChange(i)"
  22. >{{ c }}</view>
  23. </view>
  24. <scroll-view scroll-y class="goods">
  25. <view class="cat-title">{{ cats[catIndex] }}</view>
  26. <view class="g-item" v-for="item in list" :key="item.id" @click="toggle(item.id)">
  27. <image class="thumb" :src="item.image" mode="aspectFill"></image>
  28. <view class="info">
  29. <view class="name">{{ item.name }}</view>
  30. <view class="desc">{{ item.desc || '暂无描述' }}</view>
  31. <view class="price">¥{{ formatPrice(item.price) }}</view>
  32. </view>
  33. <view :class="['check', selected.includes(item.id) ? 'on' : '']">
  34. <u-icon v-if="selected.includes(item.id)" name="checkmark" color="#fff" size="22"></u-icon>
  35. </view>
  36. </view>
  37. <u-empty v-if="!loading && !list.length" text="暂无团餐商品" mode="list"></u-empty>
  38. </scroll-view>
  39. </view>
  40. <view class="footer">
  41. <view class="all" @click="toggleAll">
  42. <view :class="['check', isAll ? 'on' : '']">
  43. <u-icon v-if="isAll" name="checkmark" color="#fff" size="22"></u-icon>
  44. </view>
  45. <text>全选</text>
  46. </view>
  47. <view :class="['bind-btn', saving ? 'disabled' : '']" @click="onConfirm">{{ saving ? '绑定中...' : '绑定' }}</view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import merchantDishApi, { mapPackageToListItem } from '@/api/merchantDish.js';
  53. export default {
  54. data() {
  55. return {
  56. boxId: null,
  57. fromForm: false,
  58. keyword: '',
  59. cats: ['早餐', '午餐', '晚餐'],
  60. catIndex: 0,
  61. list: [],
  62. selected: [],
  63. loading: false,
  64. saving: false,
  65. eventChannel: null
  66. };
  67. },
  68. computed: {
  69. isAll() {
  70. const ids = this.list.map((i) => i.id);
  71. return ids.length > 0 && ids.every((id) => this.selected.includes(id));
  72. },
  73. isTempBox() {
  74. return !this.boxId || this.boxId === 'new';
  75. }
  76. },
  77. onLoad(query) {
  78. this.boxId = query.id;
  79. this.fromForm = query.from === 'form';
  80. this.eventChannel = this.getOpenerEventChannel && this.getOpenerEventChannel();
  81. if (this.eventChannel && this.eventChannel.on) {
  82. this.eventChannel.on('initBind', (data) => {
  83. this.selected = [...((data && data.bindIds) || [])];
  84. });
  85. }
  86. this.initSelected().then(() => this.fetchPackages());
  87. },
  88. methods: {
  89. formatPrice(price) {
  90. const n = Number(price);
  91. if (Number.isNaN(n)) return '0.00';
  92. return n.toFixed(2);
  93. },
  94. initSelected() {
  95. // 从表单进入:等待 eventChannel 传入已选;新建无接口数据
  96. if (this.fromForm || this.isTempBox) {
  97. return Promise.resolve();
  98. }
  99. // 从列表进入:拉接口已绑定套餐
  100. return merchantDishApi.mealBoxPackages(this.boxId).then((res) => {
  101. if (res.data.code === 200) {
  102. const pkgs = res.data.result || [];
  103. this.selected = pkgs.map((p) => p.packageId).filter(Boolean);
  104. }
  105. });
  106. },
  107. onCatChange(i) {
  108. this.catIndex = i;
  109. this.fetchPackages();
  110. },
  111. fetchPackages() {
  112. this.loading = true;
  113. merchantDishApi
  114. .packagePageList({
  115. pageNo: 1,
  116. pageSize: 100,
  117. productBelonging: 2,
  118. category: String(this.catIndex + 1),
  119. name: this.keyword || undefined
  120. })
  121. .then((res) => {
  122. if (res.data.code === 200) {
  123. const page = res.data.result || {};
  124. this.list = (page.records || []).map((item) => {
  125. const mapped = mapPackageToListItem(item);
  126. const mealBoxNames = item.mealBoxNames || '';
  127. return {
  128. ...mapped,
  129. desc: mealBoxNames || mapped.desc || '',
  130. mealBoxNames
  131. };
  132. });
  133. } else {
  134. this.$tip.toast(res.data.message || '加载失败');
  135. }
  136. })
  137. .finally(() => {
  138. this.loading = false;
  139. });
  140. },
  141. toggle(id) {
  142. const i = this.selected.indexOf(id);
  143. if (i >= 0) this.selected.splice(i, 1);
  144. else this.selected.push(id);
  145. },
  146. toggleAll() {
  147. if (this.isAll) {
  148. const ids = this.list.map((i) => i.id);
  149. this.selected = this.selected.filter((id) => !ids.includes(id));
  150. } else {
  151. const set = new Set(this.selected);
  152. this.list.forEach((i) => set.add(i.id));
  153. this.selected = Array.from(set);
  154. }
  155. },
  156. onConfirm() {
  157. if (this.saving) return;
  158. // 从表单进入:回写上一页,随表单保存一起提交接口
  159. if (this.fromForm || this.isTempBox) {
  160. if (this.eventChannel && this.eventChannel.emit) {
  161. this.eventChannel.emit('onBindConfirm', { bindIds: [...this.selected] });
  162. } else {
  163. const pages = getCurrentPages();
  164. const prev = pages[pages.length - 2];
  165. if (prev && prev.$vm && typeof prev.$vm.applyBindIds === 'function') {
  166. prev.$vm.applyBindIds([...this.selected]);
  167. }
  168. }
  169. uni.showToast({ title: '已选择', icon: 'success' });
  170. setTimeout(() => uni.navigateBack(), 400);
  171. return;
  172. }
  173. // 从列表直接绑定:立即调接口全量替换
  174. this.saving = true;
  175. merchantDishApi
  176. .mealBoxBind(this.boxId, this.selected)
  177. .then((res) => {
  178. const ok = res.data.code === 200;
  179. this.$tip.toast(res.data.message || (ok ? '绑定成功' : '绑定失败'));
  180. if (ok) setTimeout(() => uni.navigateBack(), 400);
  181. })
  182. .finally(() => {
  183. this.saving = false;
  184. });
  185. }
  186. }
  187. };
  188. </script>
  189. <style lang="scss" scoped>
  190. .page {
  191. min-height: 100vh;
  192. background: #fff;
  193. display: flex;
  194. flex-direction: column;
  195. padding-bottom: 140rpx;
  196. }
  197. .search {
  198. padding: 16rpx 24rpx;
  199. background: #fff;
  200. }
  201. .body {
  202. flex: 1;
  203. display: flex;
  204. min-height: 0;
  205. background: #fff;
  206. }
  207. .cats {
  208. width: 160rpx;
  209. background: #f5f5f5;
  210. .cat {
  211. height: 96rpx;
  212. line-height: 96rpx;
  213. text-align: center;
  214. font-size: 28rpx;
  215. color: #333;
  216. &.on {
  217. background: #fff;
  218. color: #449aff;
  219. font-weight: 500;
  220. }
  221. }
  222. }
  223. .goods {
  224. flex: 1;
  225. height: calc(100vh - 280rpx);
  226. padding: 0 24rpx 24rpx;
  227. box-sizing: border-box;
  228. background: #fff;
  229. }
  230. .cat-title {
  231. padding: 20rpx 0 8rpx;
  232. font-size: 30rpx;
  233. font-weight: 600;
  234. color: #333;
  235. }
  236. .g-item {
  237. display: flex;
  238. align-items: center;
  239. padding: 20rpx 0;
  240. .thumb {
  241. width: 120rpx;
  242. height: 120rpx;
  243. border-radius: 12rpx;
  244. background: #eee;
  245. flex-shrink: 0;
  246. }
  247. .info {
  248. flex: 1;
  249. margin: 0 20rpx;
  250. min-width: 0;
  251. .name {
  252. font-size: 28rpx;
  253. font-weight: 600;
  254. color: #333;
  255. overflow: hidden;
  256. text-overflow: ellipsis;
  257. white-space: nowrap;
  258. }
  259. .desc {
  260. font-size: 22rpx;
  261. color: #999;
  262. margin-top: 8rpx;
  263. overflow: hidden;
  264. text-overflow: ellipsis;
  265. white-space: nowrap;
  266. }
  267. .price {
  268. margin-top: 10rpx;
  269. font-size: 28rpx;
  270. font-weight: 600;
  271. color: #ff4d6a;
  272. }
  273. }
  274. }
  275. .check {
  276. width: 40rpx;
  277. height: 40rpx;
  278. border-radius: 50%;
  279. border: 2rpx solid #d0d0d0;
  280. flex-shrink: 0;
  281. display: flex;
  282. align-items: center;
  283. justify-content: center;
  284. box-sizing: border-box;
  285. &.on {
  286. background: #449aff;
  287. border-color: #449aff;
  288. }
  289. }
  290. .footer {
  291. position: fixed;
  292. left: 0;
  293. right: 0;
  294. bottom: 0;
  295. z-index: 20;
  296. display: flex;
  297. align-items: center;
  298. padding: 16rpx 24rpx calc(16rpx + env(safe-area-inset-bottom));
  299. background: #fff;
  300. border-top: 1rpx solid #f0f0f0;
  301. .all {
  302. display: flex;
  303. align-items: center;
  304. font-size: 28rpx;
  305. color: #333;
  306. margin-right: 24rpx;
  307. .check {
  308. margin-right: 12rpx;
  309. }
  310. }
  311. .bind-btn {
  312. flex: 1;
  313. height: 80rpx;
  314. line-height: 80rpx;
  315. text-align: center;
  316. color: #fff;
  317. font-size: 30rpx;
  318. border-radius: 40rpx;
  319. background: linear-gradient( 89deg, #10ABFE 0%, #2260F6 100%);
  320. &.disabled {
  321. opacity: 0.6;
  322. }
  323. }
  324. }
  325. </style>