new-shop.vue 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. <template>
  2. <view class="shop-index iPhone-bottom flex-direction box-border-box">
  3. <image v-show="!navBgType" class="bg-img" src="@/static/img/order-food/shop-top.png" mode="aspectFill"
  4. :class="scrollTop > newNavHeight?'z-4':'z-3'"></image>
  5. <image v-show="navBgType" class="bg-img-type z-4" src="@/static/img/order-food/shop-top.png" mode="aspectFill"
  6. :style="{height:newNavHeight+'rpx',opacity:opacity}"></image>
  7. <newNav :leftShow="false" :textType='false' class="z-4 mb-32" @navHeight='navHeight'>
  8. <view slot="textContentSlot" class="align-items-center flex-justify-end">
  9. <view class="ss-box b-rad-50 mr-24 align-items-center flex-justify-center z-4" @click.stop="goSearch">
  10. <image class="shop-search" src="@/static/img/order-food/shop-search.png" mode=""></image>
  11. </view>
  12. <view class="dd-box align-items-center flex-justify-center b-rad-200 mr-24 z-4" @click="goToOrder">
  13. <image class="shp-list-img mr-8" src="@/static/img/order-food/shp-list.png" mode=""></image>
  14. <view class="fs-28 fc-f">
  15. 订单
  16. </view>
  17. </view>
  18. </view>
  19. </newNav>
  20. <view class="bgc-f box-border-box plr-32 mb-32 shop-info">
  21. <view class="shop-info-box bgc-f box-border-box p-24 z-3 b-rad-20">
  22. <view class="fs-40 fw-600 text-overflow-ellipsis">
  23. {{shopsInfo.shop_name || ''}}
  24. </view>
  25. <view class="fs-24 fw-400 fc-9 text-overflow-ellipsis-one pt-15">
  26. 公告:{{shopsInfo.notice || '暂无公告'}}
  27. </view>
  28. </view>
  29. </view>
  30. <view class="b-b-eee box-border-box p-0-32-12 align-items-center flex-justify-space z-3 shop-type-box">
  31. <view class="align-items-center shop-type-list">
  32. <view class="shop-item fs-36 mb-8 mr-64" @click="seletShopType(item)"
  33. :class="shopType === item.id?'fc-3 fw-600':'fc-6'" v-for="(item,i) in shopTypeList" :key="i">
  34. {{item.name}}
  35. </view>
  36. <view class="shop-item-bs bgc-FF3333" :style="{left:shopTypeBsLeft+'rpx'}"></view>
  37. </view>
  38. <view class="align-items-center">
  39. <image class="zh-icon mr-8" src="@/static/img/order-food/zh-icon.png" mode="aspectFill"></image>
  40. <view class="fw-600 fs-32 fc-3">
  41. 桌号:{{table_number}}
  42. </view>
  43. </view>
  44. </view>
  45. <!-- <view class="bgc-f sticky-nav" :style="{ height: navBgType?`calc(100vh - ${newNavHeight}rpx - ${48}rpx)`:'auto'}"> -->
  46. <view class="bgc-F7F8FA">
  47. <view class="menu-box box-border-box align-items-flex-start" v-if="shopType === 1">
  48. <view class="menu-box-left" :style="{top: newNavHeight + 'rpx'}">
  49. <view class="menu-box-left-box pb-130 box-border-box"
  50. :style="{height: menuLeftType?`calc(100vh - ${newNavHeight}rpx - ${68}rpx)`:''}">
  51. <view class="menu-type-list fs-26 box-border-box align-items-center flex-justify-center"
  52. v-for="(item,i) in categoryList" :key="i" @click="selectType(item)"
  53. :class="type === item.category_id?'bgc-f fc-3 fw-600':'fc-6 fw-400'"
  54. :id="'left-scroll'+ item.category_id">
  55. {{item.name}}
  56. <view class="menu-type-num fc-f" v-show="item.num > 0">{{item.num > 99?'99+':item.num}}
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. <view class="menu-box-only bgc-f pb-130 box-border-box">
  62. <view class="" v-for="(item,i) in goodsList" :key="i" :id="'scroll'+ item.type">
  63. <view class="fs-28 fw-600 fc-3 sticky-nav bgc-f p-20 box-border-box"
  64. :style="{top: newNavHeight + 'rpx'}">
  65. {{item.name}}
  66. </view>
  67. <view class="" v-if="item.list <= 0">
  68. <EnBlank message='暂无商品' :customSrc='true'>
  69. <template v-slot:blankImg>
  70. <image class="black-custom-img" src="@/static/img/common/no-order.png" mode="">
  71. </image>
  72. </template>
  73. </EnBlank>
  74. </view>
  75. <view class="menu-list flex-justify-space box-border-box" v-for="(info,k) in item.list" :key="k"
  76. :id="'goods_id'+ info.goods_id">
  77. <image class="flex-shrink0 goods_img b-rad-12" :src="info.goods_image" mode="scaleToFill">
  78. </image>
  79. <view class="menu-list-right flex-direction flex-justify-space">
  80. <view class="mb-8">
  81. <view class="mb-4 goods-name text-overflow-ellipsis fs-32 fw-600">
  82. {{info.goods_name}}
  83. </view>
  84. <!-- <view class="text-overflow-ellipsis fw-400 fc-9 fs-24 mb-2 goods-name"
  85. v-if="info.spec_type == 1">
  86. {{info.goods_desc?info.goods_desc:''}}
  87. </view> -->
  88. </view>
  89. <view class="flex-justify-space align-items-flex-end">
  90. <view class="">
  91. <span class="fs-24 fw-400 fc-3">¥</span>
  92. <span
  93. class="fs-40 fw-b fc-3">{{info.seckill_money>0?info.seckill_money:info.price}}</span>
  94. <span class="fs-24 fc-9">/份起</span>
  95. </view>
  96. <view class="display-flex align-items-center" v-if="info.spec_type == 1">
  97. <image v-show="info.num > 0" class="wh-44" @click="addred(1,info)"
  98. src="@/static/img/order-food/reduce-img.png" mode=""></image>
  99. <view v-show="info.num > 0"
  100. class="right-only-right-2 text-align-center fs-28 fw-400">{{info.num}}
  101. </view>
  102. <image class="wh-44" src="@/static/img/order-food/add-img.png"
  103. @click="addred(2,info)" mode="">
  104. </image>
  105. </view>
  106. <view v-else class="right-only-right text-align-center b-rad-200"
  107. @click="addred(3,info)">
  108. <view class="fw-600 fs-28 fc-f">
  109. 选规格
  110. </view>
  111. <view class="selectedNum" v-if="info.num>0">
  112. {{info.num}}
  113. </view>
  114. </view>
  115. </view>
  116. </view>
  117. </view>
  118. </view>
  119. </view>
  120. </view>
  121. <ShopInfo style="height: 100%;" v-else-if="shopType === 2"></ShopInfo>
  122. </view>
  123. <view v-if="shopType === 1" class="shop-buttom-money box-border-box" @click="openBuyCrad()">
  124. <view
  125. class="shop-buttom-money-1 bgc-f flex-justify-space align-items-center pl-24 box-border-box pr-8 b-rad-200">
  126. <view class="align-items-center">
  127. <image class="gwc-img mr-36" src="@/static/img/order-food/gwc-img.png" mode="scaleToFill"></image>
  128. <view class="shop-buttom-num bgc-FE6B2C fs-24 fc-f text-align-center">{{cartData.total_num}}</view>
  129. <view class="align-items-baseline">
  130. <view class="fw-600 fs-28 fc-3 mr-4">¥</view>
  131. <view class="fw-600 fs-48 fc-3">{{cartData.total_goods_money}}</view>
  132. </view>
  133. </view>
  134. <view class="shop-buttom-money-2 text-align-center fc-f fs-32 fw-600 b-rad-200"
  135. :class="cartData.total_num>0?'':'opacity-4'" @click.stop="toConfirmOrder()">去结算
  136. </view>
  137. </view>
  138. </view>
  139. <uni-popup ref="popupBuy" type="bottom" background-color="transparent" borderRadius='20px 20rpx 0 0'>
  140. <view class="buy-card flex-index-css">
  141. <view class="buy-card-top box-border-box plr-32 flex-justify-space align-items-center b-rad-20-20-0-0">
  142. <view class="">
  143. <span class="fs-32 fc-3 mr-12">已选商品</span>
  144. </view>
  145. <view class="fs-28 fw-400 fc-9 align-items-center" @click.stop="setShopIngDel">
  146. <view class="">
  147. <uni-icons type="trash" size="18" color="#999"></uni-icons>
  148. </view>
  149. <view class="fs-28 fw-400 fc-9">清空</view>
  150. </view>
  151. </view>
  152. <view class="scroll-view-css bgc-f box-border-box car-scroll-box">
  153. <view class="buy-card-item b-rad-20 align-items-flex-start box-border-box"
  154. v-for="(goods,i) in cartData.goods_list" :key="i">
  155. <image class="goods_img flex-shrink0" :src="goods.goods_image" mode="scaleToFill"></image>
  156. <view class="flex-1 flex-direction flex-justify-space min-h b-b-2-F9F9F9 pb-24">
  157. <view class="">
  158. <view class="w-514 text-overflow-ellipsis-one fs-34 fw-600 mb-8">
  159. {{goods.goods_name}}
  160. </view>
  161. <view class="fc-6" v-show="goods.goods_attr">
  162. {{goods.goods_attr}}
  163. </view>
  164. </view>
  165. <view class="flex-justify-space">
  166. <view class="align-items-baseline">
  167. <view class="fs-24 fc-3 fw-400">¥</view>
  168. <priceDisplay :value="goods.price"></priceDisplay>
  169. <!-- <span class="">{{goods.price}}</span> -->
  170. </view>
  171. <view class="display-flex align-items-center">
  172. <image class="wh-44" @click.stop="setShoppingSave(goods,2)"
  173. src="@/static/img/order-food/reduce-img.png" mode="">
  174. </image>
  175. <view class="right-only-right-2 text-align-center fs-28 fw-400">
  176. {{goods.goods_number}}
  177. </view>
  178. <image class="wh-44" src="/static/img/order-food/add-img.png"
  179. @click.stop="setShoppingSave(goods,1)" mode=""></image>
  180. </view>
  181. </view>
  182. </view>
  183. </view>
  184. <en-blank navBackgroundColor='#ECECEC' v-if="cartData.goods_list.length<=0"
  185. message="快去选购商品吧!~"></en-blank>
  186. </view>
  187. <view class="pop-shop-buttom-money box-border-box" @click="openBuyCrad()">
  188. <view
  189. class="shop-buttom-money-1 bgc-f flex-justify-space align-items-center pl-24 box-border-box pr-8 b-rad-200">
  190. <view class="align-items-center">
  191. <image class="gwc-img mr-36" src="@/static/img/order-food/gwc-img.png" mode="scaleToFill">
  192. </image>
  193. <view class="shop-buttom-num bgc-FE6B2C fs-24 fc-f text-align-center">{{cartData.total_num}}
  194. </view>
  195. <view class="align-items-baseline">
  196. <view class="fw-600 fs-28 fc-3 mr-4">¥</view>
  197. <view class="fw-600 fs-48 fc-3">{{cartData.total_goods_money}}</view>
  198. </view>
  199. </view>
  200. <view class="shop-buttom-money-2 text-align-center fc-f fs-32 fw-600 b-rad-200"
  201. :class="cartData.total_num>0?'':'opacity-4'" @click.stop="toConfirmOrder()">去结算
  202. </view>
  203. </view>
  204. </view>
  205. </view>
  206. </uni-popup>
  207. </view>
  208. </template>
  209. <script>
  210. import priceDisplay from '@/components/price-display.vue';
  211. import ShopInfo from './shop-mod/info.vue';
  212. import tools from "@/service/tools";
  213. import EnBlank from "@/components/en-utils/en-blank/en-blank";
  214. import cartStore from '@/utils/cartStore.js';
  215. import {
  216. getGoodsCategory,
  217. getGoodsGoods
  218. } from '@/api/goods.js'
  219. import {
  220. receiveCoupon
  221. } from "@/api/coupon.js"
  222. import {
  223. shopInfo
  224. } from "@/api/shop.js"
  225. import {
  226. getDeliveryMoney
  227. } from "@/api/common";
  228. import {
  229. getShopIngList,
  230. setShopIngDel,
  231. shoppingSave
  232. } from "@/api/shopping";
  233. export default {
  234. components: {
  235. ShopInfo,
  236. EnBlank,
  237. priceDisplay,
  238. },
  239. data() {
  240. return {
  241. domHeigjt: 0,
  242. shopTypeList: [{
  243. name: "点单",
  244. id: 1
  245. }, {
  246. name: "商家",
  247. id: 2
  248. }],
  249. table_number: '',
  250. shopType: 1, //组件类型
  251. shopTypeBsLeft: 12,
  252. shop_id: '',
  253. operate_id: '',
  254. shopsInfo: {},
  255. isAjax: false,
  256. newNavHeight: '',
  257. navBgType: false,
  258. menuLeftType: false,
  259. scrollTop: 0,
  260. opacity: 0,
  261. heightOne: '',
  262. heightTwo: '',
  263. topHeight: '',
  264. idMark: '', //滚动自适应参数
  265. type: '', //默认分类标识
  266. categoryList: [],
  267. goodsList: [],
  268. cartData: {
  269. total_price: 0,
  270. total_num: 0,
  271. pack_price: 0,
  272. total_goods_money: 0,
  273. goods_list: [],
  274. },
  275. categoryTops: [], // 存储每个分类的top位置
  276. isScrolling: false, // 防止滚动事件冲突
  277. activeScroll: false, // 是否正在滚动
  278. scrollTimer: null, // 滚动计时器
  279. observer: null, // IntersectionObserver实例
  280. categoryDomInfos: [], // 存储分类DOM信息
  281. isManualScroll: false, // 是否是手动触发滚动
  282. lastActiveCategory: '', // 记录上一次激活的分类,避免重复更新
  283. }
  284. },
  285. // 在onUnload中销毁监听
  286. onUnload() {
  287. if (this.observer) {
  288. this.observer.disconnect();
  289. this.observer = null;
  290. }
  291. if (this.scrollTimer) {
  292. clearTimeout(this.scrollTimer);
  293. }
  294. },
  295. onShow() {
  296. this.getShopIngList();
  297. // 重新计算分类位置
  298. setTimeout(() => {
  299. this.initCategoryPositions();
  300. }, 300);
  301. },
  302. onLoad(e) {
  303. cartStore.setShopId();
  304. this.getshopInfo();
  305. },
  306. onReady() {
  307. // 确保DOM完全渲染后初始化位置
  308. setTimeout(() => {
  309. this.initCategoryPositions();
  310. }, 800);
  311. },
  312. mounted() {},
  313. onReachBottom(e) {},
  314. // 监听页面滚动
  315. onPageScroll(e) {
  316. // e.scrollTop 获取滚动条距离页面顶部的距离
  317. this.scrollTop = e.scrollTop
  318. // this.opacity = (Number(this.scrollTop) - 30)
  319. this.opacity = Math.min((this.scrollTop - 30) / 100, 1);
  320. // 可以根据滚动位置执行相应逻辑
  321. if (e.scrollTop > 25) {
  322. this.navBgType = true
  323. } else {
  324. this.navBgType = false
  325. }
  326. if (e.scrollTop > (Number(this.newNavHeight) - 4)) {
  327. this.menuLeftType = true
  328. } else {
  329. this.menuLeftType = false
  330. }
  331. // 优化:只在非手动滚动且菜单固定时更新激活分类
  332. if (!this.isManualScroll && this.menuLeftType) {
  333. this.updateActiveCategoryOnScroll();
  334. }
  335. },
  336. methods: {
  337. // 初始化分类位置信息
  338. initCategoryPositions() {
  339. this.$nextTick(() => {
  340. const query = uni.createSelectorQuery().in(this);
  341. // 清空之前的 DOM 信息
  342. this.categoryDomInfos = [];
  343. this.categoryTops = [];
  344. // 收集所有分类的位置信息
  345. this.goodsList.forEach((category, index) => {
  346. if (category.type) {
  347. query.select(`#scroll${category.type}`).boundingClientRect();
  348. }
  349. });
  350. query.exec((res) => {
  351. if (!res || res.length === 0) return;
  352. this.categoryDomInfos = res.map((rect, index) => {
  353. if (rect && this.goodsList[index]) {
  354. const categoryInfo = {
  355. id: this.goodsList[index].type,
  356. top: rect.top,
  357. height: rect.height,
  358. index: index
  359. };
  360. this.categoryTops.push(categoryInfo);
  361. return categoryInfo;
  362. }
  363. return null;
  364. }).filter(item => item !== null);
  365. console.log('分类位置信息:', this.categoryDomInfos);
  366. });
  367. });
  368. },
  369. // 更新激活的分类
  370. updateActiveCategoryOnScroll() {
  371. if (this.scrollTimer) {
  372. clearTimeout(this.scrollTimer);
  373. }
  374. this.scrollTimer = setTimeout(() => {
  375. this.findCurrentCategory();
  376. }, 150); // 增加防抖时间
  377. },
  378. // 查找当前应该激活的分类
  379. findCurrentCategory() {
  380. if (this.categoryDomInfos.length === 0 || !this.goodsList) return;
  381. const query = uni.createSelectorQuery().in(this);
  382. query.selectViewport().scrollOffset();
  383. query.exec((res) => {
  384. if (!res || !res[0]) return;
  385. const scrollTop = res[0].scrollTop;
  386. // 获取吸顶区域总高度
  387. const stickyHeight = this.getStickyHeight();
  388. // 阈值位置:视口顶部 + 吸顶高度 + 缓冲区域(分类标题高度的 1/3)
  389. const threshold = scrollTop + stickyHeight + 10;
  390. // 找当前应该激活的分类
  391. let currentCategoryId = this.categoryList[0]?.category_id;
  392. let found = false;
  393. // 从后往前遍历,找到最后一个在阈值上方的分类
  394. for (let i = this.categoryDomInfos.length - 1; i >= 0; i--) {
  395. const category = this.categoryDomInfos[i];
  396. if (category) {
  397. // 元素顶部位置(相对于视口)+ 滚动距离 = 相对于页面顶部的位置
  398. const elementTop = category.top + scrollTop;
  399. // 如果元素顶部在阈值上方或刚好对齐
  400. if (elementTop <= threshold) {
  401. currentCategoryId = category.id;
  402. found = true;
  403. break;
  404. }
  405. }
  406. }
  407. // 如果都没找到,说明第一个分类已经滚动过去了,使用第一个分类
  408. if (!found && this.categoryDomInfos.length > 0) {
  409. currentCategoryId = this.categoryDomInfos[0].id;
  410. }
  411. // 只有当分类真正改变时才更新,避免频繁触发
  412. if (currentCategoryId && this.type !== currentCategoryId) {
  413. console.log('滚动激活分类:', currentCategoryId);
  414. this.type = currentCategoryId;
  415. this.lastActiveCategory = currentCategoryId;
  416. this.$forceUpdate();
  417. }
  418. });
  419. },
  420. // 优化滚动到分类的方法
  421. scrollToCategory(categoryId) {
  422. this.isManualScroll = true;
  423. const query = uni.createSelectorQuery().in(this);
  424. const selector = `#scroll${categoryId}`;
  425. query.select(selector).boundingClientRect();
  426. query.selectViewport().scrollOffset();
  427. query.exec((res) => {
  428. if (res[0] && res[1]) {
  429. const categoryRect = res[0];
  430. const scrollTop = res[1].scrollTop;
  431. // 获取吸顶区域总高度
  432. const stickyHeight = this.getStickyHeight();
  433. // 计算目标位置(考虑吸顶高度和缓冲)
  434. const targetScrollTop = scrollTop + categoryRect.top - stickyHeight + 2;
  435. // 执行滚动
  436. uni.pageScrollTo({
  437. scrollTop: targetScrollTop,
  438. duration: 300,
  439. success: () => {
  440. // 滚动完成后,更新左侧激活状态
  441. this.type = categoryId;
  442. this.lastActiveCategory = categoryId;
  443. // 延迟恢复手动滚动标记
  444. setTimeout(() => {
  445. this.isManualScroll = false;
  446. }, 400);
  447. },
  448. fail: () => {
  449. this.isManualScroll = false;
  450. }
  451. });
  452. } else {
  453. this.isManualScroll = false;
  454. }
  455. });
  456. },
  457. // 获取吸顶区域总高度(精确计算版)
  458. getStickyHeight() {
  459. // 导航栏高度(rpx 转 px)
  460. const systemInfo = uni.getSystemInfoSync();
  461. const pixelRatio = systemInfo.pixelRatio || 2;
  462. const screenWidth = systemInfo.windowWidth || 375;
  463. const rpxToPx = screenWidth / 750;
  464. // 导航栏高度(从组件获取,单位 rpx)
  465. const navHeightRpx = this.newNavHeight || 88;
  466. // 商家信息区域高度(动态获取或估算)
  467. let shopInfoHeightRpx = 0;
  468. try {
  469. const query = uni.createSelectorQuery().in(this);
  470. query.select('.shop-info').boundingClientRect();
  471. // 同步获取可能失败,使用估算值
  472. shopInfoHeightRpx = 140; // 默认估算值
  473. } catch (e) {
  474. shopInfoHeightRpx = 140;
  475. }
  476. // 分类 tab 高度
  477. const tabHeightRpx = 48;
  478. // 额外间距
  479. const extraPaddingRpx = 8;
  480. // 总高度(rpx)
  481. const totalHeightRpx = navHeightRpx;
  482. // 转换为 px(与 pageScrollTo 保持一致)
  483. return totalHeightRpx * rpxToPx;
  484. },
  485. // 优化选择分类的方法
  486. selectType(item) {
  487. if (this.type !== item.category_id) {
  488. this.type = item.category_id;
  489. this.$forceUpdate();
  490. // 延迟执行滚动,确保DOM已更新
  491. setTimeout(() => {
  492. this.scrollToCategory(item.category_id);
  493. }, 100);
  494. }
  495. },
  496. shopInfo() {
  497. // 获取商家详情
  498. shopInfo().then((res) => {
  499. if (res.code === 1) {
  500. this.shopsInfo = res.data
  501. }
  502. })
  503. },
  504. getGoodsCategory() {
  505. //获取商家分类
  506. getGoodsCategory().then((res) => {
  507. if (res.code === 1) {
  508. if (res.data.length > 0) {
  509. this.categoryList = res.data
  510. this.type = res.data[0].category_id
  511. }
  512. }
  513. })
  514. },
  515. getGoodsGoods() {
  516. //获取商家商品列表
  517. getGoodsGoods().then((res) => {
  518. if (res.code === 1) {
  519. // 初始化商品列表
  520. this.goodsList = res.data
  521. // 从购物车数据中回显每个商品的数量
  522. this.updateGoodsNumFromCart()
  523. setTimeout(() => {
  524. this.initCategoryPositions();
  525. }, 300);
  526. }
  527. })
  528. },
  529. getShopIngList() {
  530. // 获取购物车列表
  531. const shopCart = cartStore.getShopCart(this.shop_id);
  532. if (shopCart) {
  533. this.cartData = {
  534. ...this.cartData,
  535. total_price: shopCart.total_price,
  536. total_num: shopCart.total_num,
  537. pack_price: shopCart.pack_price, //配送费总额
  538. total_goods_money: shopCart.total_goods_money, //商品价格总额
  539. goods_list: shopCart.goods_list
  540. };
  541. // 同步更新商品列表中的数量
  542. this.updateGoodsNumFromCart();
  543. }
  544. },
  545. getshopInfo() {
  546. this.table_number = uni.getStorageSync('table_number')
  547. this.shopInfo()
  548. this.getGoodsCategory()
  549. this.getGoodsGoods()
  550. },
  551. updateGoodsNumFromCart() {
  552. // 从购物车数据同步商品数量
  553. if (!this.goodsList) return;
  554. const shopCart = cartStore.getShopCart(this.shop_id);
  555. if (!shopCart) return;
  556. // 创建商品数量映射
  557. const goodsQuantityMap = {};
  558. const cartGoodsPackPriceMap = {}; // 存储商品配送费信息
  559. shopCart.goods_list.forEach(item => {
  560. if (!goodsQuantityMap[item.goods_id]) {
  561. goodsQuantityMap[item.goods_id] = 0;
  562. cartGoodsPackPriceMap[item.goods_id] = 0;
  563. }
  564. // 累加同一个商品不同规格的数量
  565. goodsQuantityMap[item.goods_id] += item.goods_number;
  566. // 累加同一个商品不同规格的配送费(按数量计算)
  567. cartGoodsPackPriceMap[item.goods_id] += parseFloat(item.pack_price || 0) * (item
  568. .goods_number || 0);
  569. });
  570. // 更新商品列表数量
  571. this.goodsList.forEach(category => {
  572. if (category.list && category.list.length > 0) {
  573. category.list.forEach(goods => {
  574. goods.num = goodsQuantityMap[goods.goods_id] || 0;
  575. });
  576. }
  577. });
  578. // 更新分类列表中的商品数量
  579. this.updateCategoryNum()
  580. },
  581. updateCategoryNum() {
  582. // 更新分类的商品总数
  583. if (!this.categoryList || !this.goodsList) {
  584. return
  585. }
  586. this.categoryList.forEach(categoryItem => {
  587. // 找到对应的商品分类
  588. const goodsCategory = this.goodsList.find(
  589. category => category.type === categoryItem.category_id
  590. )
  591. if (goodsCategory && goodsCategory.list) {
  592. // 计算该分类下所有商品的总数量
  593. const totalNum = goodsCategory.list.reduce((sum, goods) => {
  594. return sum + (goods.num || 0)
  595. }, 0)
  596. categoryItem.num = totalNum
  597. } else {
  598. categoryItem.num = 0
  599. }
  600. })
  601. },
  602. goToOrder() {
  603. uni.navigateTo({
  604. url: 'pages/order/index'
  605. })
  606. },
  607. goSearch() {
  608. uni.navigateTo({
  609. url: 'pages/index/search-record'
  610. })
  611. },
  612. // selectType(item, itemTwo) {
  613. // if (this.type != item.category_id) {
  614. // this.type = item.category_id;
  615. // this.idMark = 'scroll' + item.category_id
  616. // // 立即更新
  617. // this.$forceUpdate();
  618. // // 延迟执行滚动
  619. // setTimeout(() => {
  620. // this.scrollToCategorySimple(item.category_id);
  621. // }, 100);
  622. // }
  623. // },
  624. // 滚动方法
  625. scrollToCategorySimple(type) {
  626. const query = uni.createSelectorQuery();
  627. const selector = `#scroll${type}`;
  628. uni.pageScrollTo({
  629. selector: selector,
  630. duration: 300,
  631. });
  632. },
  633. addred(type, item) {
  634. if (type === 1) {
  635. // 减
  636. --item.num;
  637. if (item.num >= 0) {
  638. this.shoppingSave(item, item.num)
  639. }
  640. } else if (type === 2) {
  641. // 加
  642. ++item.num;
  643. this.shoppingSave(item, item.num)
  644. } else if (type === 3) {
  645. // 选规格
  646. uni.navigateTo({
  647. url: `pages-takeout/new-specs-info?goods_id=${item.goods_id}&category_id=${item.category_id}&shop_id=${this.shop_id}`
  648. })
  649. }
  650. },
  651. shoppingSave(item, num) {
  652. // 前端处理购物车
  653. if (num <= 0) {
  654. // 数量为0,移除商品
  655. cartStore.removeFromCart(item.goods_id, item.goods_spec || []);
  656. } else {
  657. // 更新商品数量
  658. const success = cartStore.updateQuantity(
  659. item.goods_id,
  660. item.goods_spec || [],
  661. num
  662. );
  663. if (!success) {
  664. // 如果更新失败,尝试添加新商品
  665. cartStore.addToCart(item, item.goods_spec || [], num);
  666. }
  667. }
  668. // 更新页面数据
  669. this.getShopIngList();
  670. this.$forceUpdate();
  671. },
  672. findGoodsInList(goodsId) {
  673. // 在商品列表中查找指定商品
  674. for (let category of this.goodsList) {
  675. if (category.list) {
  676. const goods = category.list.find(item => item.goods_id === goodsId)
  677. if (goods) {
  678. return goods
  679. }
  680. }
  681. }
  682. return null
  683. },
  684. navHeight(e) {
  685. this.newNavHeight = e
  686. },
  687. setShopIngDel() {
  688. uni.showModal({
  689. title: '提示',
  690. content: '是否清除购物车商品?',
  691. success: (res) => {
  692. if (res.confirm) {
  693. cartStore.clearCurrentShopCart();
  694. this.getShopIngList();
  695. this.$refs.popupBuy.close();
  696. uni.showToast({
  697. title: '已清空购物车',
  698. icon: 'success'
  699. });
  700. }
  701. }
  702. });
  703. },
  704. setShoppingSave(item, type) {
  705. if (type === 1) {
  706. // 加
  707. item.goods_number = item.goods_number + 1
  708. } else {
  709. // 减
  710. item.goods_number = item.goods_number - 1
  711. }
  712. if (item.goods_number <= 0) {
  713. // 数量为0,移除商品
  714. cartStore.removeFromCart(item.goods_id, item.goods_spec || []);
  715. } else {
  716. // 更新商品数量
  717. cartStore.updateQuantity(
  718. item.goods_id,
  719. item.goods_spec || [],
  720. item.goods_number
  721. );
  722. }
  723. // 更新页面数据
  724. this.getShopIngList();
  725. },
  726. setCartData(cartData) {
  727. this.cartData = cartData
  728. this.getDeliveryMoney()
  729. },
  730. getDeliveryMoney() {
  731. getDeliveryMoney({
  732. 'school_id': this.operate_id,
  733. 'shop_id': this.shop_id,
  734. }).then(res => {
  735. if (res.code === 1) {
  736. this.delivery_money = res.data.delivery_money * 1
  737. }
  738. })
  739. },
  740. toBack() {
  741. tools.leftClick()
  742. },
  743. getDomH(num) {
  744. this.$nextTick(() => {
  745. let info = uni.createSelectorQuery().select('#shopInfoBox');
  746. info.boundingClientRect((data) => {
  747. if (num === 1) {
  748. this.domHeigjt = data.height + 72
  749. } else {
  750. this.domHeigjt = data.height + 72 + 48
  751. }
  752. }).exec()
  753. })
  754. },
  755. seletShopType(item) {
  756. if (this.shopType != item.id) {
  757. this.shopType = item.id;
  758. if (this.shopType == 1) {
  759. this.shopTypeBsLeft = 12
  760. } else if (this.shopType == 2) {
  761. this.shopTypeBsLeft = 150
  762. } else {
  763. this.shopTypeBsLeft = 160 * 2
  764. }
  765. }
  766. },
  767. openBuyCrad() {
  768. this.$refs.popupBuy.open()
  769. },
  770. createOrder() {
  771. this.$refs.popupBuy.close()
  772. },
  773. toConfirmOrder() {
  774. uni.navigateTo({
  775. url: '/pages-takeout/goods-order'
  776. })
  777. },
  778. shopInfo() {
  779. const operate_id = uni.getStorageSync('operate_id')
  780. shopInfo({
  781. operate_id: operate_id,
  782. shop_id: this.shop_id
  783. }).then((res) => {
  784. if (res.code == 1) {
  785. this.shopsInfo = res.data
  786. } else {
  787. tools.error(res.msg)
  788. setTimeout(() => {
  789. uni.navigateBack({
  790. delta: 1
  791. })
  792. }, 1500)
  793. }
  794. })
  795. }
  796. },
  797. }
  798. </script>
  799. <style scoped lang="scss">
  800. .min-h {
  801. min-height: 152rpx;
  802. }
  803. .car-scroll-box {
  804. padding: 12rpx 24rpx calc(env(safe-area-inset-bottom) + 48rpx);
  805. }
  806. .zh-icon {
  807. width: 32rpx;
  808. height: 32rpx;
  809. }
  810. .shp-list-img {
  811. width: 40rpx;
  812. height: 40rpx;
  813. }
  814. .dd-box {
  815. width: 144rpx;
  816. padding: 12rpx 0;
  817. background: rgba(0, 0, 0, 0.14);
  818. }
  819. .ss-box {
  820. width: 64rpx;
  821. height: 64rpx;
  822. background: rgba(0, 0, 0, 0.14);
  823. }
  824. .shop-search {
  825. width: 40rpx;
  826. height: 40rpx;
  827. }
  828. .wh-44 {
  829. width: 44rpx;
  830. height: 44rpx;
  831. }
  832. .blank-text {
  833. line-height: 32vh;
  834. }
  835. .box-title {
  836. position: relative;
  837. }
  838. .box-close {
  839. position: absolute;
  840. right: 22rpx;
  841. }
  842. .pop-coupon {
  843. width: 100%;
  844. height: 42vh;
  845. background-color: #F5F4F9;
  846. border-radius: 24rpx 24rpx 0rpx 0rpx;
  847. padding-top: 32rpx !important;
  848. display: flex;
  849. flex-direction: column;
  850. }
  851. .item-right {
  852. padding: 24rpx;
  853. box-sizing: border-box;
  854. border-radius: 20rpx 0 0 20rpx;
  855. position: relative;
  856. }
  857. .item-hr {
  858. width: 0px;
  859. height: auto;
  860. border-right: 1rpx dashed #FB7850;
  861. }
  862. .item-hr-two {
  863. width: 0px;
  864. height: auto;
  865. border-right: 1rpx dashed #999;
  866. }
  867. .item-left {
  868. width: 220rpx;
  869. height: auto;
  870. padding: 20rpx 24rpx;
  871. box-sizing: border-box;
  872. position: relative;
  873. border-radius: 0 20rpx 20rpx 0;
  874. }
  875. .but-box {
  876. width: 152rpx;
  877. height: 52rpx;
  878. line-height: 52rpx;
  879. text-align: center;
  880. background: linear-gradient(225deg, #FB3D03 0%, #FF6901 100%);
  881. border-radius: 200rpx;
  882. }
  883. .ball {
  884. width: 24rpx;
  885. height: 24rpx;
  886. border-radius: 50%;
  887. position: absolute;
  888. top: -12rpx;
  889. left: -12rpx;
  890. background: #F5F5F5;
  891. z-index: 2;
  892. }
  893. .ball-two {
  894. width: 24rpx;
  895. height: 24rpx;
  896. border-radius: 50%;
  897. position: absolute;
  898. background: #F5F5F5;
  899. z-index: 10;
  900. bottom: -12rpx;
  901. left: -12rpx;
  902. }
  903. .shop-index {
  904. width: 100%;
  905. min-height: 100vh;
  906. height: auto;
  907. position: relative;
  908. }
  909. .right-only-right-red {
  910. width: 44rpx;
  911. height: 44rpx;
  912. line-height: 40rpx;
  913. border-radius: 12rpx;
  914. border: 1rpx solid #41ABF5;
  915. color: #41ABF5;
  916. }
  917. .right-only-right-add {
  918. width: 44rpx;
  919. height: 44rpx;
  920. line-height: 40rpx;
  921. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  922. border-radius: 12rpx;
  923. }
  924. .right-only-right-2 {
  925. min-width: 50rpx;
  926. }
  927. .w-514 {
  928. width: 480rpx;
  929. }
  930. .bg-img {
  931. position: fixed;
  932. top: 0;
  933. left: 0;
  934. width: 100%;
  935. height: 300rpx;
  936. transition: linear .3s all;
  937. // height: 300rpx;
  938. }
  939. .bg-img-type {
  940. position: fixed;
  941. top: 0;
  942. left: 0;
  943. width: 100%;
  944. // opacity: 100;
  945. transition: linear .3s all;
  946. }
  947. .mb-box {
  948. position: absolute;
  949. top: 0;
  950. left: 0;
  951. width: 100%;
  952. height: 300rpx;
  953. z-index: -1;
  954. }
  955. .nav-img {
  956. width: 56rpx;
  957. height: 56rpx;
  958. }
  959. .shop-buttom-money {
  960. width: 100%;
  961. // height: 140rpx;
  962. background: rgba(255, 255, 255, 0.8);
  963. padding-left: 24rpx;
  964. padding-right: 24rpx;
  965. padding-bottom: calc(env(safe-area-inset-bottom) + 12rpx);
  966. padding-top: 22rpx;
  967. // padding: 20rpx 24rpx;
  968. position: fixed;
  969. left: 0;
  970. bottom: 0;
  971. z-index: 5;
  972. }
  973. .pop-shop-buttom-money {
  974. width: 100%;
  975. // height: 140rpx;
  976. background: #fff;
  977. padding-left: 24rpx;
  978. padding-right: 24rpx;
  979. padding-bottom: calc(env(safe-area-inset-bottom) + 12rpx);
  980. padding-top: 22rpx;
  981. position: fixed;
  982. left: 0;
  983. bottom: 0;
  984. z-index: 5;
  985. }
  986. .shop-buttom-num {
  987. // width: 32rpx;
  988. height: 32rpx;
  989. line-height: 32rpx;
  990. border-radius: 20rpx;
  991. padding: 0 10rpx;
  992. position: absolute;
  993. top: 0rpx;
  994. left: 85rpx;
  995. border: 1rpx solid #FFFFFF;
  996. }
  997. .shop-buttom-money-2 {
  998. width: 200rpx;
  999. height: 80rpx;
  1000. line-height: 80rpx;
  1001. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  1002. }
  1003. .info-box-top {}
  1004. .box-top-right {
  1005. width: 100rpx;
  1006. height: 100rpx;
  1007. border-radius: 12rpx;
  1008. }
  1009. .box-top-left {}
  1010. .top-left-1 {
  1011. width: 480rpx;
  1012. margin-bottom: 8rpx;
  1013. }
  1014. .img-x {
  1015. width: 24rpx;
  1016. height: 24rpx;
  1017. }
  1018. .buy-card {
  1019. height: 60vh;
  1020. border-radius: 24rpx 24rpx 0rpx 0rpx;
  1021. }
  1022. .buy-card-top {
  1023. width: 100%;
  1024. height: 92rpx;
  1025. background: #FCF5DB;
  1026. border-radius: 24rpx 24rpx 0rpx 0rpx;
  1027. }
  1028. .buy-card-item {
  1029. width: 100%;
  1030. padding: 20rpx 20rpx 0;
  1031. // height: 176rpx;
  1032. margin-bottom: 32rpx;
  1033. }
  1034. .goods_img {
  1035. width: 152rpx;
  1036. height: 152rpx;
  1037. border-radius: 12rpx;
  1038. margin-right: 20rpx;
  1039. }
  1040. .shop-buttom-money-1 {
  1041. width: 100%;
  1042. height: 96rpx;
  1043. position: relative;
  1044. box-shadow: 0rpx 8rpx 56rpx 0rpx rgba(154, 152, 152, 0.34);
  1045. }
  1046. .gwc-img {
  1047. width: 88rpx;
  1048. height: 88rpx;
  1049. }
  1050. .head-box {
  1051. width: 100%;
  1052. position: relative;
  1053. }
  1054. .shop-info-box {
  1055. box-shadow: 0rpx 8rpx 56rpx 0rpx rgba(154, 152, 152, 0.34);
  1056. }
  1057. .info-box-quan-list {
  1058. width: 164rpx;
  1059. height: 48rpx;
  1060. margin: 0 8rpx 8rpx 0;
  1061. position: relative;
  1062. image {
  1063. width: 164rpx;
  1064. height: 48rpx;
  1065. position: absolute;
  1066. top: 0;
  1067. left: 0;
  1068. z-index: 1;
  1069. }
  1070. }
  1071. .quan-list-1 {
  1072. position: absolute;
  1073. top: 3rpx;
  1074. left: 8rpx;
  1075. z-index: 2;
  1076. }
  1077. .quan-list-2 {
  1078. position: absolute;
  1079. top: 8rpx;
  1080. right: 22rpx;
  1081. z-index: 2;
  1082. }
  1083. .shop-type-list {
  1084. // height: 88rpx;
  1085. position: relative;
  1086. }
  1087. .shop-item {
  1088. position: relative;
  1089. }
  1090. .pj-num {
  1091. position: absolute;
  1092. right: -80rpx;
  1093. top: 0;
  1094. text-align: left;
  1095. width: 80rpx;
  1096. }
  1097. .shop-item-bs {
  1098. position: absolute;
  1099. bottom: 0;
  1100. width: 40rpx;
  1101. height: 6rpx;
  1102. border-radius: 20rpx;
  1103. transition: left 0.5s;
  1104. }
  1105. .sticky-nav {
  1106. width: 100%;
  1107. position: sticky;
  1108. z-index: 3;
  1109. }
  1110. .wh-44 {
  1111. width: 44rpx;
  1112. height: 44rpx;
  1113. }
  1114. .menu-box-left-f {
  1115. position: sticky;
  1116. // transition: all .3s ease;
  1117. // overflow: auto;
  1118. }
  1119. .menu-box-left {
  1120. width: 160rpx;
  1121. height: auto;
  1122. position: sticky;
  1123. overflow: auto;
  1124. // transition: all .3s ease;
  1125. // height: 100%;
  1126. // background-color: #F7F8FA;
  1127. // position: absolute;
  1128. // left: 0;
  1129. // top: 0;
  1130. // border-radius: 0rpx 20rpx 0 0rpx;
  1131. }
  1132. .menu-box-left-box {
  1133. overflow-y: auto;
  1134. }
  1135. .menu-type-list {
  1136. width: 160rpx;
  1137. min-height: 108rpx;
  1138. padding: 18rpx 28rpx;
  1139. position: relative;
  1140. z-index: 10;
  1141. border-radius: 0rpx 20rpx 20rpx 0rpx;
  1142. .menu-type-num {
  1143. position: absolute;
  1144. top: 2rpx;
  1145. right: 8rpx;
  1146. padding: 0 6rpx;
  1147. min-width: 20rpx;
  1148. height: 32rpx;
  1149. text-align: center;
  1150. line-height: 32rpx;
  1151. background: #FE6B2C;
  1152. border-radius: 50%;
  1153. }
  1154. }
  1155. .menu-type-list-f {
  1156. // width: 160rpx;
  1157. background-color: #007AFF;
  1158. width: 100%;
  1159. height: 100%;
  1160. overflow-y: auto;
  1161. /* 如果分类项很多,允许内部滚动 */
  1162. -webkit-overflow-scrolling: touch;
  1163. /* 改善移动端滚动体验 */
  1164. // height: 100%;
  1165. }
  1166. .menu-box-only {
  1167. // flex: 1;
  1168. width: calc(100% - 160rpx);
  1169. }
  1170. .menu-box-right {
  1171. // flex: 1;
  1172. width: 100%;
  1173. height: 100%;
  1174. // padding: 16rpx 0;
  1175. // box-sizing: border-box;
  1176. // position: relative;
  1177. }
  1178. .menu-list {
  1179. width: 100%;
  1180. padding: 0 20rpx 40rpx;
  1181. // height: 200rpx;
  1182. // margin-bottom: 40rpx;
  1183. .goods_img {
  1184. width: 168rpx;
  1185. height: 168rpx;
  1186. border-radius: 12rpx;
  1187. margin-right: 16rpx;
  1188. }
  1189. .menu-list-right {
  1190. flex: 1;
  1191. .right-only-right {
  1192. width: 116rpx;
  1193. height: 44rpx;
  1194. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  1195. line-height: 44rpx;
  1196. position: relative;
  1197. .selectedNum {
  1198. position: absolute;
  1199. right: -10rpx;
  1200. top: -22rpx;
  1201. font-size: 24rpx;
  1202. color: #fff;
  1203. width: 32rpx;
  1204. height: 32rpx;
  1205. line-height: 32rpx;
  1206. background: #FE6B2C;
  1207. border-radius: 50%;
  1208. border: 1rpx solid #FFFFFF;
  1209. }
  1210. }
  1211. .right-only-right-red {
  1212. width: 44rpx;
  1213. height: 44rpx;
  1214. line-height: 40rpx;
  1215. border-radius: 12rpx;
  1216. border: 1rpx solid #41ABF5;
  1217. color: #41ABF5;
  1218. }
  1219. .right-only-right-add {
  1220. width: 44rpx;
  1221. height: 44rpx;
  1222. line-height: 40rpx;
  1223. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  1224. border-radius: 12rpx;
  1225. }
  1226. .right-only-right-2 {
  1227. min-width: 50rpx;
  1228. }
  1229. }
  1230. }
  1231. </style>