goods-collect.vue 6.5 KB

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