| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639 |
- <template>
- <view>
- <view class="shoppingCart copy-data">
- <view class="borRadius14">
- <view
- v-if="
- (cartList.valid.length === 0 && cartList.invalid.length === 0) ||
- cartList.valid.length > 0
- "
- class="nav acea-row row-between-wrapper"
- >
- <view
- >购物车数量
- <!-- <text class="num font-color">{{ cartCount }}</text></view> -->
- <text class="num font-color">{{ orderGoodsCount }}</text></view>
- <view
- v-if="cartList.valid.length > 0 || cartList.invalid.length > 0"
- class="administrate acea-row row-center-wrapper"
- @click="manage"
- >{{ footerswitch ? "管理" : "取消" }}
- </view>
- </view>
- </view>
- <view class="borRadius14 cartBox">
- <view
- v-if="cartList.valid.length > 0 || cartList.invalid.length > 0"
- class="pad20"
- >
- <!-- 按商家分组的商品列表 -->
- <view class="merchant-groups">
- <block v-for="(merchant, merchantIndex) in groupedCartList" :key="merchantIndex">
- <view class="merchant-group">
- <!-- 商家头部 -->
- <view class="merchant-header acea-row row-between-wrapper" @click="toMerchant(merchant)">
- <view class="merchant-info acea-row row-middle">
- <image class="merchant-logo" :src="merchant.logo" mode="aspectFill"></image>
- <text class="merchant-name">{{ merchant.name }}</text>
- <uni-icons type="right" size="14" color="#333333"></uni-icons>
- </view>
- </view>
- <!-- 商家下的商品列表 -->
- <view class="list">
- <up-checkbox-group
- @change="checkboxChange"
- shape="circle"
- v-model="shopCheckbox"
- >
- <block v-for="(item, index) in merchant.products" :key="index">
- <view class="item acea-row row-between-wrapper">
- <up-checkbox
- activeColor="#F8C008"
- :name="item.id"
- :disabled="!item.attrStatus && footerswitch"
- :checked="item.checked"
- :customStyle="{ marginRight: '10px' }"
- ></up-checkbox>
- <navigator
- :render-link="false"
- :url="'/pages/goods/goods_details/index?id=' + item.productId"
- hover-class="none"
- class="picTxt row-between-wrapper"
- >
- <view class="pictrue">
- <image :src="item.image"></image>
- </view>
- <view class="item-info">
- <view class="text">
- <view
- class="line1 namAndNum"
- :class="item.attrStatus ? '' : 'reColor'"
- >
- <view class="storeName line1 ">
- {{ item.storeName }}
- </view>
- <view v-if="item.attrStatus">
- <view
- class="carnum acea-row row-center-wrapper"
- v-show="item.editNum"
- >
- <view
- class="reduce"
- :class="item.numSub ? 'on' : ''"
- @click.stop="subCart(item.id, merchantIndex, index)"
- >-</view
- >
- <view class="num">{{ item.cartNum }}</view>
- <view
- class="plus"
- :class="item.numAdd ? 'on' : ''"
- @click.stop="addCart(item.id, merchantIndex, index)"
- >+</view
- >
- </view>
- <view class="carnumTag" v-show="!item.editNum" @click.stop="item.editNum = true">
- X{{ item.cartNum }}
- </view>
- </view>
- </view>
- <view class="attribute-box infor line1">
- <view class="attribute-suk" v-show="item.suk">属性:{{ item.suk }}</view>
- <view v-show="!item.attrStatus" class="invalid-count">数量:{{ item.cartNum }}</view>
- </view>
- <view class="money" v-if="item.attrStatus"
- >¥{{ item.storePrice }}</view
- >
- <view class="labor-costs line1" v-if="item.attrStatus"
- >
- <text class="tag">工费:{{ item.price }}/克</text>
- <text class="tag" style="margin-left: 10rpx;">附加费:{{ item.additionalAmount }}</text>
- </view>
- <!-- <view class='money' v-if="item.attrStatus">¥{{item.truePrice}}</view> -->
- <view
- class="reElection acea-row row-between-wrapper"
- v-else
- >
- <view class="title">请重新选择商品规格</view>
- <view
- class="reBnt cart-color acea-row row-center-wrapper"
- @click.stop="reElection(item)"
- >重选
- </view>
- </view>
- </view>
- </view>
- </navigator>
- </view>
- </block>
- </up-checkbox-group>
- </view>
- </view>
- </block>
- </view>
- <!-- <view class="list">-->
- <!-- <up-checkbox-group-->
- <!-- @change="checkboxChange"-->
- <!-- shape="circle"-->
- <!-- v-model="shopCheckbox"-->
- <!-- >-->
- <!-- <block v-for="(item, index) in cartList.valid" :key="index">-->
- <!-- <view class="item acea-row row-between-wrapper">-->
- <!-- <up-checkbox-->
- <!-- activeColor="#F8C008"-->
- <!-- :name="item.id"-->
- <!-- :disabled="!item.attrStatus && footerswitch"-->
- <!-- :checked="item.checked"-->
- <!-- :customStyle="{ marginRight: '10px' }"-->
- <!-- ></up-checkbox>-->
- <!-- <navigator-->
- <!-- :render-link="false"-->
- <!-- :url="'/pages/goods_details/index?id=' + item.productId"-->
- <!-- hover-class="none"-->
- <!-- class="picTxt row-between-wrapper"-->
- <!-- >-->
- <!-- <view class="pictrue">-->
- <!-- <image :src="item.image"></image>-->
- <!-- </view>-->
- <!-- <view class="item-info">-->
- <!-- <view class="text">-->
- <!-- <view-->
- <!-- class="line1"-->
- <!-- :class="item.attrStatus ? '' : 'reColor'"-->
- <!-- >{{ item.storeName }}-->
- <!-- </view>-->
- <!-- <view class="attribute-box infor line1">-->
- <!-- <view class="attribute-suk" v-show="item.suk">属性:{{ item.suk }}</view>-->
- <!-- <view v-show="!item.attrStatus" class="invalid-count">数量:{{ item.cartNum }}</view>-->
- <!-- </view>-->
- <!-- <view class="labor-costs line1" v-if="item.attrStatus"-->
- <!-- >工费:{{ item.price }}/克</view-->
- <!-- >-->
- <!-- <view class="money" v-if="item.attrStatus"-->
- <!-- >¥{{ item.storePrice }}</view-->
- <!-- >-->
- <!-- <!– <view class='money' v-if="item.attrStatus">¥{{item.truePrice}}</view> –>-->
- <!-- <view-->
- <!-- class="reElection acea-row row-between-wrapper"-->
- <!-- v-else-->
- <!-- >-->
- <!-- <view class="title">请重新选择商品规格</view>-->
- <!-- <view-->
- <!-- class="reBnt cart-color acea-row row-center-wrapper"-->
- <!-- @click.stop="reElection(item)"-->
- <!-- >重选-->
- <!-- </view>-->
- <!-- </view>-->
- <!-- </view>-->
- <!-- <view-->
- <!-- class="carnum acea-row row-center-wrapper"-->
- <!-- v-if="item.attrStatus"-->
- <!-- >-->
- <!-- <view-->
- <!-- class="reduce"-->
- <!-- :class="item.numSub ? 'on' : ''"-->
- <!-- @click.stop="subCart(index)"-->
- <!-- >-</view-->
- <!-- >-->
- <!-- <view class="num">{{ item.cartNum }}</view>-->
- <!-- <view-->
- <!-- class="plus"-->
- <!-- :class="item.numAdd ? 'on' : ''"-->
- <!-- @click.stop="addCart(index)"-->
- <!-- >+</view-->
- <!-- >-->
- <!-- </view>-->
- <!-- </view>-->
- <!-- </navigator>-->
- <!-- </view>-->
- <!-- </block>-->
- <!-- </up-checkbox-group>-->
- <!-- </view>-->
- <!-- cartList.valid.length===0 && cartList.invalid.length > 0 -->
- <view
- v-if="cartList.invalid.length > 0"
- class="invalidGoods borRadius14"
- :style="
- cartList.valid.length === 0 && cartList.invalid.length > 0
- ? 'position: relative;z-index: 111;top: -120rpx;'
- : 'position: static;'
- "
- >
- <view class="goodsNav acea-row row-between-wrapper">
- <view
- v-if="cartList.invalid.length > 1 || cartList.valid.length > 0"
- @click="goodsOpen"
- >
- <text
- class="iconfont"
- :class="
- goodsHidden == true ? 'icon-xiangxia' : 'icon-xiangshang'
- "
- ></text
- >失效商品
- </view>
- <view v-else> 失效商品 </view>
- <view class="del" @click="unsetCart"
- ><text class="iconfont icon-shanchu1"></text>清空</view
- >
- </view>
- <view class="goodsList" :hidden="goodsHidden">
- <block v-for="(item, index) in cartList.invalid" :key="index">
- <view class="item acea-row row-between-wrapper">
- <view class="invalid">失效</view>
- <view class="picTxt acea-row row-between-wrapper">
- <view class="pictrue">
- <image :src="item.image"></image>
- </view>
- <view class="text acea-row row-column-between">
- <view class="line1 name">{{ item.storeName }}</view>
- <view class="infor line1" v-if="item.suk"
- >属性:{{ item.suk }}</view
- >
- <view class="acea-row row-between-wrapper">
- <view class="end">该商品已失效</view>
- </view>
- </view>
- </view>
- </view>
- </block>
- </view>
- </view>
- <!-- <view class='loadingicon acea-row row-center-wrapper' v-if="cartList.valid.length&&!loadend">
- <text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
- </view> -->
- <view
- class="loadingicon acea-row row-center-wrapper"
- v-if="cartList.invalid.length && loadend"
- >
- <text
- class="loading iconfont icon-jiazai"
- :hidden="loadingInvalid == false"
- ></text
- >{{ loadTitleInvalid }}
- </view>
- </view>
- <view
- class="noCart"
- v-if="
- cartList.valid.length == 0 &&
- cartList.invalid.length == 0 &&
- canShow
- "
- >
- <view class="pictrue">
- <image :src="HTTP_REQUEST_URL_IMG+'noCart.png'"></image>
- </view>
- <recommend :hostProduct="hostProduct"></recommend>
- </view>
- </view>
- </view>
- <view
- class="footer acea-row row-between-wrapper"
- v-if="cartList.valid.length > 0"
- >
- <view>
- <up-checkbox-group shape="circle" @change="checkboxAllChange" v-model="allCheckboxValue">
- <!-- <checkbox value="all" :checked="!!isAllSelect" /> -->
- <up-checkbox
- :name="'all'"
- activeColor="#F8C008"
- :checked="!!isAllSelect"
- ></up-checkbox>
- <text class="checkAll">全选({{ selectValue.length }})</text>
- </up-checkbox-group>
- </view>
- <view class="money acea-row row-middle" v-if="footerswitch == true">
- <text style="font-size: 24rpx;color: #333;">合计:</text>
- <text class="font-color">¥{{ selectCountPrice }}</text>
- <form @submit="subOrder" report-submit="true">
- <button class="placeOrder bg-color" formType="submit">
- 结算
- </button>
- </form>
- </view>
- <view class="button acea-row row-middle" v-else>
- <form @submit="subCollect" report-submit="true">
- <button class="bnt cart-color" formType="submit">收藏</button>
- </form>
- <form @submit="subDel" report-submit="true">
- <button class="bnt" formType="submit">删除</button>
- </form>
- </view>
- </view>
- <productWindow
- :attr="attr"
- :isShow="1"
- :iSplus="1"
- :iScart="1"
- :showPopup="showProductPopup"
- @closePopup="closeProductPopup"
- @submit="handleSubmit"
- @myevent="onMyEvent"
- @ChangeAttr="ChangeAttr"
- @ChangeCartNum="ChangeCartNumFn"
- @attrVal="attrVal"
- @iptCartNum="iptCartNum"
- id="product-window"
- >
- </productWindow>
- <!-- <view class="uni-p-b-96"></view> -->
- <!-- <view class="uni-p-b-98"></view> -->
- <!-- #ifdef MP -->
- <!-- <authorize :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
- <!-- #endif -->
- </view>
- </template>
- <script setup>
- import { ref, reactive, computed,watch } from "vue";
- import { onLoad, onShow, onReachBottom } from "@dcloudio/uni-app";
- import { useAppStore } from "@/stores/app.js";
- import {
- getCartList,
- getCartCounts,
- changeCartNum,
- cartDel,
- getResetCart,
- } from "@/api/order.js";
- import { getProductHot, collectAll, getProductDetail } from "@/api/store.js";
- import { toLogin } from "@/libs/login.js";
- import recommend from "@/components/recommend";
- import productWindow from "@/components/productWindow";
- import util from "@/utils/util";
- import { getPreOrder } from "@/libs/order";
- import { useToast } from "@/hooks/useToast";
- import { HTTP_REQUEST_URL_IMG } from "@/config/app";
- const appStore = useAppStore();
- const { Toast } = useToast();
- const cartCount = ref(0);
- const goodsHidden = ref(false);
- const footerswitch = ref(true);
- const hostProduct = ref([]);
- const cartList = reactive({
- valid: [],
- invalid: [],
- });
- const isAllSelect = ref(false); //全选
- const selectValue = ref([]); //选中的数据
- const selectCountPrice = ref(0.0);
- const isShowAuth = ref(false); //是否隐藏授权
- const hotScroll = ref(false);
- const hotPage = ref(1);
- const hotLimit = ref(10);
- const loading = ref(false);
- const loadend = ref(false);
- const page = ref(1);
- const limit = ref(20);
- const loadingInvalid = ref(false);
- const loadendInvalid = ref(false);
- const loadTitleInvalid = ref("加载更多");
- const pageInvalid = ref(1);
- const limitInvalid = ref(20);
- const attr = reactive({
- cartAttr: false,
- productAttr: [],
- productSelect: {},
- });
- const productValue = ref([]);
- const productInfo = ref({});
- const attrValue = ref("");
- const attrTxt = ref("请选择");
- const cartId = ref(0);
- const product_id = ref(0);
- const canShow = ref(false);
- const shopCheckbox = ref([]);
- const showProductPopup = ref(false);
- const isLogin = appStore.isLogin;
- // 添加全选的值控制
- const allCheckboxValue = ref([]);
- // 监听isAllSelect的变化,同步到allCheckboxValue
- watch(isAllSelect, (newVal) => {
- if (newVal) {
- allCheckboxValue.value = ['all'];
- } else {
- allCheckboxValue.value = [];
- }
- });
- onLoad((options) => {
- if (!isLogin) {
- toLogin();
- }
- });
- onShow(() => {
- canShow.value = false;
- if (isLogin) {
- hotPage.value = 1;
- hostProduct.value = [];
- hotScroll.value = false;
- loadend.value = false;
- page.value = 1;
- cartList.valid = [];
- getCartListFn();
- loadendInvalid.value = false;
- pageInvalid.value = 1;
- cartList.invalid = [];
- getInvalidList();
- footerswitch.value = true;
- hotScroll.value = false;
- hotPage.value = 1;
- hotLimit.value = 10;
- cartList.valid = [];
- cartList.invalid = [];
- // 不要重置选中状态
- // isAllSelect.value = false;
- // selectValue.value = [];
- // selectCountPrice.value = 0.0;
- cartCount.value = 0;
- isShowAuth.value = false;
- }
- });
- onReachBottom(() => {
- if (loadend.value) {
- getInvalidList();
- }
- if (
- cartList.valid.length == 0 &&
- cartList.invalid.length == 0 &&
- hotPage.value != 1
- ) {
- getHostProduct();
- }
- });
- // 计算属性:按商家分组购物车商品
- const groupedCartList = computed(() => {
- const groups = {};
- cartList.valid.forEach(item => {
- const merchant = item.sbMerchant;
- if (merchant && merchant.id) {
- const merchantId = merchant.id;
- if (!groups[merchantId]) {
- groups[merchantId] = {
- id: merchantId,
- name: merchant.merchantName || '未知商家',
- logo: merchant.merchantLogo || '/static/avator.png',
- products: []
- };
- }
- item.editNum = false;
- groups[merchantId].products.push(item);
- } else {
- // 如果没有商家信息,放到默认分组
- const defaultId = 'default';
- if (!groups[defaultId]) {
- groups[defaultId] = {
- id: defaultId,
- name: '其他商家',
- logo: '/static/avator.png',
- products: []
- };
- }
- groups[defaultId].products.push(item);
- }
- });
- return Object.values(groups);
- });
- const orderGoodsCount = computed(() => {
- // console.log('fsdfsdfsd', cartList, cartList)
- return cartList.valid.length + cartList.invalid.length;
- });
- // 监听购物车数据变化,重新计算选中状态
- watch(() => cartList.valid, (newVal) => {
- calculateSelectStatus();
- }, { deep: true });
- // 重新计算选中状态
- function calculateSelectStatus() {
- const validList = cartList.valid;
- const selectValueArr = selectValue.value;
- let checkedCount = 0;
- let totalValidCount = 0;
- validList.forEach(item => {
- if (item.attrStatus) {
- totalValidCount++;
- if (selectValueArr.includes(item.id)) {
- checkedCount++;
- }
- }
- });
- isAllSelect.value = totalValidCount > 0 && checkedCount === totalValidCount;
- switchSelect();
- }
- const closeProductPopup = () => {
- showProductPopup.value = false;
- };
- function authColse(e) {
- isShowAuth.value = e;
- }
- async function handleSubmit() {
- try {
- const productSelect = productValue.value[attrValue.value];
- if (attr.productAttr.length && productSelect === undefined)
- return Toast({
- title: "产品库存不足,请选择其它",
- });
- const params = {
- id: cartId.value,
- productId: product_id.value,
- num: attr.productSelect.cart_num,
- unique:
- attr.productSelect !== undefined
- ? attr.productSelect.unique
- : productInfo.value.id,
- };
- await getResetCart(params);
- attr.cartAttr = false;
- Toast({
- title: "添加购物车成功",
- success: () => {
- loadend.value = false;
- page.value = 1;
- cartList.valid = [];
- getCartListFn();
- getCartNum();
- },
- });
- } catch (error) {
- Toast({
- title: error,
- });
- } finally {
- showProductPopup.value = false;
- }
- }
- function onMyEvent() {
- attr.cartAttr = false;
- }
- function reElection(item) {
- showProductPopup.value = true;
- getGoodsDetails(item);
- }
- function getGoodsDetails(item) {
- console.log("item", item);
- uni.showLoading({
- title: "加载中",
- mask: true,
- });
- cartId.value = item.id;
- product_id.value = item.productId;
- getProductDetail(item.productId)
- .then((res) => {
- uni.hideLoading();
- attr.cartAttr = true;
- productInfo.value = res.data.productInfo;
- attr.productAttr = res.data.productAttr.map((item) => ({
- ...item,
- attrValues: item.attrValues.split(","),
- }));
- productValue.value = res.data.productValue;
- DefaultSelect();
- })
- .catch((err) => {
- uni.hideLoading();
- });
- }
- function ChangeAttr(res) {
- const productSelect = productValue.value[res];
- if (productSelect && productSelect.stock > 0) {
- attr.productSelect = {
- ...attr.productSelect,
- image: productSelect.image,
- sales: productSelect.sales,
- weight: productSelect.weight,
- price: productSelect.price,
- storePrice: productSelect.storePrice,
- stock: productSelect.stock,
- unique: productSelect.id,
- cart_num: 1,
- };
- attrValue.value = res;
- attrTxt.value = "已选择";
- } else {
- attr.productSelect = {
- ...attr.productSelect,
- image: productInfo.value.image,
- weight: productSelect.weight,
- price: productInfo.value.price,
- storePrice: productSelect.storePrice,
- stock: 0,
- unique: productInfo.value.id,
- cart_num: 1,
- };
- attrValue.value = "";
- attrTxt.value = "请选择";
- }
- }
- function DefaultSelect() {
- const productAttr = attr.productAttr;
- console.log("productValue", productValue);
- let value = [];
- for (let key in productValue.value) {
- if (productValue.value[key].stock > 0) {
- value = attr.productAttr.length ? key.split(",") : [];
- break;
- }
- }
- for (let i = 0; i < productAttr.length; i++) {
- productAttr[i].index = value[i];
- }
- let productSelect = productValue.value[value.sort().join(",")];
- if (productSelect && productAttr.length) {
- attr.productSelect.storeName = productInfo.value.storeName;
- attr.productSelect.image = productSelect.image;
- attr.productSelect.price = productSelect.price;
- attr.productSelect.storePrice = productSelect.storePrice;
- attr.productSelect.stock = productSelect.stock;
- attr.productSelect.unique = productSelect.id;
- attr.productSelect.cart_num = 1;
- attrValue.value = value.sort().join(",");
- attrTxt.value = "已选择";
- } else if (!productSelect && productAttr.length) {
- attr.productSelect.storeName = productInfo.value.storeName;
- attr.productSelect.image = productInfo.value.image;
- attr.productSelect.price = productInfo.value.price;
- attr.productSelect.storePrice = productSelect.storePrice;
- attr.productSelect.stock = 0;
- attr.productSelect.unique = productInfo.value.id;
- attr.productSelect.cart_num = 0;
- attrValue.value = "";
- attrTxt.value = "请选择";
- } else if (!productSelect && !productAttr.length) {
- attr.productSelect.storeName = productInfo.value.storeName;
- attr.productSelect.image = productInfo.value.image;
- attr.productSelect.price = productInfo.value.price;
- attr.productSelect.storePrice = productSelect.storePrice;
- attr.productSelect.stock = productInfo.value.stock;
- attr.productSelect.unique = productInfo.value.id || "";
- attr.productSelect.cart_num = 1;
- attrValue.value = "";
- attrTxt.value = "请选择";
- }
- }
- function attrVal(val) {
- attr.productAttr[val.indexw].index =
- attr.productAttr[val.indexw].attrValues[val.indexn];
- }
- function ChangeCartNumFn(changeValue) {
- let productSelect = productValue.value[attrValue.value];
- if (productSelect === undefined && !attr.productAttr.length)
- productSelect = attr.productSelect;
- if (productSelect === undefined) return;
- let stock = productSelect.stock || 0;
- let num = attr.productSelect;
- if (changeValue) {
- num.cart_num++;
- if (num.cart_num > stock) {
- num.cart_num = stock ? stock : 1;
- }
- } else {
- num.cart_num--;
- if (num.cart_num < 1) {
- num.cart_num = 1;
- }
- }
- }
- function iptCartNum(e) {
- attr.productSelect.cart_num = e;
- }
- function subDel(event) {
- const selectValueArr = selectValue.value;
- if (selectValueArr.length > 0)
- cartDel(selectValueArr).then((res) => {
- loadend.value = false;
- page.value = 1;
- cartList.valid = [];
- getCartListFn();
- getCartNum();
- });
- else
- return Toast({
- title: "请选择产品",
- });
- }
- function getSelectValueProductId() {
- const validList = cartList.valid;
- const selectValueArr = selectValue.value;
- let productId = [];
- if (selectValueArr.length > 0) {
- for (let index in validList) {
- if (inArray(validList[index].id, selectValueArr)) {
- productId.push(validList[index].productId);
- }
- }
- }
- return productId;
- }
- function subCollect(event) {
- const selectValueArr = selectValue.value;
- if (selectValueArr.length > 0) {
- const selectValueProductId = getSelectValueProductId();
- collectAll(selectValueProductId)
- .then((res) => {
- return Toast({
- title: "收藏成功",
- icon: "success",
- });
- })
- .catch((err) => {
- return Toast({
- title: err,
- });
- });
- } else {
- return Toast({
- title: "请选择产品",
- });
- }
- }
- function subOrder(event) {
- if(appStore.userInfo.merchant){
- uni.showToast({
- title:'商家用户不能下单!',
- icon:'none',
- })
- return ;
- }
- const selectValueArr = selectValue.value;
- if (selectValueArr.length > 0) {
- // 检查是否选择了多个商家的商品
- if (hasMultipleMerchants()) {
- return Toast({
- title: "只能结算同一商家的商品",
- });
- }
- getPreOrderFn();
- } else {
- return Toast({
- title: "请选择产品",
- });
- }
- }
- // 检查是否选择了多个商家的商品
- function hasMultipleMerchants() {
- const selectValueArr = selectValue.value;
- const validList = cartList.valid;
- // 获取选中的商品
- const selectedItems = validList.filter(item =>
- selectValueArr.includes(item.id) && item.attrStatus
- );
- // 提取商家ID
- const merchantIds = new Set();
- selectedItems.forEach(item => {
- if (item.sbMerchant && item.sbMerchant.id) {
- merchantIds.add(item.sbMerchant.id);
- } else {
- // 如果没有商家信息,也作为一个独立的商家
- merchantIds.add('default');
- }
- });
- return merchantIds.size > 1;
- }
- function getPreOrderFn() {
- const shoppingCartId = selectValue.value.map((item) => {
- return {
- shoppingCartId: Number(item),
- };
- });
- const params = {
- mallType: 0,
- preOrderType: "shoppingCart",
- orderDetails: shoppingCartId,
- };
- getPreOrder(params);
- }
- function checkboxAllChange(value) {
- if (value.length > 0) {
- // 检查全选是否会选择多个商家
- if (footerswitch.value && willSelectMultipleMerchants()) {
- Toast({
- title: "只能选择同一商家的商品进行结算",
- });
- // 重置全选状态为未选中
- isAllSelect.value = false;
- allCheckboxValue.value = [];
- // 清空shopCheckbox的值
- shopCheckbox.value = [];
- setAllSelectValue(0);
- return;
- }else{
- setAllSelectValue(1);
- }
- } else {
- setAllSelectValue(0);
- }
- }
- // 检查全选是否会选择多个商家
- function willSelectMultipleMerchants() {
- const validList = cartList.valid.filter(item => item.attrStatus);
- const merchantIds = new Set();
- validList.forEach(item => {
- if (item.sbMerchant && item.sbMerchant.id) {
- merchantIds.add(item.sbMerchant.id);
- } else {
- merchantIds.add('default');
- }
- });
- return merchantIds.size > 1;
- }
- function setAllSelectValue(status) {
- const valid = cartList.valid;
- let selectValueArr = [];
- valid.forEach(item => {
- if (status) {
- if (footerswitch.value) {
- if (item.attrStatus) {
- item.checked = true;
- selectValueArr.push(item.id);
- } else {
- item.checked = false;
- }
- } else {
- item.checked = true;
- selectValueArr.push(item.id);
- }
- } else {
- item.checked = false;
- }
- });
- cartList.valid = [...valid];
- selectValue.value = selectValueArr;
- isAllSelect.value = status === 1;
- console.log(isAllSelect.value)
- switchSelect();
- }
- function checkboxChange(value) {
- const valid = cartList.valid;
- // 更新所有商品的选中状态
- valid.forEach(item => {
- item.checked = value.includes(item.id);
- });
- cartList.valid = [...valid];
- selectValue.value = value;
- // 计算全选状态
- if (footerswitch.value) {
- // 结算模式下,只计算有效商品
- const validItems = valid.filter(item => item.attrStatus);
- isAllSelect.value = validItems.length > 0 && value.length === validItems.length;
- } else {
- // 管理模式下,计算所有商品
- isAllSelect.value = valid.length > 0 && value.length === valid.length;
- }
- switchSelect();
- }
- function inArray(search, array) {
- for (let i in array) {
- if (array[i] == search) {
- return true;
- }
- }
- return false;
- }
- // 选中商品的价格
- function switchSelect() {
- const validList = cartList.valid;
- const selectValueArr = selectValue.value;
- let selectCount = 0.0;
- console.log('switchSelect called', {
- validListLength: validList.length,
- selectValueArrLength: selectValueArr.length,
- selectValueArr: selectValueArr
- });
- if (selectValueArr.length < 1) {
- selectCountPrice.value = '0.00';
- } else {
- for (let index in validList) {
- if (inArray(validList[index].id, selectValueArr)) {
- console.log('Calculating item:', {
- id: validList[index].id,
- cartNum: validList[index].cartNum,
- storePrice: validList[index].storePrice,
- checked: validList[index].checked
- });
- const itemTotal = util.$h.Mul(validList[index].cartNum, validList[index].storePrice);
- selectCount = util.$h.Add(selectCount, itemTotal);
- }
- }
- // 保留两位小数
- selectCountPrice.value = selectCount.toFixed(2);
- }
- console.log('Final selectCountPrice:', selectCountPrice.value);
- }
- function iptCartNumInput(index) {
- const item = cartList.valid[index];
- if (item.cartNum) {
- setCartNum(item.id, item.cartNum);
- }
- switchSelect();
- }
- function blurInput(index) {
- const item = cartList.valid[index];
- if (!item.cartNum) {
- item.cartNum = 1;
- }
- }
- function subCart(cartIdVal, merchantIndex, productIndex) {
- const item = groupedCartList.value[merchantIndex].products[productIndex];
- let status = false;
- item.cartNum = Number(item.cartNum) - 1;
- if (item.cartNum < 1) status = true;
- if (item.cartNum <= 1) {
- item.cartNum = 1;
- item.numSub = true;
- } else {
- item.numSub = false;
- item.numAdd = false;
- }
- if (false == status) {
- setCartNum(item.id, item.cartNum, function (data) {
- switchSelect();
- getCartNum();
- });
- }
- }
- function addCart(cartIdVal, merchantIndex, productIndex) {
- console.log(cartIdVal)
- const item = groupedCartList.value[merchantIndex].products[productIndex];
- item.cartNum = Number(item.cartNum) + 1;
- if (item.cartNum >= item.stock) {
- item.cartNum = item.stock;
- item.numAdd = true;
- item.numSub = false;
- } else {
- item.numAdd = false;
- item.numSub = false;
- }
- setCartNum(item.id, item.cartNum, function (data) {
- // 更新UI
- switchSelect();
- getCartNum();
- });
- }
- function setCartNum(cartIdVal, cartNum, successCallback) {
- changeCartNum(cartIdVal, cartNum).then((res) => {
- successCallback && successCallback(res.data);
- });
- }
- function getCartNum() {
- getCartCounts(true, "sum").then((res) => {
- cartCount.value = res.data.count;
- });
- }
- function getCartData(data) {
- return new Promise((resolve, reject) => {
- getCartList(data)
- .then((res) => {
- // 加载数据后,同步选中状态
- const data = res.data;
- if (data.list && data.list.length > 0) {
- data.list.forEach(item => {
- // 如果这个商品ID在选中列表中,设置checked为true
- item.checked = selectValue.value.includes(item.id);
- });
- }
- resolve(data);
- })
- .catch(function (err) {
- loading.value = false;
- canShow.value = true;
- Toast({
- title: err,
- });
- });
- });
- }
- async function getCartListFn() {
- uni.showLoading({
- title: "加载中",
- mask: true,
- });
- let data = {
- page: page.value,
- limit: limit.value,
- isValid: true,
- };
- getCartCounts(true, "sum").then(async (c) => {
- // 获取购物车商品数量
- cartCount.value = c.data.count;
- // 当没有商品时,获取推荐商品
- if (c.data.count === 0) getHostProduct();
- for (let i = 0; i < Math.ceil(cartCount.value / limit.value); i++) {
- let cartListData = await getCartData(data);
- let valid = cartListData.list;
- let validList = [...cartList.valid, ...valid];
- let selectValueArr = [];
- if (validList.length > 0) {
- for (let index in validList) {
- // 购物车加减状态
- if (validList[index].cartNum == 1) {
- validList[index].numSub = true;
- } else {
- validList[index].numSub = false;
- }
- // 库存
- let stock = validList[index].stock ? validList[index].stock : 0;
- // 当购物车数量大于库存时,设置购物车数量为库存
- if (validList[index].cartNum == stock) {
- validList[index].numAdd = true;
- } else if (validList[index].cartNum == validList[index].stock) {
- validList[index].numAdd = true;
- } else {
- validList[index].numAdd = false;
- }
- // attrStatus 判断商品是否有效
- if (validList[index].attrStatus) {
- // 关键修改:根据 selectValue 中的ID来设置选中状态
- if (selectValue.value.includes(validList[index].id)) {
- validList[index].checked = true;
- selectValueArr.push(validList[index].id);
- } else {
- validList[index].checked = false;
- }
- } else {
- validList[index].checked = false;
- }
- }
- }
- cartList.valid = validList;
- data.page += 1;
- // 不要重置 selectValue,保持原来的选中状态
- // selectValue.value = selectValueArr;
- let newArr = validList.filter((item) => item.attrStatus);
- // 全选状态
- isAllSelect.value = newArr.length > 0 && selectValue.value.length === newArr.length;
- switchSelect();
- }
- loading.value = false;
- canShow.value = true;
- uni.hideLoading();
- });
- }
- function getInvalidList() {
- if (loadendInvalid.value) return false;
- if (loadingInvalid.value) return false;
- let data = {
- page: pageInvalid.value,
- limit: limitInvalid.value,
- isValid: false,
- };
- getCartList(data)
- .then((res) => {
- let invalid = res.data.list,
- loadendInvalidFlag = invalid.length < limitInvalid.value;
- cartList.invalid = [...invalid, ...cartList.invalid];
- loadendInvalid.value = loadendInvalidFlag;
- loadTitleInvalid.value = loadendInvalidFlag
- ? "我也是有底线的"
- : "加载更多";
- pageInvalid.value = pageInvalid.value + 1;
- loadingInvalid.value = false;
- })
- .catch((res) => {
- loadingInvalid.value = false;
- loadTitleInvalid.value = "加载更多";
- });
- }
- function getHostProduct() {
- if (hotScroll.value) return;
- getProductHot(hotPage.value, hotLimit.value).then((res) => {
- hotPage.value++;
- hotScroll.value = res.data.list.length < hotLimit.value;
- hostProduct.value = hostProduct.value.concat(res.data.list);
- });
- }
- function goodsOpen() {
- goodsHidden.value = !goodsHidden.value;
- }
- function manage() {
- footerswitch.value = !footerswitch.value;
- let arr1 = [];
- let arr2 = [];
- let newValid = cartList.valid.map((item) => {
- if (footerswitch.value) {
- if (item.attrStatus) {
- if (item.checked) {
- arr1.push(item.id);
- }
- } else {
- item.checked = false;
- arr2.push(item);
- }
- } else {
- if (item.checked) {
- arr1.push(item.id);
- }
- }
- return item;
- });
- cartList.valid = newValid;
- if (footerswitch.value) {
- isAllSelect.value = newValid.length === arr1.length + arr2.length;
- } else {
- isAllSelect.value = newValid.length === arr1.length;
- }
- selectValue.value = arr1;
- switchSelect();
- }
- function unsetCart() {
- let ids = [];
- for (let i = 0, len = cartList.invalid.length; i < len; i++) {
- ids.push(cartList.invalid[i].id);
- }
- cartDel(ids)
- .then((res) => {
- Toast({
- title: "清除成功",
- });
- cartList.invalid = [];
- getHostProduct();
- })
- .catch((res) => {});
- }
- const toMerchant = (obj) => {
- uni.navigateTo({ url:"/pages/merchantCenters/merchant?merchantId="+obj.id });
- }
- </script>
- <style scoped lang="scss">
- .cartBox {
- margin-top: 20rpx;
- }
- .shoppingCart {
- // .labelNav
- .labelNav {
- position: sticky;
- height: 178rpx;
- padding: 30rpx 30rpx 0 30rpx;
- font-size: 22rpx;
- color: #fff;
- position: fixed;
- left: 0;
- width: 100%;
- box-sizing: border-box;
- background-color: $theme-color;
- z-index: 5;
- top: 0;
- height: calc(178rpx + 44px + 45rpx);
- padding-top: calc(44px + 50rpx);
- .item {
- .iconfont {
- font-size: 25rpx;
- margin-right: 10rpx;
- }
- }
- }
- // .nav
- .nav {
- height: 90rpx;
- background-color: #fff;
- padding: 0 24rpx;
- box-sizing: border-box;
- font-size: 28rpx;
- color: #282828;
- border-top-left-radius: 14rpx;
- border-top-right-radius: 14rpx;
- .num {
- margin-left: 12rpx;
- }
- .administrate {
- font-size: 28rpx;
- color: #333;
- }
- }
- // .noCart
- .noCart {
- background-color: #fff;
- padding-top: 0.1rpx;
- .pictrue {
- width: 414rpx;
- height: 336rpx;
- margin: 78rpx auto 56rpx auto;
- image {
- width: 100%;
- height: 100%;
- }
- }
- }
- // .list
- .list {
- width: 100%;
- overflow: hidden;
- border-bottom-left-radius: 14rpx;
- border-bottom-right-radius: 14rpx;
- .item {
- width: 100%;
- padding: 24rpx;
- background-color: #fff;
- display: flex;
- .picTxt {
- flex: 1;
- position: relative;
- display: flex;
- align-items: center;
- .pictrue {
- width: 160rpx;
- height: 160rpx;
- margin-right: 15rpx;
- image {
- width: 100%;
- height: 100%;
- border-radius: 6rpx;
- }
- }
- .item-info,
- .text {
- width: 402rpx;
- font-size: 28rpx;
- color: #282828;
- .storeName{
- width: 40%;
- }
- .reColor {
- color: #999;
- }
- .reElection {
- margin-top: 20rpx;
- .title {
- font-size: 24rpx;
- }
- .reBnt {
- width: 120rpx;
- height: 46rpx;
- border-radius: 23rpx;
- font-size: 26rpx;
- }
- }
- .attribute-box {
- display: flex;
- justify-content: space-between;
- .attribute-suk {
- }
- .invalid-count {
- text-align: right;
- }
- }
- .infor {
- font-size: 24rpx;
- color: #999;
- //margin-top: 10rpx;
- }
- .labor-costs {
- font-size: 24rpx;
- color: #FD5F3C;
- margin-top: 10rpx;
- .tag{
- padding: 10rpx 16rpx;
- background-color: #FFEFEC;
- border-radius: 8rpx;
- display: inline-block;
- }
- }
- .money {
- font-size: 32rpx;
- color: #e93323;
- margin-top: 20rpx;
- font-weight: 600;
- }
- }
- .carnum {
- height: 47rpx;
- //position: absolute;
- //bottom: 7rpx;
- //right: 0;
- view {
- border: 1rpx solid #a4a4a4;
- width: 66rpx;
- text-align: center;
- height: 100%;
- line-height: 44rpx;
- font-size: 28rpx;
- color: #a4a4a4;
- }
- .reduce {
- border-right: 0;
- border-radius: 22rpx 0 0 22rpx;
- font-size: 34rpx;
- line-height: 40rpx;
- &.on {
- border-color: #e3e3e3;
- color: #dedede;
- }
- }
- .plus {
- border-left: 0;
- border-radius: 0 22rpx 22rpx 0;
- font-size: 34rpx;
- line-height: 40rpx;
- }
- .num {
- color: #282828;
- }
- }
- }
- }
- }
- // .invalidGoods
- .invalidGoods {
- background-color: #fff;
- margin-top: 30rpx;
- .goodsNav {
- width: 100%;
- height: 90rpx;
- padding: 0 24rpx;
- box-sizing: border-box;
- font-size: 28rpx;
- color: #333;
- .iconfont {
- color: #424242;
- font-size: 28rpx;
- margin-right: 17rpx;
- }
- .del {
- font-size: 26rpx;
- color: #333;
- .icon-shanchu1 {
- color: #333;
- font-size: 33rpx;
- vertical-align: -2rpx;
- margin-right: 8rpx;
- }
- }
- }
- .goodsList {
- .item {
- padding: 24rpx;
- .invalid {
- font-size: 22rpx;
- color: #ccc;
- height: 36rpx;
- border-radius: 3rpx;
- text-align: center;
- line-height: 36rpx;
- }
- .picTxt {
- width: 576rpx;
- .pictrue {
- width: 160rpx;
- height: 160rpx;
- image {
- width: 100%;
- height: 100%;
- border-radius: 6rpx;
- }
- }
- .text {
- width: 396rpx;
- font-size: 28rpx;
- color: #999;
- height: 140rpx;
- .name {
- width: 100%;
- }
- .infor {
- font-size: 24rpx;
- }
- .end {
- font-size: 26rpx;
- color: #bbb;
- }
- }
- }
- }
- }
- }
- }
- // footer
- .footer {
- z-index: 9;
- width: 100%;
- height: 132rpx;
- background-color: #fff;
- position: fixed;
- padding: 0 24rpx;
- box-sizing: border-box;
- border-top: 1rpx solid #eee;
- bottom: 0;
- .checkAll {
- font-size: 28rpx;
- line-height: 56rpx;
- color: #333333;
- margin-left: 14rpx;
- }
- .money {
- font-size: 30rpx;
- .font-color {
- font-weight: 600;
- }
- }
- .placeOrder {
- color: #333;
- font-size: 32rpx;
- width: 192rpx;
- height: 88rpx;
- border-radius: 16rpx;
- text-align: center;
- line-height: 88rpx;
- margin-left: 22rpx;
- }
- .button {
- .bnt {
- font-size: 28rpx;
- color: #999;
- border-radius: 50rpx;
- border: 1px solid #999;
- width: 160rpx;
- height: 60rpx;
- text-align: center;
- line-height: 60rpx;
- }
- form ~ form {
- margin-left: 17rpx;
- }
- }
- }
- // 其它样式
- .invalidClas {
- position: relative;
- z-index: 111;
- top: -120rpx;
- }
- .invalidClasNO {
- position: static;
- margin-top: 15px;
- }
- .uni-p-b-96 {
- height: 96rpx;
- }
- .merchant-groups {
- margin-bottom: 20rpx;
- }
- .merchant-group {
- background: #fff;
- border-radius: 16rpx;
- margin-bottom: 20rpx;
- overflow: hidden;
- .merchant-header {
- padding: 24rpx;
- .merchant-info {
- .merchant-logo {
- width: 40rpx;
- height: 40rpx;
- border-radius: 50%;
- margin-right: 20rpx;
- }
- .merchant-name {
- font-size: 24rpx;
- color: #333;
- }
- }
- .merchant-action {
- display: flex;
- align-items: center;
- color: #999;
- font-size: 24rpx;
- text {
- margin-right: 8rpx;
- }
- }
- }
- .merchant-products {
- padding: 0 24rpx;
- }
- }
- // 调整商品项样式以适应分组布局
- .item {
- padding: 24rpx 0;
- border-bottom: 1rpx solid #f5f5f5;
- &:last-child {
- border-bottom: none;
- }
- }
- // 其他原有样式保持不变
- .cartBox {
- margin-top: 20rpx;
- }
- .namAndNum{
- display: flex;
- align-items: center;
- justify-content: space-between;
- }
- .carnumTag{
- font-size: 24rpx;
- color: #666666;
- background: #F1F3F8;
- border-radius: 8rpx;
- padding: 10rpx 16rpx;
- }
- </style>
|