index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <Theme>
  3. <view class="wrap">
  4. <Navbar fixed leftShow leftIconColor="var(--bg)">
  5. <template #center>
  6. <view class="nav_title">
  7. <trans _t="购物车" />
  8. </view>
  9. <view class="nav_title" v-if="cartNum">({{ cartNum }})</view>
  10. </template>
  11. <template #right>
  12. <view class="nav_right" @click.stop="delCart" v-if="ids.length">
  13. <trans _t="删除" />
  14. </view>
  15. </template>
  16. </Navbar>
  17. <view
  18. class="content"
  19. :style="{ '--height': footerHeight + tabbarHeight + 'px' }"
  20. >
  21. <view class="cont_wrap">
  22. <shop-model :list="cartList" ref="shopRef" @getIds="getIds" />
  23. </view>
  24. <view
  25. class="footer"
  26. id="footer"
  27. :style="{ '--tabbarHeight': tabbarHeight + 'px' }"
  28. >
  29. <view class="footer_left">
  30. <up-checkbox
  31. :label="t('全部')"
  32. :disabled="!cartList.length"
  33. activeColor="var(--black)"
  34. shape="circle"
  35. labelSize="14"
  36. labelColor="#676969"
  37. iconSize="16"
  38. name="agree"
  39. usedAlone
  40. v-model:checked="selectAllChecked"
  41. @change="selectAllChange"
  42. >
  43. </up-checkbox>
  44. </view>
  45. <view class="footer_right">
  46. <view class="total_info">
  47. <view class="total_price">
  48. <!-- {{ t(`已选${money.total_num}件,合计:`) }} -->
  49. <text
  50. >{{ symbol.symbol }} {{ Moneyhtml(money.total_price) }}</text
  51. >
  52. </view>
  53. <view class="total_desc">
  54. <trans _t="不包括运费" />
  55. </view>
  56. </view>
  57. <view
  58. class="total_btn"
  59. @click.stop="submit"
  60. :style="{
  61. opacity: canCheckout ? 1 : 0.5,
  62. 'pointer-events': canCheckout ? 'auto' : 'none',
  63. }"
  64. >
  65. <trans _t="去结算" />
  66. </view>
  67. </view>
  68. </view>
  69. </view>
  70. </view>
  71. <Tabbar page="shop" @getTabbarHeight="getTabbarHeight" />
  72. </Theme>
  73. </template>
  74. <script setup>
  75. import { computed, ref, onMounted, nextTick, watch } from "vue";
  76. import Tabbar from "@/components/tabbar";
  77. import Navbar from "@/components/navbar";
  78. import Tab from "@/components/tabs";
  79. import { useUserStore, useShopStore, useSystemStore } from "@/store";
  80. import { SELLER_DEL_CART } from "@/api";
  81. import { t } from "@/locale";
  82. import { onShow } from "@dcloudio/uni-app";
  83. import shopModel from "@/pages/shop/components/shopModel";
  84. import { query, Moneyhtml } from "@/utils";
  85. const useUser = useUserStore();
  86. const useShop = useShopStore();
  87. const useSystem = useSystemStore();
  88. const symbol = computed(() => useSystem.getSymbol);
  89. const cartList = computed(() => useShop.getSellerList);
  90. const cartNum = computed(() => useShop.getSellerNum);
  91. const selectAllChecked = ref(true);
  92. const shopRef = ref(null);
  93. const footerHeight = ref(0);
  94. const tabbarHeight = ref(0);
  95. const ids = ref([]);
  96. const idsObj = ref({});
  97. const userInfo = computed(() => useUser.getuserInfo);
  98. const token = computed(() => useUser.getToken);
  99. const money = computed(() => {
  100. if (!cartList.value.length) {
  101. selectAllChecked.value = false;
  102. }
  103. let result = cartList.value.reduce(
  104. (acc, seller) => {
  105. const sellerId = seller.seller_id;
  106. if (idsObj.value[sellerId]) {
  107. const totalPrice = seller.goods
  108. .filter((good) => idsObj.value[sellerId].includes(good.id))
  109. .reduce((sum, good) => sum + parseFloat(good.price) * good.total, 0);
  110. acc.total_price = (acc.total_price - 0 + (totalPrice - 0)).toFixed(2);
  111. const totalNum = seller.goods
  112. .filter((good) => idsObj.value[sellerId].includes(good.id))
  113. .reduce((sum, good) => sum + good.total, 0);
  114. acc.total_num = acc.total_num - 0 + (totalNum - 0);
  115. }
  116. return acc;
  117. },
  118. {
  119. total_price: 0,
  120. total_num: 0,
  121. orginal_price: 0,
  122. }
  123. );
  124. return result;
  125. });
  126. const canCheckout = computed(() => {
  127. if (!ids.value.length) return false;
  128. return true;
  129. });
  130. watch(
  131. () => cartList.value,
  132. (list) => {
  133. const hasData = Array.isArray(list) && list.length > 0;
  134. if (!hasData) {
  135. selectAllChecked.value = false;
  136. return;
  137. }
  138. nextTick(() => {
  139. selectAllChecked.value = true;
  140. selectAllChange(true);
  141. });
  142. },
  143. { immediate: true, deep: true }
  144. );
  145. const selectAllChange = (e) => {
  146. shopRef.value && shopRef.value.allStatus(e);
  147. };
  148. const getIds = (arr, flag) => {
  149. ids.value = Object.values(arr).flat(1);
  150. idsObj.value = arr;
  151. selectAllChecked.value = flag;
  152. };
  153. const submit = () => {
  154. if (!canCheckout.value) return;
  155. uni.navigateTo({
  156. url: `/pagesBuyer/shop/shopConfirm?comfirmId=${ids.value.join(",")}`,
  157. });
  158. };
  159. const delCart = () => {
  160. removeCart();
  161. };
  162. const removeCart = async () => {
  163. try {
  164. let id = (ids.value.length && ids.value.join(",")) || "";
  165. await SELLER_DEL_CART(id);
  166. useShop.setSellerList();
  167. shopRef.value && shopRef.value.allStatus(false);
  168. } catch (error) {
  169. toast(error.msg);
  170. }
  171. };
  172. const getHeight = async () => {
  173. try {
  174. const res = await query("#footer");
  175. nextTick(() => {
  176. footerHeight.value = res.height;
  177. });
  178. } catch (error) {}
  179. };
  180. const getTabbarHeight = (height) => {
  181. tabbarHeight.value = height;
  182. };
  183. onMounted(() => {
  184. getHeight();
  185. });
  186. onShow(() => {
  187. token.value && useShop.setSellerList();
  188. setTimeout(() => {
  189. getHeight();
  190. }, 0);
  191. });
  192. </script>
  193. <style lang="less" scoped>
  194. @import url("@/style.less");
  195. .wrap {
  196. min-height: 100bh;
  197. background: var(--bg);
  198. overflow: hidden;
  199. .flex();
  200. flex-direction: column;
  201. :deep(.u-navbar__content) {
  202. background-color: var(--black) !important;
  203. }
  204. .nav_title {
  205. color: var(--light);
  206. }
  207. .nav_right {
  208. color: var(--primary);
  209. .size(24rpx);
  210. font-weight: 500;
  211. }
  212. .content {
  213. flex-grow: 1;
  214. height: calc(100vh - 44px - var(--height));
  215. overflow-y: scroll;
  216. .flex();
  217. flex-direction: column;
  218. .cont_wrap {
  219. flex-grow: 1;
  220. overflow: hidden scroll;
  221. }
  222. .footer {
  223. .flex_position(space-between);
  224. flex-wrap: wrap;
  225. padding: 8px 12px;
  226. background-color: var(--light);
  227. box-shadow: 0 -4px 6px #0000000d;
  228. position: fixed;
  229. bottom: var(--tabbarHeight);
  230. // bottom: calc(var(--tabbarHeight) + constant(safe-area-inset-bottom));
  231. // bottom: calc(var(--tabbarHeight) + env(safe-area-inset-bottom));
  232. left: 0;
  233. right: 0;
  234. box-sizing: border-box;
  235. &_left {
  236. }
  237. &_right {
  238. .ver();
  239. .total_info {
  240. .total_price {
  241. text-align: right;
  242. color: var(--red);
  243. .size();
  244. font-weight: 700;
  245. line-height: 48rpx;
  246. }
  247. .total_desc {
  248. color: var(--text-01);
  249. .size(24rpx);
  250. line-height: 40rpx;
  251. text-align: right;
  252. .icon-font {
  253. .size();
  254. }
  255. }
  256. }
  257. .total_btn {
  258. .size(28rpx);
  259. font-weight: 700;
  260. height: 38px;
  261. margin-left: 16rpx;
  262. min-width: 180rpx;
  263. background-color: var(--black);
  264. color: var(--light);
  265. border-radius: 16rpx;
  266. padding: 16rpx 30rpx;
  267. text-align: center;
  268. }
  269. }
  270. }
  271. }
  272. }
  273. </style>