menu.vue 17 KB

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