index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <template>
  2. <view>
  3. <!-- <view class="hdbj"></view> -->
  4. <view class="collectionGoods" v-if="collectProductList.length">
  5. <view class="nav acea-row row-between-wrapper">
  6. <view
  7. >当前共 <text class="num">{{ totals }}</text
  8. >件商品</view
  9. >
  10. <view class="administrate acea-row row-center-wrapper" @click="manage"
  11. >{{ footerswitch ? "管理" : "取消" }}
  12. </view>
  13. </view>
  14. <view class="list">
  15. <up-checkbox-group
  16. shape="circle"
  17. v-model="selectValue"
  18. @change="checkboxChange"
  19. class="centent"
  20. activeColor="#F8C008"
  21. >
  22. <view
  23. v-for="(item, index) in collectProductList"
  24. :key="item.id"
  25. class="item acea-row row-middle"
  26. style="width: 100%"
  27. >
  28. <up-checkbox
  29. :name="item.id.toString()"
  30. :checked="item.checked"
  31. v-if="!footerswitch"
  32. style="margin-right: 10rpx"
  33. activeColor="#F8C008"
  34. />
  35. <view
  36. class="acea-row"
  37. style="align-items: center"
  38. @click="goDetail(item)"
  39. >
  40. <view class="pictrue">
  41. <image :src="item.image" mode="aspectFill"></image>
  42. </view>
  43. <view class="desc">
  44. <view class="name">{{ item.storeName }}</view>
  45. <view class="money"
  46. >¥<view class="price">
  47. {{ calcPrice(item) }} </view
  48. ><view class="qi"> 起 </view>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </up-checkbox-group>
  54. </view>
  55. <view class="loadingicon acea-row row-center-wrapper">
  56. <text
  57. class="loading iconfont icon-jiazai"
  58. :hidden="loading == false"
  59. ></text
  60. >{{ loadTitle }}
  61. </view>
  62. <view v-if="!footerswitch" class="footer acea-row row-between-wrapper">
  63. <view>
  64. <up-checkbox-group shape="circle" @change="checkboxAllChange">
  65. <up-checkbox
  66. value="all"
  67. :checked="!!isAllSelect"
  68. activeColor="#F8C008"
  69. />
  70. <text class="checkAll" @click="toggleSelectAll">全选</text>
  71. </up-checkbox-group>
  72. </view>
  73. <view class="button acea-row row-middle">
  74. <form @submit="delCollectionAll" report-submit="true">
  75. <button class="bnt" formType="submit">取消收藏</button>
  76. </form>
  77. </view>
  78. </view>
  79. </view>
  80. <view
  81. class="noCommodity"
  82. v-else-if="!collectProductList.length && page > 1"
  83. >
  84. <view class="pictrue">
  85. <image :src="HTTP_REQUEST_URL_IMG + 'noCollection.png'"></image>
  86. </view>
  87. <!-- <recommend :hostProduct="hostProduct"></recommend>-->
  88. </view>
  89. <!-- <home></home>-->
  90. </view>
  91. </template>
  92. <script setup>
  93. import { ref, computed, onMounted, onActivated } from "vue";
  94. import { onLoad, onShow, onReachBottom } from "@dcloudio/uni-app";
  95. import { storeToRefs } from "pinia";
  96. import { useAppStore } from "@/stores/app.js";
  97. import { HTTP_REQUEST_URL_IMG } from "@/config/app";
  98. import {
  99. getCollectUserList,
  100. getProductHot,
  101. collectDelete,
  102. } from "@/api/store.js";
  103. import { toLogin } from "@/libs/login.js";
  104. import recommend from "@/components/recommend";
  105. // 获取实物金价
  106. import useRealGoldPrice from "@/hooks/useRealGoldPrice";
  107. const {
  108. realGoldprice, // 黄金实时销售价(基础)
  109. realPtprice, // 铂金实时销售价(基础)
  110. realAgprice, // 白银实时销售价(基础)
  111. } = useRealGoldPrice({});
  112. import home from "@/components/home";
  113. import Util from "@/utils/util";
  114. // Store
  115. const appStore = useAppStore();
  116. const isLogin = appStore.isLogin;
  117. // 响应式数据
  118. const footerswitch = ref(true);
  119. const hostProduct = ref([]);
  120. const loadTitle = ref("加载更多");
  121. const loading = ref(false);
  122. const loadend = ref(false);
  123. const collectProductList = ref([]);
  124. const limit = 8;
  125. const page = ref(1);
  126. const isAuto = ref(false);
  127. const isShowAuth = ref(false);
  128. const hotScroll = ref(false);
  129. const hotPage = ref(1);
  130. const hotLimit = 10;
  131. const isAllSelect = ref(false);
  132. const selectValue = ref([]);
  133. const delBtnWidth = 80;
  134. const totals = ref(0);
  135. const startX = ref(0);
  136. const goDetail = (item) => {
  137. if (!footerswitch.value) {
  138. let id = String(item.id);
  139. if (selectValue.value.includes(id)) {
  140. collectProductList.value.forEach((item) => {
  141. if (item.id == id) {
  142. item.checked = false;
  143. }
  144. });
  145. selectValue.value = selectValue.value.filter((n) => n != id);
  146. } else {
  147. selectValue.value.push(id);
  148. }
  149. isAllSelect.value =
  150. collectProductList.value.length === selectValue.value.length;
  151. return;
  152. }
  153. uni.navigateTo({
  154. url: "/pages/goods/goods_details/index?id=" + item.productId,
  155. });
  156. };
  157. const calcPrice = (product) => {
  158. const price = Number(product.price);
  159. // 2. 计算乘积
  160. const total = (price + product.sales) * realGoldprice.value;
  161. // 3. 向上取整到两位小数(先放大100倍取整,再缩小100倍)
  162. const roundedTotal = Math.ceil(total * 100) / 100;
  163. // 4. 格式化保留两位小数
  164. const formattedTotal = roundedTotal.toFixed(2);
  165. const totalLaborCost = Number(product.totalLaborCost);
  166. const additionalAmount = Number(product.additionalAmount);
  167. const totalPrice = (totalLaborCost + additionalAmount).toFixed(2);
  168. return totalPrice;
  169. };
  170. // 管理切换
  171. const manage = () => {
  172. footerswitch.value = !footerswitch.value;
  173. };
  174. // 多选框变化
  175. const checkboxChange = (event) => {
  176. console.log(event);
  177. const values = event;
  178. collectProductList.value.forEach((item) => {
  179. item.checked = values.includes(item.id.toString());
  180. });
  181. // selectValue.value = values.toString();
  182. isAllSelect.value = collectProductList.value.length === values.length;
  183. };
  184. // 全选变化
  185. const checkboxAllChange = (event) => {
  186. if (event.length > 0) {
  187. setAllSelectValue(1);
  188. } else {
  189. setAllSelectValue(0);
  190. }
  191. };
  192. const toggleSelectAll = () => {
  193. if (isAllSelect.value) {
  194. setAllSelectValue(0);
  195. } else {
  196. setAllSelectValue(1);
  197. }
  198. };
  199. // 设置全选状态
  200. const setAllSelectValue = (status) => {
  201. const selectValues = [];
  202. collectProductList.value.forEach((item) => {
  203. if (status) {
  204. item.checked = true;
  205. selectValues.push(String(item.id));
  206. isAllSelect.value = true;
  207. } else {
  208. item.checked = false;
  209. isAllSelect.value = false;
  210. }
  211. });
  212. console.log(selectValues);
  213. selectValue.value = selectValues;
  214. };
  215. // 授权回调
  216. const onLoadFun = () => {
  217. get_user_collect_product();
  218. // get_host_product();
  219. };
  220. // 授权关闭
  221. const authColse = (e) => {
  222. isShowAuth.value = e;
  223. };
  224. // 获取收藏产品
  225. const get_user_collect_product = async () => {
  226. if (loading.value || loadend.value) return;
  227. loading.value = true;
  228. loadTitle.value = "";
  229. try {
  230. const res = await getCollectUserList({
  231. page: page.value,
  232. limit: limit,
  233. });
  234. res.data.list.forEach((item) => {
  235. item.right = 0;
  236. });
  237. totals.value = res.data.total;
  238. const collectProductListData = res.data.list;
  239. const isLoadend = collectProductListData.length < limit;
  240. // 重置逻辑优化
  241. if (page.value === 1) {
  242. // 第一页,清空原数组
  243. collectProductList.value = [...collectProductListData];
  244. } else {
  245. // 后续分页,追加数据
  246. collectProductList.value = [
  247. ...collectProductList.value,
  248. ...collectProductListData,
  249. ];
  250. }
  251. loadend.value = isLoadend;
  252. loadTitle.value = isLoadend ? "我是有底线的" : "加载更多";
  253. page.value++;
  254. } catch (err) {
  255. console.error(err);
  256. if (page.value === 1) {
  257. collectProductList.value = [];
  258. }
  259. } finally {
  260. loading.value = false;
  261. }
  262. };
  263. // 取消收藏多个
  264. const delCollectionAll = () => {
  265. console.log(selectValue.value);
  266. if (!selectValue.value || selectValue.value.length === 0) {
  267. uni.showToast({
  268. title: "请选择商品",
  269. icon: "none",
  270. });
  271. }
  272. del({
  273. ids: selectValue.value.toString(),
  274. }).then(() => {
  275. selectValue.value = [];
  276. });
  277. };
  278. // 删除操作
  279. const del = async (data) => {
  280. try {
  281. await collectDelete(data);
  282. uni.showToast({
  283. title: "取消收藏成功",
  284. icon: "success",
  285. });
  286. collectProductList.value = [];
  287. loadend.value = false;
  288. page.value = 1;
  289. get_user_collect_product();
  290. } catch (err) {
  291. uni.showToast({
  292. title: err.message || "操作失败",
  293. });
  294. }
  295. };
  296. // 获取热门商品
  297. const get_host_product = async () => {
  298. if (hotScroll.value) return;
  299. try {
  300. const res = await getProductHot(hotPage.value, hotLimit);
  301. hotPage.value++;
  302. hotScroll.value = res.data.list.length < hotLimit;
  303. hostProduct.value = hostProduct.value.concat(res.data.list);
  304. } catch (err) {
  305. console.error(err);
  306. }
  307. };
  308. // 页面加载
  309. onLoad(() => {
  310. // if (isLogin) {
  311. // loadend.value = false;
  312. // page.value = 1;
  313. // collectProductList.value = [];
  314. // get_user_collect_product();
  315. // } else {
  316. // // toLogin();
  317. // }
  318. });
  319. // 页面显示
  320. onShow(() => {
  321. if (isLogin) {
  322. loadend.value = false;
  323. page.value = 1;
  324. collectProductList.value = [];
  325. get_user_collect_product();
  326. } else {
  327. // toLogin();
  328. }
  329. });
  330. // 页面上拉触底
  331. onReachBottom(() => {
  332. get_user_collect_product();
  333. // get_host_product();
  334. });
  335. </script>
  336. <style scoped lang="scss">
  337. /* 样式部分保持不变 */
  338. // .hdbj {
  339. // width: 100%;
  340. // height: 30rpx;
  341. // background-color: #f5f5f5;
  342. // z-index: 999999;
  343. // position: fixed;
  344. // top: 0;
  345. // }
  346. page {
  347. height: 100%;
  348. background-color: #f9f7f0;
  349. }
  350. .order-item {
  351. width: 100%;
  352. display: flex;
  353. position: relative;
  354. align-items: right;
  355. flex-direction: row;
  356. }
  357. .remove {
  358. width: 120rpx;
  359. height: 100%;
  360. background-color: $theme-color;
  361. color: white;
  362. position: absolute;
  363. top: 0;
  364. right: -160rpx;
  365. display: flex;
  366. justify-content: center;
  367. align-items: center;
  368. font-size: 24rpx;
  369. }
  370. .collectionGoods {
  371. padding-top: 80rpx;
  372. .nav {
  373. height: 80rpx;
  374. background-color: #f9f7f0;
  375. padding: 0 16rpx;
  376. -webkit-box-sizing: border-box;
  377. box-sizing: border-box;
  378. font-size: 28rpx;
  379. color: #333;
  380. position: fixed;
  381. z-index: 5;
  382. left: 0;
  383. top: 0;
  384. right: 0;
  385. border-radius: 16rpx;
  386. .num {
  387. font-size: 32rpx;
  388. color: #f8c008;
  389. font-weight: bold;
  390. }
  391. .administrate {
  392. font-size: 28rpx;
  393. color: #f8c008;
  394. font-weight: bold;
  395. }
  396. }
  397. .list {
  398. padding: 16rpx;
  399. // margin-top: 110rpx;
  400. .desc {
  401. display: flex;
  402. flex-direction: column;
  403. justify-content: center;
  404. align-items: flex-start;
  405. width: 434rpx;
  406. // flex: 1;
  407. }
  408. .name {
  409. width: 434rpx;
  410. margin-bottom: 32rpx;
  411. }
  412. }
  413. .centent {
  414. /* #ifdef H5 || MP */
  415. // background-color: #fff;
  416. /* #endif */
  417. border-bottom-left-radius: 14rpx;
  418. border-bottom-right-radius: 14rpx;
  419. }
  420. }
  421. .collectionGoods .item {
  422. background-color: #fff;
  423. // padding-left: 24rpx;
  424. // height: 180rpx;
  425. padding: 16rpx;
  426. margin-bottom: 20rpx;
  427. border-radius: 14rpx;
  428. }
  429. .collectionGoods .item .pictrue {
  430. width: 160rpx;
  431. height: 160rpx;
  432. border-radius: 16rpx;
  433. margin-right: 16rpx;
  434. flex-shrink: 0;
  435. }
  436. .collectionGoods .item .pictrue image {
  437. width: 100%;
  438. height: 100%;
  439. border-radius: 16rpx;
  440. }
  441. .collectionGoods .item {
  442. width: 535rpx;
  443. // height: 130rpx;
  444. font-size: 32rpx;
  445. color: #333;
  446. }
  447. .collectionGoods .item .name {
  448. width: 100%;
  449. line-height: 48rpx;
  450. font-weight: bold;
  451. }
  452. .collectionGoods .item .money {
  453. display: flex;
  454. align-items: flex-end;
  455. font-size: 28rpx;
  456. color: #fd5f3c;
  457. .price {
  458. font-size: 36rpx;
  459. font-weight: bold;
  460. margin: 0 5rpx;
  461. }
  462. .qi {
  463. font-size: 20rpx;
  464. }
  465. }
  466. .collectionGoods .item .delete {
  467. font-size: 26rpx;
  468. color: #282828;
  469. width: 144rpx;
  470. height: 46rpx;
  471. border: 1px solid #bbb;
  472. border-radius: 4rpx;
  473. text-align: center;
  474. line-height: 46rpx;
  475. }
  476. .noCommodity {
  477. background-color: #fff;
  478. padding-top: 1rpx;
  479. padding-bottom: 30rpx;
  480. border-top: 0;
  481. }
  482. .footer {
  483. z-index: 9;
  484. width: 100%;
  485. height: 128rpx;
  486. background-color: #fff;
  487. position: fixed;
  488. padding: 0 30rpx;
  489. box-sizing: border-box;
  490. border-top: 1rpx solid #eee;
  491. border-bottom: 1px solid #eeeeee;
  492. /* #ifdef H5 || MP */
  493. bottom: 0rpx;
  494. padding-bottom: 32rpx;
  495. /* #endif */
  496. /* #ifndef MP */
  497. // bottom: 98rpx;
  498. // bottom: calc(98rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  499. // bottom: calc(98rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  500. /* #endif */
  501. .checkAll {
  502. font-size: 28rpx;
  503. color: #282828;
  504. margin-left: 16rpx;
  505. line-height: 56rpx;
  506. }
  507. .button .bnt {
  508. font-size: 28rpx;
  509. color: #333333;
  510. font-weight: bold;
  511. border-radius: 8rpx;
  512. background-color: #f8c008;
  513. width: 180rpx;
  514. height: 60rpx;
  515. line-height: 60rpx;
  516. text-align: center;
  517. border: none;
  518. }
  519. }
  520. </style>