goods-collect.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <!-- 我的商品收藏 -->
  2. <template>
  3. <s-layout title="商品收藏">
  4. <view class="cart-box ss-flex ss-flex-col ss-row-between">
  5. <!-- 头部 -->
  6. <view class="cart-header ss-flex ss-col-center ss-row-between ss-p-x-30">
  7. <view class="header-left ss-flex ss-col-center ss-font-26">
  8. <text class="goods-number ui-TC-Main ss-flex">{{ state.pagination.total }}</text> 件商品
  9. </view>
  10. <view class="header-right">
  11. <button
  12. v-if="state.editMode && state.pagination.total"
  13. class="ss-reset-button"
  14. @tap="state.editMode = false"
  15. >
  16. 取消
  17. </button>
  18. <button
  19. v-if="!state.editMode && state.pagination.total"
  20. class="ss-reset-button ui-TC-Main"
  21. @tap="state.editMode = true"
  22. >
  23. 编辑
  24. </button>
  25. </view>
  26. </view>
  27. <!-- 内容 -->
  28. <view class="cart-content">
  29. <view
  30. class="goods-box ss-r-10 ss-m-b-14"
  31. v-for="item in state.pagination.list"
  32. :key="item.id"
  33. >
  34. <view class="ss-flex ss-col-center">
  35. <label
  36. class="check-box ss-flex ss-col-center ss-p-l-10"
  37. v-if="state.editMode"
  38. @tap="onSelect(item.spuId)"
  39. >
  40. <radio
  41. :checked="state.selectedCollectList.includes(item.spuId)"
  42. color="var(--ui-BG-Main)"
  43. style="transform: scale(0.8)"
  44. @tap.stop="onSelect(item.spuId)"
  45. />
  46. </label>
  47. <s-goods-item
  48. :title="item.spuName"
  49. :img="item.picUrl"
  50. :price="item.price"
  51. priceColor="#FF3000"
  52. :titleWidth="400"
  53. @tap="
  54. sheep.$router.go('/pages/goods/index', {
  55. id: item.spuId,
  56. })
  57. "
  58. />
  59. </view>
  60. </view>
  61. </view>
  62. <!-- 底部 -->
  63. <su-fixed bottom :val="0" placeholder v-show="state.editMode">
  64. <view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
  65. <view class="footer-left ss-flex ss-col-center">
  66. <label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll">
  67. <radio
  68. :checked="state.selectAll"
  69. color="var(--ui-BG-Main)"
  70. style="transform: scale(0.7)"
  71. @tap.stop="onSelectAll"
  72. />
  73. <view> 全选 </view>
  74. </label>
  75. </view>
  76. <view class="footer-right">
  77. <button
  78. class="ss-reset-button ui-BG-Main-Gradient pay-btn ss-font-28 ui-Shadow-Main"
  79. @tap="onCancel"
  80. >
  81. 取消收藏
  82. </button>
  83. </view>
  84. </view>
  85. </su-fixed>
  86. </view>
  87. <uni-load-more
  88. v-if="state.pagination.total > 0"
  89. :status="state.loadStatus"
  90. :content-text="{
  91. contentdown: '上拉加载更多',
  92. }"
  93. @tap="loadMore"
  94. />
  95. <s-empty v-if="state.pagination.total === 0" text="暂无收藏" icon="/static/collect-empty.png" />
  96. </s-layout>
  97. </template>
  98. <script setup>
  99. import sheep from '@/sheep';
  100. import { reactive } from 'vue';
  101. import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  102. import _ from 'lodash-es';
  103. import FavoriteApi from '@/sheep/api/product/favorite';
  104. import { resetPagination } from '@/sheep/util';
  105. const sys_navBar = sheep.$platform.navbar;
  106. const state = reactive({
  107. pagination: {
  108. list: [],
  109. total: 0,
  110. pageNo: 1,
  111. pageSize: 6,
  112. },
  113. loadStatus: '',
  114. editMode: false,
  115. selectedCollectList: [], // 选中的 SPU 数组
  116. selectAll: false,
  117. });
  118. async function getData() {
  119. state.loadStatus = 'loading';
  120. const { code, data } = await FavoriteApi.getFavoritePage({
  121. pageNo: state.pagination.pageNo,
  122. pageSize: state.pagination.pageSize,
  123. });
  124. if (code !== 0) {
  125. return;
  126. }
  127. state.pagination.list = _.concat(state.pagination.list, data.list);
  128. state.pagination.total = data.total;
  129. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  130. }
  131. // 单选选中
  132. const onSelect = (spuId) => {
  133. if (!state.selectedCollectList.includes(spuId)) {
  134. state.selectedCollectList.push(spuId);
  135. } else {
  136. state.selectedCollectList.splice(state.selectedCollectList.indexOf(spuId), 1);
  137. }
  138. state.selectAll = state.selectedCollectList.length === state.pagination.list.length;
  139. };
  140. // 全选
  141. const onSelectAll = () => {
  142. state.selectAll = !state.selectAll;
  143. if (!state.selectAll) {
  144. state.selectedCollectList = [];
  145. } else {
  146. state.selectedCollectList = state.pagination.list.map((item) => item.spuId);
  147. }
  148. };
  149. async function onCancel() {
  150. if (!state.selectedCollectList) {
  151. return;
  152. }
  153. // 取消收藏
  154. for (const spuId of state.selectedCollectList) {
  155. await FavoriteApi.deleteFavorite(spuId);
  156. }
  157. // 清空选择 + 重新加载
  158. state.editMode = false;
  159. state.selectedCollectList = [];
  160. state.selectAll = false;
  161. resetPagination(state.pagination);
  162. await getData();
  163. }
  164. // 加载更多
  165. function loadMore() {
  166. if (state.loadStatus === 'noMore') {
  167. return;
  168. }
  169. state.pagination.pageNo++;
  170. getData();
  171. }
  172. onReachBottom(() => {
  173. loadMore();
  174. });
  175. onLoad(() => {
  176. getData();
  177. });
  178. </script>
  179. <style lang="scss" scoped>
  180. .cart-box {
  181. .cart-header {
  182. height: 70rpx;
  183. background-color: #f6f6f6;
  184. width: 100%;
  185. position: fixed;
  186. left: 0;
  187. top: v-bind('sys_navBar') rpx;
  188. z-index: 1000;
  189. box-sizing: border-box;
  190. }
  191. .cart-footer {
  192. height: 100rpx;
  193. background-color: #fff;
  194. .pay-btn {
  195. height: 80rpx;
  196. line-height: 80rpx;
  197. border-radius: 40rpx;
  198. padding: 0 40rpx;
  199. min-width: 200rpx;
  200. }
  201. }
  202. .cart-content {
  203. width: 100%;
  204. margin-top: 70rpx;
  205. padding: 0 20rpx;
  206. box-sizing: border-box;
  207. .goods-box {
  208. background-color: #fff;
  209. &:last-child {
  210. margin-bottom: 40rpx;
  211. }
  212. }
  213. }
  214. }
  215. </style>