new-specs-info.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <template>
  2. <view class="flex-common-css iPhone-bottom box-border-box bgc-f">
  3. <newNav :leftShow="false" :textType='false' class="z-4" @navHeight='navHeight'>
  4. <view slot="leftContent" class="align-items-center nav-left-box pl-32 box-border-box">
  5. <view class="nav-left box-border-box b-rad-32 align-items-center flex-justify-space">
  6. <uni-icons type="left" size="25" :color="leftIconColor" @click="goBack"></uni-icons>
  7. <view class="align-items-center">
  8. <view class="hr mr-16"></view>
  9. <image class="fx-icon" src="/static/img/order-food/fx.png" mode=""></image>
  10. </view>
  11. </view>
  12. </view>
  13. </newNav>
  14. <image class="goods-img" src="/pages-buy/static/fl-bg.png" mode="aspectFill"></image>
  15. <view class="image-box" :style="{height: `calc(41vh - ${newNavHeight}rpx)`}"></view>
  16. <view class="goods-name-box">
  17. <view class="goods-name b-rad-24-24-0-0 p-32-32-0 box-border-box bgc-f">
  18. <view class="fs-36 fw-600 fc-3">
  19. 红烧牛肉面红烧牛111面11牛肉面牛肉面牛肉 红烧牛肉面红烧牛111面11牛肉面牛
  20. </view>
  21. <view class="pt-20 fs-28 fc-6">
  22. 商品描述商品描述商品描述
  23. </view>
  24. </view>
  25. </view>
  26. <view class="scroll-view-css plr-32 box-border-box">
  27. <view class="b-t-eee ptb-30">
  28. <view class="mb-16" v-for="(item,i) in specsData" :key="i">
  29. <view class="fs-28 fc-7 mb-16">
  30. {{item.sku_name}}
  31. </view>
  32. <view class="align-items-center flex-wrap-wrap w-add-16">
  33. <view
  34. class="specs-item b-rad-12 align-items-center flex-justify-evenly mr-16 mb-16 plr-12 box-border-box"
  35. :class="spec_key.indexOf(info.spec_id)>=0?'s-item bgc-FAE9E9 b-FF3333':'n-item bgc-F5F5F5'"
  36. v-for="(info,index_two) in item.spec_list" :key="index_two" @click="selectSpec(item,info)">
  37. <view class="count-box b-rad-50 bgc-FE6B2C fs-22 fc-f text-align-center"
  38. v-if="item.affect_quantity === 1 && info.spec_num > 0">
  39. {{info.spec_num}}
  40. </view>
  41. <image v-if="spec_key.indexOf(info.spec_id)>=0 && info.spec_num > 0"
  42. class="set-num-img mr-30" src="@/static/img/order-food/reduce-img.png"
  43. @click.stop="setNum(item,info,0)" mode="">
  44. </image>
  45. <view class="align-items-center info-text">
  46. <view class="" :class="spec_key.indexOf(info.spec_id)>=0?'fc-FF3333':'fc-0'">
  47. {{info.spec_name}}
  48. </view>
  49. <view class="" :class="spec_key.indexOf(info.spec_id)>=0?'fc-FF3333':'fc-0'"
  50. v-if="info.spec_price && item.affect_quantity != 0">
  51. ¥{{info.spec_price}}
  52. </view>
  53. </view>
  54. <image v-if="spec_key.indexOf(info.spec_id)>=0 && info.spec_num > 0"
  55. class="set-num-img ml-30" src="@/static/img/order-food/add-img.png"
  56. @click.stop="setNum(item,info,1)" mode=""></image>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. <view class="bot-box box-border-box">
  63. <view class="align-items-center flex-justify-space mb-20">
  64. <view class="">
  65. <view class="align-items-baseline">
  66. <view class="fs-32 fc-3 fw-600 mr-4">
  67. ¥
  68. </view>
  69. <view class="fs-48 fc-3 fw-600">
  70. {{total_price.toFixed(2)}}
  71. </view>
  72. </view>
  73. <view class="fs-24 fc-9">
  74. {{spec_text}}
  75. </view>
  76. </view>
  77. <view class="align-items-center">
  78. <image class="cz-img" src="@/static/img/order-food/reduce-img.png" mode=""
  79. @click="changeGoodsNum(0)"></image>
  80. <view class="plr-16 fs-32 fc-3 fw-400">
  81. {{postData.number}}
  82. </view>
  83. <image class="cz-img" src="@/static/img/order-food/add-img.png" mode="" @click="changeGoodsNum(1)">
  84. </image>
  85. </view>
  86. </view>
  87. <view class="but-box text-align-center fs-32 fc-f fw-600 b-rad-200" @click="addShoppingCart">
  88. 加入购物车
  89. </view>
  90. </view>
  91. </view>
  92. </template>
  93. <script>
  94. import {
  95. getGoodsGoodsSpec
  96. } from "@/api/goods.js"
  97. export default {
  98. components: {},
  99. data() {
  100. return {
  101. infoData: {},
  102. newNavHeight: '',
  103. specsData: [{
  104. sku_name: '口味',
  105. sku_id: 8,
  106. sku_type: 1, //1:单选, 2:多选
  107. affect_quantity: 0, //是否影响购物车数量: 0-不影响, 1-影响
  108. spec_list: [{
  109. spec_name: '不辣',
  110. spec_price: 8,
  111. spec_id: 0
  112. }, {
  113. spec_name: '微辣',
  114. spec_price: 8,
  115. spec_id: 1
  116. }, {
  117. spec_name: '中辣',
  118. spec_price: 8,
  119. spec_id: 2
  120. }, {
  121. spec_name: '特辣',
  122. spec_price: 8,
  123. spec_id: 3
  124. }]
  125. },{
  126. sku_name: '粗细',
  127. sku_id: 10,
  128. sku_type: 1, //1:单选, 2:多选
  129. affect_quantity: 0, //是否影响购物车数量: 0-不影响, 1-影响
  130. spec_list: [{
  131. spec_name: '特细',
  132. spec_price: 0,
  133. spec_id: 11
  134. }, {
  135. spec_name: '细',
  136. spec_price: 0,
  137. spec_id: 12
  138. }, {
  139. spec_name: '粗',
  140. spec_price: 0,
  141. spec_id: 13
  142. }, {
  143. spec_name: '韭叶',
  144. spec_price: 0,
  145. spec_id: 14
  146. }]
  147. }
  148. , {
  149. sku_name: '小料',
  150. sku_type: 2,
  151. sku_id: 9,
  152. affect_quantity: 1, //是否影响购物车数量: 0-不影响, 1-影响
  153. spec_list: [{
  154. spec_name: '煎蛋',
  155. spec_price: 3,
  156. spec_id: 4
  157. }, {
  158. spec_name: '酱牛肉',
  159. spec_price: 8,
  160. spec_id: 5
  161. }, {
  162. spec_name: '卤蛋',
  163. spec_price: 2,
  164. spec_id: 6
  165. }, {
  166. spec_name: '卤鸡腿',
  167. spec_price: 2,
  168. spec_id: 7
  169. }]
  170. }],
  171. spec_key: [],
  172. total_price: 0,
  173. spec_text: '请选择规格',
  174. postData: {
  175. shop_id: '',
  176. goods_id: '',
  177. category_id: '',
  178. goods_spec: [],
  179. number: 1
  180. }
  181. }
  182. },
  183. watch: {
  184. // 监听相关数据变化,自动计算总价和拼接规格文字
  185. 'postData.goods_spec': {
  186. handler() {
  187. this.updatePriceAndText();
  188. },
  189. deep: true
  190. },
  191. 'postData.number': {
  192. handler() {
  193. this.updatePriceAndText();
  194. }
  195. }
  196. },
  197. onLoad(data) {
  198. this.addSpecNum()
  199. },
  200. mounted() {},
  201. methods: {
  202. // 添加商品数量
  203. changeGoodsNum(type) {
  204. if (type === 0) {
  205. // 减少数量
  206. if (this.postData.number > 1) {
  207. this.postData.number--;
  208. }
  209. } else {
  210. // 增加数量
  211. this.postData.number++;
  212. }
  213. },
  214. // 更新总价和规格文字
  215. updatePriceAndText() {
  216. let basePrice = 0; // 假设商品基础价格,可以从接口获取
  217. let specPrice = 0;
  218. let specArray = [];
  219. // 遍历所有规格组
  220. this.specsData.forEach(group => {
  221. group.spec_list.forEach(spec => {
  222. // 检查规格是否被选中
  223. if (this.spec_key.includes(spec.spec_id)) {
  224. // 获取规格数量
  225. let specNum = group.affect_quantity === 1 ? spec.spec_num : 1;
  226. // 累加价格
  227. specPrice += spec.spec_price * specNum;
  228. // 构建规格文字
  229. let specName = spec.spec_name;
  230. if (group.sku_type === 2 && specNum > 1) {
  231. // 多选且数量大于1,显示 ×数量
  232. specName += `×${specNum}`;
  233. }
  234. specArray.push(specName);
  235. }
  236. });
  237. });
  238. // 计算总价:基础价格 + 规格价格 × 商品数量
  239. this.total_price = (basePrice + specPrice) * this.postData.number;
  240. // 拼接规格文字
  241. if (specArray.length > 0) {
  242. this.spec_text = specArray.join('、');
  243. } else {
  244. this.spec_text = '请选择规格';
  245. }
  246. // this.$forceUpdate();
  247. },
  248. addShoppingCart() {
  249. console.log(this.postData, "提交数据");
  250. },
  251. setNum(item, info, type) {
  252. if (type === 0) {
  253. info.spec_num -= 1;
  254. if (info.spec_num <= 0) {
  255. const infoKey = this.spec_key.indexOf(info.spec_id)
  256. if (infoKey != -1) {
  257. this.spec_key.splice(infoKey, 1)
  258. this.postData.goods_spec.splice(infoKey, 1)
  259. }
  260. }
  261. } else {
  262. info.spec_num += 1;
  263. }
  264. const targetSpec = this.postData.goods_spec.find(e => e.id === info.spec_id);
  265. if (targetSpec) {
  266. targetSpec.number = info.spec_num;
  267. }
  268. this.$forceUpdate();
  269. },
  270. addSpecNum() {
  271. this.specsData.forEach((v, k) => {
  272. if (v.affect_quantity === 1) {
  273. v.spec_list.forEach(spec => {
  274. spec.spec_num = 0;
  275. });
  276. } else {
  277. this.addSpec(v.spec_list[0])
  278. }
  279. })
  280. },
  281. getGoodsGoodsSpec() {
  282. getGoodsGoodsSpec({
  283. goods_id: '5354'
  284. }).then((res) => {
  285. if (res.code * 1 === 1) {
  286. console.log(res);
  287. }
  288. })
  289. },
  290. navHeight(e) {
  291. this.newNavHeight = e
  292. },
  293. goBack() {
  294. uni.navigateBack({
  295. delta: 1
  296. })
  297. },
  298. selectSpec(item, info) {
  299. let specKey = this.spec_key.indexOf(info.spec_id)
  300. if (item.sku_type === 1) {
  301. //单选
  302. item.spec_list.forEach((e, i) => {
  303. let infoKey = this.spec_key.indexOf(e.spec_id)
  304. if (infoKey >= 0) {
  305. this.spec_key.splice(infoKey, 1)
  306. this.postData.goods_spec.splice(infoKey, 1)
  307. }
  308. })
  309. this.addSpec(info)
  310. } else {
  311. //多选
  312. if (specKey < 0) {
  313. if (info.spec_num === 0) {
  314. info.spec_num += 1
  315. this.addSpec(info)
  316. }
  317. }
  318. }
  319. },
  320. addSpec(info) {
  321. this.spec_key.push(info.spec_id);
  322. this.postData.goods_spec.push({
  323. id: info.spec_id,
  324. number: info.spec_num || 1
  325. });
  326. }
  327. }
  328. }
  329. </script>
  330. <style lang="scss" scoped>
  331. .nav-left-box {
  332. width: 100%;
  333. height: 100%;
  334. }
  335. .nav-left {
  336. width: 174rpx;
  337. height: 100%;
  338. background: rgba(255, 255, 255, 0.45);
  339. border: 1rpx solid #E3E6EB;
  340. padding: 0 20rpx 0 14rpx;
  341. }
  342. .goods-img {
  343. width: 100%;
  344. height: 41vh;
  345. position: absolute;
  346. top: 0;
  347. left: 0;
  348. }
  349. .fx-icon {
  350. width: 48rpx;
  351. height: 48rpx;
  352. }
  353. .hr {
  354. width: 1rpx;
  355. height: 36rpx;
  356. background: #e7e7e7;
  357. }
  358. .goods-name-box {
  359. position: relative;
  360. }
  361. .goods-name {
  362. width: 100%;
  363. position: relative;
  364. top: -32rpx;
  365. left: 0;
  366. }
  367. .bot-box {
  368. padding: 20rpx 32rpx 12rpx;
  369. box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(51, 51, 51, 0.1);
  370. }
  371. .cz-img {
  372. width: 44rpx;
  373. height: 44rpx;
  374. }
  375. .but-box {
  376. width: 100%;
  377. height: 96rpx;
  378. line-height: 96rpx;
  379. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  380. }
  381. .specs-item {
  382. position: relative;
  383. min-width: 218rpx;
  384. height: 72rpx;
  385. }
  386. .w-add-16 {
  387. width: calc(100% + 22rpx);
  388. }
  389. .item-box {
  390. position: relative;
  391. }
  392. .count-box {
  393. width: 28rpx;
  394. height: 28rpx;
  395. line-height: 28rpx;
  396. position: absolute;
  397. right: -8rpx;
  398. top: -8rpx;
  399. }
  400. .set-num-img {
  401. width: 44rpx;
  402. height: 44rpx;
  403. }
  404. </style>