menu.vue 17 KB

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