new-specs-info.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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="infoData.goods_image" 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. {{infoData.goods_name || ''}}
  20. </view>
  21. <view class="pt-20 fs-28 fc-6" v-if="infoData.goods_desc">
  22. {{infoData.goods_desc}}
  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 class="specs-item b-rad-12 align-items-center flex-justify-evenly mr-16 mb-16 plr-12 box-border-box"
  34. :class="spec_key.indexOf(info.spec_id)>=0?'s-item bgc-FAE9E9 b-FF3333':'n-item bgc-F5F5F5'"
  35. v-for="(info,index_two) in item.spec_list" :key="index_two" @click="selectSpec(item,info)">
  36. <view class="count-box b-rad-50 bgc-FE6B2C fs-22 fc-f text-align-center"
  37. v-if="item.affect_quantity === 1 && info.spec_num > 0">
  38. {{info.spec_num}}
  39. </view>
  40. <image v-if="spec_key.indexOf(info.spec_id)>=0 && info.spec_num > 0" class="set-num-img mr-30"
  41. src="@/static/img/order-food/reduce-img.png" @click.stop="setNum(item,info,0)" mode="">
  42. </image>
  43. <view class="align-items-center info-text">
  44. <view class="" :class="spec_key.indexOf(info.spec_id)>=0?'fc-FF3333':'fc-0'">
  45. {{info.spec_name}}
  46. </view>
  47. <view class="" :class="spec_key.indexOf(info.spec_id)>=0?'fc-FF3333':'fc-0'"
  48. v-if="info.spec_price && item.affect_quantity != 0">
  49. ¥{{info.spec_price}}
  50. </view>
  51. </view>
  52. <image v-if="spec_key.indexOf(info.spec_id)>=0 && info.spec_num > 0" class="set-num-img ml-30"
  53. src="@/static/img/order-food/add-img.png" @click.stop="setNum(item,info,1)" mode=""></image>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. <view class="bot-box box-border-box">
  60. <view class="align-items-center flex-justify-space mb-20">
  61. <view class="">
  62. <view class="align-items-baseline">
  63. <view class="fs-32 fc-3 fw-600 mr-4">
  64. ¥
  65. </view>
  66. <view class="fs-48 fc-3 fw-600">
  67. {{total_price.toFixed(2)}}
  68. </view>
  69. </view>
  70. <view class="fs-24 fc-9">
  71. {{spec_text}}
  72. </view>
  73. </view>
  74. <view class="align-items-center">
  75. <image class="cz-img" src="@/static/img/order-food/reduce-img.png" mode="" @click="changeGoodsNum(0)"></image>
  76. <view class="plr-16 fs-32 fc-3 fw-400">
  77. {{postData.number}}
  78. </view>
  79. <image class="cz-img" src="@/static/img/order-food/add-img.png" mode="" @click="changeGoodsNum(1)">
  80. </image>
  81. </view>
  82. </view>
  83. <view class="but-box text-align-center fs-32 fc-f fw-600 b-rad-200" @click="addShoppingCart">
  84. 加入购物车
  85. </view>
  86. </view>
  87. </view>
  88. </template>
  89. <script>
  90. import cartStore from '@/utils/cartStore.js';
  91. import {
  92. getGoodsGoodsSpec
  93. } from "@/api/goods.js"
  94. import {
  95. shoppingSave
  96. } from "@/api/shopping.js"
  97. import tools from "@/service/tools"
  98. export default {
  99. components: {},
  100. data() {
  101. return {
  102. infoData: {},
  103. newNavHeight: '',
  104. specsData: [],
  105. spec_key: [],
  106. total_price: 0,
  107. spec_text: '请选择规格',
  108. postData: {
  109. goods_id: '',
  110. category_id: '',
  111. goods_spec: [],
  112. number: 1
  113. }
  114. }
  115. },
  116. watch: {
  117. // 监听相关数据变化,自动计算总价和拼接规格文字
  118. 'postData.goods_spec': {
  119. handler() {
  120. this.updatePriceAndText();
  121. },
  122. deep: true
  123. },
  124. 'postData.number': {
  125. handler() {
  126. this.updatePriceAndText();
  127. }
  128. }
  129. },
  130. onLoad(data) {
  131. if (data) {
  132. this.postData.goods_id = data.goods_id
  133. this.postData.category_id = data.category_id
  134. cartStore.setShopId();
  135. this.getGoodsGoodsSpec(data.goods_id)
  136. }
  137. },
  138. mounted() {},
  139. methods: {
  140. // 添加商品数量
  141. changeGoodsNum(type) {
  142. if (type === 0) {
  143. // 减少数量
  144. if (this.postData.number > 1) {
  145. this.postData.number--;
  146. }
  147. } else {
  148. // 增加数量
  149. this.postData.number++;
  150. }
  151. },
  152. // 更新总价和规格文字
  153. updatePriceAndText() {
  154. let basePrice = 0; // 假设商品基础价格,可以从接口获取
  155. let specPrice = 0;
  156. let specArray = [];
  157. // 遍历所有规格组
  158. this.specsData.forEach(group => {
  159. group.spec_list.forEach(spec => {
  160. // 检查规格是否被选中
  161. if (this.spec_key.includes(spec.spec_id)) {
  162. // 获取规格数量
  163. let specNum = group.affect_quantity === 1 ? spec.spec_num : 1;
  164. // 累加价格
  165. specPrice += spec.spec_price * specNum;
  166. // 构建规格文字
  167. let specName = spec.spec_name;
  168. if (group.sku_type === 2 && specNum > 1) {
  169. // 多选且数量大于1,显示 ×数量
  170. specName += `×${specNum}`;
  171. }
  172. specArray.push(specName);
  173. }
  174. });
  175. });
  176. // 计算总价:基础价格 + 规格价格 × 商品数量
  177. this.total_price = ((basePrice + specPrice) * this.postData.number) + Number(this.infoData.price);
  178. // 拼接规格文字
  179. if (specArray.length > 0) {
  180. this.spec_text = specArray.join('、');
  181. } else {
  182. this.spec_text = '请选择规格';
  183. }
  184. // this.$forceUpdate();
  185. },
  186. addShoppingCart() {
  187. // 构建商品对象
  188. const goodsData = {
  189. goods_id: this.postData.goods_id,
  190. goods_name: this.infoData.goods_name,
  191. goods_image: this.infoData.goods_image,
  192. goods_attr: this.spec_text,
  193. category_id: this.postData.category_id,
  194. seckill_money: this.infoData.seckill_money || 0,
  195. price: this.infoData.price || 0,
  196. pack_price: this.infoData.pack_price || 0, // 添加配送费
  197. spec_type: 2, // 多规格商品
  198. goods_desc: this.infoData.goods_desc || ''
  199. };
  200. // 计算单价
  201. const unitPrice = this.total_price / this.postData.number;
  202. // 构建规格数组
  203. const specs = this.postData.goods_spec.map(spec => {
  204. return {
  205. id: spec.id,
  206. number: spec.number,
  207. // 这里需要从规格数据中获取规格名称和价格
  208. name: this.getSpecNameById(spec.id),
  209. price: this.getSpecPriceById(spec.id)
  210. };
  211. });
  212. // 添加到购物车
  213. const success = cartStore.addToCart(
  214. goodsData,
  215. specs,
  216. this.postData.number
  217. );
  218. if (success) {
  219. uni.showToast({
  220. title: '加入购物车成功',
  221. icon: 'success'
  222. });
  223. setTimeout(() => {
  224. // 返回时传递成功信息
  225. const eventChannel = this.getOpenerEventChannel();
  226. if (eventChannel) {
  227. eventChannel.emit('cartUpdated', {
  228. success: true
  229. });
  230. }
  231. uni.navigateBack({
  232. delta: 1
  233. });
  234. }, 1000);
  235. } else {
  236. uni.showToast({
  237. title: '加入购物车失败',
  238. icon: 'error'
  239. });
  240. }
  241. },
  242. getSpecNameById(specId) {
  243. // 根据规格ID获取规格名称
  244. for (let group of this.specsData) {
  245. for (let spec of group.spec_list) {
  246. if (spec.spec_id === specId) {
  247. return spec.spec_name;
  248. }
  249. }
  250. }
  251. return '';
  252. },
  253. getSpecPriceById(specId) {
  254. // 根据规格ID获取规格价格
  255. for (let group of this.specsData) {
  256. for (let spec of group.spec_list) {
  257. if (spec.spec_id === specId) {
  258. return spec.spec_price || 0;
  259. }
  260. }
  261. }
  262. return 0;
  263. },
  264. shoppingSave() {
  265. shoppingSave(this.postData).then((res) => {
  266. if (res.code === 1) {
  267. tools.success(res.msg)
  268. this.isAjax = false
  269. setTimeout(() => {
  270. uni.navigateBack({
  271. delta: 1
  272. })
  273. }, 1000)
  274. } else {
  275. this.isAjax = false
  276. tools.error(res.msg)
  277. }
  278. })
  279. },
  280. setNum(item, info, type) {
  281. if (type === 0) {
  282. info.spec_num -= 1;
  283. if (info.spec_num <= 0) {
  284. const infoKey = this.spec_key.indexOf(info.spec_id)
  285. if (infoKey != -1) {
  286. this.spec_key.splice(infoKey, 1)
  287. this.postData.goods_spec.splice(infoKey, 1)
  288. }
  289. }
  290. } else {
  291. info.spec_num += 1;
  292. }
  293. const targetSpec = this.postData.goods_spec.find(e => e.id === info.spec_id);
  294. if (targetSpec) {
  295. targetSpec.number = info.spec_num;
  296. }
  297. this.$forceUpdate();
  298. },
  299. getGoodsGoodsSpec(goods_id) {
  300. getGoodsGoodsSpec({
  301. goods_id: goods_id
  302. }).then((res) => {
  303. if (res.code * 1 === 1) {
  304. this.specsData = res.data.sku
  305. this.infoData = res.data.goods
  306. this.addSpecNum()
  307. }
  308. })
  309. },
  310. addSpecNum() {
  311. this.specsData.forEach((v, k) => {
  312. if (v.affect_quantity === 1) {
  313. v.spec_list.forEach(spec => {
  314. spec.spec_num = 0;
  315. });
  316. } else {
  317. this.addSpec(v.spec_list[0])
  318. }
  319. })
  320. },
  321. navHeight(e) {
  322. this.newNavHeight = e
  323. },
  324. goBack() {
  325. uni.navigateBack({
  326. delta: 1
  327. })
  328. },
  329. selectSpec(item, info) {
  330. let specKey = this.spec_key.indexOf(info.spec_id)
  331. if (item.sku_type === 1) {
  332. //单选
  333. item.spec_list.forEach((e, i) => {
  334. let infoKey = this.spec_key.indexOf(e.spec_id)
  335. if (infoKey >= 0) {
  336. this.spec_key.splice(infoKey, 1)
  337. this.postData.goods_spec.splice(infoKey, 1)
  338. }
  339. })
  340. this.addSpec(info)
  341. } else {
  342. //多选
  343. if (specKey < 0) {
  344. if (info.spec_num === 0) {
  345. info.spec_num += 1
  346. this.addSpec(info)
  347. }
  348. }
  349. }
  350. },
  351. addSpec(info) {
  352. this.spec_key.push(info.spec_id);
  353. this.postData.goods_spec.push({
  354. id: info.spec_id,
  355. number: info.spec_num || 1
  356. });
  357. }
  358. }
  359. }
  360. </script>
  361. <style lang="scss" scoped>
  362. .nav-left-box {
  363. width: 100%;
  364. height: 100%;
  365. }
  366. .nav-left {
  367. width: 174rpx;
  368. height: 100%;
  369. background: rgba(255, 255, 255, 0.45);
  370. border: 1rpx solid #E3E6EB;
  371. padding: 0 20rpx 0 14rpx;
  372. }
  373. .goods-img {
  374. width: 100%;
  375. height: 41vh;
  376. position: absolute;
  377. top: 0;
  378. left: 0;
  379. }
  380. .fx-icon {
  381. width: 48rpx;
  382. height: 48rpx;
  383. }
  384. .hr {
  385. width: 1rpx;
  386. height: 36rpx;
  387. background: #e7e7e7;
  388. }
  389. .goods-name-box {
  390. position: relative;
  391. }
  392. .goods-name {
  393. width: 100%;
  394. position: relative;
  395. top: -32rpx;
  396. left: 0;
  397. }
  398. .bot-box {
  399. padding: 20rpx 32rpx 12rpx;
  400. box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(51, 51, 51, 0.1);
  401. }
  402. .cz-img {
  403. width: 44rpx;
  404. height: 44rpx;
  405. }
  406. .but-box {
  407. width: 100%;
  408. height: 96rpx;
  409. line-height: 96rpx;
  410. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  411. }
  412. .specs-item {
  413. position: relative;
  414. min-width: 218rpx;
  415. height: 72rpx;
  416. }
  417. .w-add-16 {
  418. width: calc(100% + 22rpx);
  419. }
  420. .item-box {
  421. position: relative;
  422. }
  423. .count-box {
  424. width: 28rpx;
  425. height: 28rpx;
  426. line-height: 28rpx;
  427. position: absolute;
  428. right: -8rpx;
  429. top: -8rpx;
  430. }
  431. .set-num-img {
  432. width: 44rpx;
  433. height: 44rpx;
  434. }
  435. </style>