product-detail.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <view class="container">
  3. <view v-if="loading" class="loading-wrap">
  4. <text class="loading-text">加载中...</text>
  5. </view>
  6. <scroll-view v-else-if="product.id" scroll-y class="content">
  7. <image
  8. class="cover"
  9. :src="product.coverImage || '/static/default-product.png'"
  10. mode="widthFix"
  11. @error="onImageError"
  12. />
  13. <view class="info-section">
  14. <view class="name-row">
  15. <text class="product-name">{{ product.name }}</text>
  16. </view>
  17. <view class="price-row">
  18. <text class="price">{{ formatPriceWithSymbol(product.price) }}</text>
  19. <text v-if="product.memberPrice" class="member-price">会员价 {{ formatPriceWithSymbol(product.memberPrice) }}</text>
  20. </view>
  21. <view class="meta-row">
  22. <text class="meta-item" v-if="product.productType">{{ typeLabel }}</text>
  23. <text class="meta-item" v-if="product.domain">{{ domainLabel }}</text>
  24. <text class="meta-item">库存 {{ product.stock || 0 }}</text>
  25. </view>
  26. <view class="vendor-row">
  27. <text class="vendor-label">供应商:</text>
  28. <text class="vendor-name">{{ product.vendorName || '平台官方' }}</text>
  29. </view>
  30. </view>
  31. <view class="desc-section">
  32. <text class="section-title">商品介绍</text>
  33. <text class="desc-text">{{ product.description || '暂无介绍' }}</text>
  34. </view>
  35. <view v-if="product.intro" class="intro-section">
  36. <text class="section-title">详细介绍</text>
  37. <text class="intro-text">{{ product.intro }}</text>
  38. </view>
  39. <view class="bottom-bar">
  40. <view class="price-info">
  41. <text class="bottom-price">{{ formatPriceWithSymbol(product.price) }}</text>
  42. <text v-if="product.memberPrice" class="bottom-member">会员{{ formatPriceWithSymbol(product.memberPrice) }}</text>
  43. </view>
  44. <view class="action-btns">
  45. <view class="action-extra">
  46. <button class="btn-cart" @click="onAddToCart">加入购物车</button>
  47. </view>
  48. <button class="btn-buy" @click="onBuy">立即购买</button>
  49. </view>
  50. </view>
  51. </scroll-view>
  52. <view v-else class="empty-wrap">
  53. <text class="empty-text">商品不存在或已下架</text>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. import { productDetail, productOrderCreate, productOrderPay, cartAdd } from '@/utils/api.js'
  59. export default {
  60. data() {
  61. return {
  62. product: {},
  63. loading: false
  64. }
  65. },
  66. computed: {
  67. typeLabel() {
  68. const map = {
  69. activity: '活动',
  70. course: '课程',
  71. physical: '实物',
  72. digital: '数字',
  73. service: '服务'
  74. }
  75. return map[this.product.productType] || this.product.productType
  76. },
  77. domainLabel() {
  78. const map = {
  79. action: '行动',
  80. mind: '心智',
  81. body: '身体',
  82. wisdom: '智慧',
  83. wealth: '财富'
  84. }
  85. return map[this.product.domain] || this.product.domain
  86. }
  87. },
  88. onLoad(options) {
  89. if (options.id) {
  90. this.loadDetail(options.id)
  91. }
  92. },
  93. methods: {
  94. onImageError(e) {
  95. // 图片加载失败时,使用占位图(default-activity.png 已存在于 static/)
  96. this.product.coverImage = '/static/default-activity.png'
  97. },
  98. loadDetail(id) {
  99. this.loading = true
  100. productDetail({ id: parseInt(id) }).then(res => {
  101. this.loading = false
  102. if (res.code === 200 && res.data) {
  103. this.product = res.data
  104. }
  105. }).catch(() => {
  106. this.loading = false
  107. })
  108. },
  109. onBuy() {
  110. const userId = uni.getStorageSync('userId')
  111. if (!userId) {
  112. uni.navigateTo({ url: '/pages/login/login' })
  113. return
  114. }
  115. uni.showLoading({ title: '创建订单...' })
  116. productOrderCreate({
  117. productId: this.product.id,
  118. quantity: 1,
  119. paymentMethod: 'wechat'
  120. }).then(res => {
  121. uni.hideLoading()
  122. if (res.code === 200 && res.data) {
  123. const orderNo = res.data.orderNo
  124. uni.showModal({
  125. title: '订单已创建',
  126. content: '订单号:' + orderNo + ',确定支付吗?',
  127. success: (confirm) => {
  128. if (confirm.confirm) {
  129. this.doPay(orderNo)
  130. }
  131. }
  132. })
  133. } else {
  134. uni.showToast({ title: res.message || '创建订单失败', icon: 'none' })
  135. }
  136. }).catch(() => {
  137. uni.hideLoading()
  138. uni.showToast({ title: '创建订单失败', icon: 'none' })
  139. })
  140. },
  141. onAddToCart() {
  142. const userId = uni.getStorageSync('userId')
  143. if (!userId) {
  144. uni.navigateTo({ url: '/pages/login/login' })
  145. return
  146. }
  147. uni.showLoading({ title: '加入购物车...' })
  148. cartAdd({
  149. userId: userId,
  150. productId: this.product.id,
  151. quantity: 1
  152. }).then(res => {
  153. uni.hideLoading()
  154. if (res.code === 200) {
  155. uni.showToast({ title: '已加入购物车', icon: 'success' })
  156. } else {
  157. uni.showToast({ title: res.message || '加入购物车失败', icon: 'none' })
  158. }
  159. }).catch(() => {
  160. uni.hideLoading()
  161. uni.showToast({ title: '网络异常', icon: 'none' })
  162. })
  163. },
  164. doPay(orderNo) {
  165. uni.showLoading({ title: '支付中...' })
  166. productOrderPay({ orderNo }).then(res => {
  167. uni.hideLoading()
  168. if (res.code === 200) {
  169. uni.showToast({ title: '支付成功', icon: 'success' })
  170. setTimeout(() => {
  171. uni.navigateTo({ url: '/pages/shop/order-list/order-list' })
  172. }, 1500)
  173. } else {
  174. uni.showToast({ title: res.message || '支付失败', icon: 'none' })
  175. }
  176. }).catch(() => {
  177. uni.hideLoading()
  178. uni.showToast({ title: '支付失败', icon: 'none' })
  179. })
  180. }
  181. }
  182. }
  183. </script>
  184. <style scoped>
  185. .container {
  186. min-height: 100vh;
  187. background: #f5f5f5;
  188. }
  189. .loading-wrap,
  190. .empty-wrap {
  191. display: flex;
  192. justify-content: center;
  193. padding-top: 200rpx;
  194. }
  195. .loading-text,
  196. .empty-text {
  197. font-size: 28rpx;
  198. color: #999;
  199. }
  200. .content {
  201. height: calc(100vh - 120rpx);
  202. padding-bottom: 120rpx;
  203. }
  204. .cover {
  205. width: 100%;
  206. background: #f0f0f0;
  207. }
  208. .info-section {
  209. background: #fff;
  210. padding: 30rpx;
  211. margin-bottom: 20rpx;
  212. }
  213. .name-row {
  214. margin-bottom: 16rpx;
  215. }
  216. .product-name {
  217. font-size: 34rpx;
  218. font-weight: bold;
  219. color: #333;
  220. line-height: 1.4;
  221. }
  222. .price-row {
  223. display: flex;
  224. align-items: baseline;
  225. margin-bottom: 16rpx;
  226. }
  227. .price {
  228. font-size: 40rpx;
  229. color: #F97316;
  230. font-weight: bold;
  231. margin-right: 20rpx;
  232. }
  233. .member-price {
  234. font-size: 26rpx;
  235. color: #999;
  236. }
  237. .meta-row {
  238. display: flex;
  239. flex-wrap: wrap;
  240. margin-bottom: 12rpx;
  241. }
  242. .meta-item {
  243. font-size: 22rpx;
  244. color: #999;
  245. background: #f5f5f5;
  246. padding: 4rpx 16rpx;
  247. border-radius: 4rpx;
  248. margin-right: 12rpx;
  249. margin-bottom: 8rpx;
  250. }
  251. .vendor-row {
  252. display: flex;
  253. align-items: center;
  254. }
  255. .vendor-label {
  256. font-size: 24rpx;
  257. color: #999;
  258. }
  259. .vendor-name {
  260. font-size: 24rpx;
  261. color: #666;
  262. }
  263. .desc-section,
  264. .intro-section {
  265. background: #fff;
  266. padding: 30rpx;
  267. margin-bottom: 20rpx;
  268. }
  269. .section-title {
  270. font-size: 28rpx;
  271. font-weight: bold;
  272. color: #333;
  273. margin-bottom: 16rpx;
  274. display: block;
  275. }
  276. .desc-text,
  277. .intro-text {
  278. font-size: 26rpx;
  279. color: #666;
  280. line-height: 1.6;
  281. display: block;
  282. }
  283. .bottom-bar {
  284. position: fixed;
  285. bottom: 0;
  286. left: 0;
  287. right: 0;
  288. height: 100rpx;
  289. background: #fff;
  290. border-top: 1rpx solid #eee;
  291. display: flex;
  292. align-items: center;
  293. padding: 0 30rpx;
  294. justify-content: space-between;
  295. z-index: 100;
  296. }
  297. .price-info {
  298. display: flex;
  299. align-items: baseline;
  300. }
  301. .bottom-price {
  302. font-size: 36rpx;
  303. color: #F97316;
  304. font-weight: bold;
  305. }
  306. .bottom-member {
  307. font-size: 24rpx;
  308. color: #999;
  309. margin-left: 10rpx;
  310. }
  311. .action-btns {
  312. display: flex;
  313. align-items: center;
  314. }
  315. .btn-buy {
  316. background: linear-gradient(135deg, #F97316, #FB923C);
  317. color: #fff;
  318. font-size: 28rpx;
  319. font-weight: bold;
  320. padding: 0 60rpx;
  321. height: 72rpx;
  322. line-height: 72rpx;
  323. border-radius: 36rpx;
  324. border: none;
  325. }
  326. .btn-buy::after {
  327. border: none;
  328. }
  329. .action-extra {
  330. display: flex;
  331. align-items: center;
  332. margin-right: 20rpx;
  333. }
  334. .btn-favorite {
  335. font-size: 40rpx;
  336. margin-right: 16rpx;
  337. padding: 0 8rpx;
  338. }
  339. .btn-cart {
  340. background: #fff;
  341. color: #F97316;
  342. border: 2rpx solid #F97316;
  343. font-size: 26rpx;
  344. padding: 0 24rpx;
  345. height: 64rpx;
  346. line-height: 64rpx;
  347. border-radius: 32rpx;
  348. }
  349. .btn-cart::after {
  350. border: none;
  351. }
  352. </style>