menu.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. console.log(this.popData.goods_spec_id)
  170. if (!this.popData.goods_spec_id) {
  171. tools.error('请选择规格')
  172. return;
  173. }
  174. if (this.isAjax) {
  175. return
  176. }
  177. this.isAjax = true
  178. if (type === 1) {
  179. // 减
  180. if (this.popData.num > 0) {
  181. --this.popData.num;
  182. } else {
  183. this.isAjax = false
  184. return
  185. }
  186. } else {
  187. // 加
  188. ++this.popData.num;
  189. }
  190. let cartKey = this.cartData.goods_list.findIndex(row => this.popData.goods_spec_id === row.goods_spec_id)
  191. console.log(cartKey)
  192. console.log(type === 2 && cartKey < 0)
  193. if (type === 2 && cartKey < 0) {
  194. this.setShopIngAdd(this.popData.goods_id, this.popData.goodsItem.category_id, this.popData
  195. .goods_spec_id)
  196. } else {
  197. this.setShopIngNumber(this.popData.goods_id, this.popData.goodsItem.category_id, type, this.popData
  198. .goods_spec_id)
  199. }
  200. },
  201. getShopGoodsSpec(item) {
  202. shopGoodsSpec({
  203. 'goods_id': item.goods_id
  204. }).then(res => {
  205. this.popData.goodsItem = item
  206. if (this.popData.goods_id !== item.goods_id) {
  207. this.popData.goods_id = item.goods_id
  208. this.popData.spec_name = []
  209. this.popData.price = '0.00'
  210. this.popData.line_price = ''
  211. this.popData.spec_key = ''
  212. this.popData.num = 0
  213. this.popData.spec_ids = []
  214. }
  215. this.popData.spec_list = res.data.spec_list
  216. this.popData.spec_attr = res.data.spec_attr
  217. this.$refs.popupGG.open();
  218. this.setGoodsSpecPrice()
  219. this.isAjax = false
  220. }).catch(() => {
  221. tools.error('规格信息异常')
  222. this.isAjax = false
  223. })
  224. },
  225. setGoodsSpecPrice() {
  226. if (this.popData.spec_ids.length > 0) {
  227. let spec_ids = JSON.parse(JSON.stringify(this.popData.spec_ids))
  228. spec_ids.sort((a, b) => a - b);
  229. let spec_key = spec_ids.join('_')
  230. this.popData.spec_key = spec_key
  231. console.log(this.popData.spec_list[spec_key])
  232. if (this.popData.spec_list[spec_key] !== undefined) {
  233. this.popData.price = this.popData.spec_list[spec_key].seckill_money > 0 ? this.popData.spec_list[
  234. spec_key].seckill_money : this.popData.spec_list[spec_key].price
  235. this.popData.line_price = this.popData.spec_list[spec_key].line_price
  236. this.popData.goods_spec_id = this.popData.spec_list[spec_key].goods_spec_id + ''
  237. let cartKey = this.cartData.goods_list.findIndex(row => this.popData.goods_spec_id === row
  238. .goods_spec_id)
  239. if (cartKey >= 0) {
  240. this.popData.num = this.cartData.goods_list[cartKey].goods_number
  241. }
  242. }
  243. }
  244. },
  245. setShopIngList(cartData) {
  246. this.cartData = cartData
  247. this.setGoodsNum()
  248. },
  249. getShopIngList(isStart) {
  250. getShopIngList({
  251. 'shop_id': this.shop_id,
  252. }).then(res => {
  253. if (res.code === 1) {
  254. this.cartData = res.data
  255. this.$emit('setCartData', this.cartData)
  256. if (isStart) this.setGoodsNum()
  257. }
  258. })
  259. },
  260. setGoodsNum() {
  261. if (this.cartData.goods_list.length > 0) {
  262. this.cartData.goods_list.forEach(item => {
  263. this.categoryData.forEach(categoryItem => {
  264. if (categoryItem.type === item.category_id) {
  265. categoryItem.list.forEach(goodsItem => {
  266. console.log(item.goods_id, goodsItem.goods_id)
  267. if (item.goods_id === goodsItem.goods_id) {
  268. goodsItem.num = item.goods_number * 1
  269. }
  270. })
  271. }
  272. })
  273. })
  274. } else {
  275. //购物车数据清除
  276. this.categoryData.forEach(categoryItem => {
  277. categoryItem.list.forEach(goodsItem => {
  278. goodsItem.num = 0
  279. })
  280. })
  281. }
  282. },
  283. getShopGoods() {
  284. if (this.shop_id < 0 || !this.operate_id) {
  285. return;
  286. }
  287. shopGoods({
  288. 'shop_id': this.shop_id,
  289. operate_id: this.operate_id,
  290. }).then(res => {
  291. if (res.code === 1) {
  292. this.categoryData = res.data
  293. if (this.categoryData.length > 0) {
  294. this.selectType(this.categoryData[0])
  295. }
  296. this.categoryData.forEach(categoryItem => {
  297. categoryItem.list.forEach(goodsItem => {
  298. goodsItem.payNum = 0
  299. goodsItem.spec = []
  300. })
  301. })
  302. // console.log(res)
  303. setTimeout(()=>{
  304. this.setGoodsNum()
  305. },1000)
  306. }
  307. })
  308. },
  309. selectType(item) {
  310. if (this.type != item.type) {
  311. this.type = item.type;
  312. this.idMark = 'scroll' + item.type
  313. }
  314. },
  315. menuScroll() {
  316. let _this = this;
  317. let query = uni.createSelectorQuery().in(this);
  318. query.selectAll(".menu-box-only").boundingClientRect(data => {
  319. data.forEach((v) => {
  320. if (v.top < 329 && v.top > 0) {
  321. _this.idMark = v.id;
  322. let arr = v.id.split('scroll');
  323. _this.type = Number(arr[1]);
  324. }
  325. })
  326. }).exec();
  327. },
  328. addred(type, i, k, item) {
  329. if (this.isAjax) {
  330. return
  331. }
  332. this.isAjax = true
  333. if (type === 1) {
  334. // 减
  335. if (item.num > 0) {
  336. --item.num;
  337. this.setSub(item, type);
  338. } else {
  339. this.isAjax = false
  340. }
  341. } else if (type === 2) {
  342. // 加
  343. if (item.is_infinite === 1 || item.stock > item.num) {
  344. ++item.num;
  345. this.setSub(item, type);
  346. } else {
  347. this.isAjax = false
  348. }
  349. // this.menuList[i].list[k].num += 1;
  350. // this.setSub(item);
  351. } else if (type === 3) {
  352. // 选规格
  353. this.getShopGoodsSpec(item)
  354. } else {
  355. this.$refs.popupGG.close();
  356. this.isAjax = false
  357. }
  358. },
  359. setSub(item, type) {
  360. let cartKey = this.cartData.goods_list.findIndex(row => row.goods_id === item.goods_id)
  361. if (type === 2 && cartKey < 0) {
  362. this.setShopIngAdd(item.goods_id, item.category_id, '')
  363. } else {
  364. this.setShopIngNumber(item.goods_id, item.category_id, type, '')
  365. }
  366. },
  367. setShopIngAdd(goodsId, categoryId, goodsSpecId) {
  368. setShopIngAdd({
  369. 'type': 1,
  370. 'goods_id': goodsId,
  371. 'shop_id': this.shop_id,
  372. 'category_id': categoryId,
  373. 'goods_spec_id': goodsSpecId,
  374. }).then(res => {
  375. this.isAjax = false
  376. this.getShopIngList()
  377. }).catch(() => {
  378. this.isAjax = false
  379. })
  380. },
  381. setShopIngNumber(goodsId, categoryId, operation, goodsSpecId) {
  382. setShopIngNumber({
  383. 'type': 1,
  384. 'goods_id': goodsId,
  385. 'shop_id': this.shop_id,
  386. 'category_id': categoryId,
  387. 'goods_spec_id': goodsSpecId,
  388. 'operation': operation === 2 ? 1 : 2,
  389. }).then(res => {
  390. this.isAjax = false
  391. this.getShopIngList()
  392. }).catch(() => {
  393. this.isAjax = false
  394. })
  395. },
  396. setShopIngDel() {},
  397. // -------弹窗选择规格-----------
  398. selectSpec(info, groupKey) {
  399. let specKey = this.popData.spec_ids.indexOf(info.item_id)
  400. if (specKey >= 0) {
  401. this.popData.spec_ids.splice(specKey, 1)
  402. this.popData.spec_name.splice(specKey, 1)
  403. } else {
  404. this.popData.spec_ids.push(info.item_id)
  405. this.popData.spec_name.push(info.spec_value)
  406. for (const attrKey in this.popData.spec_attr[groupKey].spec_items) {
  407. if (this.popData.spec_attr[groupKey].spec_items[attrKey].item_id !== info.item_id) {
  408. let oldKey = this.popData.spec_ids.indexOf(this.popData.spec_attr[groupKey].spec_items[attrKey]
  409. .item_id)
  410. if (oldKey >= 0) {
  411. this.popData.spec_ids.splice(oldKey, 1)
  412. this.popData.spec_name.splice(oldKey, 1)
  413. }
  414. }
  415. }
  416. }
  417. // this.spec_ids = this.popData.spec_list.join('_')
  418. this.setGoodsSpecPrice()
  419. },
  420. },
  421. }
  422. </script>
  423. <style scoped lang="scss">
  424. .wh-44 {
  425. width: 44rpx;
  426. height: 44rpx;
  427. }
  428. .menu-box {
  429. height: 100%;
  430. .menu-box-left {
  431. width: 160rpx;
  432. height: 100%;
  433. background-color: #F7F8FA;
  434. // border-radius: 0rpx 20rpx 0 0rpx;
  435. .menu-type-list-f {
  436. width: 160rpx;
  437. height: 100%;
  438. .menu-type-list {
  439. width: 160rpx;
  440. min-height: 108rpx;
  441. padding: 18rpx 28rpx;
  442. position: relative;
  443. .menu-type-num {
  444. position: absolute;
  445. top: 2rpx;
  446. right: 8rpx;
  447. padding: 0 6rpx;
  448. min-width: 20rpx;
  449. height: 32rpx;
  450. text-align: center;
  451. line-height: 32rpx;
  452. background: #FB3D03;
  453. border-radius: 50%;
  454. }
  455. }
  456. }
  457. }
  458. .menu-box-right {
  459. // flex: 1;
  460. width: 100%;
  461. height: 100%;
  462. padding: 16rpx 12rpx 16rpx 16rpx;
  463. box-sizing: border-box;
  464. .menu-list {
  465. width: 100%;
  466. // height: 200rpx;
  467. margin-bottom: 40rpx;
  468. .goods_img {
  469. width: 200rpx;
  470. height: 200rpx;
  471. border-radius: 12rpx;
  472. margin-right: 16rpx;
  473. }
  474. .menu-list-right {
  475. flex: 1;
  476. .right-only-right {
  477. width: 100rpx;
  478. height: 44rpx;
  479. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  480. border-radius: 12rpx;
  481. line-height: 44rpx;
  482. }
  483. .right-only-right-red {
  484. width: 44rpx;
  485. height: 44rpx;
  486. line-height: 40rpx;
  487. border-radius: 12rpx;
  488. border: 1rpx solid #41ABF5;
  489. color: #41ABF5;
  490. }
  491. .right-only-right-add {
  492. width: 44rpx;
  493. height: 44rpx;
  494. line-height: 40rpx;
  495. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  496. border-radius: 12rpx;
  497. }
  498. .right-only-right-2 {
  499. min-width: 50rpx;
  500. }
  501. }
  502. }
  503. }
  504. .gg-box {
  505. width: 750rpx;
  506. height: 1208rpx;
  507. background: #FFFFFF;
  508. border-radius: 24rpx 24rpx 0rpx 0rpx;
  509. .goods-info {
  510. border-bottom: 1rpx solid #EEEEEE;
  511. image {
  512. width: 170rpx;
  513. height: 160rpx;
  514. border-radius: 8rpx;
  515. margin-right: 22rpx;
  516. margin-bottom: 32rpx;
  517. }
  518. }
  519. .pop-buttom {
  520. width: 100%;
  521. height: 250rpx;
  522. box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(51, 51, 51, 0.1);
  523. .pop-buttom-left {
  524. padding-top: 12rpx;
  525. }
  526. .pop-buttom-2 {
  527. width: 100%;
  528. height: 96rpx;
  529. line-height: 96rpx;
  530. background: #41ABF5;
  531. border-radius: 200rpx
  532. }
  533. .right-only-right-red {
  534. width: 44rpx;
  535. height: 44rpx;
  536. line-height: 40rpx;
  537. border-radius: 12rpx;
  538. border: 1rpx solid #41ABF5;
  539. color: #41ABF5;
  540. }
  541. .right-only-right-add {
  542. width: 44rpx;
  543. height: 44rpx;
  544. line-height: 40rpx;
  545. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  546. border-radius: 12rpx;
  547. }
  548. .right-only-right-2 {
  549. min-width: 50rpx;
  550. }
  551. }
  552. .spec-item {
  553. width: 210rpx;
  554. height: 66rpx;
  555. line-height: 66rpx;
  556. box-sizing: border-box;
  557. border-radius: 12rpx;
  558. margin: 0 26rpx 26rpx 0;
  559. }
  560. .spec-item:nth-child(3) {
  561. margin-right: 0rpx !important;
  562. }
  563. .s-item {
  564. background-color: #ECF7FE;
  565. border: 2rpx solid #41ABF5;
  566. color: #41ABF5;
  567. }
  568. .n-item {
  569. border: 2rpx solid #EEEEEE;
  570. color: #000;
  571. }
  572. }
  573. }
  574. .goods-name {
  575. max-width: 310rpx;
  576. }
  577. </style>