index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. console.log(event);
  156. const values = event;
  157. collectProductList.value.forEach(item => {
  158. item.checked = values.includes(item.id.toString());
  159. });
  160. selectValue.value = values.toString();
  161. isAllSelect.value = collectProductList.value.length === values.length;
  162. };
  163. // 全选变化
  164. const checkboxAllChange = (event) => {
  165. if (event.length > 0) {
  166. setAllSelectValue(1);
  167. } else {
  168. setAllSelectValue(0);
  169. }
  170. };
  171. // 设置全选状态
  172. const setAllSelectValue = (status) => {
  173. const selectValues = [];
  174. collectProductList.value.forEach(item => {
  175. if (status) {
  176. item.checked = true;
  177. selectValues.push(item.id);
  178. isAllSelect.value = true;
  179. } else {
  180. item.checked = false;
  181. isAllSelect.value = false;
  182. }
  183. });
  184. selectValue.value = selectValues.toString();
  185. };
  186. // 授权回调
  187. const onLoadFun = () => {
  188. get_user_collect_product();
  189. // get_host_product();
  190. };
  191. // 授权关闭
  192. const authColse = (e) => {
  193. isShowAuth.value = e;
  194. };
  195. // 获取收藏产品
  196. const get_user_collect_product = async () => {
  197. if (loading.value || loadend.value) return;
  198. loading.value = true;
  199. loadTitle.value = '';
  200. try {
  201. const res = await getCollectUserList({
  202. page: page.value,
  203. limit: limit
  204. });
  205. res.data.list.forEach(item => {
  206. item.right = 0;
  207. });
  208. totals.value = res.data.total;
  209. const collectProductListData = res.data.list;
  210. const isLoadend = collectProductListData.length < limit;
  211. // 重置逻辑优化
  212. if (page.value === 1) {
  213. // 第一页,清空原数组
  214. collectList.value = [...collectProductListData];
  215. } else {
  216. // 后续分页,追加数据
  217. collectList.value = [...collectList.value, ...collectProductListData];
  218. }
  219. loadend.value = isLoadend;
  220. loadTitle.value = isLoadend ? '我是有底线的' : '加载更多';
  221. page.value++;
  222. } catch (err) {
  223. console.error(err);
  224. if (page.value === 1) {
  225. collectList.value = [];
  226. }
  227. } finally {
  228. loading.value = false;
  229. }
  230. };
  231. // 取消收藏单个
  232. const delCollection = (id, index) => {
  233. selectValue.value = id;
  234. del({ ids: selectValue.value.toString() });
  235. };
  236. // 取消收藏多个
  237. const delCollectionAll = () => {
  238. if (!selectValue.value || selectValue.value.length === 0) {
  239. uni.showToast({ title: '请选择商品',icon:'none' });
  240. }
  241. del({ ids: selectValue.value });
  242. };
  243. // 删除操作
  244. const del = async (data) => {
  245. try {
  246. await collectDelete(data);
  247. uni.showToast({ title: '取消收藏成功', icon: 'success' });
  248. collectProductList.value = [];
  249. loadend.value = false;
  250. page.value = 1;
  251. get_user_collect_product();
  252. } catch (err) {
  253. uni.showToast({ title: err.message || '操作失败' });
  254. }
  255. };
  256. // 获取热门商品
  257. const get_host_product = async () => {
  258. if (hotScroll.value) return;
  259. try {
  260. const res = await getProductHot(hotPage.value, hotLimit);
  261. hotPage.value++;
  262. hotScroll.value = res.data.list.length < hotLimit;
  263. hostProduct.value = hostProduct.value.concat(res.data.list);
  264. } catch (err) {
  265. console.error(err);
  266. }
  267. };
  268. // 页面加载
  269. onLoad(() => {
  270. // if (isLogin) {
  271. // loadend.value = false;
  272. // page.value = 1;
  273. // collectProductList.value = [];
  274. // get_user_collect_product();
  275. // } else {
  276. // // toLogin();
  277. // }
  278. });
  279. // 页面显示
  280. onShow(() => {
  281. if (isLogin) {
  282. loadend.value = false;
  283. page.value = 1;
  284. collectProductList.value = [];
  285. get_user_collect_product();
  286. } else {
  287. // toLogin();
  288. }
  289. });
  290. // 页面上拉触底
  291. onReachBottom(() => {
  292. get_user_collect_product();
  293. // get_host_product();
  294. });
  295. </script>
  296. <style scoped lang="scss">
  297. /* 样式部分保持不变 */
  298. .hdbj {
  299. width: 100%;
  300. height: 30rpx;
  301. background-color: #f5f5f5;
  302. z-index: 999999;
  303. position: fixed;
  304. top: 0;
  305. }
  306. .order-item {
  307. width: 100%;
  308. display: flex;
  309. position: relative;
  310. align-items: right;
  311. flex-direction: row;
  312. }
  313. .remove {
  314. width: 120rpx;
  315. height: 100%;
  316. background-color: $theme-color;
  317. color: white;
  318. position: absolute;
  319. top: 0;
  320. right: -160rpx;
  321. display: flex;
  322. justify-content: center;
  323. align-items: center;
  324. font-size: 24rpx;
  325. }
  326. .collectionGoods {
  327. .nav {
  328. height: 90rpx;
  329. background-color: #fff;
  330. padding: 0 24rpx;
  331. -webkit-box-sizing: border-box;
  332. box-sizing: border-box;
  333. font-size: 28rpx;
  334. color: #282828;
  335. position: fixed;
  336. left: 20rpx;
  337. z-index: 5;
  338. top: 20rpx;
  339. right: 20rpx;
  340. border-bottom: 1px solid #EEEEEE;
  341. border-radius: 16rpx;
  342. }
  343. .list {
  344. padding: 20rpx;
  345. margin-top: 110rpx;
  346. .name {
  347. width: 434rpx;
  348. margin-bottom: 30rpx;
  349. }
  350. }
  351. .centent {
  352. /* #ifdef H5 || MP */
  353. background-color: #fff;
  354. /* #endif */
  355. border-bottom-left-radius: 14rpx;
  356. border-bottom-right-radius: 14rpx;
  357. }
  358. }
  359. .collectionGoods .item {
  360. background-color: #fff;
  361. padding-left: 24rpx;
  362. height: 180rpx;
  363. margin-bottom: 20rpx;
  364. border-radius: 14rpx;
  365. }
  366. .collectionGoods .item .pictrue {
  367. width: 100rpx;
  368. height: 100rpx;
  369. margin-right: 20rpx;
  370. }
  371. .collectionGoods .item .pictrue image {
  372. width: 100%;
  373. height: 100%;
  374. border-radius: 16rpx;
  375. }
  376. .collectionGoods .item .text {
  377. width: 535rpx;
  378. height: 130rpx;
  379. font-size: 28rpx;
  380. color: #282828;
  381. }
  382. .collectionGoods .item .text .name {
  383. width: 100%;
  384. }
  385. .collectionGoods .item .text .money {
  386. font-size: 26rpx;
  387. }
  388. .collectionGoods .item .text .delete {
  389. font-size: 26rpx;
  390. color: #282828;
  391. width: 144rpx;
  392. height: 46rpx;
  393. border: 1px solid #bbb;
  394. border-radius: 4rpx;
  395. text-align: center;
  396. line-height: 46rpx;
  397. }
  398. .noCommodity {
  399. background-color: #fff;
  400. padding-top: 1rpx;
  401. padding-bottom: 30rpx;
  402. border-top: 0;
  403. }
  404. .footer {
  405. z-index: 9;
  406. width: 100%;
  407. height: 96rpx;
  408. background-color: #fff;
  409. position: fixed;
  410. padding: 0 30rpx;
  411. box-sizing: border-box;
  412. border-top: 1rpx solid #eee;
  413. border-bottom: 1px solid #EEEEEE;
  414. /* #ifdef H5 || MP */
  415. bottom: 0rpx;
  416. /* #endif */
  417. /* #ifndef MP */
  418. // bottom: 98rpx;
  419. // bottom: calc(98rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  420. // bottom: calc(98rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  421. /* #endif */
  422. .checkAll {
  423. font-size: 28rpx;
  424. color: #282828;
  425. margin-left: 16rpx;
  426. line-height: 56rpx;
  427. }
  428. .button .bnt {
  429. font-size: 28rpx;
  430. color: #333333;
  431. border-radius: 8rpx;
  432. background-color: #F8C008;
  433. width: 180rpx;
  434. height: 60rpx;
  435. line-height: 60rpx;
  436. text-align: center;
  437. border: none;
  438. }
  439. }
  440. </style>