menu.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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 && cartKey < 0) {
  384. this.setShopIngAdd(item.goods_id, item.category_id, '', 1, item)
  385. } else {
  386. this.setShopIngNumber(item.goods_id, item.category_id, type, '', 1)
  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. this.$forceUpdate()
  404. }).catch(() => {
  405. this.isAjax = false
  406. })
  407. },
  408. setShopIngNumber(goodsId, categoryId, operation, goodsSpecId, number) {
  409. setShopIngNumber({
  410. 'type': 1,
  411. 'goods_id': goodsId,
  412. 'shop_id': this.shop_id,
  413. 'category_id': categoryId,
  414. 'goods_spec_id': goodsSpecId,
  415. 'number': number,
  416. 'operation': operation === 2 ? 1 : 2,
  417. }).then(res => {
  418. if (item) {
  419. --item.num;
  420. }
  421. this.isAjax = false
  422. this.getShopIngList()
  423. this.$forceUpdate()
  424. }).catch(() => {
  425. this.isAjax = false
  426. })
  427. },
  428. setShopIngDel() {},
  429. // -------弹窗选择规格-----------
  430. selectSpec(info, groupKey) {
  431. let specKey = this.popData.spec_ids.indexOf(info.item_id)
  432. if (specKey >= 0) {
  433. this.popData.spec_ids.splice(specKey, 1)
  434. this.popData.spec_name.splice(specKey, 1)
  435. } else {
  436. this.popData.spec_ids.push(info.item_id)
  437. this.popData.spec_name.push(info.spec_value)
  438. for (const attrKey in this.popData.spec_attr[groupKey].spec_items) {
  439. if (this.popData.spec_attr[groupKey].spec_items[attrKey].item_id !== info.item_id) {
  440. let oldKey = this.popData.spec_ids.indexOf(this.popData.spec_attr[groupKey].spec_items[attrKey]
  441. .item_id)
  442. if (oldKey >= 0) {
  443. this.popData.spec_ids.splice(oldKey, 1)
  444. this.popData.spec_name.splice(oldKey, 1)
  445. }
  446. }
  447. }
  448. }
  449. // this.spec_ids = this.popData.spec_list.join('_')
  450. this.setGoodsSpecPrice()
  451. },
  452. },
  453. }
  454. </script>
  455. <style scoped lang="scss">
  456. .wh-44 {
  457. width: 44rpx;
  458. height: 44rpx;
  459. }
  460. .menu-box {
  461. height: 100%;
  462. .menu-box-left {
  463. width: 160rpx;
  464. height: 100%;
  465. background-color: #F7F8FA;
  466. // border-radius: 0rpx 20rpx 0 0rpx;
  467. .menu-type-list-f {
  468. width: 160rpx;
  469. height: 100%;
  470. .menu-type-list {
  471. width: 160rpx;
  472. min-height: 108rpx;
  473. padding: 18rpx 28rpx;
  474. position: relative;
  475. .menu-type-num {
  476. position: absolute;
  477. top: 2rpx;
  478. right: 8rpx;
  479. padding: 0 6rpx;
  480. min-width: 20rpx;
  481. height: 32rpx;
  482. text-align: center;
  483. line-height: 32rpx;
  484. background: #FB3D03;
  485. border-radius: 50%;
  486. }
  487. }
  488. }
  489. }
  490. .menu-box-right {
  491. // flex: 1;
  492. width: 100%;
  493. height: 100%;
  494. padding: 16rpx 12rpx 16rpx 16rpx;
  495. box-sizing: border-box;
  496. .menu-list {
  497. width: 100%;
  498. // height: 200rpx;
  499. margin-bottom: 40rpx;
  500. .goods_img {
  501. width: 200rpx;
  502. height: 200rpx;
  503. border-radius: 12rpx;
  504. margin-right: 16rpx;
  505. }
  506. .menu-list-right {
  507. flex: 1;
  508. .right-only-right {
  509. width: 100rpx;
  510. height: 44rpx;
  511. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  512. border-radius: 12rpx;
  513. line-height: 44rpx;
  514. }
  515. .right-only-right-red {
  516. width: 44rpx;
  517. height: 44rpx;
  518. line-height: 40rpx;
  519. border-radius: 12rpx;
  520. border: 1rpx solid #41ABF5;
  521. color: #41ABF5;
  522. }
  523. .right-only-right-add {
  524. width: 44rpx;
  525. height: 44rpx;
  526. line-height: 40rpx;
  527. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  528. border-radius: 12rpx;
  529. }
  530. .right-only-right-2 {
  531. min-width: 50rpx;
  532. }
  533. }
  534. }
  535. }
  536. .gg-box {
  537. width: 750rpx;
  538. height: 1208rpx;
  539. background: #FFFFFF;
  540. border-radius: 24rpx 24rpx 0rpx 0rpx;
  541. .goods-info {
  542. border-bottom: 1rpx solid #EEEEEE;
  543. image {
  544. width: 170rpx;
  545. height: 160rpx;
  546. border-radius: 8rpx;
  547. margin-right: 22rpx;
  548. margin-bottom: 32rpx;
  549. }
  550. }
  551. .pop-buttom {
  552. width: 100%;
  553. // height: 250rpx;
  554. box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(51, 51, 51, 0.1);
  555. .pop-buttom-left {
  556. padding-top: 12rpx;
  557. }
  558. .pop-buttom-2 {
  559. width: 100%;
  560. height: 96rpx;
  561. line-height: 96rpx;
  562. background: #41ABF5;
  563. border-radius: 200rpx
  564. }
  565. .right-only-right-red {
  566. width: 44rpx;
  567. height: 44rpx;
  568. line-height: 40rpx;
  569. border-radius: 12rpx;
  570. border: 1rpx solid #41ABF5;
  571. color: #41ABF5;
  572. }
  573. .right-only-right-add {
  574. width: 44rpx;
  575. height: 44rpx;
  576. line-height: 40rpx;
  577. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  578. border-radius: 12rpx;
  579. }
  580. .right-only-right-2 {
  581. min-width: 50rpx;
  582. }
  583. }
  584. .spec-item {
  585. min-width: 210rpx;
  586. height: 66rpx;
  587. line-height: 66rpx;
  588. box-sizing: border-box;
  589. border-radius: 12rpx;
  590. margin: 0 26rpx 26rpx 0;
  591. padding: 0 10rpx;
  592. }
  593. .spec-item:nth-child(3) {
  594. margin-right: 0rpx !important;
  595. }
  596. .s-item {
  597. background-color: #ECF7FE;
  598. border: 2rpx solid #41ABF5;
  599. color: #41ABF5;
  600. }
  601. .n-item {
  602. border: 2rpx solid #EEEEEE;
  603. color: #000;
  604. }
  605. }
  606. }
  607. .goods-name {
  608. max-width: 310rpx;
  609. }
  610. </style>