index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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.totalPrice}}</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. // 获取实时金价
  66. import useRealGoldPrice from "@/hooks/useRealGoldPrice";
  67. const {
  68. realGoldprice, // 黄金实时销售价(基础)
  69. realPtprice, // 铂金实时销售价(基础)
  70. realAgprice, // 白银实时销售价(基础)
  71. } = useRealGoldPrice({});
  72. import home from '@/components/home';
  73. import Util from '@/utils/util';
  74. // Store
  75. const appStore = useAppStore();
  76. const isLogin = appStore.isLogin;
  77. // 响应式数据
  78. const footerswitch = ref(true);
  79. const hostProduct = ref([]);
  80. const loadTitle = ref('加载更多');
  81. const loading = ref(false);
  82. const loadend = ref(false);
  83. const collectList = ref([]);
  84. const limit = 8;
  85. const page = ref(1);
  86. const isAuto = ref(false);
  87. const isShowAuth = ref(false);
  88. const hotScroll = ref(false);
  89. const hotPage = ref(1);
  90. const hotLimit = 10;
  91. const isAllSelect = ref(false);
  92. const selectValue = ref([]);
  93. const delBtnWidth = 80;
  94. const totals = ref(0);
  95. const startX = ref(0);
  96. const collectProductList = computed(() => {
  97. // 计算逻辑与原代码一致,但基于响应式数据 products
  98. return collectList.value.map((product) => {
  99. // 1. 将price字符串转为数字
  100. const price = Number(product.price);
  101. // 2. 计算乘积
  102. const total = (price + product.sales) * realGoldprice.value;
  103. // 3. 向上取整到两位小数(先放大100倍取整,再缩小100倍)
  104. const roundedTotal = Math.ceil(total * 100) / 100;
  105. // 4. 格式化保留两位小数
  106. const formattedTotal = roundedTotal.toFixed(2);
  107. const totalLaborCost = Number(product.totalLaborCost);
  108. const additionalAmount = Number(product.additionalAmount);
  109. const totalPrice = (totalLaborCost+additionalAmount).toFixed(2);
  110. return {
  111. ...product,
  112. calculatedTotal: formattedTotal, // 新增计算结果字段
  113. totalPrice
  114. };
  115. });
  116. });
  117. // #ifdef MP
  118. // 触摸开始
  119. const drawStart = (e) => {
  120. const touch = e.touches[0];
  121. startX.value = touch.clientX;
  122. };
  123. // 触摸滑动
  124. const drawMove = (e) => {
  125. const touch = e.touches[0];
  126. const index = e.currentTarget.dataset.index;
  127. const disX = startX.value - touch.clientX;
  128. if (disX >= 20) {
  129. if (disX > delBtnWidth) {
  130. collectProductList.value[index].right = delBtnWidth;
  131. } else {
  132. collectProductList.value[index].right = disX;
  133. }
  134. } else {
  135. collectProductList.value[index].right = 0;
  136. }
  137. };
  138. // 触摸滑动结束
  139. const drawEnd = (e) => {
  140. const index = e.currentTarget.dataset.index;
  141. const item = collectProductList.value[index];
  142. if (item.right >= delBtnWidth / 2) {
  143. item.right = delBtnWidth;
  144. } else {
  145. item.right = 0;
  146. }
  147. };
  148. // #endif
  149. // 管理切换
  150. const manage = () => {
  151. footerswitch.value = !footerswitch.value;
  152. };
  153. // 多选框变化
  154. const checkboxChange = (event) => {
  155. const values = event.detail.value;
  156. collectProductList.value.forEach(item => {
  157. item.checked = values.includes(item.id.toString());
  158. });
  159. selectValue.value = values.toString();
  160. isAllSelect.value = calculatedProducts.value.length === values.length;
  161. };
  162. // 全选变化
  163. const checkboxAllChange = (event) => {
  164. if (event.length > 0) {
  165. setAllSelectValue(1);
  166. } else {
  167. setAllSelectValue(0);
  168. }
  169. };
  170. // 设置全选状态
  171. const setAllSelectValue = (status) => {
  172. const selectValues = [];
  173. collectProductList.value.forEach(item => {
  174. if (status) {
  175. item.checked = true;
  176. selectValues.push(item.id);
  177. isAllSelect.value = true;
  178. } else {
  179. item.checked = false;
  180. isAllSelect.value = false;
  181. }
  182. });
  183. selectValue.value = selectValues.toString();
  184. };
  185. // 授权回调
  186. const onLoadFun = () => {
  187. get_user_collect_product();
  188. // get_host_product();
  189. };
  190. // 授权关闭
  191. const authColse = (e) => {
  192. isShowAuth.value = e;
  193. };
  194. // 获取收藏产品
  195. const get_user_collect_product = async () => {
  196. if (loading.value || loadend.value) return;
  197. loading.value = true;
  198. loadTitle.value = '';
  199. try {
  200. const res = await getCollectUserList({
  201. page: page.value,
  202. limit: limit
  203. });
  204. res.data.list.forEach(item => {
  205. item.right = 0;
  206. });
  207. totals.value = res.data.total;
  208. const collectProductListData = res.data.list;
  209. const isLoadend = collectProductListData.length < limit;
  210. // 重置逻辑优化
  211. if (page.value === 1) {
  212. // 第一页,清空原数组
  213. collectList.value = [...collectProductListData];
  214. } else {
  215. // 后续分页,追加数据
  216. collectList.value = [...collectList.value, ...collectProductListData];
  217. }
  218. loadend.value = isLoadend;
  219. loadTitle.value = isLoadend ? '我是有底线的' : '加载更多';
  220. page.value++;
  221. } catch (err) {
  222. console.error(err);
  223. if (page.value === 1) {
  224. collectList.value = [];
  225. }
  226. } finally {
  227. loading.value = false;
  228. }
  229. };
  230. // 取消收藏单个
  231. const delCollection = (id, index) => {
  232. selectValue.value = id;
  233. del({ ids: selectValue.value.toString() });
  234. };
  235. // 取消收藏多个
  236. const delCollectionAll = () => {
  237. if (!selectValue.value || selectValue.value.length === 0) {
  238. uni.showToast({ title: '请选择商品',icon:none });
  239. }
  240. del({ ids: selectValue.value });
  241. };
  242. // 删除操作
  243. const del = async (data) => {
  244. try {
  245. await collectDelete(data);
  246. uni.showToast({ title: '取消收藏成功', icon: 'success' });
  247. collectProductList.value = [];
  248. loadend.value = false;
  249. page.value = 1;
  250. get_user_collect_product();
  251. } catch (err) {
  252. uni.showToast({ title: err.message || '操作失败' });
  253. }
  254. };
  255. // 获取热门商品
  256. const get_host_product = async () => {
  257. if (hotScroll.value) return;
  258. try {
  259. const res = await getProductHot(hotPage.value, hotLimit);
  260. hotPage.value++;
  261. hotScroll.value = res.data.list.length < hotLimit;
  262. hostProduct.value = hostProduct.value.concat(res.data.list);
  263. } catch (err) {
  264. console.error(err);
  265. }
  266. };
  267. // 页面加载
  268. onLoad(() => {
  269. // if (isLogin) {
  270. // loadend.value = false;
  271. // page.value = 1;
  272. // collectProductList.value = [];
  273. // get_user_collect_product();
  274. // } else {
  275. // // toLogin();
  276. // }
  277. });
  278. // 页面显示
  279. onShow(() => {
  280. if (isLogin) {
  281. loadend.value = false;
  282. page.value = 1;
  283. collectProductList.value = [];
  284. get_user_collect_product();
  285. } else {
  286. // toLogin();
  287. }
  288. });
  289. // 页面上拉触底
  290. onReachBottom(() => {
  291. get_user_collect_product();
  292. // get_host_product();
  293. });
  294. </script>
  295. <style scoped lang="scss">
  296. /* 样式部分保持不变 */
  297. .hdbj {
  298. width: 100%;
  299. height: 30rpx;
  300. background-color: #f5f5f5;
  301. z-index: 999999;
  302. position: fixed;
  303. top: 0;
  304. }
  305. .order-item {
  306. width: 100%;
  307. display: flex;
  308. position: relative;
  309. align-items: right;
  310. flex-direction: row;
  311. }
  312. .remove {
  313. width: 120rpx;
  314. height: 100%;
  315. background-color: $theme-color;
  316. color: white;
  317. position: absolute;
  318. top: 0;
  319. right: -160rpx;
  320. display: flex;
  321. justify-content: center;
  322. align-items: center;
  323. font-size: 24rpx;
  324. }
  325. .collectionGoods {
  326. .nav {
  327. height: 90rpx;
  328. background-color: #fff;
  329. padding: 0 24rpx;
  330. -webkit-box-sizing: border-box;
  331. box-sizing: border-box;
  332. font-size: 28rpx;
  333. color: #282828;
  334. position: fixed;
  335. left: 20rpx;
  336. z-index: 5;
  337. top: 20rpx;
  338. right: 20rpx;
  339. border-bottom: 1px solid #EEEEEE;
  340. border-radius: 16rpx;
  341. }
  342. .list {
  343. padding: 20rpx;
  344. margin-top: 110rpx;
  345. .name {
  346. width: 434rpx;
  347. margin-bottom: 30rpx;
  348. }
  349. }
  350. .centent {
  351. /* #ifdef H5 || MP */
  352. background-color: #fff;
  353. /* #endif */
  354. border-bottom-left-radius: 14rpx;
  355. border-bottom-right-radius: 14rpx;
  356. }
  357. }
  358. .collectionGoods .item {
  359. background-color: #fff;
  360. padding-left: 24rpx;
  361. height: 180rpx;
  362. margin-bottom: 20rpx;
  363. border-radius: 14rpx;
  364. }
  365. .collectionGoods .item .pictrue {
  366. width: 100rpx;
  367. height: 100rpx;
  368. margin-right: 20rpx;
  369. }
  370. .collectionGoods .item .pictrue image {
  371. width: 100%;
  372. height: 100%;
  373. border-radius: 16rpx;
  374. }
  375. .collectionGoods .item .text {
  376. width: 535rpx;
  377. height: 130rpx;
  378. font-size: 28rpx;
  379. color: #282828;
  380. }
  381. .collectionGoods .item .text .name {
  382. width: 100%;
  383. }
  384. .collectionGoods .item .text .money {
  385. font-size: 26rpx;
  386. }
  387. .collectionGoods .item .text .delete {
  388. font-size: 26rpx;
  389. color: #282828;
  390. width: 144rpx;
  391. height: 46rpx;
  392. border: 1px solid #bbb;
  393. border-radius: 4rpx;
  394. text-align: center;
  395. line-height: 46rpx;
  396. }
  397. .noCommodity {
  398. background-color: #fff;
  399. padding-top: 1rpx;
  400. padding-bottom: 30rpx;
  401. border-top: 0;
  402. }
  403. .footer {
  404. z-index: 9;
  405. width: 100%;
  406. height: 96rpx;
  407. background-color: #fff;
  408. position: fixed;
  409. padding: 0 30rpx;
  410. box-sizing: border-box;
  411. border-top: 1rpx solid #eee;
  412. border-bottom: 1px solid #EEEEEE;
  413. /* #ifdef H5 || MP */
  414. bottom: 0rpx;
  415. /* #endif */
  416. /* #ifndef MP */
  417. // bottom: 98rpx;
  418. // bottom: calc(98rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  419. // bottom: calc(98rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  420. /* #endif */
  421. .checkAll {
  422. font-size: 28rpx;
  423. color: #282828;
  424. margin-left: 16rpx;
  425. line-height: 56rpx;
  426. }
  427. .button .bnt {
  428. font-size: 28rpx;
  429. color: #333333;
  430. border-radius: 8rpx;
  431. background-color: #F8C008;
  432. width: 180rpx;
  433. height: 60rpx;
  434. line-height: 60rpx;
  435. text-align: center;
  436. border: none;
  437. }
  438. }
  439. </style>