menu.vue 17 KB

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