menu.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. }
  304. })
  305. },
  306. selectType(item) {
  307. if (this.type != item.type) {
  308. this.type = item.type;
  309. this.idMark = 'scroll' + item.type
  310. }
  311. },
  312. menuScroll() {
  313. let _this = this;
  314. let query = uni.createSelectorQuery().in(this);
  315. query.selectAll(".menu-box-only").boundingClientRect(data => {
  316. data.forEach((v) => {
  317. if (v.top < 329 && v.top > 0) {
  318. _this.idMark = v.id;
  319. let arr = v.id.split('scroll');
  320. _this.type = Number(arr[1]);
  321. }
  322. })
  323. }).exec();
  324. },
  325. addred(type, i, k, item) {
  326. if (this.isAjax) {
  327. return
  328. }
  329. this.isAjax = true
  330. if (type === 1) {
  331. // 减
  332. if (item.num > 0) {
  333. --item.num;
  334. this.setSub(item, type);
  335. } else {
  336. this.isAjax = false
  337. }
  338. } else if (type === 2) {
  339. // 加
  340. if (item.is_infinite === 1 || item.stock > item.num) {
  341. ++item.num;
  342. this.setSub(item, type);
  343. } else {
  344. this.isAjax = false
  345. }
  346. // this.menuList[i].list[k].num += 1;
  347. // this.setSub(item);
  348. } else if (type === 3) {
  349. // 选规格
  350. this.getShopGoodsSpec(item)
  351. } else {
  352. this.$refs.popupGG.close();
  353. this.isAjax = false
  354. }
  355. },
  356. setSub(item, type) {
  357. let cartKey = this.cartData.goods_list.findIndex(row => row.goods_id === item.goods_id)
  358. if (type === 2 && cartKey < 0) {
  359. this.setShopIngAdd(item.goods_id, item.category_id, '')
  360. } else {
  361. this.setShopIngNumber(item.goods_id, item.category_id, type, '')
  362. }
  363. },
  364. setShopIngAdd(goodsId, categoryId, goodsSpecId) {
  365. setShopIngAdd({
  366. 'type': 1,
  367. 'goods_id': goodsId,
  368. 'shop_id': this.shop_id,
  369. 'category_id': categoryId,
  370. 'goods_spec_id': goodsSpecId,
  371. }).then(res => {
  372. this.isAjax = false
  373. this.getShopIngList()
  374. }).catch(() => {
  375. this.isAjax = false
  376. })
  377. },
  378. setShopIngNumber(goodsId, categoryId, operation, goodsSpecId) {
  379. setShopIngNumber({
  380. 'type': 1,
  381. 'goods_id': goodsId,
  382. 'shop_id': this.shop_id,
  383. 'category_id': categoryId,
  384. 'goods_spec_id': goodsSpecId,
  385. 'operation': operation === 2 ? 1 : 2,
  386. }).then(res => {
  387. this.isAjax = false
  388. this.getShopIngList()
  389. }).catch(() => {
  390. this.isAjax = false
  391. })
  392. },
  393. setShopIngDel() {},
  394. // -------弹窗选择规格-----------
  395. selectSpec(info, groupKey) {
  396. let specKey = this.popData.spec_ids.indexOf(info.item_id)
  397. if (specKey >= 0) {
  398. this.popData.spec_ids.splice(specKey, 1)
  399. this.popData.spec_name.splice(specKey, 1)
  400. } else {
  401. this.popData.spec_ids.push(info.item_id)
  402. this.popData.spec_name.push(info.spec_value)
  403. for (const attrKey in this.popData.spec_attr[groupKey].spec_items) {
  404. if (this.popData.spec_attr[groupKey].spec_items[attrKey].item_id !== info.item_id) {
  405. let oldKey = this.popData.spec_ids.indexOf(this.popData.spec_attr[groupKey].spec_items[attrKey]
  406. .item_id)
  407. if (oldKey >= 0) {
  408. this.popData.spec_ids.splice(oldKey, 1)
  409. this.popData.spec_name.splice(oldKey, 1)
  410. }
  411. }
  412. }
  413. }
  414. // this.spec_ids = this.popData.spec_list.join('_')
  415. this.setGoodsSpecPrice()
  416. },
  417. },
  418. }
  419. </script>
  420. <style scoped lang="scss">
  421. .wh-44 {
  422. width: 44rpx;
  423. height: 44rpx;
  424. }
  425. .menu-box {
  426. height: 100%;
  427. .menu-box-left {
  428. width: 160rpx;
  429. height: 100%;
  430. background-color: #F7F8FA;
  431. // border-radius: 0rpx 20rpx 0 0rpx;
  432. .menu-type-list-f {
  433. width: 160rpx;
  434. height: 100%;
  435. .menu-type-list {
  436. width: 160rpx;
  437. min-height: 108rpx;
  438. padding: 18rpx 28rpx;
  439. position: relative;
  440. .menu-type-num {
  441. position: absolute;
  442. top: 2rpx;
  443. right: 8rpx;
  444. padding: 0 6rpx;
  445. min-width: 20rpx;
  446. height: 32rpx;
  447. text-align: center;
  448. line-height: 32rpx;
  449. background: #FB3D03;
  450. border-radius: 50%;
  451. }
  452. }
  453. }
  454. }
  455. .menu-box-right {
  456. // flex: 1;
  457. width: 100%;
  458. height: 100%;
  459. padding: 16rpx 12rpx 16rpx 16rpx;
  460. box-sizing: border-box;
  461. .menu-list {
  462. width: 100%;
  463. // height: 200rpx;
  464. margin-bottom: 40rpx;
  465. .goods_img {
  466. width: 200rpx;
  467. height: 200rpx;
  468. border-radius: 12rpx;
  469. margin-right: 16rpx;
  470. }
  471. .menu-list-right {
  472. flex: 1;
  473. .right-only-right {
  474. width: 100rpx;
  475. height: 44rpx;
  476. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  477. border-radius: 12rpx;
  478. line-height: 44rpx;
  479. }
  480. .right-only-right-red {
  481. width: 44rpx;
  482. height: 44rpx;
  483. line-height: 40rpx;
  484. border-radius: 12rpx;
  485. border: 1rpx solid #41ABF5;
  486. color: #41ABF5;
  487. }
  488. .right-only-right-add {
  489. width: 44rpx;
  490. height: 44rpx;
  491. line-height: 40rpx;
  492. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  493. border-radius: 12rpx;
  494. }
  495. .right-only-right-2 {
  496. min-width: 50rpx;
  497. }
  498. }
  499. }
  500. }
  501. .gg-box {
  502. width: 750rpx;
  503. height: 1208rpx;
  504. background: #FFFFFF;
  505. border-radius: 24rpx 24rpx 0rpx 0rpx;
  506. .goods-info {
  507. border-bottom: 1rpx solid #EEEEEE;
  508. image {
  509. width: 170rpx;
  510. height: 160rpx;
  511. border-radius: 8rpx;
  512. margin-right: 22rpx;
  513. margin-bottom: 32rpx;
  514. }
  515. }
  516. .pop-buttom {
  517. width: 100%;
  518. height: 250rpx;
  519. box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(51, 51, 51, 0.1);
  520. .pop-buttom-left {
  521. padding-top: 12rpx;
  522. }
  523. .pop-buttom-2 {
  524. width: 100%;
  525. height: 96rpx;
  526. line-height: 96rpx;
  527. background: #41ABF5;
  528. border-radius: 200rpx
  529. }
  530. .right-only-right-red {
  531. width: 44rpx;
  532. height: 44rpx;
  533. line-height: 40rpx;
  534. border-radius: 12rpx;
  535. border: 1rpx solid #41ABF5;
  536. color: #41ABF5;
  537. }
  538. .right-only-right-add {
  539. width: 44rpx;
  540. height: 44rpx;
  541. line-height: 40rpx;
  542. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  543. border-radius: 12rpx;
  544. }
  545. .right-only-right-2 {
  546. min-width: 50rpx;
  547. }
  548. }
  549. .spec-item {
  550. width: 210rpx;
  551. height: 66rpx;
  552. line-height: 66rpx;
  553. box-sizing: border-box;
  554. border-radius: 12rpx;
  555. margin: 0 26rpx 26rpx 0;
  556. }
  557. .spec-item:nth-child(3) {
  558. margin-right: 0rpx !important;
  559. }
  560. .s-item {
  561. background-color: #ECF7FE;
  562. border: 2rpx solid #41ABF5;
  563. color: #41ABF5;
  564. }
  565. .n-item {
  566. border: 2rpx solid #EEEEEE;
  567. color: #000;
  568. }
  569. }
  570. }
  571. .goods-name {
  572. max-width: 310rpx;
  573. }
  574. </style>