index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <template>
  2. <view>
  3. <view class="hdbj"></view>
  4. <view class='collectionGoods' v-if="collectProductList.length">
  5. <!-- #ifdef H5 || MP-->
  6. <view class='nav acea-row row-between-wrapper'>
  7. <view>当前共 <text class='num font-color'>{{ totals }}</text>件商品</view>
  8. <view class='administrate acea-row row-center-wrapper' @click='manage'>{{ footerswitch ? '管理' : '取消'}}
  9. </view>
  10. </view>
  11. <!-- #endif -->
  12. <view class="list">
  13. <checkbox-group @change="checkboxChange" class="centent" activeColor="#ffe079">
  14. <view v-for="(item,index) in collectProductList" :key="index" class='item acea-row row-middle'>
  15. <checkbox :value="item.id.toString()" :checked="item.checked" v-if="!footerswitch"
  16. style="margin-right: 10rpx;" activeColor="#ffe079" />
  17. <navigator :url='"/pages/goods_details/index?id="+item.productId' hover-class='none'
  18. class="acea-row">
  19. <view class='pictrue'>
  20. <image :src="item.image" mode="aspectFill"></image>
  21. </view>
  22. <view>
  23. <view class='name line1'>{{item.storeName}}</view>
  24. <view class='money font-color'>¥{{item.price}}</view>
  25. </view>
  26. </navigator>
  27. </view>
  28. </checkbox-group>
  29. </view>
  30. <view class='loadingicon acea-row row-center-wrapper'>
  31. <text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
  32. </view>
  33. <view v-if="!footerswitch" class='footer acea-row row-between-wrapper'>
  34. <view>
  35. <checkbox-group @change="checkboxAllChange">
  36. <checkbox value="all" :checked="!!isAllSelect" />
  37. <text class='checkAll'>全选</text>
  38. </checkbox-group>
  39. </view>
  40. <view class='button acea-row row-middle'>
  41. <form @submit="delCollectionAll" report-submit='true'>
  42. <button class='bnt cart-color' formType="submit">取消收藏</button>
  43. </form>
  44. </view>
  45. </view>
  46. </view>
  47. <view class='noCommodity' v-else-if="!collectProductList.length && page > 1">
  48. <view class='pictrue'>
  49. <image :src="HTTP_REQUEST_URL_IMG+'noCollection.png'"></image>
  50. </view>
  51. <recommend :hostProduct="hostProduct"></recommend>
  52. </view>
  53. <home></home>
  54. </view>
  55. </template>
  56. <script setup>
  57. import { ref, computed, onMounted, onActivated } from 'vue';
  58. import { onLoad, onShow, onReachBottom } from '@dcloudio/uni-app';
  59. import { storeToRefs } from 'pinia';
  60. import { useAppStore } from "@/stores/app.js";
  61. import { HTTP_REQUEST_URL_IMG } from "@/config/app";
  62. import { getCollectUserList, getProductHot, collectDelete } from '@/api/store.js';
  63. import { toLogin } from "@/libs/login.js";
  64. import recommend from '@/components/recommend';
  65. import home from '@/components/home';
  66. import Util from '@/utils/util';
  67. // Store
  68. const appStore = useAppStore();
  69. const isLogin = appStore.isLogin;
  70. // 响应式数据
  71. const footerswitch = ref(true);
  72. const hostProduct = ref([]);
  73. const loadTitle = ref('加载更多');
  74. const loading = ref(false);
  75. const loadend = ref(false);
  76. const collectProductList = ref([]);
  77. const limit = 8;
  78. const page = ref(1);
  79. const isAuto = ref(false);
  80. const isShowAuth = ref(false);
  81. const hotScroll = ref(false);
  82. const hotPage = ref(1);
  83. const hotLimit = 10;
  84. const isAllSelect = ref(false);
  85. const selectValue = ref([]);
  86. const delBtnWidth = 80;
  87. const totals = ref(0);
  88. const startX = ref(0);
  89. // #ifdef MP
  90. // 触摸开始
  91. const drawStart = (e) => {
  92. const touch = e.touches[0];
  93. startX.value = touch.clientX;
  94. };
  95. // 触摸滑动
  96. const drawMove = (e) => {
  97. const touch = e.touches[0];
  98. const index = e.currentTarget.dataset.index;
  99. const disX = startX.value - touch.clientX;
  100. if (disX >= 20) {
  101. if (disX > delBtnWidth) {
  102. collectProductList.value[index].right = delBtnWidth;
  103. } else {
  104. collectProductList.value[index].right = disX;
  105. }
  106. } else {
  107. collectProductList.value[index].right = 0;
  108. }
  109. };
  110. // 触摸滑动结束
  111. const drawEnd = (e) => {
  112. const index = e.currentTarget.dataset.index;
  113. const item = collectProductList.value[index];
  114. if (item.right >= delBtnWidth / 2) {
  115. item.right = delBtnWidth;
  116. } else {
  117. item.right = 0;
  118. }
  119. };
  120. // #endif
  121. // 管理切换
  122. const manage = () => {
  123. footerswitch.value = !footerswitch.value;
  124. };
  125. // 多选框变化
  126. const checkboxChange = (event) => {
  127. const values = event.detail.value;
  128. collectProductList.value.forEach(item => {
  129. item.checked = values.includes(item.id.toString());
  130. });
  131. selectValue.value = values.toString();
  132. isAllSelect.value = collectProductList.value.length === values.length;
  133. };
  134. // 全选变化
  135. const checkboxAllChange = (event) => {
  136. if (event.detail.value.length > 0) {
  137. setAllSelectValue(1);
  138. } else {
  139. setAllSelectValue(0);
  140. }
  141. };
  142. // 设置全选状态
  143. const setAllSelectValue = (status) => {
  144. const selectValues = [];
  145. collectProductList.value.forEach(item => {
  146. if (status) {
  147. item.checked = true;
  148. selectValues.push(item.id);
  149. isAllSelect.value = true;
  150. } else {
  151. item.checked = false;
  152. isAllSelect.value = false;
  153. }
  154. });
  155. selectValue.value = selectValues.toString();
  156. };
  157. // 授权回调
  158. const onLoadFun = () => {
  159. get_user_collect_product();
  160. get_host_product();
  161. };
  162. // 授权关闭
  163. const authColse = (e) => {
  164. isShowAuth.value = e;
  165. };
  166. // 获取收藏产品
  167. const get_user_collect_product = async () => {
  168. if (loading.value || loadend.value) return;
  169. loading.value = true;
  170. loadTitle.value = '';
  171. try {
  172. const res = await getCollectUserList({
  173. page: page.value,
  174. limit: limit
  175. });
  176. res.data.list.forEach(item => {
  177. item.right = 0;
  178. });
  179. totals.value = res.data.total;
  180. const collectProductListData = res.data.list;
  181. const isLoadend = collectProductListData.length < limit;
  182. collectProductList.value = Util.SplitArray(collectProductListData, collectProductList.value);
  183. if (collectProductList.value.length === 0) {
  184. get_host_product();
  185. }
  186. loadend.value = isLoadend;
  187. loadTitle.value = isLoadend ? '已全部加载' : '加载更多';
  188. page.value++;
  189. } catch (err) {
  190. console.error(err);
  191. } finally {
  192. loading.value = false;
  193. loadTitle.value = '加载更多';
  194. }
  195. };
  196. // 取消收藏单个
  197. const delCollection = (id, index) => {
  198. selectValue.value = id;
  199. del({ ids: selectValue.value.toString() });
  200. };
  201. // 取消收藏多个
  202. const delCollectionAll = () => {
  203. if (!selectValue.value || selectValue.value.length === 0) {
  204. uni.showToast({ title: '请选择商品',icon:none });
  205. }
  206. del({ ids: selectValue.value });
  207. };
  208. // 删除操作
  209. const del = async (data) => {
  210. try {
  211. await collectDelete(data);
  212. uni.showToast({ title: '取消收藏成功', icon: 'success' });
  213. collectProductList.value = [];
  214. loadend.value = false;
  215. page.value = 1;
  216. get_user_collect_product();
  217. } catch (err) {
  218. uni.showToast({ title: err.message || '操作失败' });
  219. }
  220. };
  221. // 获取热门商品
  222. const get_host_product = async () => {
  223. if (hotScroll.value) return;
  224. try {
  225. const res = await getProductHot(hotPage.value, hotLimit);
  226. hotPage.value++;
  227. hotScroll.value = res.data.list.length < hotLimit;
  228. hostProduct.value = hostProduct.value.concat(res.data.list);
  229. } catch (err) {
  230. console.error(err);
  231. }
  232. };
  233. // 页面加载
  234. onLoad(() => {
  235. if (isLogin) {
  236. loadend.value = false;
  237. page.value = 1;
  238. collectProductList.value = [];
  239. get_user_collect_product();
  240. } else {
  241. toLogin();
  242. }
  243. });
  244. // 页面显示
  245. onShow(() => {
  246. loadend.value = false;
  247. page.value = 1;
  248. collectProductList.value = [];
  249. get_user_collect_product();
  250. });
  251. // 页面上拉触底
  252. onReachBottom(() => {
  253. get_user_collect_product();
  254. get_host_product();
  255. });
  256. </script>
  257. <style scoped lang="scss">
  258. /* 样式部分保持不变 */
  259. .hdbj {
  260. width: 100%;
  261. height: 30rpx;
  262. background-color: #f5f5f5;
  263. z-index: 999999;
  264. position: fixed;
  265. top: 0;
  266. }
  267. .order-item {
  268. width: 100%;
  269. display: flex;
  270. position: relative;
  271. align-items: right;
  272. flex-direction: row;
  273. }
  274. .remove {
  275. width: 120rpx;
  276. height: 100%;
  277. background-color: $theme-color;
  278. color: white;
  279. position: absolute;
  280. top: 0;
  281. right: -160rpx;
  282. display: flex;
  283. justify-content: center;
  284. align-items: center;
  285. font-size: 24rpx;
  286. }
  287. .collectionGoods {
  288. .nav {
  289. width: 92%;
  290. height: 90rpx;
  291. background-color: #fff;
  292. padding: 0 24rpx;
  293. -webkit-box-sizing: border-box;
  294. box-sizing: border-box;
  295. font-size: 28rpx;
  296. color: #282828;
  297. position: fixed;
  298. left: 30rpx;
  299. z-index: 5;
  300. top: 30rpx;
  301. border-bottom: 1px solid #EEEEEE;
  302. border-top-left-radius: 14rpx;
  303. border-top-right-radius: 14rpx;
  304. }
  305. .list {
  306. padding: 30rpx;
  307. margin-top: 90rpx;
  308. .name {
  309. width: 434rpx;
  310. margin-bottom: 56rpx;
  311. }
  312. }
  313. .centent {
  314. /* #ifdef H5 || MP */
  315. background-color: #fff;
  316. /* #endif */
  317. border-bottom-left-radius: 14rpx;
  318. border-bottom-right-radius: 14rpx;
  319. }
  320. }
  321. .collectionGoods .item {
  322. background-color: #fff;
  323. padding-left: 24rpx;
  324. height: 180rpx;
  325. margin-bottom: 15rpx;
  326. border-radius: 14rpx;
  327. }
  328. .collectionGoods .item .pictrue {
  329. width: 130rpx;
  330. height: 130rpx;
  331. margin-right: 20rpx;
  332. }
  333. .collectionGoods .item .pictrue image {
  334. width: 100%;
  335. height: 100%;
  336. border-radius: 14rpx;
  337. }
  338. .collectionGoods .item .text {
  339. width: 535rpx;
  340. height: 130rpx;
  341. font-size: 28rpx;
  342. color: #282828;
  343. }
  344. .collectionGoods .item .text .name {
  345. width: 100%;
  346. }
  347. .collectionGoods .item .text .money {
  348. font-size: 26rpx;
  349. }
  350. .collectionGoods .item .text .delete {
  351. font-size: 26rpx;
  352. color: #282828;
  353. width: 144rpx;
  354. height: 46rpx;
  355. border: 1px solid #bbb;
  356. border-radius: 4rpx;
  357. text-align: center;
  358. line-height: 46rpx;
  359. }
  360. .noCommodity {
  361. background-color: #fff;
  362. padding-top: 1rpx;
  363. border-top: 0;
  364. }
  365. .footer {
  366. z-index: 9;
  367. width: 100%;
  368. height: 96rpx;
  369. background-color: #fff;
  370. position: fixed;
  371. padding: 0 30rpx;
  372. box-sizing: border-box;
  373. border-top: 1rpx solid #eee;
  374. border-bottom: 1px solid #EEEEEE;
  375. /* #ifdef H5 || MP */
  376. bottom: 0rpx;
  377. /* #endif */
  378. /* #ifndef MP */
  379. // bottom: 98rpx;
  380. // bottom: calc(98rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  381. // bottom: calc(98rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  382. /* #endif */
  383. .checkAll {
  384. font-size: 28rpx;
  385. color: #282828;
  386. margin-left: 16rpx;
  387. }
  388. .button .bnt {
  389. font-size: 28rpx;
  390. color: #999;
  391. border-radius: 30rpx;
  392. border: 1px solid #999;
  393. width: 160rpx;
  394. height: 60rpx;
  395. text-align: center;
  396. line-height: 60rpx;
  397. }
  398. }
  399. </style>