product-detail.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <view class="product-detail">
  3. <!-- 顶部导航栏 -->
  4. <view class="header">
  5. <view class="back-icon" @click="handleBack"></view>
  6. <text class="header-title">商品详情</text>
  7. <view class="header-right"></view>
  8. </view>
  9. <!-- 商品图片 -->
  10. <view class="product-image-section">
  11. <image :src="product.imageUrl || '/static/default-product.png'" mode="aspectFit" class="product-image"></image>
  12. </view>
  13. <!-- 商品信息 -->
  14. <view class="product-info-section">
  15. <view class="product-name">{{ product.name }}</view>
  16. <view class="product-price">
  17. <text class="price-value">{{ product.pointsPrice }}</text>
  18. <text class="price-unit">积分</text>
  19. </view>
  20. <view class="product-stock">库存: {{ product.stock || 0 }} 件</view>
  21. </view>
  22. <!-- 商品描述 -->
  23. <view class="product-desc-section">
  24. <view class="section-title">商品描述</view>
  25. <view class="desc-content">{{ product.description || '暂无描述' }}</view>
  26. </view>
  27. <!-- 收货信息 -->
  28. <view class="receiver-section" v-if="product.category === 1">
  29. <view class="section-title">收货信息</view>
  30. <view class="form-item">
  31. <text class="label">收货人</text>
  32. <input class="input" v-model="receiverName" placeholder="请输入收货人姓名" />
  33. </view>
  34. <view class="form-item">
  35. <text class="label">联系电话</text>
  36. <input class="input" v-model="receiverPhone" placeholder="请输入联系电话" type="number" />
  37. </view>
  38. <view class="form-item">
  39. <text class="label">收货地址</text>
  40. <textarea class="textarea" v-model="receiverAddress" placeholder="请输入详细收货地址"></textarea>
  41. </view>
  42. </view>
  43. <!-- 底部操作栏 -->
  44. <view class="bottom-bar">
  45. <view class="points-info">
  46. <text class="label">我的积分:</text>
  47. <text class="value">{{ userPoints }}</text>
  48. </view>
  49. <view class="exchange-btn" :class="{ disabled: !canExchange }" @click="handleExchange">
  50. 立即兑换
  51. </view>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. import api from '@/utils/api.js'
  57. export default {
  58. name: 'product-detail',
  59. data() {
  60. return {
  61. productId: null,
  62. product: {},
  63. userPoints: 0,
  64. makerId: null,
  65. receiverName: '',
  66. receiverPhone: '',
  67. receiverAddress: '',
  68. loading: false
  69. }
  70. },
  71. computed: {
  72. canExchange() {
  73. if (this.loading) return false
  74. if (!this.product.pointsPrice) return false
  75. if (this.userPoints < this.product.pointsPrice) return false
  76. if (this.product.stock <= 0) return false
  77. // 实物商品需要填写收货信息
  78. if (this.product.category === 1) {
  79. if (!this.receiverName || !this.receiverPhone || !this.receiverAddress) {
  80. return false
  81. }
  82. }
  83. return true
  84. }
  85. },
  86. onLoad(options) {
  87. if (options.id) {
  88. this.productId = options.id
  89. }
  90. this.initData()
  91. },
  92. methods: {
  93. async initData() {
  94. // 获取红娘ID
  95. const userInfo = uni.getStorageSync('userInfo')
  96. if (userInfo && userInfo.matchmakerId) {
  97. this.makerId = userInfo.matchmakerId
  98. } else if (userInfo && userInfo.userId) {
  99. this.makerId = userInfo.userId
  100. }
  101. await Promise.all([
  102. this.loadProduct(),
  103. this.loadBalance()
  104. ])
  105. },
  106. async loadProduct() {
  107. if (!this.productId) return
  108. try {
  109. const res = await api.pointsMall.getProductDetail(this.productId)
  110. this.product = res || {}
  111. } catch (e) {
  112. console.error('获取商品详情失败:', e)
  113. uni.showToast({ title: '获取商品详情失败', icon: 'none' })
  114. }
  115. },
  116. async loadBalance() {
  117. if (!this.makerId) return
  118. try {
  119. const res = await api.pointsMall.getBalance(this.makerId)
  120. this.userPoints = res.balance || 0
  121. } catch (e) {
  122. console.error('获取积分余额失败:', e)
  123. }
  124. },
  125. handleBack() {
  126. uni.navigateBack()
  127. },
  128. async handleExchange() {
  129. if (!this.canExchange) {
  130. if (this.userPoints < this.product.pointsPrice) {
  131. uni.showToast({ title: '积分不足', icon: 'none' })
  132. } else if (this.product.stock <= 0) {
  133. uni.showToast({ title: '库存不足', icon: 'none' })
  134. } else if (this.product.category === 1 && (!this.receiverName || !this.receiverPhone || !this.receiverAddress)) {
  135. uni.showToast({ title: '请填写完整收货信息', icon: 'none' })
  136. }
  137. return
  138. }
  139. uni.showModal({
  140. title: '确认兑换',
  141. content: `确定使用 ${this.product.pointsPrice} 积分兑换「${this.product.name}」吗?`,
  142. success: async (res) => {
  143. if (res.confirm) {
  144. await this.doExchange()
  145. }
  146. }
  147. })
  148. },
  149. async doExchange() {
  150. this.loading = true
  151. try {
  152. const data = {
  153. makerId: this.makerId,
  154. productId: this.productId,
  155. quantity: 1,
  156. receiverName: this.receiverName,
  157. receiverPhone: this.receiverPhone,
  158. receiverAddress: this.receiverAddress
  159. }
  160. await api.pointsMall.exchange(data)
  161. uni.showToast({ title: '兑换成功', icon: 'success' })
  162. // 刷新积分余额
  163. await this.loadBalance()
  164. // 延迟返回
  165. setTimeout(() => {
  166. uni.navigateBack()
  167. }, 1500)
  168. } catch (e) {
  169. console.error('兑换失败:', e)
  170. uni.showToast({ title: e.message || '兑换失败', icon: 'none' })
  171. } finally {
  172. this.loading = false
  173. }
  174. }
  175. }
  176. }
  177. </script>
  178. <style lang="scss" scoped>
  179. .product-detail {
  180. min-height: 100vh;
  181. background: #F5F5F5;
  182. padding-bottom: 120rpx;
  183. }
  184. .header {
  185. display: flex;
  186. align-items: center;
  187. justify-content: space-between;
  188. padding: 25rpx 30rpx;
  189. padding-top: calc(25rpx + env(safe-area-inset-top));
  190. background: #FFFFFF;
  191. .back-icon {
  192. width: 44rpx;
  193. height: 44rpx;
  194. display: flex;
  195. align-items: center;
  196. justify-content: center;
  197. font-size: 32rpx;
  198. color: #333;
  199. &::before { content: '‹'; }
  200. }
  201. .header-title {
  202. font-size: 36rpx;
  203. font-weight: bold;
  204. color: #333;
  205. }
  206. .header-right {
  207. width: 44rpx;
  208. }
  209. }
  210. .product-image-section {
  211. width: 100%;
  212. height: 600rpx;
  213. background: #FFFFFF;
  214. display: flex;
  215. align-items: center;
  216. justify-content: center;
  217. .product-image {
  218. width: 100%;
  219. height: 100%;
  220. object-fit: contain;
  221. }
  222. }
  223. .product-info-section {
  224. background: #FFFFFF;
  225. padding: 30rpx;
  226. margin-top: 20rpx;
  227. .product-name {
  228. font-size: 36rpx;
  229. font-weight: bold;
  230. color: #333;
  231. margin-bottom: 20rpx;
  232. }
  233. .product-price {
  234. display: flex;
  235. align-items: baseline;
  236. margin-bottom: 15rpx;
  237. .price-value {
  238. font-size: 48rpx;
  239. font-weight: bold;
  240. color: #9C27B0;
  241. }
  242. .price-unit {
  243. font-size: 28rpx;
  244. color: #9C27B0;
  245. margin-left: 10rpx;
  246. }
  247. }
  248. .product-stock {
  249. font-size: 26rpx;
  250. color: #999;
  251. }
  252. }
  253. .product-desc-section {
  254. background: #FFFFFF;
  255. padding: 30rpx;
  256. margin-top: 20rpx;
  257. .section-title {
  258. font-size: 30rpx;
  259. font-weight: bold;
  260. color: #333;
  261. margin-bottom: 20rpx;
  262. }
  263. .desc-content {
  264. font-size: 28rpx;
  265. color: #666;
  266. line-height: 1.6;
  267. }
  268. }
  269. .receiver-section {
  270. background: #FFFFFF;
  271. padding: 30rpx;
  272. margin-top: 20rpx;
  273. .section-title {
  274. font-size: 30rpx;
  275. font-weight: bold;
  276. color: #333;
  277. margin-bottom: 20rpx;
  278. }
  279. .form-item {
  280. display: flex;
  281. align-items: flex-start;
  282. margin-bottom: 20rpx;
  283. .label {
  284. width: 150rpx;
  285. font-size: 28rpx;
  286. color: #333;
  287. padding-top: 10rpx;
  288. }
  289. .input {
  290. flex: 1;
  291. height: 70rpx;
  292. background: #F5F5F5;
  293. border-radius: 10rpx;
  294. padding: 0 20rpx;
  295. font-size: 28rpx;
  296. }
  297. .textarea {
  298. flex: 1;
  299. height: 150rpx;
  300. background: #F5F5F5;
  301. border-radius: 10rpx;
  302. padding: 20rpx;
  303. font-size: 28rpx;
  304. }
  305. }
  306. }
  307. .bottom-bar {
  308. position: fixed;
  309. bottom: 0;
  310. left: 0;
  311. right: 0;
  312. height: 100rpx;
  313. background: #FFFFFF;
  314. display: flex;
  315. align-items: center;
  316. justify-content: space-between;
  317. padding: 0 30rpx;
  318. padding-bottom: env(safe-area-inset-bottom);
  319. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  320. .points-info {
  321. display: flex;
  322. align-items: center;
  323. .label {
  324. font-size: 26rpx;
  325. color: #666;
  326. }
  327. .value {
  328. font-size: 32rpx;
  329. font-weight: bold;
  330. color: #9C27B0;
  331. margin-left: 10rpx;
  332. }
  333. }
  334. .exchange-btn {
  335. padding: 20rpx 60rpx;
  336. background: linear-gradient(135deg, #9C27B0 0%, #E91E63 100%);
  337. color: #FFFFFF;
  338. font-size: 30rpx;
  339. font-weight: bold;
  340. border-radius: 50rpx;
  341. &.disabled {
  342. background: #CCCCCC;
  343. }
  344. }
  345. }
  346. </style>