new-specs-info.vue 12 KB

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