| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358 |
- <template>
- <view class="shop-index iPhone-bottom flex-direction box-border-box">
- <image v-show="!navBgType" class="bg-img" src="@/static/img/order-food/shop-top.png" mode="aspectFill"
- :class="scrollTop > newNavHeight?'z-4':'z-3'"></image>
- <image v-show="navBgType" class="bg-img-type z-4" src="@/static/img/order-food/shop-top.png" mode="aspectFill"
- :style="{height:newNavHeight+'rpx',opacity:opacity}"></image>
- <newNav :leftShow="false" :textType='false' class="z-4 mb-32" @navHeight='navHeight'>
- <view slot="textContentSlot" class="align-items-center flex-justify-end">
- <view class="ss-box b-rad-50 mr-24 align-items-center flex-justify-center z-4" @click.stop="goSearch">
- <image class="shop-search" src="@/static/img/order-food/shop-search.png" mode=""></image>
- </view>
- <view class="dd-box align-items-center flex-justify-center b-rad-200 mr-24 z-4" @click="goToOrder">
- <image class="shp-list-img mr-8" src="@/static/img/order-food/shp-list.png" mode=""></image>
- <view class="fs-28 fc-f">
- 订单
- </view>
- </view>
- </view>
- </newNav>
- <view class="bgc-f box-border-box plr-32 mb-32 shop-info">
- <view class="shop-info-box bgc-f box-border-box p-24 z-3 b-rad-20">
- <view class="fs-40 fw-600 text-overflow-ellipsis">
- {{shopsInfo.shop_name || ''}}
- </view>
- <view class="fs-24 fw-400 fc-9 text-overflow-ellipsis-one pt-15">
- 公告:{{shopsInfo.notice || '暂无公告'}}
- </view>
- </view>
- </view>
- <view class="b-b-eee box-border-box p-0-32-12 align-items-center flex-justify-space z-3 shop-type-box">
- <view class="align-items-center shop-type-list">
- <view class="shop-item fs-36 mb-8 mr-64" @click="seletShopType(item)"
- :class="shopType === item.id?'fc-3 fw-600':'fc-6'" v-for="(item,i) in shopTypeList" :key="i">
- {{item.name}}
- </view>
- <view class="shop-item-bs bgc-FF3333" :style="{left:shopTypeBsLeft+'rpx'}"></view>
- </view>
- <view class="align-items-center">
- <image class="zh-icon mr-8" src="@/static/img/order-food/zh-icon.png" mode="aspectFill"></image>
- <view class="fw-600 fs-32 fc-3">
- 桌号:{{table_number}}
- </view>
- </view>
- </view>
- <!-- <view class="bgc-f sticky-nav" :style="{ height: navBgType?`calc(100vh - ${newNavHeight}rpx - ${48}rpx)`:'auto'}"> -->
- <view class="bgc-F7F8FA">
- <view class="menu-box box-border-box align-items-flex-start" v-if="shopType === 1">
- <view class="menu-box-left" :style="{top: newNavHeight + 'rpx'}">
- <view class="menu-box-left-box pb-130 box-border-box"
- :style="{height: menuLeftType?`calc(100vh - ${newNavHeight}rpx - ${68}rpx)`:''}">
- <view class="menu-type-list fs-26 box-border-box align-items-center flex-justify-center"
- v-for="(item,i) in categoryList" :key="i" @click="selectType(item)"
- :class="type === item.category_id?'bgc-f fc-3 fw-600':'fc-6 fw-400'"
- :id="'left-scroll'+ item.category_id">
- {{item.name}}
- <view class="menu-type-num fc-f" v-show="item.num > 0">{{item.num > 99?'99+':item.num}}
- </view>
- </view>
- </view>
- </view>
- <view class="menu-box-only bgc-f pb-130 box-border-box">
- <view class="" v-for="(item,i) in goodsList" :key="i" :id="'scroll'+ item.type">
- <view class="fs-28 fw-600 fc-3 sticky-nav bgc-f p-20 box-border-box"
- :style="{top: newNavHeight + 'rpx'}">
- {{item.name}}
- </view>
- <view class="" v-if="item.list <= 0">
- <EnBlank message='暂无商品' :customSrc='true'>
- <template v-slot:blankImg>
- <image class="black-custom-img" src="@/static/img/common/no-order.png" mode="">
- </image>
- </template>
- </EnBlank>
- </view>
- <view class="menu-list flex-justify-space box-border-box" v-for="(info,k) in item.list" :key="k"
- :id="'goods_id'+ info.goods_id">
- <image class="flex-shrink0 goods_img b-rad-12" :src="info.goods_image" mode="scaleToFill">
- </image>
- <view class="menu-list-right flex-direction flex-justify-space">
- <view class="mb-8">
- <view class="mb-4 goods-name text-overflow-ellipsis fs-32 fw-600">
- {{info.goods_name}}
- </view>
- <!-- <view class="text-overflow-ellipsis fw-400 fc-9 fs-24 mb-2 goods-name"
- v-if="info.spec_type == 1">
- {{info.goods_desc?info.goods_desc:''}}
- </view> -->
- </view>
- <view class="flex-justify-space align-items-flex-end">
- <view class="align-items-baseline">
- <view class="fs-24 fw-400 fc-3">¥</view>
- <priceDisplay :value="info.price"></priceDisplay>
- <!-- <span class="fs-40 fw-b fc-3">{{info.seckill_money>0?info.seckill_money:info.price}}</span> -->
- <view class="fs-24 fc-9">/份起</view>
- </view>
- <view class="display-flex align-items-center" v-if="info.spec_type == 1">
- <image v-show="info.num > 0" class="wh-44" @click="addred(1,info)"
- src="@/static/img/order-food/reduce-img.png" mode=""></image>
- <view v-show="info.num > 0"
- class="right-only-right-2 text-align-center fs-28 fw-400">{{info.num}}
- </view>
- <image class="wh-44" src="@/static/img/order-food/add-img.png"
- @click="addred(2,info)" mode="">
- </image>
- </view>
- <view v-else class="right-only-right text-align-center b-rad-200"
- @click="addred(3,info)">
- <view class="fw-600 fs-28 fc-f">
- 选规格
- </view>
- <view class="selectedNum" v-if="info.num>0">
- {{info.num}}
- </view>
- </view>
- </view>
- </view>
- </view>
- </view>
- </view>
- </view>
- <ShopInfo style="height: 100%;" v-else-if="shopType === 2"></ShopInfo>
- </view>
- <view v-if="shopType === 1" class="shop-buttom-money box-border-box" @click="openBuyCrad()">
- <view
- class="shop-buttom-money-1 bgc-f flex-justify-space align-items-center pl-24 box-border-box pr-8 b-rad-200">
- <view class="align-items-center">
- <image class="gwc-img mr-36" src="@/static/img/order-food/gwc-img.png" mode="scaleToFill"></image>
- <view class="shop-buttom-num bgc-FE6B2C fs-24 fc-f text-align-center">{{cartData.total_num}}</view>
- <view class="align-items-baseline">
- <view class="fw-600 fs-28 fc-3 mr-4">¥</view>
- <view class="fw-600 fs-48 fc-3">{{cartData.total_goods_money}}</view>
- </view>
- </view>
- <view class="shop-buttom-money-2 text-align-center fc-f fs-32 fw-600 b-rad-200"
- :class="cartData.total_num>0?'':'opacity-4'" @click.stop="toConfirmOrder()">去结算
- </view>
- </view>
- </view>
- <uni-popup ref="popupBuy" type="bottom" background-color="transparent" borderRadius='20px 20rpx 0 0'>
- <view class="buy-card flex-index-css">
- <view class="buy-card-top box-border-box plr-32 flex-justify-space align-items-center b-rad-20-20-0-0">
- <view class="">
- <span class="fs-32 fc-3 mr-12">已选商品</span>
- </view>
- <view class="fs-28 fw-400 fc-9 align-items-center" @click.stop="setShopIngDel">
- <view class="">
- <uni-icons type="trash" size="18" color="#999"></uni-icons>
- </view>
- <view class="fs-28 fw-400 fc-9">清空</view>
- </view>
- </view>
- <view class="scroll-view-css bgc-f box-border-box car-scroll-box">
- <view class="buy-card-item b-rad-20 align-items-flex-start box-border-box"
- v-for="(goods,i) in cartData.goods_list" :key="i">
- <image class="goods_img flex-shrink0" :src="goods.goods_image" mode="scaleToFill"></image>
- <view class="flex-1 flex-direction flex-justify-space min-h b-b-2-F9F9F9 pb-24">
- <view class="">
- <view class="w-514 text-overflow-ellipsis-one fs-34 fw-600 mb-8">
- {{goods.goods_name}}
- </view>
- <view class="fc-6" v-show="goods.goods_attr">
- {{goods.goods_attr}}
- </view>
- </view>
- <view class="flex-justify-space">
- <view class="align-items-baseline">
- <view class="fs-24 fc-3 fw-400">¥</view>
- <priceDisplay :value="goods.price"></priceDisplay>
- <!-- <span class="">{{goods.price}}</span> -->
- </view>
- <view class="display-flex align-items-center">
- <image class="wh-44" @click.stop="setShoppingSave(goods,2)"
- src="@/static/img/order-food/reduce-img.png" mode="">
- </image>
- <view class="right-only-right-2 text-align-center fs-28 fw-400">
- {{goods.goods_number}}
- </view>
- <image class="wh-44" src="/static/img/order-food/add-img.png"
- @click.stop="setShoppingSave(goods,1)" mode=""></image>
- </view>
- </view>
- </view>
- </view>
- <en-blank v-if="cartData.goods_list.length<=0"
- message="快去选购商品吧!~"></en-blank>
- </view>
- <view class="pop-shop-buttom-money box-border-box" @click="openBuyCrad()">
- <view
- class="shop-buttom-money-1 bgc-f flex-justify-space align-items-center pl-24 box-border-box pr-8 b-rad-200">
- <view class="align-items-center">
- <image class="gwc-img mr-36" src="@/static/img/order-food/gwc-img.png" mode="scaleToFill">
- </image>
- <view class="shop-buttom-num bgc-FE6B2C fs-24 fc-f text-align-center">{{cartData.total_num}}
- </view>
- <view class="align-items-baseline">
- <view class="fw-600 fs-28 fc-3 mr-4">¥</view>
- <view class="fw-600 fs-48 fc-3">{{cartData.total_goods_money}}</view>
- </view>
- </view>
- <view class="shop-buttom-money-2 text-align-center fc-f fs-32 fw-600 b-rad-200"
- :class="cartData.total_num>0?'':'opacity-4'" @click.stop="toConfirmOrder()">去结算
- </view>
- </view>
- </view>
- </view>
- </uni-popup>
- </view>
- </template>
- <script>
- import priceDisplay from '@/components/price-display.vue';
- import ShopInfo from './shop-mod/info.vue';
- import tools from "@/service/tools";
- import EnBlank from "@/components/en-utils/en-blank/en-blank";
- import cartStore from '@/utils/cartStore.js';
- import {
- getGoodsCategory,
- getGoodsGoods
- } from '@/api/goods.js'
- import {
- receiveCoupon
- } from "@/api/coupon.js"
- import {
- shopInfo
- } from "@/api/shop.js"
- import {
- getDeliveryMoney
- } from "@/api/common";
- import {
- getShopIngList,
- setShopIngDel,
- shoppingSave
- } from "@/api/shopping";
- export default {
- components: {
- ShopInfo,
- EnBlank,
- priceDisplay,
- },
- data() {
- return {
- domHeigjt: 0,
- shopTypeList: [{
- name: "点单",
- id: 1
- }, {
- name: "商家",
- id: 2
- }],
- table_number: '',
- shopType: 1, //组件类型
- shopTypeBsLeft: 12,
- shop_id: '',
- operate_id: '',
- shopsInfo: {},
- isAjax: false,
- newNavHeight: '',
- navBgType: false,
- menuLeftType: false,
- scrollTop: 0,
- opacity: 0,
- heightOne: '',
- heightTwo: '',
- topHeight: '',
- idMark: '', //滚动自适应参数
- type: '', //默认分类标识
- categoryList: [],
- goodsList: [],
- cartData: {
- total_price: 0,
- total_num: 0,
- pack_price: 0,
- total_goods_money: 0,
- goods_list: [],
- },
- categoryTops: [], // 存储每个分类的top位置
- isScrolling: false, // 防止滚动事件冲突
- activeScroll: false, // 是否正在滚动
- scrollTimer: null, // 滚动计时器
- observer: null, // IntersectionObserver实例
- categoryDomInfos: [], // 存储分类DOM信息
- isManualScroll: false, // 是否是手动触发滚动
- lastActiveCategory: '', // 记录上一次激活的分类,避免重复更新
- }
- },
- // 在onUnload中销毁监听
- onUnload() {
- if (this.observer) {
- this.observer.disconnect();
- this.observer = null;
- }
- if (this.scrollTimer) {
- clearTimeout(this.scrollTimer);
- }
- },
- onShow() {
- this.getShopIngList();
- // 重新计算分类位置
- setTimeout(() => {
- this.initCategoryPositions();
- }, 300);
- },
- onLoad(e) {
- cartStore.setShopId();
- this.getshopInfo();
- },
- onReady() {
- // 确保DOM完全渲染后初始化位置
- setTimeout(() => {
- this.initCategoryPositions();
- }, 800);
- },
- mounted() {},
- onReachBottom(e) {},
- // 监听页面滚动
- onPageScroll(e) {
- // e.scrollTop 获取滚动条距离页面顶部的距离
- this.scrollTop = e.scrollTop
- // this.opacity = (Number(this.scrollTop) - 30)
- this.opacity = Math.min((this.scrollTop - 30) / 100, 1);
- // 可以根据滚动位置执行相应逻辑
- if (e.scrollTop > 25) {
- this.navBgType = true
- } else {
- this.navBgType = false
- }
- if (e.scrollTop > (Number(this.newNavHeight) - 4)) {
- this.menuLeftType = true
- } else {
- this.menuLeftType = false
- }
- // 优化:只在非手动滚动且菜单固定时更新激活分类
- if (!this.isManualScroll && this.menuLeftType) {
- this.updateActiveCategoryOnScroll();
- }
- },
- methods: {
- // 初始化分类位置信息
- initCategoryPositions() {
- this.$nextTick(() => {
- const query = uni.createSelectorQuery().in(this);
- // 清空之前的 DOM 信息
- this.categoryDomInfos = [];
- this.categoryTops = [];
- // 收集所有分类的位置信息
- this.goodsList.forEach((category, index) => {
- if (category.type) {
- query.select(`#scroll${category.type}`).boundingClientRect();
- }
- });
- query.exec((res) => {
- if (!res || res.length === 0) return;
- this.categoryDomInfos = res.map((rect, index) => {
- if (rect && this.goodsList[index]) {
- const categoryInfo = {
- id: this.goodsList[index].type,
- top: rect.top,
- height: rect.height,
- index: index
- };
- this.categoryTops.push(categoryInfo);
- return categoryInfo;
- }
- return null;
- }).filter(item => item !== null);
- console.log('分类位置信息:', this.categoryDomInfos);
- });
- });
- },
- // 更新激活的分类
- updateActiveCategoryOnScroll() {
- if (this.scrollTimer) {
- clearTimeout(this.scrollTimer);
- }
- this.scrollTimer = setTimeout(() => {
- this.findCurrentCategory();
- }, 150); // 增加防抖时间
- },
- // 查找当前应该激活的分类
- findCurrentCategory() {
- if (this.categoryDomInfos.length === 0 || !this.goodsList) return;
- const query = uni.createSelectorQuery().in(this);
- query.selectViewport().scrollOffset();
- query.exec((res) => {
- if (!res || !res[0]) return;
- const scrollTop = res[0].scrollTop;
- // 获取吸顶区域总高度
- const stickyHeight = this.getStickyHeight();
- // 阈值位置:视口顶部 + 吸顶高度 + 缓冲区域(分类标题高度的 1/3)
- const threshold = scrollTop + stickyHeight + 10;
- // 找当前应该激活的分类
- let currentCategoryId = this.categoryList[0]?.category_id;
- let found = false;
- // 从后往前遍历,找到最后一个在阈值上方的分类
- for (let i = this.categoryDomInfos.length - 1; i >= 0; i--) {
- const category = this.categoryDomInfos[i];
- if (category) {
- // 元素顶部位置(相对于视口)+ 滚动距离 = 相对于页面顶部的位置
- const elementTop = category.top + scrollTop;
- // 如果元素顶部在阈值上方或刚好对齐
- if (elementTop <= threshold) {
- currentCategoryId = category.id;
- found = true;
- break;
- }
- }
- }
- // 如果都没找到,说明第一个分类已经滚动过去了,使用第一个分类
- if (!found && this.categoryDomInfos.length > 0) {
- currentCategoryId = this.categoryDomInfos[0].id;
- }
- // 只有当分类真正改变时才更新,避免频繁触发
- if (currentCategoryId && this.type !== currentCategoryId) {
- console.log('滚动激活分类:', currentCategoryId);
- this.type = currentCategoryId;
- this.lastActiveCategory = currentCategoryId;
- this.$forceUpdate();
- }
- });
- },
- // 优化滚动到分类的方法
- scrollToCategory(categoryId) {
- this.isManualScroll = true;
- const query = uni.createSelectorQuery().in(this);
- const selector = `#scroll${categoryId}`;
- query.select(selector).boundingClientRect();
- query.selectViewport().scrollOffset();
- query.exec((res) => {
- if (res[0] && res[1]) {
- const categoryRect = res[0];
- const scrollTop = res[1].scrollTop;
- // 获取吸顶区域总高度
- const stickyHeight = this.getStickyHeight();
- // 计算目标位置(考虑吸顶高度和缓冲)
- const targetScrollTop = scrollTop + categoryRect.top - stickyHeight + 2;
- // 执行滚动
- uni.pageScrollTo({
- scrollTop: targetScrollTop,
- duration: 300,
- success: () => {
- // 滚动完成后,更新左侧激活状态
- this.type = categoryId;
- this.lastActiveCategory = categoryId;
- // 延迟恢复手动滚动标记
- setTimeout(() => {
- this.isManualScroll = false;
- }, 400);
- },
- fail: () => {
- this.isManualScroll = false;
- }
- });
- } else {
- this.isManualScroll = false;
- }
- });
- },
- // 获取吸顶区域总高度(精确计算版)
- getStickyHeight() {
- // 导航栏高度(rpx 转 px)
- const systemInfo = uni.getSystemInfoSync();
- const pixelRatio = systemInfo.pixelRatio || 2;
- const screenWidth = systemInfo.windowWidth || 375;
- const rpxToPx = screenWidth / 750;
- // 导航栏高度(从组件获取,单位 rpx)
- const navHeightRpx = this.newNavHeight || 88;
- // 商家信息区域高度(动态获取或估算)
- let shopInfoHeightRpx = 0;
- try {
- const query = uni.createSelectorQuery().in(this);
- query.select('.shop-info').boundingClientRect();
- // 同步获取可能失败,使用估算值
- shopInfoHeightRpx = 140; // 默认估算值
- } catch (e) {
- shopInfoHeightRpx = 140;
- }
- // 分类 tab 高度
- const tabHeightRpx = 48;
- // 额外间距
- const extraPaddingRpx = 8;
- // 总高度(rpx)
- const totalHeightRpx = navHeightRpx;
- // 转换为 px(与 pageScrollTo 保持一致)
- return totalHeightRpx * rpxToPx;
- },
- // 优化选择分类的方法
- selectType(item) {
- if (this.type !== item.category_id) {
- this.type = item.category_id;
- this.$forceUpdate();
- // 延迟执行滚动,确保DOM已更新
- setTimeout(() => {
- this.scrollToCategory(item.category_id);
- }, 100);
- }
- },
- shopInfo() {
- // 获取商家详情
- shopInfo().then((res) => {
- if (res.code === 1) {
- this.shopsInfo = res.data
- }
- })
- },
- getGoodsCategory() {
- //获取商家分类
- getGoodsCategory().then((res) => {
- if (res.code === 1) {
- if (res.data.length > 0) {
- this.categoryList = res.data
- this.type = res.data[0].category_id
- }
- }
- })
- },
- getGoodsGoods() {
- //获取商家商品列表
- getGoodsGoods().then((res) => {
- if (res.code === 1) {
- // 初始化商品列表
- this.goodsList = res.data
- // 从购物车数据中回显每个商品的数量
- this.updateGoodsNumFromCart()
- setTimeout(() => {
- this.initCategoryPositions();
- }, 300);
- }
- })
- },
- getShopIngList() {
- // 获取购物车列表
- const shopCart = cartStore.getShopCart(this.shop_id);
- if (shopCart) {
- this.cartData = {
- ...this.cartData,
- total_price: shopCart.total_price,
- total_num: shopCart.total_num,
- pack_price: shopCart.pack_price, //配送费总额
- total_goods_money: shopCart.total_goods_money, //商品价格总额
- goods_list: shopCart.goods_list
- };
- // 同步更新商品列表中的数量
- this.updateGoodsNumFromCart();
- }
- },
- getshopInfo() {
- this.table_number = uni.getStorageSync('table_number')
- this.shopInfo()
- this.getGoodsCategory()
- this.getGoodsGoods()
- },
- updateGoodsNumFromCart() {
- // 从购物车数据同步商品数量
- if (!this.goodsList) return;
- const shopCart = cartStore.getShopCart(this.shop_id);
- if (!shopCart) return;
- // 创建商品数量映射
- const goodsQuantityMap = {};
- const cartGoodsPackPriceMap = {}; // 存储商品配送费信息
- shopCart.goods_list.forEach(item => {
- if (!goodsQuantityMap[item.goods_id]) {
- goodsQuantityMap[item.goods_id] = 0;
- cartGoodsPackPriceMap[item.goods_id] = 0;
- }
- // 累加同一个商品不同规格的数量
- goodsQuantityMap[item.goods_id] += item.goods_number;
- // 累加同一个商品不同规格的配送费(按数量计算)
- cartGoodsPackPriceMap[item.goods_id] += parseFloat(item.pack_price || 0) * (item
- .goods_number || 0);
- });
- // 更新商品列表数量
- this.goodsList.forEach(category => {
- if (category.list && category.list.length > 0) {
- category.list.forEach(goods => {
- goods.num = goodsQuantityMap[goods.goods_id] || 0;
- });
- }
- });
- // 更新分类列表中的商品数量
- this.updateCategoryNum()
- },
- updateCategoryNum() {
- // 更新分类的商品总数
- if (!this.categoryList || !this.goodsList) {
- return
- }
- this.categoryList.forEach(categoryItem => {
- // 找到对应的商品分类
- const goodsCategory = this.goodsList.find(
- category => category.type === categoryItem.category_id
- )
- if (goodsCategory && goodsCategory.list) {
- // 计算该分类下所有商品的总数量
- const totalNum = goodsCategory.list.reduce((sum, goods) => {
- return sum + (goods.num || 0)
- }, 0)
- categoryItem.num = totalNum
- } else {
- categoryItem.num = 0
- }
- })
- },
- goToOrder() {
- uni.navigateTo({
- url: 'pages/order/index'
- })
- },
- goSearch() {
- uni.navigateTo({
- url: 'pages/index/search-record'
- })
- },
- // selectType(item, itemTwo) {
- // if (this.type != item.category_id) {
- // this.type = item.category_id;
- // this.idMark = 'scroll' + item.category_id
- // // 立即更新
- // this.$forceUpdate();
- // // 延迟执行滚动
- // setTimeout(() => {
- // this.scrollToCategorySimple(item.category_id);
- // }, 100);
- // }
- // },
- // 滚动方法
- scrollToCategorySimple(type) {
- const query = uni.createSelectorQuery();
- const selector = `#scroll${type}`;
- uni.pageScrollTo({
- selector: selector,
- duration: 300,
- });
- },
- addred(type, item) {
- if (type === 1) {
- // 减
- --item.num;
- if (item.num >= 0) {
- this.shoppingSave(item, item.num)
- }
- } else if (type === 2) {
- // 加
- ++item.num;
- this.shoppingSave(item, item.num)
- } else if (type === 3) {
- // 选规格
- uni.navigateTo({
- url: `pages-takeout/new-specs-info?goods_id=${item.goods_id}&category_id=${item.category_id}&shop_id=${this.shop_id}`
- })
- }
- },
- shoppingSave(item, num) {
- // 前端处理购物车
- if (num <= 0) {
- // 数量为0,移除商品
- cartStore.removeFromCart(item.goods_id, item.goods_spec || []);
- } else {
- // 更新商品数量
- const success = cartStore.updateQuantity(
- item.goods_id,
- item.goods_spec || [],
- num
- );
- if (!success) {
- // 如果更新失败,尝试添加新商品
- cartStore.addToCart(item, item.goods_spec || [], num);
- }
- }
- // 更新页面数据
- this.getShopIngList();
- this.$forceUpdate();
- },
- findGoodsInList(goodsId) {
- // 在商品列表中查找指定商品
- for (let category of this.goodsList) {
- if (category.list) {
- const goods = category.list.find(item => item.goods_id === goodsId)
- if (goods) {
- return goods
- }
- }
- }
- return null
- },
- navHeight(e) {
- this.newNavHeight = e
- },
- setShopIngDel() {
- uni.showModal({
- title: '提示',
- content: '是否清除购物车商品?',
- success: (res) => {
- if (res.confirm) {
- cartStore.clearCurrentShopCart();
- this.getShopIngList();
- this.$refs.popupBuy.close();
- uni.showToast({
- title: '已清空购物车',
- icon: 'success'
- });
- }
- }
- });
- },
- setShoppingSave(item, type) {
- if (type === 1) {
- // 加
- item.goods_number = item.goods_number + 1
- } else {
- // 减
- item.goods_number = item.goods_number - 1
- }
- if (item.goods_number <= 0) {
- // 数量为0,移除商品
- cartStore.removeFromCart(item.goods_id, item.goods_spec || []);
- } else {
- // 更新商品数量
- cartStore.updateQuantity(
- item.goods_id,
- item.goods_spec || [],
- item.goods_number
- );
- }
- // 更新页面数据
- this.getShopIngList();
- },
- setCartData(cartData) {
- this.cartData = cartData
- this.getDeliveryMoney()
- },
- getDeliveryMoney() {
- getDeliveryMoney({
- 'school_id': this.operate_id,
- 'shop_id': this.shop_id,
- }).then(res => {
- if (res.code === 1) {
- this.delivery_money = res.data.delivery_money * 1
- }
- })
- },
- toBack() {
- tools.leftClick()
- },
- getDomH(num) {
- this.$nextTick(() => {
- let info = uni.createSelectorQuery().select('#shopInfoBox');
- info.boundingClientRect((data) => {
- if (num === 1) {
- this.domHeigjt = data.height + 72
- } else {
- this.domHeigjt = data.height + 72 + 48
- }
- }).exec()
- })
- },
- seletShopType(item) {
- if (this.shopType != item.id) {
- this.shopType = item.id;
- if (this.shopType == 1) {
- this.shopTypeBsLeft = 12
- } else if (this.shopType == 2) {
- this.shopTypeBsLeft = 150
- } else {
- this.shopTypeBsLeft = 160 * 2
- }
- }
- },
- openBuyCrad() {
- this.$refs.popupBuy.open()
- },
- createOrder() {
- this.$refs.popupBuy.close()
- },
- toConfirmOrder() {
- uni.navigateTo({
- url: '/pages-takeout/goods-order'
- })
- },
- shopInfo() {
- const operate_id = uni.getStorageSync('operate_id')
- shopInfo({
- operate_id: operate_id,
- shop_id: this.shop_id
- }).then((res) => {
- if (res.code == 1) {
- this.shopsInfo = res.data
- } else {
- tools.error(res.msg)
- setTimeout(() => {
- uni.navigateBack({
- delta: 1
- })
- }, 1500)
- }
- })
- }
- },
- }
- </script>
- <style scoped lang="scss">
- .min-h {
- min-height: 152rpx;
- }
- .car-scroll-box {
- padding: 12rpx 24rpx calc(env(safe-area-inset-bottom) + 48rpx);
- }
- .zh-icon {
- width: 32rpx;
- height: 32rpx;
- }
- .shp-list-img {
- width: 40rpx;
- height: 40rpx;
- }
- .dd-box {
- width: 144rpx;
- padding: 12rpx 0;
- background: rgba(0, 0, 0, 0.14);
- }
- .ss-box {
- width: 64rpx;
- height: 64rpx;
- background: rgba(0, 0, 0, 0.14);
- }
- .shop-search {
- width: 40rpx;
- height: 40rpx;
- }
- .wh-44 {
- width: 44rpx;
- height: 44rpx;
- }
- .blank-text {
- line-height: 32vh;
- }
- .box-title {
- position: relative;
- }
- .box-close {
- position: absolute;
- right: 22rpx;
- }
- .pop-coupon {
- width: 100%;
- height: 42vh;
- background-color: #F5F4F9;
- border-radius: 24rpx 24rpx 0rpx 0rpx;
- padding-top: 32rpx !important;
- display: flex;
- flex-direction: column;
- }
- .item-right {
- padding: 24rpx;
- box-sizing: border-box;
- border-radius: 20rpx 0 0 20rpx;
- position: relative;
- }
- .item-hr {
- width: 0px;
- height: auto;
- border-right: 1rpx dashed #FB7850;
- }
- .item-hr-two {
- width: 0px;
- height: auto;
- border-right: 1rpx dashed #999;
- }
- .item-left {
- width: 220rpx;
- height: auto;
- padding: 20rpx 24rpx;
- box-sizing: border-box;
- position: relative;
- border-radius: 0 20rpx 20rpx 0;
- }
- .but-box {
- width: 152rpx;
- height: 52rpx;
- line-height: 52rpx;
- text-align: center;
- background: linear-gradient(225deg, #FB3D03 0%, #FF6901 100%);
- border-radius: 200rpx;
- }
- .ball {
- width: 24rpx;
- height: 24rpx;
- border-radius: 50%;
- position: absolute;
- top: -12rpx;
- left: -12rpx;
- background: #F5F5F5;
- z-index: 2;
- }
- .ball-two {
- width: 24rpx;
- height: 24rpx;
- border-radius: 50%;
- position: absolute;
- background: #F5F5F5;
- z-index: 10;
- bottom: -12rpx;
- left: -12rpx;
- }
- .shop-index {
- width: 100%;
- min-height: 100vh;
- height: auto;
- position: relative;
- }
- .right-only-right-red {
- width: 44rpx;
- height: 44rpx;
- line-height: 40rpx;
- border-radius: 12rpx;
- border: 1rpx solid #41ABF5;
- color: #41ABF5;
- }
- .right-only-right-add {
- width: 44rpx;
- height: 44rpx;
- line-height: 40rpx;
- background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
- border-radius: 12rpx;
- }
- .right-only-right-2 {
- min-width: 50rpx;
- }
- .w-514 {
- width: 480rpx;
- }
- .bg-img {
- position: fixed;
- top: 0;
- left: 0;
- width: 100%;
- height: 300rpx;
- transition: linear .3s all;
- // height: 300rpx;
- }
- .bg-img-type {
- position: fixed;
- top: 0;
- left: 0;
- width: 100%;
- // opacity: 100;
- transition: linear .3s all;
- }
- .mb-box {
- position: absolute;
- top: 0;
- left: 0;
- width: 100%;
- height: 300rpx;
- z-index: -1;
- }
- .nav-img {
- width: 56rpx;
- height: 56rpx;
- }
- .shop-buttom-money {
- width: 100%;
- // height: 140rpx;
- background: rgba(255, 255, 255, 0.8);
- padding-left: 24rpx;
- padding-right: 24rpx;
- padding-bottom: calc(env(safe-area-inset-bottom) + 12rpx);
- padding-top: 22rpx;
- // padding: 20rpx 24rpx;
- position: fixed;
- left: 0;
- bottom: 0;
- z-index: 5;
- }
- .pop-shop-buttom-money {
- width: 100%;
- // height: 140rpx;
- background: #fff;
- padding-left: 24rpx;
- padding-right: 24rpx;
- padding-bottom: calc(env(safe-area-inset-bottom) + 12rpx);
- padding-top: 22rpx;
- position: fixed;
- left: 0;
- bottom: 0;
- z-index: 5;
- }
- .shop-buttom-num {
- // width: 32rpx;
- height: 32rpx;
- line-height: 32rpx;
- border-radius: 20rpx;
- padding: 0 10rpx;
- position: absolute;
- top: 0rpx;
- left: 85rpx;
- border: 1rpx solid #FFFFFF;
- }
- .shop-buttom-money-2 {
- width: 200rpx;
- height: 80rpx;
- line-height: 80rpx;
- background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
- }
- .info-box-top {}
- .box-top-right {
- width: 100rpx;
- height: 100rpx;
- border-radius: 12rpx;
- }
- .box-top-left {}
- .top-left-1 {
- width: 480rpx;
- margin-bottom: 8rpx;
- }
- .img-x {
- width: 24rpx;
- height: 24rpx;
- }
- .buy-card {
- height: 60vh;
- border-radius: 24rpx 24rpx 0rpx 0rpx;
- }
- .buy-card-top {
- width: 100%;
- height: 92rpx;
- background: #FCF5DB;
- border-radius: 24rpx 24rpx 0rpx 0rpx;
- }
- .buy-card-item {
- width: 100%;
- padding: 20rpx 20rpx 0;
- // height: 176rpx;
- margin-bottom: 32rpx;
- }
- .goods_img {
- width: 152rpx;
- height: 152rpx;
- border-radius: 12rpx;
- margin-right: 20rpx;
- }
- .shop-buttom-money-1 {
- width: 100%;
- height: 96rpx;
- position: relative;
- box-shadow: 0rpx 8rpx 56rpx 0rpx rgba(154, 152, 152, 0.34);
- }
- .gwc-img {
- width: 88rpx;
- height: 88rpx;
- }
- .head-box {
- width: 100%;
- position: relative;
- }
- .shop-info-box {
- box-shadow: 0rpx 8rpx 56rpx 0rpx rgba(154, 152, 152, 0.34);
- }
- .info-box-quan-list {
- width: 164rpx;
- height: 48rpx;
- margin: 0 8rpx 8rpx 0;
- position: relative;
- image {
- width: 164rpx;
- height: 48rpx;
- position: absolute;
- top: 0;
- left: 0;
- z-index: 1;
- }
- }
- .quan-list-1 {
- position: absolute;
- top: 3rpx;
- left: 8rpx;
- z-index: 2;
- }
- .quan-list-2 {
- position: absolute;
- top: 8rpx;
- right: 22rpx;
- z-index: 2;
- }
- .shop-type-list {
- // height: 88rpx;
- position: relative;
- }
- .shop-item {
- position: relative;
- }
- .pj-num {
- position: absolute;
- right: -80rpx;
- top: 0;
- text-align: left;
- width: 80rpx;
- }
- .shop-item-bs {
- position: absolute;
- bottom: 0;
- width: 40rpx;
- height: 6rpx;
- border-radius: 20rpx;
- transition: left 0.5s;
- }
- .sticky-nav {
- width: 100%;
- position: sticky;
- z-index: 3;
- }
- .wh-44 {
- width: 44rpx;
- height: 44rpx;
- }
- .menu-box-left-f {
- position: sticky;
- // transition: all .3s ease;
- // overflow: auto;
- }
- .menu-box-left {
- width: 160rpx;
- height: auto;
- position: sticky;
- overflow: auto;
- // transition: all .3s ease;
- // height: 100%;
- // background-color: #F7F8FA;
- // position: absolute;
- // left: 0;
- // top: 0;
- // border-radius: 0rpx 20rpx 0 0rpx;
- }
- .menu-box-left-box {
- overflow-y: auto;
- }
- .menu-type-list {
- width: 160rpx;
- min-height: 108rpx;
- padding: 18rpx 28rpx;
- position: relative;
- z-index: 10;
- border-radius: 0rpx 20rpx 20rpx 0rpx;
- .menu-type-num {
- position: absolute;
- top: 2rpx;
- right: 8rpx;
- padding: 0 6rpx;
- min-width: 20rpx;
- height: 32rpx;
- text-align: center;
- line-height: 32rpx;
- background: #FE6B2C;
- border-radius: 50%;
- }
- }
- .menu-type-list-f {
- // width: 160rpx;
- background-color: #007AFF;
- width: 100%;
- height: 100%;
- overflow-y: auto;
- /* 如果分类项很多,允许内部滚动 */
- -webkit-overflow-scrolling: touch;
- /* 改善移动端滚动体验 */
- // height: 100%;
- }
- .menu-box-only {
- // flex: 1;
- width: calc(100% - 160rpx);
- }
- .menu-box-right {
- // flex: 1;
- width: 100%;
- height: 100%;
- // padding: 16rpx 0;
- // box-sizing: border-box;
- // position: relative;
- }
- .menu-list {
- width: 100%;
- padding: 0 20rpx 40rpx;
- // height: 200rpx;
- // margin-bottom: 40rpx;
- .goods_img {
- width: 168rpx;
- height: 168rpx;
- border-radius: 12rpx;
- margin-right: 16rpx;
- }
- .menu-list-right {
- flex: 1;
- .right-only-right {
- width: 116rpx;
- height: 44rpx;
- background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
- line-height: 44rpx;
- position: relative;
- .selectedNum {
- position: absolute;
- right: -10rpx;
- top: -22rpx;
- font-size: 24rpx;
- color: #fff;
- width: 32rpx;
- height: 32rpx;
- line-height: 32rpx;
- background: #FE6B2C;
- border-radius: 50%;
- border: 1rpx solid #FFFFFF;
- }
- }
- .right-only-right-red {
- width: 44rpx;
- height: 44rpx;
- line-height: 40rpx;
- border-radius: 12rpx;
- border: 1rpx solid #41ABF5;
- color: #41ABF5;
- }
- .right-only-right-add {
- width: 44rpx;
- height: 44rpx;
- line-height: 40rpx;
- background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
- border-radius: 12rpx;
- }
- .right-only-right-2 {
- min-width: 50rpx;
- }
- }
- }
- </style>
|