index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. <up-checkbox-group shape="circle" @change="checkboxChange" class="centent" activeColor="#F8C008">
  14. <view v-for="(item,index) in collectProductList" :key="index" class='item acea-row row-middle' style="width: 100%;">
  15. <up-checkbox :name="item.id.toString()" :checked="item.checked" v-if="!footerswitch"
  16. style="margin-right: 10rpx;" activeColor="#F8C008" />
  17. <navigator :url='"/pages/goods/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. </up-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. <up-checkbox-group shape="circle" @change="checkboxAllChange">
  36. <up-checkbox value="all" :checked="!!isAllSelect" activeColor="#F8C008" />
  37. <text class='checkAll'>全选</text>
  38. </up-checkbox-group>
  39. </view>
  40. <view class='button acea-row row-middle'>
  41. <form @submit="delCollectionAll" report-submit='true'>
  42. <button class='bnt' 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.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. console.log(isLoadend)
  188. loadTitle.value = isLoadend ? '我是有底线的' : '加载更多';
  189. console.log(loadTitle.value)
  190. page.value++;
  191. } catch (err) {
  192. console.error(err);
  193. } finally {
  194. loading.value = false;
  195. }
  196. };
  197. // 取消收藏单个
  198. const delCollection = (id, index) => {
  199. selectValue.value = id;
  200. del({ ids: selectValue.value.toString() });
  201. };
  202. // 取消收藏多个
  203. const delCollectionAll = () => {
  204. if (!selectValue.value || selectValue.value.length === 0) {
  205. uni.showToast({ title: '请选择商品',icon:none });
  206. }
  207. del({ ids: selectValue.value });
  208. };
  209. // 删除操作
  210. const del = async (data) => {
  211. try {
  212. await collectDelete(data);
  213. uni.showToast({ title: '取消收藏成功', icon: 'success' });
  214. collectProductList.value = [];
  215. loadend.value = false;
  216. page.value = 1;
  217. get_user_collect_product();
  218. } catch (err) {
  219. uni.showToast({ title: err.message || '操作失败' });
  220. }
  221. };
  222. // 获取热门商品
  223. const get_host_product = async () => {
  224. if (hotScroll.value) return;
  225. try {
  226. const res = await getProductHot(hotPage.value, hotLimit);
  227. hotPage.value++;
  228. hotScroll.value = res.data.list.length < hotLimit;
  229. hostProduct.value = hostProduct.value.concat(res.data.list);
  230. } catch (err) {
  231. console.error(err);
  232. }
  233. };
  234. // 页面加载
  235. onLoad(() => {
  236. if (isLogin) {
  237. loadend.value = false;
  238. page.value = 1;
  239. collectProductList.value = [];
  240. get_user_collect_product();
  241. } else {
  242. toLogin();
  243. }
  244. });
  245. // 页面显示
  246. onShow(() => {
  247. loadend.value = false;
  248. page.value = 1;
  249. collectProductList.value = [];
  250. get_user_collect_product();
  251. });
  252. // 页面上拉触底
  253. onReachBottom(() => {
  254. get_user_collect_product();
  255. get_host_product();
  256. });
  257. </script>
  258. <style scoped lang="scss">
  259. /* 样式部分保持不变 */
  260. .hdbj {
  261. width: 100%;
  262. height: 30rpx;
  263. background-color: #f5f5f5;
  264. z-index: 999999;
  265. position: fixed;
  266. top: 0;
  267. }
  268. .order-item {
  269. width: 100%;
  270. display: flex;
  271. position: relative;
  272. align-items: right;
  273. flex-direction: row;
  274. }
  275. .remove {
  276. width: 120rpx;
  277. height: 100%;
  278. background-color: $theme-color;
  279. color: white;
  280. position: absolute;
  281. top: 0;
  282. right: -160rpx;
  283. display: flex;
  284. justify-content: center;
  285. align-items: center;
  286. font-size: 24rpx;
  287. }
  288. .collectionGoods {
  289. .nav {
  290. width: 92%;
  291. height: 90rpx;
  292. background-color: #fff;
  293. padding: 0 24rpx;
  294. -webkit-box-sizing: border-box;
  295. box-sizing: border-box;
  296. font-size: 28rpx;
  297. color: #282828;
  298. position: fixed;
  299. left: 30rpx;
  300. z-index: 5;
  301. top: 30rpx;
  302. border-bottom: 1px solid #EEEEEE;
  303. border-top-left-radius: 14rpx;
  304. border-top-right-radius: 14rpx;
  305. }
  306. .list {
  307. padding: 30rpx;
  308. margin-top: 110rpx;
  309. .name {
  310. width: 434rpx;
  311. margin-bottom: 56rpx;
  312. }
  313. }
  314. .centent {
  315. /* #ifdef H5 || MP */
  316. background-color: #fff;
  317. /* #endif */
  318. border-bottom-left-radius: 14rpx;
  319. border-bottom-right-radius: 14rpx;
  320. }
  321. }
  322. .collectionGoods .item {
  323. background-color: #fff;
  324. padding-left: 24rpx;
  325. height: 180rpx;
  326. margin-bottom: 20rpx;
  327. border-radius: 14rpx;
  328. }
  329. .collectionGoods .item .pictrue {
  330. width: 130rpx;
  331. height: 130rpx;
  332. margin-right: 20rpx;
  333. }
  334. .collectionGoods .item .pictrue image {
  335. width: 100%;
  336. height: 100%;
  337. border-radius: 14rpx;
  338. }
  339. .collectionGoods .item .text {
  340. width: 535rpx;
  341. height: 130rpx;
  342. font-size: 28rpx;
  343. color: #282828;
  344. }
  345. .collectionGoods .item .text .name {
  346. width: 100%;
  347. }
  348. .collectionGoods .item .text .money {
  349. font-size: 26rpx;
  350. }
  351. .collectionGoods .item .text .delete {
  352. font-size: 26rpx;
  353. color: #282828;
  354. width: 144rpx;
  355. height: 46rpx;
  356. border: 1px solid #bbb;
  357. border-radius: 4rpx;
  358. text-align: center;
  359. line-height: 46rpx;
  360. }
  361. .noCommodity {
  362. background-color: #fff;
  363. padding-top: 1rpx;
  364. border-top: 0;
  365. }
  366. .footer {
  367. z-index: 9;
  368. width: 100%;
  369. height: 96rpx;
  370. background-color: #fff;
  371. position: fixed;
  372. padding: 0 30rpx;
  373. box-sizing: border-box;
  374. border-top: 1rpx solid #eee;
  375. border-bottom: 1px solid #EEEEEE;
  376. /* #ifdef H5 || MP */
  377. bottom: 0rpx;
  378. /* #endif */
  379. /* #ifndef MP */
  380. // bottom: 98rpx;
  381. // bottom: calc(98rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  382. // bottom: calc(98rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  383. /* #endif */
  384. .checkAll {
  385. font-size: 28rpx;
  386. color: #282828;
  387. margin-left: 16rpx;
  388. line-height: 56rpx;
  389. }
  390. .button .bnt {
  391. font-size: 28rpx;
  392. color: #333333;
  393. border-radius: 8rpx;
  394. background-color: #F8C008;
  395. width: 180rpx;
  396. height: 60rpx;
  397. line-height: 60rpx;
  398. text-align: center;
  399. border: none;
  400. }
  401. }
  402. </style>