index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. console.log('totalPrice',totalPrice)
  111. return {
  112. ...product,
  113. calculatedTotal: formattedTotal, // 新增计算结果字段
  114. totalPrice
  115. };
  116. });
  117. });
  118. // #ifdef MP
  119. // 触摸开始
  120. const drawStart = (e) => {
  121. const touch = e.touches[0];
  122. startX.value = touch.clientX;
  123. };
  124. // 触摸滑动
  125. const drawMove = (e) => {
  126. const touch = e.touches[0];
  127. const index = e.currentTarget.dataset.index;
  128. const disX = startX.value - touch.clientX;
  129. if (disX >= 20) {
  130. if (disX > delBtnWidth) {
  131. collectProductList.value[index].right = delBtnWidth;
  132. } else {
  133. collectProductList.value[index].right = disX;
  134. }
  135. } else {
  136. collectProductList.value[index].right = 0;
  137. }
  138. };
  139. // 触摸滑动结束
  140. const drawEnd = (e) => {
  141. const index = e.currentTarget.dataset.index;
  142. const item = collectProductList.value[index];
  143. if (item.right >= delBtnWidth / 2) {
  144. item.right = delBtnWidth;
  145. } else {
  146. item.right = 0;
  147. }
  148. };
  149. // #endif
  150. // 管理切换
  151. const manage = () => {
  152. footerswitch.value = !footerswitch.value;
  153. };
  154. // 多选框变化
  155. const checkboxChange = (event) => {
  156. const values = event.detail.value;
  157. collectProductList.value.forEach(item => {
  158. item.checked = values.includes(item.id.toString());
  159. });
  160. selectValue.value = values.toString();
  161. isAllSelect.value = calculatedProducts.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. collectList.value = Util.SplitArray(collectProductListData, collectProductList.value);
  212. if (collectList.value.length === 0) {
  213. // get_host_product();
  214. }
  215. loadend.value = isLoadend;
  216. console.log(isLoadend)
  217. loadTitle.value = isLoadend ? '我是有底线的' : '加载更多';
  218. console.log(loadTitle.value)
  219. page.value++;
  220. } catch (err) {
  221. console.error(err);
  222. } finally {
  223. loading.value = false;
  224. }
  225. };
  226. // 取消收藏单个
  227. const delCollection = (id, index) => {
  228. selectValue.value = id;
  229. del({ ids: selectValue.value.toString() });
  230. };
  231. // 取消收藏多个
  232. const delCollectionAll = () => {
  233. if (!selectValue.value || selectValue.value.length === 0) {
  234. uni.showToast({ title: '请选择商品',icon:none });
  235. }
  236. del({ ids: selectValue.value });
  237. };
  238. // 删除操作
  239. const del = async (data) => {
  240. try {
  241. await collectDelete(data);
  242. uni.showToast({ title: '取消收藏成功', icon: 'success' });
  243. collectProductList.value = [];
  244. loadend.value = false;
  245. page.value = 1;
  246. get_user_collect_product();
  247. } catch (err) {
  248. uni.showToast({ title: err.message || '操作失败' });
  249. }
  250. };
  251. // 获取热门商品
  252. const get_host_product = async () => {
  253. if (hotScroll.value) return;
  254. try {
  255. const res = await getProductHot(hotPage.value, hotLimit);
  256. hotPage.value++;
  257. hotScroll.value = res.data.list.length < hotLimit;
  258. hostProduct.value = hostProduct.value.concat(res.data.list);
  259. } catch (err) {
  260. console.error(err);
  261. }
  262. };
  263. // 页面加载
  264. onLoad(() => {
  265. if (isLogin) {
  266. loadend.value = false;
  267. page.value = 1;
  268. collectProductList.value = [];
  269. get_user_collect_product();
  270. } else {
  271. // toLogin();
  272. }
  273. });
  274. // 页面显示
  275. onShow(() => {
  276. loadend.value = false;
  277. page.value = 1;
  278. collectProductList.value = [];
  279. get_user_collect_product();
  280. });
  281. // 页面上拉触底
  282. onReachBottom(() => {
  283. get_user_collect_product();
  284. // get_host_product();
  285. });
  286. </script>
  287. <style scoped lang="scss">
  288. /* 样式部分保持不变 */
  289. .hdbj {
  290. width: 100%;
  291. height: 30rpx;
  292. background-color: #f5f5f5;
  293. z-index: 999999;
  294. position: fixed;
  295. top: 0;
  296. }
  297. .order-item {
  298. width: 100%;
  299. display: flex;
  300. position: relative;
  301. align-items: right;
  302. flex-direction: row;
  303. }
  304. .remove {
  305. width: 120rpx;
  306. height: 100%;
  307. background-color: $theme-color;
  308. color: white;
  309. position: absolute;
  310. top: 0;
  311. right: -160rpx;
  312. display: flex;
  313. justify-content: center;
  314. align-items: center;
  315. font-size: 24rpx;
  316. }
  317. .collectionGoods {
  318. .nav {
  319. height: 90rpx;
  320. background-color: #fff;
  321. padding: 0 24rpx;
  322. -webkit-box-sizing: border-box;
  323. box-sizing: border-box;
  324. font-size: 28rpx;
  325. color: #282828;
  326. position: fixed;
  327. left: 20rpx;
  328. z-index: 5;
  329. top: 20rpx;
  330. right: 20rpx;
  331. border-bottom: 1px solid #EEEEEE;
  332. border-radius: 16rpx;
  333. }
  334. .list {
  335. padding: 20rpx;
  336. margin-top: 110rpx;
  337. .name {
  338. width: 434rpx;
  339. margin-bottom: 30rpx;
  340. }
  341. }
  342. .centent {
  343. /* #ifdef H5 || MP */
  344. background-color: #fff;
  345. /* #endif */
  346. border-bottom-left-radius: 14rpx;
  347. border-bottom-right-radius: 14rpx;
  348. }
  349. }
  350. .collectionGoods .item {
  351. background-color: #fff;
  352. padding-left: 24rpx;
  353. height: 180rpx;
  354. margin-bottom: 20rpx;
  355. border-radius: 14rpx;
  356. }
  357. .collectionGoods .item .pictrue {
  358. width: 100rpx;
  359. height: 100rpx;
  360. margin-right: 20rpx;
  361. }
  362. .collectionGoods .item .pictrue image {
  363. width: 100%;
  364. height: 100%;
  365. border-radius: 16rpx;
  366. }
  367. .collectionGoods .item .text {
  368. width: 535rpx;
  369. height: 130rpx;
  370. font-size: 28rpx;
  371. color: #282828;
  372. }
  373. .collectionGoods .item .text .name {
  374. width: 100%;
  375. }
  376. .collectionGoods .item .text .money {
  377. font-size: 26rpx;
  378. }
  379. .collectionGoods .item .text .delete {
  380. font-size: 26rpx;
  381. color: #282828;
  382. width: 144rpx;
  383. height: 46rpx;
  384. border: 1px solid #bbb;
  385. border-radius: 4rpx;
  386. text-align: center;
  387. line-height: 46rpx;
  388. }
  389. .noCommodity {
  390. background-color: #fff;
  391. padding-top: 1rpx;
  392. padding-bottom: 30rpx;
  393. border-top: 0;
  394. }
  395. .footer {
  396. z-index: 9;
  397. width: 100%;
  398. height: 96rpx;
  399. background-color: #fff;
  400. position: fixed;
  401. padding: 0 30rpx;
  402. box-sizing: border-box;
  403. border-top: 1rpx solid #eee;
  404. border-bottom: 1px solid #EEEEEE;
  405. /* #ifdef H5 || MP */
  406. bottom: 0rpx;
  407. /* #endif */
  408. /* #ifndef MP */
  409. // bottom: 98rpx;
  410. // bottom: calc(98rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  411. // bottom: calc(98rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  412. /* #endif */
  413. .checkAll {
  414. font-size: 28rpx;
  415. color: #282828;
  416. margin-left: 16rpx;
  417. line-height: 56rpx;
  418. }
  419. .button .bnt {
  420. font-size: 28rpx;
  421. color: #333333;
  422. border-radius: 8rpx;
  423. background-color: #F8C008;
  424. width: 180rpx;
  425. height: 60rpx;
  426. line-height: 60rpx;
  427. text-align: center;
  428. border: none;
  429. }
  430. }
  431. </style>