menu.vue 17 KB

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