goods-collect.vue 6.3 KB

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