goods-collect.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. <label
  35. class="check-box ss-flex ss-col-center ss-p-l-10"
  36. v-if="state.editMode"
  37. @tap="onSelect(item.goods_id)"
  38. >
  39. <radio
  40. :checked="state.selectedCollectList.includes(item.goods_id)"
  41. color="var(--ui-BG-Main)"
  42. style="transform: scale(0.8)"
  43. @tap.stop="onSelect(item.goods_id)"
  44. />
  45. </label>
  46. <s-goods-item
  47. :title="item.goods.title"
  48. :img="item.goods.image"
  49. :price="item.goods.price[0]"
  50. :skuText="item.goods.subtitle"
  51. priceColor="#FF3000"
  52. :titleWidth="400"
  53. @tap="
  54. sheep.$router.go('/pages/goods/index', {
  55. id: item.goods_id,
  56. })
  57. "
  58. >
  59. </s-goods-item>
  60. </view>
  61. </view>
  62. </view>
  63. <!-- 底部 -->
  64. <su-fixed bottom :val="0" placeholder v-show="state.editMode">
  65. <view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
  66. <view class="footer-left ss-flex ss-col-center">
  67. <label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll">
  68. <radio
  69. :checked="state.selectAll"
  70. color="var(--ui-BG-Main)"
  71. style="transform: scale(0.7)"
  72. @tap.stop="onSelectAll"
  73. />
  74. <view> 全选 </view>
  75. </label>
  76. </view>
  77. <view class="footer-right">
  78. <button
  79. class="ss-reset-button ui-BG-Main-Gradient pay-btn ss-font-28 ui-Shadow-Main"
  80. @tap="onCancel"
  81. >取消收藏</button
  82. >
  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';
  103. const sys_navBar = sheep.$platform.navbar;
  104. const pagination = {
  105. data: [],
  106. current_page: 1,
  107. total: 1,
  108. last_page: 1,
  109. };
  110. const state = reactive({
  111. pagination: {
  112. data: [],
  113. current_page: 1,
  114. total: 1,
  115. last_page: 1,
  116. },
  117. loadStatus: '',
  118. editMode: false,
  119. selectedCollectList: [],
  120. selectAll: false,
  121. });
  122. async function getData(page = 1, list_rows = 10) {
  123. state.loadStatus = 'loading';
  124. let res = await sheep.$api.user.goodsLog.list({
  125. type: 'favorite',
  126. list_rows,
  127. page,
  128. });
  129. if (res.code === 1) {
  130. let orderList = _.concat(state.pagination.data, res.data.data);
  131. state.pagination = {
  132. ...res.data,
  133. data: orderList,
  134. };
  135. if (state.pagination.current_page < state.pagination.last_page) {
  136. state.loadStatus = 'more';
  137. } else {
  138. state.loadStatus = 'noMore';
  139. }
  140. }
  141. }
  142. // 格式化价格
  143. function formatPrice(e) {
  144. return e.length === 1 ? e[0] : e.join('~');
  145. }
  146. // 单选选中
  147. const onSelect = (id) => {
  148. if (!state.selectedCollectList.includes(id)) {
  149. state.selectedCollectList.push(id);
  150. } else {
  151. state.selectedCollectList.splice(state.selectedCollectList.indexOf(id), 1);
  152. }
  153. state.selectAll = state.selectedCollectList.length === state.pagination.data.length;
  154. };
  155. // 全选
  156. const onSelectAll = () => {
  157. state.selectAll = !state.selectAll;
  158. if (!state.selectAll) {
  159. state.selectedCollectList = [];
  160. } else {
  161. state.pagination.data.forEach((item) => {
  162. if (state.selectedCollectList.includes(item.goods_id)) {
  163. state.selectedCollectList.splice(state.selectedCollectList.indexOf(item.goods_id), 1);
  164. }
  165. state.selectedCollectList.push(item.goods_id);
  166. });
  167. }
  168. };
  169. async function onCancel() {
  170. if (state.selectedCollectList) {
  171. state.selectedCollectList = state.selectedCollectList.toString();
  172. const { code } = await sheep.$api.user.favorite.cancel(state.selectedCollectList);
  173. if (code === 1) {
  174. state.editMode = false;
  175. state.selectedCollectList = [];
  176. state.selectAll = false;
  177. state.pagination = pagination;
  178. getData();
  179. }
  180. }
  181. }
  182. // 加载更多
  183. function loadmore() {
  184. if (state.loadStatus !== 'noMore') {
  185. getData(state.pagination.current_page + 1);
  186. }
  187. }
  188. onReachBottom(() => {
  189. loadmore();
  190. });
  191. onLoad(() => {
  192. getData();
  193. });
  194. </script>
  195. <style lang="scss" scoped>
  196. .cart-box {
  197. .cart-header {
  198. height: 70rpx;
  199. background-color: #f6f6f6;
  200. width: 100%;
  201. position: fixed;
  202. left: 0;
  203. top: v-bind('sys_navBar') rpx;
  204. z-index: 1000;
  205. box-sizing: border-box;
  206. }
  207. .cart-footer {
  208. height: 100rpx;
  209. background-color: #fff;
  210. .pay-btn {
  211. height: 80rpx;
  212. line-height: 80rpx;
  213. border-radius: 40rpx;
  214. padding: 0 40rpx;
  215. min-width: 200rpx;
  216. }
  217. }
  218. .cart-content {
  219. width: 100%;
  220. margin-top: 70rpx;
  221. padding: 0 20rpx;
  222. box-sizing: border-box;
  223. .goods-box {
  224. background-color: #fff;
  225. &:last-child {
  226. margin-bottom: 40rpx;
  227. }
  228. }
  229. }
  230. }
  231. </style>