menu.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <template>
  2. <view class="menu-box display-flex">
  3. <scroll-view scroll-y="true" class="menu-box-left" :scroll-into-view="idMark" :scroll-with-animation='true'>
  4. <view class="menu-type-list-f">
  5. <view class="menu-type-list fs-26 box-border-box align-items-center flex-justify-center"
  6. v-for="(item,i) in categoryData" :key="i" @click="selectType(item)"
  7. :class="type === item.type?'bgc-f fc-3 fw-600':'fc-6 fw-400'" :id="'scroll'+ item.type">
  8. {{item.name}}
  9. <view class="menu-type-num fc-f" v-show="item.num > 0">{{item.num > 99?'99+':item.num}}</view>
  10. </view>
  11. </view>
  12. </scroll-view>
  13. <scroll-view :scroll-y="true" :scroll-with-animation='true' :scroll-into-view="idMark" @scroll="menuScroll">
  14. <view class="menu-box-right">
  15. <view class="menu-box-only" v-for="(item,i) in categoryData" :key="i" :id="'scroll'+ item.type">
  16. <view class="mb-32 fs-30">{{item.name}}</view>
  17. <view class="menu-list flex-justify-space" v-for="(info,k) in item.list" :key="k">
  18. <image class="flex-shrink0 goods_img" :src="info.goods_image" mode="scaleToFill"></image>
  19. <view class="menu-list-right">
  20. <view class="mb-4 goods-name text-overflow-ellipsis fs-34 fw-600">{{info.goods_name}}</view>
  21. <view class="mb-20 fw-400 fc-9 fs-24">月销{{info.sales}}</view>
  22. <view class="flex-justify-space align-items-flex-end">
  23. <view class="">
  24. <span class="fs-24 fw-400 fc-FB3D03">¥</span>
  25. <span
  26. class="fs-40 fw-b fc-FB3D03">{{info.seckill_money>0?info.seckill_money:info.price}}</span>
  27. <view class="">
  28. <span class="fs-24 fc-9 fw-400 text-line-through">¥{{info.line_price}}</span>
  29. </view>
  30. </view>
  31. <view class="display-flex align-items-center" v-if="info.spec_type == 1">
  32. <image v-show="info.num > 0" class="wh-44" @click="addred(1,i,k,info)"
  33. src="@/static/img/takeout/j.png" mode=""></image>
  34. <view v-show="info.num > 0"
  35. class="right-only-right-2 text-align-center fs-28 fw-400">{{info.num}}</view>
  36. <image class="wh-44" src="@/static/img/takeout/add.png" @click="addred(2,i,k,info)"
  37. mode=""></image>
  38. </view>
  39. <view v-else class="right-only-right text-align-center fw-600 fs-28 fc-f"
  40. @click="addred(3,i,k,info)">选规格</view>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. </scroll-view>
  47. <uni-popup ref="popupGG" type="bottom">
  48. <view class="gg-box flex-index-css">
  49. <view class="flex-justify-space box-border-box p-32">
  50. <view class="fs-32 fw-500 fc-0">商品详情规格</view>
  51. <view class="" @click="addred(4)">
  52. <uni-icons type="closeempty" size="20"></uni-icons>
  53. </view>
  54. </view>
  55. <view class="goods-info flex-justify-space mb-32 box-border-box plr-32">
  56. <image v-if="popData.goodsItem.goods_image" :src="popData.goodsItem.goods_image" mode="scaleToFill">
  57. </image>
  58. <view class="flex-1">
  59. <view style="width: 446rpx;" class="fw-600 fs-34 text-overflow-ellipsis-one">
  60. {{popData.goodsItem.goods_name}}
  61. </view>
  62. <view class="fw-400 fc-9 fs-24">月销{{popData.goodsItem.sales}}</view>
  63. </view>
  64. </view>
  65. <view class="scroll-view-css box-border-box plr-32">
  66. <view class="" v-for="(item,i) in popData.spec_attr" :key="i">
  67. <view class="fs-32 fw-500 fc-0 mb-32">{{item.group_name}}</view>
  68. <view class="flex-wrap-wrap">
  69. <view class="spec-item text-align-center fs-28"
  70. :class="popData.spec_ids.indexOf(info.item_id)>=0?'s-item':'n-item'"
  71. v-for="(info,k) in item.spec_items" :key="k" @click="selectSpec(info,i)">
  72. {{info.spec_value}}
  73. </view>
  74. </view>
  75. </view>
  76. </view>
  77. <view class="pop-buttom box-border-box plr-32">
  78. <view class="flex-justify-space mb-28">
  79. <view class="pop-buttom-left">
  80. <view class="">
  81. <span class="fw-600 fs-32 fc-ED301D">¥</span>
  82. <span class="fw-600 fs-48 fc-ED301D">{{popData.price}}</span>
  83. </view>
  84. <view class="">
  85. <span class="fw-400 fs-24 fc-9">已选规格:</span>
  86. <span class="fw-400 fs-24">{{popData.spec_name.join(';')}}</span>
  87. </view>
  88. </view>
  89. <view class="display-flex align-items-center">
  90. <image class="wh-44" @click.stop="setSpecNum(1)" src="@/static/img/takeout/j.png" mode="">
  91. </image>
  92. <view class="right-only-right-2 text-align-center fs-28 fw-400">{{popData.num}}</view>
  93. <image class="wh-44" src="@/static/img/takeout/add.png" @click.stop="setSpecNum(2)" mode="">
  94. </image>
  95. </view>
  96. </view>
  97. <view class="pop-buttom-2 text-align-center fs-32 fw-600 fc-f" @click="addred(4)">选好了</view>
  98. </view>
  99. </view>
  100. </uni-popup>
  101. </view>
  102. </template>
  103. <script>
  104. import tools from "@/service/tools";
  105. import {
  106. shopCategory,
  107. shopGoods,
  108. shopGoodsSpec
  109. } from "@/api/shop";
  110. import {
  111. getShopIngList,
  112. setShopIngAdd,
  113. setShopIngDel,
  114. setShopIngNumber
  115. } from "@/api/shopping";
  116. export default {
  117. name: 'menu-box',
  118. props: {
  119. 'shop_id': {
  120. default: 0
  121. }
  122. },
  123. data() {
  124. return {
  125. idMark: '', //滚动自适应参数
  126. type: 1, //分类标识
  127. typeList: [],
  128. categoryData: [],
  129. menuList: [],
  130. cartData: {
  131. total_price: 0,
  132. total_num: 0,
  133. total_goods_money: 0,
  134. pack_price: 0,
  135. must_category: 0,
  136. is_place_order: 0,
  137. err_msg: 0,
  138. goods_list: [],
  139. },
  140. isAjax: false,
  141. operate_id: '',
  142. popData: {
  143. goodsItem: {},
  144. price: '0.00',
  145. line_price: '',
  146. num: 0,
  147. goods_id: '',
  148. spec_key: '',
  149. goods_spec_id: '',
  150. spec_name: [],
  151. spec_ids: [],
  152. spec_list: [],
  153. spec_attr: [],
  154. },
  155. }
  156. },
  157. watch: {
  158. 'shop_id': function() {
  159. this.getShopGoods();
  160. }
  161. },
  162. mounted() {
  163. this.operate_id = uni.getStorageSync('operate_id')
  164. this.getShopGoods();
  165. this.getShopIngList(true);
  166. },
  167. methods: {
  168. setSpecNum(type) {
  169. if (!this.popData.goods_spec_id) {
  170. tools.error('请选择规格')
  171. return;
  172. }
  173. if (type === 1) {
  174. // 减
  175. if (this.popData.num > 0) {
  176. --this.popData.num;
  177. } else {
  178. this.isAjax = false
  179. }
  180. } else {
  181. // 加
  182. ++this.popData.num;
  183. }
  184. },
  185. getShopGoodsSpec(item) {
  186. shopGoodsSpec({
  187. 'goods_id': item.goods_id
  188. }).then(res => {
  189. this.popData.goodsItem = item
  190. if (this.popData.goods_id !== item.goods_id) {
  191. this.popData.goods_id = item.goods_id
  192. this.popData.spec_name = []
  193. this.popData.price = '0.00'
  194. this.popData.line_price = ''
  195. this.popData.spec_key = ''
  196. this.popData.num = 1
  197. this.popData.spec_ids = []
  198. }else {
  199. this.popData.num =1;
  200. if(this.cartData.goods_list.length>0){
  201. this.cartData.goods_list.forEach(item=>{
  202. if(item.spec_sku_id===this.popData.spec_key){
  203. this.popData.num = item.goods_number
  204. }
  205. })
  206. }
  207. }
  208. this.popData.spec_list = res.data.spec_list
  209. this.popData.spec_attr = res.data.spec_attr
  210. this.$refs.popupGG.open();
  211. this.setGoodsSpecPrice()
  212. this.isAjax = false
  213. }).catch(() => {
  214. tools.error('规格信息异常')
  215. this.isAjax = false
  216. })
  217. },
  218. setGoodsSpecPrice() {
  219. if (this.popData.spec_ids.length > 0) {
  220. let spec_ids = JSON.parse(JSON.stringify(this.popData.spec_ids))
  221. spec_ids.sort((a, b) => a - b);
  222. let spec_key = spec_ids.join('_')
  223. this.popData.spec_key = spec_key
  224. if (this.popData.spec_list[spec_key] !== undefined) {
  225. this.popData.price = this.popData.spec_list[spec_key].seckill_money > 0 ? this.popData.spec_list[spec_key].seckill_money : this.popData.spec_list[spec_key].price
  226. this.popData.line_price = this.popData.spec_list[spec_key].line_price
  227. this.popData.goods_spec_id = this.popData.spec_list[spec_key].goods_spec_id + ''
  228. let cartKey = this.cartData.goods_list.findIndex(row => this.popData.goods_spec_id*1 === row.goods_spec_id*1)
  229. if (cartKey >= 0) {
  230. this.popData.num = this.cartData.goods_list[cartKey].goods_number
  231. }
  232. }
  233. }
  234. },
  235. setShopIngList(cartData) {
  236. this.cartData = cartData
  237. this.setGoodsNum()
  238. },
  239. getShopIngList(isStart) {
  240. getShopIngList({
  241. 'shop_id': this.shop_id,
  242. }).then(res => {
  243. if (res.code === 1) {
  244. this.cartData = res.data
  245. this.$emit('setCartData', this.cartData)
  246. if (isStart) this.setGoodsNum()
  247. }
  248. })
  249. },
  250. setGoodsNum() {
  251. if (this.cartData.goods_list.length > 0) {
  252. this.cartData.goods_list.forEach(item => {
  253. this.categoryData.forEach(categoryItem => {
  254. if (categoryItem.type === item.category_id) {
  255. categoryItem.list.forEach(goodsItem => {
  256. console.log(item.goods_id, goodsItem.goods_id)
  257. if (item.goods_id === goodsItem.goods_id) {
  258. goodsItem.num = item.goods_number * 1
  259. }
  260. })
  261. }
  262. })
  263. })
  264. } else {
  265. //购物车数据清除
  266. this.categoryData.forEach(categoryItem => {
  267. categoryItem.list.forEach(goodsItem => {
  268. goodsItem.num = 0
  269. })
  270. })
  271. }
  272. },
  273. getShopGoods() {
  274. if (this.shop_id < 0 || !this.operate_id) {
  275. return;
  276. }
  277. shopGoods({
  278. 'shop_id': this.shop_id,
  279. operate_id: this.operate_id,
  280. }).then(res => {
  281. if (res.code === 1) {
  282. this.categoryData = res.data
  283. if (this.categoryData.length > 0) {
  284. this.selectType(this.categoryData[0])
  285. }
  286. this.categoryData.forEach(categoryItem => {
  287. categoryItem.list.forEach(goodsItem => {
  288. goodsItem.payNum = 0
  289. goodsItem.spec = []
  290. })
  291. })
  292. // console.log(res)
  293. setTimeout(()=>{
  294. this.setGoodsNum()
  295. },1000)
  296. }
  297. })
  298. },
  299. selectType(item) {
  300. if (this.type != item.type) {
  301. this.type = item.type;
  302. this.idMark = 'scroll' + item.type
  303. }
  304. },
  305. menuScroll() {
  306. let _this = this;
  307. let query = uni.createSelectorQuery().in(this);
  308. query.selectAll(".menu-box-only").boundingClientRect(data => {
  309. data.forEach((v) => {
  310. if (v.top < 329 && v.top > 0) {
  311. _this.idMark = v.id;
  312. let arr = v.id.split('scroll');
  313. _this.type = Number(arr[1]);
  314. }
  315. })
  316. }).exec();
  317. },
  318. addred(type, i, k, item) {
  319. if (this.isAjax) {
  320. return
  321. }
  322. this.isAjax = true
  323. if (type === 1) {
  324. // 减
  325. if (item.num > 0) {
  326. --item.num;
  327. this.setSub(item, type);
  328. } else {
  329. this.isAjax = false
  330. }
  331. } else if (type === 2) {
  332. // 加
  333. if (item.is_infinite === 1 || item.stock > item.num) {
  334. ++item.num;
  335. this.setSub(item, type);
  336. } else {
  337. this.isAjax = false
  338. }
  339. // this.menuList[i].list[k].num += 1;
  340. // this.setSub(item);
  341. } else if (type === 3) {
  342. // 选规格
  343. this.getShopGoodsSpec(item)
  344. } else {
  345. this.$refs.popupGG.close();
  346. this.isAjax = false
  347. let cartKey = this.cartData.goods_list.findIndex(row => this.popData.goods_spec_id === row.goods_spec_id)
  348. console.log(cartKey)
  349. let addType=this.popData.num>0?2:1
  350. console.log('addType:'+addType)
  351. if (addType === 2 && cartKey < 0) {
  352. this.setShopIngAdd(this.popData.goods_id, this.popData.goodsItem.category_id, this.popData
  353. .goods_spec_id,this.popData.num)
  354. } else {
  355. this.setShopIngNumber(this.popData.goods_id, this.popData.goodsItem.category_id, type, this.popData
  356. .goods_spec_id,this.popData.num)
  357. }
  358. }
  359. },
  360. setSub(item, type) {
  361. let cartKey = this.cartData.goods_list.findIndex(row => row.goods_id === item.goods_id)
  362. if (type === 2 && cartKey < 0) {
  363. this.setShopIngAdd(item.goods_id, item.category_id, '',1)
  364. } else {
  365. this.setShopIngNumber(item.goods_id, item.category_id, type, '',1)
  366. }
  367. },
  368. setShopIngAdd(goodsId, categoryId, goodsSpecId,number) {
  369. setShopIngAdd({
  370. 'type': 1,
  371. 'goods_id': goodsId,
  372. 'shop_id': this.shop_id,
  373. 'category_id': categoryId,
  374. 'goods_spec_id': goodsSpecId,
  375. 'number': number,
  376. }).then(res => {
  377. this.isAjax = false
  378. this.getShopIngList()
  379. }).catch(() => {
  380. this.isAjax = false
  381. })
  382. },
  383. setShopIngNumber(goodsId, categoryId, operation, goodsSpecId,number) {
  384. setShopIngNumber({
  385. 'type': 1,
  386. 'goods_id': goodsId,
  387. 'shop_id': this.shop_id,
  388. 'category_id': categoryId,
  389. 'goods_spec_id': goodsSpecId,
  390. 'number': number,
  391. 'operation': operation === 2 ? 1 : 2,
  392. }).then(res => {
  393. this.isAjax = false
  394. this.getShopIngList()
  395. }).catch(() => {
  396. this.isAjax = false
  397. })
  398. },
  399. setShopIngDel() {},
  400. // -------弹窗选择规格-----------
  401. selectSpec(info, groupKey) {
  402. let specKey = this.popData.spec_ids.indexOf(info.item_id)
  403. if (specKey >= 0) {
  404. this.popData.spec_ids.splice(specKey, 1)
  405. this.popData.spec_name.splice(specKey, 1)
  406. } else {
  407. this.popData.spec_ids.push(info.item_id)
  408. this.popData.spec_name.push(info.spec_value)
  409. for (const attrKey in this.popData.spec_attr[groupKey].spec_items) {
  410. if (this.popData.spec_attr[groupKey].spec_items[attrKey].item_id !== info.item_id) {
  411. let oldKey = this.popData.spec_ids.indexOf(this.popData.spec_attr[groupKey].spec_items[attrKey]
  412. .item_id)
  413. if (oldKey >= 0) {
  414. this.popData.spec_ids.splice(oldKey, 1)
  415. this.popData.spec_name.splice(oldKey, 1)
  416. }
  417. }
  418. }
  419. }
  420. // this.spec_ids = this.popData.spec_list.join('_')
  421. this.setGoodsSpecPrice()
  422. },
  423. },
  424. }
  425. </script>
  426. <style scoped lang="scss">
  427. .wh-44 {
  428. width: 44rpx;
  429. height: 44rpx;
  430. }
  431. .menu-box {
  432. height: 100%;
  433. .menu-box-left {
  434. width: 160rpx;
  435. height: 100%;
  436. background-color: #F7F8FA;
  437. // border-radius: 0rpx 20rpx 0 0rpx;
  438. .menu-type-list-f {
  439. width: 160rpx;
  440. height: 100%;
  441. .menu-type-list {
  442. width: 160rpx;
  443. min-height: 108rpx;
  444. padding: 18rpx 28rpx;
  445. position: relative;
  446. .menu-type-num {
  447. position: absolute;
  448. top: 2rpx;
  449. right: 8rpx;
  450. padding: 0 6rpx;
  451. min-width: 20rpx;
  452. height: 32rpx;
  453. text-align: center;
  454. line-height: 32rpx;
  455. background: #FB3D03;
  456. border-radius: 50%;
  457. }
  458. }
  459. }
  460. }
  461. .menu-box-right {
  462. // flex: 1;
  463. width: 100%;
  464. height: 100%;
  465. padding: 16rpx 12rpx 16rpx 16rpx;
  466. box-sizing: border-box;
  467. .menu-list {
  468. width: 100%;
  469. // height: 200rpx;
  470. margin-bottom: 40rpx;
  471. .goods_img {
  472. width: 200rpx;
  473. height: 200rpx;
  474. border-radius: 12rpx;
  475. margin-right: 16rpx;
  476. }
  477. .menu-list-right {
  478. flex: 1;
  479. .right-only-right {
  480. width: 100rpx;
  481. height: 44rpx;
  482. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  483. border-radius: 12rpx;
  484. line-height: 44rpx;
  485. }
  486. .right-only-right-red {
  487. width: 44rpx;
  488. height: 44rpx;
  489. line-height: 40rpx;
  490. border-radius: 12rpx;
  491. border: 1rpx solid #41ABF5;
  492. color: #41ABF5;
  493. }
  494. .right-only-right-add {
  495. width: 44rpx;
  496. height: 44rpx;
  497. line-height: 40rpx;
  498. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  499. border-radius: 12rpx;
  500. }
  501. .right-only-right-2 {
  502. min-width: 50rpx;
  503. }
  504. }
  505. }
  506. }
  507. .gg-box {
  508. width: 750rpx;
  509. height: 1208rpx;
  510. background: #FFFFFF;
  511. border-radius: 24rpx 24rpx 0rpx 0rpx;
  512. .goods-info {
  513. border-bottom: 1rpx solid #EEEEEE;
  514. image {
  515. width: 170rpx;
  516. height: 160rpx;
  517. border-radius: 8rpx;
  518. margin-right: 22rpx;
  519. margin-bottom: 32rpx;
  520. }
  521. }
  522. .pop-buttom {
  523. width: 100%;
  524. // height: 250rpx;
  525. box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(51, 51, 51, 0.1);
  526. .pop-buttom-left {
  527. padding-top: 12rpx;
  528. }
  529. .pop-buttom-2 {
  530. width: 100%;
  531. height: 96rpx;
  532. line-height: 96rpx;
  533. background: #41ABF5;
  534. border-radius: 200rpx
  535. }
  536. .right-only-right-red {
  537. width: 44rpx;
  538. height: 44rpx;
  539. line-height: 40rpx;
  540. border-radius: 12rpx;
  541. border: 1rpx solid #41ABF5;
  542. color: #41ABF5;
  543. }
  544. .right-only-right-add {
  545. width: 44rpx;
  546. height: 44rpx;
  547. line-height: 40rpx;
  548. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  549. border-radius: 12rpx;
  550. }
  551. .right-only-right-2 {
  552. min-width: 50rpx;
  553. }
  554. }
  555. .spec-item {
  556. min-width: 210rpx;
  557. height: 66rpx;
  558. line-height: 66rpx;
  559. box-sizing: border-box;
  560. border-radius: 12rpx;
  561. margin: 0 26rpx 26rpx 0;
  562. padding: 0 10rpx;
  563. }
  564. .spec-item:nth-child(3) {
  565. margin-right: 0rpx !important;
  566. }
  567. .s-item {
  568. background-color: #ECF7FE;
  569. border: 2rpx solid #41ABF5;
  570. color: #41ABF5;
  571. }
  572. .n-item {
  573. border: 2rpx solid #EEEEEE;
  574. color: #000;
  575. }
  576. }
  577. }
  578. .goods-name {
  579. max-width: 310rpx;
  580. }
  581. </style>