product-detail.vue 8.9 KB

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