goods-collect.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "
  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. </button>
  82. </view>
  83. </view>
  84. </su-fixed>
  85. </view>
  86. <uni-load-more
  87. v-if="state.pagination.total > 0"
  88. :status="state.loadStatus"
  89. :content-text="{
  90. contentdown: '上拉加载更多',
  91. }"
  92. @tap="loadMore"
  93. />
  94. <s-empty v-if="state.pagination.total === 0" text="暂无收藏" icon="/static/collect-empty.png" />
  95. </s-layout>
  96. </template>
  97. <script setup>
  98. import sheep from '@/sheep';
  99. import { reactive } from 'vue';
  100. import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  101. import _ from 'lodash';
  102. import FavoriteApi from '@/sheep/api/product/favorite';
  103. import { resetPagination } from '@/sheep/util';
  104. const sys_navBar = sheep.$platform.navbar;
  105. const state = reactive({
  106. pagination: {
  107. list: [],
  108. total: 0,
  109. pageNo: 1,
  110. pageSize: 6,
  111. },
  112. loadStatus: '',
  113. editMode: false,
  114. selectedCollectList: [], // 选中的 SPU 数组
  115. selectAll: false,
  116. });
  117. async function getData() {
  118. state.loadStatus = 'loading';
  119. const { code, data } = await FavoriteApi.getFavoritePage({
  120. pageNo: state.pagination.pageNo,
  121. pageSize: state.pagination.pageSize,
  122. });
  123. if (code !== 0) {
  124. return;
  125. }
  126. state.pagination.list = _.concat(state.pagination.list, data.list)
  127. state.pagination.total = data.total;
  128. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  129. }
  130. // 单选选中
  131. const onSelect = (spuId) => {
  132. if (!state.selectedCollectList.includes(spuId)) {
  133. state.selectedCollectList.push(spuId);
  134. } else {
  135. state.selectedCollectList.splice(state.selectedCollectList.indexOf(spuId), 1);
  136. }
  137. state.selectAll = state.selectedCollectList.length === state.pagination.list.length;
  138. };
  139. // 全选
  140. const onSelectAll = () => {
  141. state.selectAll = !state.selectAll;
  142. if (!state.selectAll) {
  143. state.selectedCollectList = [];
  144. } else {
  145. state.selectedCollectList = state.pagination.list.map((item) => item.spuId);
  146. }
  147. };
  148. async function onCancel() {
  149. if (!state.selectedCollectList) {
  150. return;
  151. }
  152. // 取消收藏
  153. for (const spuId of state.selectedCollectList) {
  154. await FavoriteApi.deleteFavorite(spuId);
  155. }
  156. // 清空选择 + 重新加载
  157. state.editMode = false;
  158. state.selectedCollectList = [];
  159. state.selectAll = false;
  160. resetPagination(state.pagination);
  161. await getData();
  162. }
  163. // 加载更多
  164. function loadMore() {
  165. if (state.loadStatus === 'noMore') {
  166. return
  167. }
  168. state.pagination.pageNo++;
  169. getData();
  170. }
  171. onReachBottom(() => {
  172. loadMore();
  173. });
  174. onLoad(() => {
  175. getData();
  176. });
  177. </script>
  178. <style lang="scss" scoped>
  179. .cart-box {
  180. .cart-header {
  181. height: 70rpx;
  182. background-color: #f6f6f6;
  183. width: 100%;
  184. position: fixed;
  185. left: 0;
  186. top: v-bind('sys_navBar') rpx;
  187. z-index: 1000;
  188. box-sizing: border-box;
  189. }
  190. .cart-footer {
  191. height: 100rpx;
  192. background-color: #fff;
  193. .pay-btn {
  194. height: 80rpx;
  195. line-height: 80rpx;
  196. border-radius: 40rpx;
  197. padding: 0 40rpx;
  198. min-width: 200rpx;
  199. }
  200. }
  201. .cart-content {
  202. width: 100%;
  203. margin-top: 70rpx;
  204. padding: 0 20rpx;
  205. box-sizing: border-box;
  206. .goods-box {
  207. background-color: #fff;
  208. &:last-child {
  209. margin-bottom: 40rpx;
  210. }
  211. }
  212. }
  213. }
  214. </style>