index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <template>
  2. <view :style="colorStyle">
  3. <view>
  4. <view class="fixed-lt w-full h-212 bg_mask z-900" :style="{ top: statusBarHeight + 'px' }">
  5. <view class="px-20 h-80 flex mt-12">
  6. <view class="w-56 h-56 rd-50-p111- bg-black flex-center fs-42 text--w111-fff" @tap="backPage()">
  7. <text class="iconfont icon-ic_leftarrow"></text>
  8. </view>
  9. </view>
  10. </view>
  11. <!-- #ifdef MP || APP-PLUS -->
  12. <view :style="{'height': statusBarHeight + 'px'}"></view>
  13. <view class="px-20 p-b-20">
  14. <!-- #endif -->
  15. <!-- #ifndef MP -->
  16. <view class="p-20">
  17. <!-- #endif -->
  18. <view class="rd-24rpx pro_card" v-for="(item, index) in discountsData" :key="index">
  19. <view class="w-full h-710 rd-t-24rpx relative">
  20. <image class="w-full h-full rd-t-24rpx block" mode="aspectFill" :src="item.image"></image>
  21. <view class="match-box rd-24rpx p-24 text--w111-fff">
  22. <view class="fs-26 lh-36rpx w-full line1">{{item.title}}</view>
  23. <view class="fs-22 lh-30rpx w-full line1 mt-12rpx">{{item.products[0].title}}</view>
  24. </view>
  25. </view>
  26. <view class="rd-b-24rpx bg--w111-fff pt-24 pb-32">
  27. <scroll-view scroll-x="true" scroll-with-animation
  28. class="white-nowrap vertical-middle w-686 ml-24" show-scrollbar="false">
  29. <view class="inline-block mr-24" v-for="(pro,indexn) in item.products" :key="indexn">
  30. <view class="w-222 rd-24rpx con-border relative">
  31. <image class="w-full h-222 rd-t-24rpx block" :src="pro.image"
  32. @tap="checkPro(pro, indexn, item)"></image>
  33. <view class="bg--w111-fff py-16 mx-16">
  34. <view class="fs-24 lh-34rpx w-full line1">{{ pro.title }}</view>
  35. <view
  36. class="w-full px-16 mt-8 h-36 bg--w111-f5f5f5 rd-20rpx px-12 flex-between-center fs-20"
  37. @tap="selecAttr(pro,index,indexn)">
  38. <view class="w-138 line1">{{pro.suk}}</view>
  39. <text class="iconfont icon-ic_downarrow text--w111-999 fs-20"></text>
  40. </view>
  41. <view class="mt-16 pl-16 SemiBold font-num fs-30">¥{{pro.price}}</view>
  42. </view>
  43. <text class="iconfont fs-38 set-icon"
  44. :class="pro.select ? 'icon-a-ic_CompleteSelect font-color' : 'icon-ic_unselect text--w111-fff'"
  45. @tap="checkPro(pro, indexn, item)"></text>
  46. <div class="bgIcon" v-if="pro.select"></div>
  47. </view>
  48. </view>
  49. </scroll-view>
  50. <view class="mt-32 flex-between-center px-24">
  51. <view class="flex items-baseline">
  52. <text class="fs-24 lh-34rpx text--w111-999">共2件,合计:</text>
  53. <baseMoney :money="item.totalPrice" symbolSize="28" integerSize="44"
  54. decimalSize="28" weight></baseMoney>
  55. </view>
  56. <view class="w-186 h-64 rd-36rpx flex-center fs-24 text--w111-fff bg-gradient1"
  57. @tap="subData(item)">购买此套餐</view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <base-drawer mode="bottom" :visible="visible" background-color="transparent" mask maskClosable
  64. @close="closeDrawer">
  65. <view class="scroll-content bg--w111-fff rd-t-40rpx">
  66. <view class="w-full pt-32">
  67. <view class="px-32 flex">
  68. <image class="w-180 h-180 rd-16rpx" :src="attr.productSelect.image"></image>
  69. <view class="pl-24">
  70. <baseMoney :money="attr.productSelect.price" symbolSize="32" integerSize="48"
  71. decimalSize="32" incolor="var(--primary-theme-con)" weight></baseMoney>
  72. <view class="mt-20 fs-24 text--w111-999">库存:{{ attr.productSelect.stock }}</view>
  73. </view>
  74. </view>
  75. </view>
  76. <view class="px-32">
  77. <view class="item mt-32" v-for="(item, indexw) in attr.productAttr" :key="indexw">
  78. <view class="fs-28">{{ item.attr_name }}</view>
  79. <view class="flex-y-center flex-wrap">
  80. <view class="sku-item" :class="item.index === itemn.attr ? 'active' : ''"
  81. v-for="(itemn, indexn) in item.attr_value" @click="tapAttr(indexw, indexn)"
  82. :key="indexn">
  83. {{ itemn.attr }}
  84. </view>
  85. </view>
  86. </view>
  87. </view>
  88. <view class="mx-20 pb-box">
  89. <view class="mt-52 h-72 flex-center rd-36px bg-color fs-26 text--w111-fff" @click="closeDrawer">
  90. 确定</view>
  91. </view>
  92. </view>
  93. </base-drawer>
  94. </view>
  95. </view>
  96. </template>
  97. <script>
  98. /*
  99. * 这是一段很重要的注释,方便您以后理解
  100. * 以前的逻辑较为混乱,用一个对象去管理多个套餐多个商品的sku属性
  101. * 这次修改以后,没有复用sku弹窗组件,主要是这里的逻辑可以独立处理
  102. * 初始化请求到套餐列表用下标为所有商品赋值sku名称和unique码
  103. * 在点击商品打开sku弹窗以后,拿到选中的sku以后,将suk和unique赋值到商品中,
  104. * 同时更新页面视图,在选择套餐时就省事多了,直接穿参操作即可
  105. * */
  106. let sysHeight = uni.getSystemInfoSync().statusBarHeight;
  107. import {
  108. matchPayListApi,
  109. postCartAdd
  110. } from "@/api/store.js"
  111. import colors from '@/mixins/color.js';
  112. import productWindow from '@/components/productWindow';
  113. import baseDrawer from '@/components/tui-drawer/tui-drawer.vue';
  114. export default {
  115. data() {
  116. return {
  117. statusBarHeight: sysHeight,
  118. id: 0,
  119. params: {
  120. page: 1,
  121. limit: 10
  122. },
  123. discountsData: [],
  124. selectValue: [],
  125. selectAttr: [],
  126. loading: false,
  127. attr: {
  128. cartAttr: false,
  129. productAttr: [],
  130. productSelect: {},
  131. },
  132. productValue: [], //系统属性
  133. index: 0,
  134. indexn: 0,
  135. visible: false
  136. }
  137. },
  138. components: {
  139. baseDrawer
  140. },
  141. mixins: [colors],
  142. onLoad(e) {
  143. this.id = e.id ? e.id : 0;
  144. this.getList()
  145. },
  146. methods: {
  147. getList() {
  148. if (this.loading) return;
  149. this.loading = true;
  150. matchPayListApi(this.id, this.params).then(res => {
  151. this.loading = false;
  152. if (!res.data.length) {
  153. return uni.navigateBack({
  154. delta: 1
  155. });
  156. }
  157. res.data.map((item, index) => {
  158. this.$set(item, 'totalPrice', 0);
  159. item.products.map((item1, i) => {
  160. let skuData = this.DefaultSelect(item1);
  161. Object.assign(item1, skuData);
  162. if(!item.type){
  163. this.$set(item1, 'select', true);
  164. item.totalPrice += Number(item1.price);
  165. }else{
  166. this.$set(item1, 'select', i == 0 ? true : false);
  167. this.$set(item, 'totalPrice', Number(item.products[0].price));
  168. }
  169. })
  170. })
  171. this.discountsData = this.discountsData.concat(res.data);
  172. this.params.page = this.params.page + 1;
  173. }).catch(err => {
  174. this.$util.Tips({
  175. title: err
  176. });
  177. })
  178. },
  179. backPage() {
  180. uni.navigateBack()
  181. },
  182. checkPro(pro, indexn, item) {
  183. if(!item.type) return this.$util.Tips({
  184. title: '本套餐为固定套餐,不可更改'
  185. })
  186. if(item.type && indexn == 0) return this.$util.Tips({
  187. title: '套餐主商品不可取消'
  188. })
  189. pro.select = !pro.select;
  190. if (pro.select) {
  191. item.totalPrice = (Number(item.totalPrice) + Number(pro.price)).toFixed(2);
  192. } else {
  193. item.totalPrice = (Number(item.totalPrice) - Number(pro.price)).toFixed(2);
  194. let i = this.selectValue.findIndex(v => v == pro.id);
  195. }
  196. },
  197. tapAttr: function(indexw, indexn) {
  198. let that = this;
  199. this.$set(this.attr.productAttr[indexw], 'index', this.attr.productAttr[indexw].attr_values[indexn]);
  200. let value = that.getCheckedValue().join(",");
  201. let skuData = {
  202. price: this.productValue[value].price,
  203. unique: this.productValue[value].unique || '',
  204. suk: this.productValue[value].suk || '默认',
  205. };
  206. this.attr.productSelect = {
  207. price: this.productValue[value].price,
  208. image: this.productValue[value].image,
  209. stock: this.productValue[value].stock
  210. };
  211. this.$nextTick(() => {
  212. Object.assign(this.discountsData[this.index].products[this.indexn], skuData);
  213. let priceNum = 0;
  214. for (let i = 0; i < this.discountsData[this.index].products.length; i++) {
  215. priceNum += parseFloat(this.discountsData[this.index].products[i].price)
  216. }
  217. this.$set(this.discountsData[this.index],'totalPrice',priceNum);
  218. })
  219. },
  220. //获取被选中属性;
  221. getCheckedValue: function() {
  222. let productAttr = this.attr.productAttr;
  223. let value = [];
  224. for (let i = 0; i < productAttr.length; i++) {
  225. for (let j = 0; j < productAttr[i].attr_values.length; j++) {
  226. if (productAttr[i].index === productAttr[i].attr_values[j]) {
  227. value.push(productAttr[i].attr_values[j]);
  228. }
  229. }
  230. }
  231. return value;
  232. },
  233. DefaultSelect(item) {
  234. let productAttr = item.productAttr;
  235. let value = [];
  236. let arrPrice = []
  237. for (var key in item.productValue) {
  238. arrPrice.push(item.productValue[key].price)
  239. }
  240. let min = Math.min.apply(null, arrPrice);
  241. for (var key in item.productValue) {
  242. if (item.productValue[key].product_stock > 0 && item.productValue[key].price == min) {
  243. value = item.productAttr.length ? key.split(',') : [];
  244. break;
  245. }
  246. }
  247. for (let i = 0; i < item.productAttr.length; i++) {
  248. this.$set(item.productAttr[i], 'index', value[i]);
  249. }
  250. let productSelect = item.productValue[value.join(',')];
  251. let skuData = {
  252. price: productSelect.price,
  253. unique: productSelect.unique || '',
  254. suk: productSelect.suk || '默认',
  255. stock: productSelect.stock
  256. };
  257. return skuData
  258. },
  259. closeDrawer() {
  260. this.visible = false;
  261. },
  262. selecAttr(item, index, indexn) {
  263. this.index = index;
  264. this.indexn = indexn;
  265. this.productValue = item.productValue;
  266. this.attr = {
  267. productAttr: item.productAttr,
  268. productSelect: {
  269. price: item.price,
  270. image: item.image,
  271. stock: item.stock
  272. },
  273. };
  274. this.visible = true;
  275. },
  276. subData(item) {
  277. let reqData = {
  278. new: 1,
  279. discountId: item.id,
  280. discountInfos: []
  281. };
  282. let count = 0;
  283. item.products.forEach((pro, i) => {
  284. if (pro.select) {
  285. count++;
  286. reqData.discountInfos.push({
  287. id: pro.id,
  288. unique: pro.unique,
  289. product_id: pro.product_id
  290. })
  291. }
  292. })
  293. if (count < 2) {
  294. return this.$util.Tips({
  295. title: '请先选择套餐商品'
  296. });
  297. }
  298. postCartAdd(reqData).then(res => {
  299. uni.navigateTo({
  300. url: '/pages/goods/order_confirm/index?new=1&noCoupon=1&cartId=' + res.data.cartId
  301. .join(',')
  302. });
  303. }).catch(err => {
  304. return this.$util.Tips({
  305. title: err
  306. });
  307. });
  308. },
  309. },
  310. }
  311. </script>
  312. <style>
  313. page{
  314. background-color: #000;
  315. }
  316. .pro_card~.pro_card {
  317. margin-top: 20rpx;
  318. }
  319. .z-900 {
  320. z-index: 900;
  321. }
  322. .con-border {
  323. border: 1rpx solid var(--view-theme);
  324. }
  325. .SemiBold {
  326. font-family: SemiBold;
  327. }
  328. .jianbian {
  329. background: linear-gradient(90deg, #FF7931 0%, #E93323 100%);
  330. }
  331. .bg-black {
  332. background-color: rgba(0, 0, 0, 0.4);
  333. }
  334. .set-icon {
  335. position: absolute;
  336. right: 8rpx;
  337. top: 8rpx;
  338. z-index: 9;
  339. }
  340. .bgIcon{
  341. position: absolute;
  342. right: 12rpx;
  343. top: 12rpx;
  344. width: 30rpx;
  345. height: 30rpx;
  346. background-color: #fff;
  347. border-radius: 50%;
  348. }
  349. .match-box {
  350. width: 424rpx;
  351. background-color: rgba(0, 0, 0, 0.3);
  352. position: absolute;
  353. left: 24rpx;
  354. bottom: 24rpx;
  355. }
  356. .sku-item {
  357. height: 56rpx;
  358. line-height: 56rpx;
  359. border: 1px solid #F2F2F2;
  360. font-size: 24rpx;
  361. color: #333;
  362. padding: 0 44rpx;
  363. border-radius: 28rpx;
  364. margin: 24rpx 0 0 16rpx;
  365. background-color: #F2F2F2;
  366. word-break: break-all;
  367. }
  368. .active {
  369. color: var(--view-theme);
  370. background: var(--view-minorColorT);
  371. border-color: var(--view-theme);
  372. }
  373. .scroll-content {
  374. max-height: 800rpx;
  375. overflow-y: auto;
  376. }
  377. .pb-box{
  378. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  379. }
  380. </style>