combination.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. <template>
  2. <view class="combinationBox">
  3. <global-loading />
  4. <view class="topInfo">
  5. <view class="topBg">
  6. <swiper class="swiper" :circular="circular" :autoplay="autoplay" :vertical="vertical" :duration="duration">
  7. <swiper-item v-for="(item, index) in ruleList" :key="index">
  8. <view class="swiper-item uni-bg-red">{{ item.price }}元任选{{ item.number }}件</view>
  9. </swiper-item>
  10. </swiper>
  11. </view>
  12. </view>
  13. <view class="combinationList">
  14. <view
  15. v-for="(item, index) in productList" :key="index" class="combinationItem"
  16. @click="goodsDateils(item.shopId, item.productId, item.skuId)"
  17. >
  18. <view class="imgBox">
  19. <image :src="item.image" class="pic-img default-img"></image>
  20. </view>
  21. <view class="itemInfoBox">
  22. <view class="itemTit">{{ item.productName }}</view>
  23. <view class="itemNum">{{ item.number }}人付款</view>
  24. <view class="addInfo">
  25. <view class="price">¥{{ item.price }}</view>
  26. <view class="selectBtn" @click.stop="showRuleBox(item, index)">
  27. <image v-if="item.selected === 0" src="../../static/images/origin/cart1.png"></image>
  28. <image v-else src="../../static/images/origin/cart2.png"></image>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. <!-- 商品详情 -->
  35. <u-popup v-model="goosDetailshowFlag" mode="bottom" border-radius="14">
  36. <view class="goosDetailshow-box">
  37. <view class="detailImg-box flex-row-plus">
  38. <image class="detailImg default-img" :src="skuImg"></image>
  39. <view class="flex-column-plus mar-left-40">
  40. <view class="font-color-C5AA7B">
  41. <label class="fs24">¥</label>
  42. <label class="fs36 mar-left-10" v-text="detailList.activityType == 1 ? skuOriginalPrice : skuPrice"></label>
  43. </view>
  44. <label class="fs24 font-color-999 mar-top-20">库存 {{ stockNumber }} 件</label>
  45. <label class="fs24 mar-top-20">已选</label>
  46. </view>
  47. </view>
  48. <view class="color-box flex-column-plus">
  49. <view v-for="(attritem, index) in skuProdList.names" :key="index">
  50. <label class="fs24 font-color-999">{{ attritem.skuName }}</label>
  51. <view class="colorName-box">
  52. <view v-for="(attrRes, resIndex) in attritem.values" :key="resIndex" class="pad-bot-30">
  53. <view
  54. class="colorName" :class="{ 'colorName-on': subIndex[index] == resIndex }"
  55. @click="colorActiveClick(attrRes, index, resIndex)"
  56. >
  57. {{ attrRes.skuValue }}
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <view class="goodsNum-box flex-row-plus flex-sp-between">
  64. <label class="font-color-999 fs24">数量</label>
  65. <view class="goodsNum">
  66. <text class="subtract" @click="numSub()">-</text>
  67. <text v-model="buyNum" class="goodsNumber">{{ buyNum }}</text>
  68. <text class="add" @click.stop="numAdd()">+</text>
  69. </view>
  70. </view>
  71. <view class="flex-items flex-sp-around btnBox">
  72. <view class="goosDetailbut-box flex-row-plus" @click="goosDetailshowFlag = false">
  73. <view class="buyNowBut cancelBtn">取消</view>
  74. </view>
  75. <view class="goosDetailbut-box flex-items-plus" @click="goosDetailshowFlag = false">
  76. <view class="buyNowBut" @click="addCart">确定</view>
  77. </view>
  78. </view>
  79. </view>
  80. </u-popup>
  81. </view>
  82. </template>
  83. <script>
  84. // selectCompose
  85. const NET = require('../../utils/request')
  86. const API = require('../../config/api')
  87. export default {
  88. name: 'Combination',
  89. data() {
  90. return {
  91. page: 1,
  92. pageSize: 10,
  93. list: [],
  94. pricing: {},
  95. productList: [],
  96. ruleList: [],
  97. autoplay: true,
  98. duration: 2000,
  99. vertical: true,
  100. circular: true,
  101. buyNum: 1,
  102. goosDetailshowFlag: false, // 规格选择
  103. shopId: 0,
  104. skuId: 0,
  105. terminal: 1,
  106. skuPrice: '',
  107. skuOriginalPrice: '',
  108. attrItemIdArr: [],
  109. detailList: [],
  110. subIndex: [],
  111. selectArr: [], // 存放被选中的值
  112. skuProdList: [],
  113. stockNumber: 0,
  114. skuImg: '',
  115. currentIndex: null,
  116. priceId: null
  117. }
  118. },
  119. onLoad(option) {
  120. this.priceId = option.priceId
  121. this.getSelectByPriceId() // 获取定价
  122. this.getSelectProductListByPriceId()
  123. },
  124. methods: {
  125. getSelectByPriceId() {
  126. // uni.showLoading({
  127. // mask: true,
  128. // title: '加载中...',
  129. // })
  130. NET.request(API.selectByPriceId, {
  131. priceId: this.priceId
  132. }, 'GET').then((res) => {
  133. uni.hideLoading()
  134. this.ruleList = res.data
  135. })
  136. .catch((res) => {
  137. })
  138. },
  139. showRuleBox(item, index) {
  140. this.shopId = item.shopId
  141. this.productId = item.productId
  142. this.skuId = item.skuId
  143. this.currentIndex = index
  144. this.goosDetailshowFlag = true
  145. this.getProductSku()
  146. this.queryProductDetail()
  147. },
  148. getSelectProductListByPriceId() {
  149. NET.request(API.selectProductListByPriceId, {
  150. priceId: this.priceId,
  151. page: this.page,
  152. pageSize: this.pageSize
  153. }, 'GET').then((res) => {
  154. uni.hideLoading()
  155. // this.productList = res.data.list
  156. var proList = res.data.list
  157. proList.forEach((item) => {
  158. item.selected = 0
  159. this.productList.push(item)
  160. })
  161. })
  162. .catch((res) => {
  163. })
  164. },
  165. // 商品详情
  166. goodsDateils(shopId, productId, skuId) {
  167. uni.navigateTo({
  168. url: 'goodsDetails?shopId=' + shopId + '&productId=' + productId + '&skuId=' + skuId
  169. })
  170. },
  171. // 获取商品详情
  172. queryProductDetail() {
  173. NET.request(
  174. API.QueryProductDetail, {
  175. shopId: this.shopId,
  176. productId: this.productId,
  177. skuId: this.skuId,
  178. terminal: 1
  179. },
  180. 'GET'
  181. ).then((res) => {
  182. this.detailList = res.data
  183. this.skuPrice = res.data.price
  184. this.skuOriginalPrice = res.data.originalPrice
  185. for (let i = 0; i < res.data.attrList.length; i++) {
  186. this.subIndex[i] = 0
  187. }
  188. this.attrItemIdArr[0] = res.data.attrList[0].attrValueList[0].id
  189. this.$forceUpdate()
  190. })
  191. .catch((res) => {
  192. })
  193. },
  194. getProductSku() {
  195. NET.request(API.QueryProductSku, {
  196. skuId: this.skuId,
  197. productId: this.productId
  198. }, 'GET').then((res) => {
  199. this.skuProdList = res.data
  200. this.attrList = res.data.names
  201. this.attrValueList = res.data.names[0].values
  202. console.log(this.attrValueList[0], 'arr')
  203. // 渲染商详之后,默认先选中第一个规格
  204. this.colorActiveClick(this.attrValueList[0], 0, 0)
  205. this.skuProdId = this.skuId
  206. // this.skuImg = res.data.image
  207. this.skuPrice = res.data.price
  208. this.skuOriginalPrice = res.data.originalPrice
  209. this.stockNumber = res.data.stockNumber
  210. this.detailList.ifHuabei = res.data.ifHuabei
  211. // this.renderHuabei(this.skuPrice)
  212. this.$forceUpdate()
  213. })
  214. .catch((res) => {
  215. })
  216. },
  217. // 颜色选中事件
  218. colorActiveClick(res, index, resIndex) {
  219. const t = this
  220. t.selectArr[index] = res
  221. t.subIndex[index] = resIndex
  222. t.attrItemIdArr[index] = res.valueCode
  223. t.checkItem()
  224. t.checkItemDataClick(t.attrItemIdArr)
  225. },
  226. checkItemDataClick(attrItemIdArr) {
  227. let attrkey = ''
  228. for (let i = 0; i < attrItemIdArr.length; i++) {
  229. attrkey += attrItemIdArr[i] + ','
  230. }
  231. attrkey = attrkey.substring(0, attrkey.length - 1)
  232. const mapinfo = this.skuProdList.map
  233. for (var key in mapinfo) {
  234. if (attrkey == key) {
  235. this.skuProdId = mapinfo[key].skuId
  236. this.skuImg = mapinfo[key].skuImg
  237. this.skuPrice = mapinfo[key].price
  238. this.skuOriginalPrice = mapinfo[key].originalPrice
  239. this.stockNumber = mapinfo[key].stockNumber
  240. }
  241. }
  242. // console.log(attrItemIdArr.length, 'num')
  243. var currentCode = ''
  244. for (var i = 0; i < attrItemIdArr.length; i++) {
  245. if (i === 1 && attrItemIdArr.length > 1) {
  246. currentCode += ',' + attrItemIdArr[i]
  247. } else {
  248. currentCode += attrItemIdArr[i]
  249. }
  250. }
  251. for (const key in this.skuProdList.map) {
  252. if (key == currentCode) {
  253. this.skuImg = this.skuProdList.map[key].image
  254. this.detailList.originalPrice = this.skuProdList.map[key].originalPrice
  255. if (this.skuProdList.map[key].activityType == 1) {
  256. this.ifGroupPro = true
  257. return false
  258. }
  259. this.ifGroupPro = false
  260. }
  261. }
  262. },
  263. checkItem() {
  264. var self = this
  265. var option = self.attrList
  266. var result = [] // 定义数组存储被选中的值
  267. for (const i in option) {
  268. result[i] = self.selectArr[i] ? self.selectArr[i] : ''
  269. if (!self.subIndex[i]) {
  270. self.subIndex[i] = 0
  271. }
  272. if (!self.attrItemIdArr[i]) {
  273. self.attrItemIdArr[i] = option[i].values[0].valueCode
  274. }
  275. }
  276. for (const i in option) {
  277. var last = result[i] // 把选中的值存放到字符串last去
  278. for (const k in option[i].item) {
  279. result[i] = option[i].item[k].name // 赋值,存在直接覆盖,不存在往里面添加name值
  280. }
  281. result[i] = last // 还原,目的是记录点下去那个值,避免下一次执行循环时避免被覆盖
  282. }
  283. self.$forceUpdate() // 重绘
  284. },
  285. // 数量减
  286. numSub() {
  287. if (this.buyNum > 1) {
  288. this.buyNum = this.buyNum - 1
  289. } else {
  290. uni.showToast({
  291. title: '亲!至少一件哦!',
  292. icon: 'none'
  293. })
  294. }
  295. },
  296. // 数量加
  297. numAdd() {
  298. if (this.buyNum < this.stockNumber) {
  299. this.buyNum = this.buyNum + 1
  300. } else {
  301. uni.showToast({
  302. title: '库存不足!',
  303. icon: 'none'
  304. })
  305. }
  306. },
  307. // 加入购物车
  308. addCart() {
  309. // 判断是否登录
  310. if (this.stockNumber < 1) {
  311. uni.showToast({
  312. title: '库存不足',
  313. icon: 'none'
  314. })
  315. } else {
  316. // uni.showLoading({
  317. // mask: true,
  318. // title: '添加中...',
  319. // })
  320. NET.request(API.ShoppingaddCart, {
  321. skuId: this.skuProdId,
  322. number: this.buyNum
  323. }, 'POST').then((res) => {
  324. uni.hideLoading()
  325. uni.showToast({
  326. title: '添加成功',
  327. icon: 'none'
  328. })
  329. setTimeout(() => {
  330. this.productList[this.currentIndex].selected = 1
  331. this.goosDetailshowFlag = false
  332. uni.showModal({
  333. title: '温馨提示',
  334. content: '商品已成功加入购物车?',
  335. confirmText: '去结算',
  336. cancelText: '继续添加',
  337. success: (res) => {
  338. if (res.confirm) {
  339. uni.switchTab({
  340. url: '../../pages/tabbar/cart/index'
  341. })
  342. } else if (res.cancel) {
  343. }
  344. }
  345. })
  346. }, 1000)
  347. this.buyNum = 1
  348. })
  349. .catch((res) => {
  350. uni.hideLoading()
  351. if (res.data.code == 40005) {
  352. uni.navigateTo({
  353. url: '../../pages_category_page2/userModule/login'
  354. })
  355. }
  356. })
  357. }
  358. }
  359. }
  360. }
  361. </script>
  362. <style lang="scss" scoped>
  363. page {
  364. background: #333333;
  365. }
  366. .combinationBox {
  367. .topInfo {
  368. margin: 40rpx 0;
  369. .topBg {
  370. margin: 30rpx auto;
  371. width: 670rpx;
  372. height: 86rpx;
  373. line-height: 86rpx;
  374. text-align: center;
  375. background: url("../../static/images/origin/combinationBg.png") no-repeat center center;
  376. background-size: contain;
  377. overflow: hidden;
  378. }
  379. }
  380. .combinationList {
  381. display: flex;
  382. flex-flow: wrap;
  383. justify-content: space-between;
  384. padding: 0 20rpx;
  385. .combinationItem {
  386. margin-bottom: 30rpx;
  387. background: #FFFFFF;
  388. .imgBox {
  389. width: 348rpx;
  390. height: 314rpx;
  391. background: #F5F5F5;
  392. image {
  393. width: 100%;
  394. height: 100%;
  395. object-fit: contain
  396. }
  397. }
  398. .itemInfoBox {
  399. padding: 20rpx;
  400. width: 348rpx;
  401. .itemTit {
  402. color: #333333;
  403. font-size: 28rpx;
  404. overflow: hidden;
  405. text-overflow: ellipsis;
  406. white-space: nowrap;
  407. margin-bottom: 15rpx;
  408. }
  409. .itemNum {
  410. color: #C5AA7B;
  411. font-size: 20rpx;
  412. font-weight: 400;
  413. border: 2rpx solid #E4E5E6;
  414. display: inline;
  415. padding: 5rpx 10rpx;
  416. }
  417. .addInfo {
  418. display: flex;
  419. align-items: center;
  420. justify-content: space-between;
  421. .price {
  422. font-size: 40rpx;
  423. color: #C83732;
  424. }
  425. .selectBtn {
  426. width: 80rpx;
  427. height: 80rpx;
  428. display: flex;
  429. align-items: center;
  430. justify-content: center;
  431. image {
  432. width: 54rpx;
  433. height: 54rpx;
  434. }
  435. }
  436. }
  437. }
  438. }
  439. }
  440. }
  441. .goosDetailshow-box {
  442. padding-bottom: 10upx;
  443. .detailImg-box {
  444. margin-top: 30upx;
  445. margin-left: 30upx;
  446. border-bottom: 2upx solid #EDEDED;
  447. padding-bottom: 20upx;
  448. width: 100%;
  449. .detailImg {
  450. width: 180upx;
  451. height: 180upx;
  452. }
  453. }
  454. .color-box {
  455. padding: 30upx 30upx;
  456. border-bottom: 1upx solid #EDEDED;
  457. width: 100%;
  458. .colorName-box {
  459. display: flex;
  460. flex-wrap: wrap;
  461. flex-direction: row;
  462. justify-content: flex-start;
  463. align-items: center;
  464. margin-top: 30upx;
  465. margin-left: -30upx;
  466. .colorName {
  467. background-color: #FFFFFF;
  468. margin-left: 30upx;
  469. padding: 10upx 32upx;
  470. font-size: 26upx;
  471. border: 2rpx solid #E4E5E6;
  472. z-index: 2;
  473. color: #333333;
  474. }
  475. .colorName-on {
  476. box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1);
  477. color: #C5AA7B;
  478. margin-left: 30upx;
  479. padding: 10upx 32upx;
  480. font-size: 26upx;
  481. text-align: center;
  482. z-index: 1;
  483. border: none;
  484. }
  485. }
  486. }
  487. .modelNum-box {
  488. padding: 30upx 30upx;
  489. border-bottom: 1upx solid #EDEDED;
  490. width: 690upx;
  491. .modelNumName-box {
  492. display: flex;
  493. flex-wrap: wrap;
  494. flex-direction: row;
  495. justify-content: flex-start;
  496. align-items: center;
  497. margin-top: 30upx;
  498. margin-left: -30upx;
  499. .modelNumName-on {
  500. background-color: #FFE4D0;
  501. color: #FF7800;
  502. margin-left: 30upx;
  503. padding: 10upx 32upx;
  504. border-radius: 28upx;
  505. border: 1upx solid #FF7800;
  506. font-size: 26upx;
  507. text-align: center;
  508. }
  509. .modelNumName {
  510. background-color: #F5F5F5;
  511. margin-left: 30upx;
  512. padding: 10upx 32upx;
  513. border-radius: 28upx;
  514. font-size: 26upx;
  515. }
  516. }
  517. }
  518. .goodsNum-box {
  519. padding: 30upx 30upx;
  520. width: 100%;
  521. padding-bottom: 200upx;
  522. .goodsNum {
  523. height: 50upx;
  524. text {
  525. display: inline-block;
  526. width: 50upx;
  527. height: 50upx;
  528. border: 1upx solid #999999;
  529. text-align: center;
  530. line-height: 50upx;
  531. }
  532. .subtract {
  533. border-right: 0upx;
  534. }
  535. .add {
  536. border-left: 0upx;
  537. }
  538. }
  539. }
  540. .bottom-line {
  541. border-bottom: 1upx solid #EDEDED;
  542. }
  543. .huabei-box {
  544. padding: 30upx 30upx;
  545. width: 690upx;
  546. .fenqi-box {
  547. margin-top: 15upx;
  548. width: 120%;
  549. .huabei-item {
  550. display: inline-block;
  551. background: #f3f3f3;
  552. padding: 16upx 24upx;
  553. margin: 5upx 10upx;
  554. border-radius: 15upx;
  555. text-align: center;
  556. font-size: 7upx;
  557. .huabei-period {
  558. display: block;
  559. }
  560. }
  561. .fenqi-on {
  562. border: 1px solid #EF7F93;
  563. color: #EF7F93;
  564. }
  565. .disabled {
  566. color: #cacaca;
  567. }
  568. }
  569. }
  570. .goosDetailbut-box {
  571. justify-content: center;
  572. .joinShopCartBut {
  573. width: 343upx;
  574. height: 80upx;
  575. border-radius: 40upx 0 0 40upx;
  576. background-color: #FFC300;
  577. color: #FFFEFE;
  578. font-size: 28upx;
  579. line-height: 80upx;
  580. text-align: center;
  581. margin-left: 30upx;
  582. }
  583. .buyNowBut {
  584. width: 343upx;
  585. height: 80upx;
  586. background-color: #333333;
  587. font-size: 28upx;
  588. line-height: 80upx;
  589. text-align: center;
  590. color: #FFEBC4;
  591. }
  592. .cancelBtn {
  593. background-color: #FFFFFF !important;
  594. border: 2rpx solid #333333;
  595. color: #333333;
  596. }
  597. }
  598. .btnBox {
  599. padding-bottom: 20rpx;
  600. }
  601. }
  602. </style>