menu.vue 17 KB

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