index.vue 12 KB

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