product-detail.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. }
  103. }
  104. await Promise.all([
  105. this.loadProduct(),
  106. this.loadBalance()
  107. ])
  108. },
  109. async loadProduct() {
  110. if (!this.productId) return
  111. try {
  112. const res = await api.pointsMall.getProductDetail(this.productId)
  113. this.product = res || {}
  114. } catch (e) {
  115. uni.showToast({ title: '获取商品详情失败', icon: 'none' })
  116. }
  117. },
  118. async loadBalance() {
  119. if (!this.makerId) return
  120. try {
  121. const res = await api.pointsMall.getBalance(this.makerId)
  122. this.userPoints = res.balance || 0
  123. } catch (e) {
  124. }
  125. },
  126. handleBack() {
  127. uni.navigateBack()
  128. },
  129. async handleExchange() {
  130. if (!this.canExchange) {
  131. if (this.userPoints < this.product.pointsPrice) {
  132. uni.showToast({ title: '积分不足', icon: 'none' })
  133. } else if (this.product.stock <= 0) {
  134. uni.showToast({ title: '库存不足', icon: 'none' })
  135. } else if (!this.contactPhone || this.contactPhone.length !== 11) {
  136. uni.showToast({ title: '请输入正确的11位手机号', icon: 'none' })
  137. }
  138. return
  139. }
  140. uni.showModal({
  141. title: '确认兑换',
  142. content: `确定使用 ${this.product.pointsPrice} 积分兑换「${this.product.name}」吗?`,
  143. success: async (res) => {
  144. if (res.confirm) {
  145. await this.doExchange()
  146. }
  147. }
  148. })
  149. },
  150. async doExchange() {
  151. this.loading = true
  152. try {
  153. const data = {
  154. makerId: this.makerId,
  155. productId: this.productId,
  156. quantity: 1,
  157. contactPhone: this.contactPhone
  158. }
  159. await api.pointsMall.exchange(data)
  160. uni.showToast({ title: '提交成功,等待审核', icon: 'success' })
  161. // 刷新积分余额
  162. await this.loadBalance()
  163. // 延迟返回
  164. setTimeout(() => {
  165. uni.navigateBack()
  166. }, 1500)
  167. } catch (e) {
  168. uni.showToast({ title: e.message || '兑换失败', icon: 'none' })
  169. } finally {
  170. this.loading = false
  171. }
  172. }
  173. }
  174. }
  175. </script>
  176. <style lang="scss" scoped>
  177. .product-detail {
  178. min-height: 100vh;
  179. background: #F5F5F5;
  180. padding-bottom: 120rpx;
  181. }
  182. .header {
  183. display: flex;
  184. align-items: center;
  185. justify-content: space-between;
  186. padding: 25rpx 30rpx;
  187. padding-top: calc(25rpx + env(safe-area-inset-top));
  188. background: #FFFFFF;
  189. .back-icon {
  190. width: 44rpx;
  191. height: 44rpx;
  192. display: flex;
  193. align-items: center;
  194. justify-content: center;
  195. font-size: 32rpx;
  196. color: #333;
  197. &::before { content: '‹'; }
  198. }
  199. .header-title {
  200. font-size: 36rpx;
  201. font-weight: bold;
  202. color: #333;
  203. }
  204. .header-right {
  205. width: 44rpx;
  206. }
  207. }
  208. .product-image-section {
  209. width: 100%;
  210. height: 600rpx;
  211. background: #FFFFFF;
  212. display: flex;
  213. align-items: center;
  214. justify-content: center;
  215. .product-image {
  216. width: 100%;
  217. height: 100%;
  218. object-fit: contain;
  219. }
  220. }
  221. .product-info-section {
  222. background: #FFFFFF;
  223. padding: 30rpx;
  224. margin-top: 20rpx;
  225. .product-name {
  226. font-size: 36rpx;
  227. font-weight: bold;
  228. color: #333;
  229. margin-bottom: 20rpx;
  230. }
  231. .product-price {
  232. display: flex;
  233. align-items: baseline;
  234. margin-bottom: 15rpx;
  235. .price-value {
  236. font-size: 48rpx;
  237. font-weight: bold;
  238. color: #9C27B0;
  239. }
  240. .price-unit {
  241. font-size: 28rpx;
  242. color: #9C27B0;
  243. margin-left: 10rpx;
  244. }
  245. }
  246. .product-stock {
  247. font-size: 26rpx;
  248. color: #999;
  249. }
  250. }
  251. .product-desc-section {
  252. background: #FFFFFF;
  253. padding: 30rpx;
  254. margin-top: 20rpx;
  255. .section-title {
  256. font-size: 30rpx;
  257. font-weight: bold;
  258. color: #333;
  259. margin-bottom: 20rpx;
  260. }
  261. .desc-content {
  262. font-size: 28rpx;
  263. color: #666;
  264. line-height: 1.6;
  265. }
  266. }
  267. .receiver-section {
  268. background: #FFFFFF;
  269. padding: 30rpx;
  270. margin-top: 20rpx;
  271. .section-title {
  272. font-size: 30rpx;
  273. font-weight: bold;
  274. color: #333;
  275. margin-bottom: 20rpx;
  276. }
  277. .form-item {
  278. display: flex;
  279. align-items: center;
  280. margin-bottom: 20rpx;
  281. .label {
  282. width: 150rpx;
  283. font-size: 28rpx;
  284. color: #333;
  285. }
  286. .input {
  287. flex: 1;
  288. height: 70rpx;
  289. background: #F5F5F5;
  290. border-radius: 10rpx;
  291. padding: 0 20rpx;
  292. font-size: 28rpx;
  293. }
  294. }
  295. .tips {
  296. font-size: 24rpx;
  297. color: #999;
  298. margin-top: 10rpx;
  299. padding: 15rpx;
  300. background: #FFF8E1;
  301. border-radius: 10rpx;
  302. line-height: 1.5;
  303. }
  304. }
  305. .bottom-bar {
  306. position: fixed;
  307. bottom: 0;
  308. left: 0;
  309. right: 0;
  310. height: 100rpx;
  311. background: #FFFFFF;
  312. display: flex;
  313. align-items: center;
  314. justify-content: space-between;
  315. padding: 0 30rpx;
  316. padding-bottom: env(safe-area-inset-bottom);
  317. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  318. .points-info {
  319. display: flex;
  320. align-items: center;
  321. .label {
  322. font-size: 26rpx;
  323. color: #666;
  324. }
  325. .value {
  326. font-size: 32rpx;
  327. font-weight: bold;
  328. color: #9C27B0;
  329. margin-left: 10rpx;
  330. }
  331. }
  332. .exchange-btn {
  333. padding: 20rpx 60rpx;
  334. background: linear-gradient(135deg, #9C27B0 0%, #E91E63 100%);
  335. color: #FFFFFF;
  336. font-size: 30rpx;
  337. font-weight: bold;
  338. border-radius: 50rpx;
  339. &.disabled {
  340. background: #CCCCCC;
  341. }
  342. }
  343. }
  344. </style>