product-spec-popup.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <template>
  2. <px-popup-bottom ref="popup" :visible.sync="localVisible" title=" " radius="32" maxHeight="1024" @close="handleClose">
  3. <view class="product-spec">
  4. <!-- 商品信息 -->
  5. <view class="pro-info">
  6. <view class="img-box" @click="showImg(productValueSelect.image)">
  7. <image
  8. :src="productValueSelect.image==null||productValueSelect.image==''?product.image:productValueSelect.image"
  9. mode="aspectFill"></image>
  10. </view>
  11. <view class="info-text">
  12. <view class="product-name">{{(product.productName || product.title) || '产品名称'}}</view>
  13. <view class="price">
  14. <text class="title">会员价</text>
  15. <text class="unit">¥</text><text
  16. class="bold">{{splitPrice(productValueSelect.price).integer}}</text>.{{splitPrice(productValueSelect.price).decimal}}
  17. </view>
  18. <view class="desc-box">
  19. <text class="text">月售{{product.sales || '0'}}件</text>
  20. <text v-if="selectedSpec" class="text">库存{{selectedSpec.stock || 0}}件</text>
  21. </view>
  22. </view>
  23. </view>
  24. <!-- 规格 -->
  25. <view v-if="productSpecs && productSpecs.length > 0" class="spec-box">
  26. <view class="title">规格</view>
  27. <view class="spec-list">
  28. <view v-for="(spec, index) in productSpecs" :key="index"
  29. :class="selectedSpec && selectedSpec.id == spec.id ? 'item active':'item'" @click="selectSpec(spec)">
  30. {{ spec.sku || '默认规格' }}
  31. </view>
  32. </view>
  33. </view>
  34. <!-- 数量 -->
  35. <view class="price-num">
  36. <view class="label">数量</view>
  37. <view class="num-box">
  38. <view class="img-box" @click="lessNum()">
  39. <image v-if="specNum <= 1" src="https://cdn.his.cdwjyyh.com/images/jian.png" mode=""></image>
  40. <image v-else src="https://cdn.his.cdwjyyh.com/images/jian2.png" mode=""></image>
  41. </view>
  42. <input type="number" @change="changeNum" v-model="specNum" />
  43. <view class="img-box" @click="addNum()">
  44. <image src="https://cdn.his.cdwjyyh.com/images/add.png" mode=""></image>
  45. </view>
  46. </view>
  47. </view>
  48. <button :class="isSubmitting?'btnsel sub-btn':'sub-btn'" :disabled="isSubmitting"
  49. @click="submit">{{ isSubmitting ? (isBuyMode ? '正在跳转...' : '订单生成中...') : (isBuyMode ? '立即购买' : '确定') }}</button>
  50. </view>
  51. </px-popup-bottom>
  52. </template>
  53. <script>
  54. import PxPopupBottom from '@/components/px-popup-bottom/px-popup-bottom.vue'
  55. import { addCart, getProductValues } from '@/api/index'
  56. export default {
  57. name: 'ProductSpecPopup',
  58. components: {
  59. PxPopupBottom
  60. },
  61. props: {
  62. visible: {
  63. type: Boolean,
  64. default: false
  65. },
  66. product: {
  67. type: Object,
  68. default: () => ({})
  69. },
  70. isBuyMode: {
  71. type: Boolean,
  72. default: false
  73. }
  74. },
  75. data() {
  76. return {
  77. localVisible: false,
  78. productValueSelect: {
  79. image: '',
  80. price: 0
  81. },
  82. specNum: 1,
  83. isSubmitting: false,
  84. productSpecs: [],
  85. selectedSpec: null
  86. }
  87. },
  88. watch: {
  89. visible: {
  90. handler(newVal) {
  91. this.localVisible = newVal
  92. if (newVal && this.product) {
  93. this.initData()
  94. this.getProductValuesList()
  95. }
  96. },
  97. immediate: true
  98. },
  99. localVisible: {
  100. handler(newVal) {
  101. if (!newVal) {
  102. this.$emit('update:visible', false)
  103. }
  104. }
  105. },
  106. product: {
  107. handler(newVal) {
  108. if (newVal && this.localVisible) {
  109. this.initData()
  110. this.getProductValuesList()
  111. }
  112. },
  113. deep: true
  114. }
  115. },
  116. methods: {
  117. // 处理弹窗关闭
  118. handleClose() {
  119. this.localVisible = false
  120. this.$emit('update:visible', false)
  121. },
  122. initData() {
  123. this.specNum = 1
  124. this.isSubmitting = false
  125. this.selectedSpec = null
  126. this.productValueSelect = {
  127. image: this.product.image || this.product.firstImage || '',
  128. price: this.product.price || 0
  129. }
  130. },
  131. // 获取商品规格列表
  132. getProductValuesList() {
  133. const productId = this.product.id || this.product.productId
  134. if (!productId) return
  135. getProductValues(productId).then(
  136. res => {
  137. if (res.code == 200) {
  138. console.log("获取商品规格列表", res.data)
  139. // 保存商品规格列表
  140. this.productSpecs = res.data || []
  141. // 默认选择第一个规格
  142. if (res.data && res.data.length > 0) {
  143. this.selectedSpec = res.data[0]
  144. this.productValueSelect = {
  145. image: res.data[0].image || this.product.image || this.product.firstImage || '',
  146. price: res.data[0].price || 0
  147. }
  148. }
  149. }
  150. },
  151. rej => {
  152. console.error("获取商品规格列表失败", rej)
  153. }
  154. )
  155. },
  156. // 分割价格
  157. splitPrice(price) {
  158. const priceStr = parseFloat(price).toFixed(2).toString()
  159. return {
  160. integer: priceStr.split('.')[0],
  161. decimal: priceStr.split('.')[1]
  162. }
  163. },
  164. // 显示图片
  165. showImg(image) {
  166. console.log('显示图片:', image)
  167. // 可以实现图片预览功能
  168. },
  169. // 减少数量
  170. lessNum() {
  171. if (this.specNum > 1) {
  172. this.specNum--
  173. }
  174. },
  175. // 增加数量
  176. addNum() {
  177. this.specNum++
  178. },
  179. // 改变数量
  180. changeNum(e) {
  181. let value = parseInt(e.target.value)
  182. if (isNaN(value) || value < 1) {
  183. value = 1
  184. }
  185. this.specNum = value
  186. },
  187. // 选择规格
  188. selectSpec(spec) {
  189. console.log('选择商品规格:', spec)
  190. this.selectedSpec = spec
  191. this.productValueSelect = {
  192. image: spec.image || this.product.image || this.product.firstImage || '',
  193. price: spec.price || 0,
  194. sales: spec.sales || 0
  195. }
  196. },
  197. // 提交
  198. submit() {
  199. console.log('提交订单')
  200. this.isSubmitting = true
  201. if (this.isBuyMode) {
  202. // 购买模式:跳转到订单确认页面
  203. console.log('立即购买:', this.product, this.specNum)
  204. this.isSubmitting = false
  205. this.localVisible = false
  206. this.$emit('update:visible', false)
  207. // 跳转到订单确认页面,传递商品信息和数量
  208. uni.navigateTo({
  209. url: `/pages_shopping/shopping/confirmCreateOrder?productId=${this.product.productId}&buyNum=${this.specNum}&attrValueId=${this.selectedSpec ? this.selectedSpec.id : ''}&indexBuy=true`
  210. })
  211. } else {
  212. // 添加购物车模式:调用添加购物车接口
  213. addCart({
  214. productId: this.product.id || this.product.productId,
  215. cartNum: this.specNum,
  216. attrValueId: this.selectedSpec ? this.selectedSpec.id : '' // 使用选中的规格ID
  217. }).then(res => {
  218. if (res.code == 200) {
  219. uni.showToast({
  220. title: '添加购物车成功',
  221. icon: 'success'
  222. })
  223. // 通知父组件更新购物车数量
  224. this.$emit('cart-updated')
  225. this.localVisible = false
  226. this.$emit('update:visible', false)
  227. } else {
  228. uni.showToast({
  229. title: res.msg || '添加购物车失败',
  230. icon: 'none'
  231. })
  232. }
  233. this.isSubmitting = false
  234. }).catch(error => {
  235. console.error('添加购物车失败:', error)
  236. uni.showToast({
  237. title: '网络错误,请重试',
  238. icon: 'none'
  239. })
  240. this.isSubmitting = false
  241. })
  242. }
  243. }
  244. }
  245. }
  246. </script>
  247. <style lang="scss" scoped>
  248. .product-spec {
  249. padding: 40rpx 30rpx;
  250. .pro-info {
  251. display: flex;
  252. align-items: center;
  253. .img-box {
  254. width: 200rpx;
  255. height: 200rpx;
  256. background: #FFFFFF;
  257. border-radius: 16rpx;
  258. overflow: hidden;
  259. margin-right: 30rpx;
  260. image {
  261. width: 100%;
  262. height: 100%;
  263. }
  264. }
  265. .product-name {
  266. font-weight: 600;
  267. font-size: 32rpx;
  268. color: #333333;
  269. }
  270. .info-text {
  271. height: 200rpx;
  272. display: flex;
  273. flex-direction: column;
  274. .price {
  275. margin-top: 16rpx;
  276. font-weight: 600;
  277. font-size: 26rpx;
  278. color: #FA341E;
  279. .title {
  280. font-weight: 500;
  281. font-size: 24rpx;
  282. color: #FF5030;
  283. margin-right: 8rpx;
  284. }
  285. .unit {
  286. font-size: 20rpx;
  287. color: #FA341E;
  288. }
  289. .bold {
  290. font-size: 48upx;
  291. font-weight: 600;
  292. color: #FA341E;
  293. }
  294. }
  295. .desc-box {
  296. margin-top: 16rpx;
  297. display: flex;
  298. flex-direction: column;
  299. padding-bottom: 9rpx;
  300. .text {
  301. font-size: 26rpx;
  302. font-family: PingFang SC;
  303. font-weight: 500;
  304. color: #999999;
  305. margin-top: 27rpx;
  306. line-height: 1;
  307. &:first-child {
  308. margin-top: 0;
  309. }
  310. }
  311. }
  312. }
  313. }
  314. .spec-box {
  315. padding-top: 50rpx;
  316. .title {
  317. font-size: 34rpx;
  318. font-family: PingFang SC;
  319. font-weight: bold;
  320. color: #111111;
  321. line-height: 1;
  322. }
  323. .spec-list {
  324. display: flex;
  325. flex-wrap: wrap;
  326. margin-top: 30rpx;
  327. .item {
  328. box-sizing: border-box;
  329. padding: 12rpx 24rpx;
  330. font-size: 28rpx;
  331. color: #333333;
  332. background: #F5F7FA;
  333. border-radius: 28rpx 28rpx 28rpx 28rpx;
  334. margin-right: 24rpx;
  335. margin-bottom: 30rpx;
  336. &.active {
  337. font-size: 24rpx;
  338. color: #02B176;
  339. background: #EBFAF6;
  340. border: 2rpx solid #02B176;
  341. }
  342. }
  343. }
  344. }
  345. .price-num {
  346. display: flex;
  347. align-items: center;
  348. justify-content: space-between;
  349. margin-top: 14rpx;
  350. .label {
  351. font-size: 34rpx;
  352. font-family: PingFang SC;
  353. font-weight: bold;
  354. color: #111111;
  355. }
  356. .num-box {
  357. display: flex;
  358. align-items: center;
  359. .img-box {
  360. width: 60rpx;
  361. height: 60rpx;
  362. display: flex;
  363. align-items: center;
  364. justify-content: center;
  365. image {
  366. width: 25rpx;
  367. height: 25rpx;
  368. }
  369. }
  370. input {
  371. width: 60rpx;
  372. height: 60rpx;
  373. line-height: 60rpx;
  374. font-size: 28rpx;
  375. font-family: PingFang SC;
  376. font-weight: 500;
  377. color: #111111;
  378. background: #F5F7FA;
  379. text-align: center;
  380. border: none;
  381. }
  382. }
  383. }
  384. .sub-btn {
  385. width: 100%;
  386. height: 88rpx;
  387. line-height: 88rpx;
  388. text-align: center;
  389. font-size: 30rpx;
  390. font-family: PingFang SC;
  391. font-weight: bold;
  392. color: #FFFFFF;
  393. border-radius: 44rpx;
  394. margin-top: 30rpx;
  395. background: linear-gradient(136deg, #38D97D 0%, #02B176 100%);
  396. border: none;
  397. &.btnsel {
  398. opacity: 0.6;
  399. }
  400. }
  401. }
  402. </style>