payment.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <template>
  2. <view class="container">
  3. <!-- 顶部导航栏 -->
  4. <view class="header">
  5. <view class="back-btn" @click="goBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <text class="header-title">支付</text>
  9. <view class="header-right"></view>
  10. </view>
  11. <scroll-view class="scroll-content" scroll-y>
  12. <!-- 支付金额 -->
  13. <view class="amount-section">
  14. <text class="amount-label">支付金额</text>
  15. <text class="amount-value">¥ {{ paymentAmount.toFixed(2) }}</text>
  16. </view>
  17. <!-- 支付方式选择 -->
  18. <view class="payment-card">
  19. <view
  20. class="payment-item"
  21. v-for="(method, index) in paymentMethods"
  22. :key="index"
  23. @click="selectPaymentMethod(index)"
  24. >
  25. <view class="payment-left">
  26. <view class="payment-icon" :style="{ backgroundColor: method.iconBg }">
  27. <text class="payment-icon-text">{{ method.icon }}</text>
  28. </view>
  29. <text class="payment-name">{{ method.name }}</text>
  30. </view>
  31. <view class="payment-radio" :class="{ checked: selectedMethod === index }">
  32. <view class="radio-dot" v-if="selectedMethod === index"></view>
  33. </view>
  34. </view>
  35. </view>
  36. </scroll-view>
  37. <!-- 确认支付按钮 -->
  38. <view class="confirm-section">
  39. <button class="confirm-btn" @click="confirmPayment">确认支付</button>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import { purchaseVip } from '@/utils/api.js'
  45. export default {
  46. data() {
  47. return {
  48. paymentAmount: 30.00,
  49. selectedMethod: 0,
  50. planName: '',
  51. planType: 'month',
  52. planId: 0,
  53. isProcessing: false,
  54. paymentMethods: [
  55. {
  56. name: '微信支付',
  57. icon: '💬',
  58. iconBg: '#07C160',
  59. type: 'wechat'
  60. },
  61. {
  62. name: '支付宝支付',
  63. icon: '💰',
  64. iconBg: '#1677FF',
  65. type: 'alipay'
  66. },
  67. {
  68. name: '其他支付',
  69. icon: '💳',
  70. iconBg: '#999999',
  71. type: 'other'
  72. }
  73. ]
  74. }
  75. },
  76. onLoad(options) {
  77. if (options.price) {
  78. this.paymentAmount = parseFloat(options.price)
  79. }
  80. if (options.planName) {
  81. this.planName = decodeURIComponent(options.planName)
  82. }
  83. if (options.planType) {
  84. this.planType = options.planType
  85. }
  86. if (options.planId) {
  87. this.planId = parseInt(options.planId)
  88. }
  89. },
  90. methods: {
  91. goBack() {
  92. uni.navigateBack()
  93. },
  94. selectPaymentMethod(index) {
  95. this.selectedMethod = index
  96. },
  97. async confirmPayment() {
  98. // 获取用户信息
  99. const userInfo = uni.getStorageSync('userInfo')
  100. if (!userInfo || !userInfo.id) {
  101. uni.showToast({
  102. title: '请先登录',
  103. icon: 'none'
  104. })
  105. setTimeout(() => {
  106. uni.navigateTo({
  107. url: '/pages/login/login'
  108. })
  109. }, 1500)
  110. return
  111. }
  112. if (this.isProcessing) {
  113. return
  114. }
  115. const method = this.paymentMethods[this.selectedMethod]
  116. uni.showModal({
  117. title: '确认支付',
  118. content: `使用${method.name}支付 ¥${this.paymentAmount.toFixed(2)}?`,
  119. success: async (res) => {
  120. if (res.confirm) {
  121. await this.processPayment(method, userInfo.id)
  122. }
  123. }
  124. })
  125. },
  126. async processPayment(method, userId) {
  127. this.isProcessing = true
  128. uni.showLoading({
  129. title: '支付中...',
  130. mask: true
  131. })
  132. try {
  133. // 调用购买VIP接口
  134. const purchaseData = {
  135. userId: userId,
  136. vipType: this.planType,
  137. vipName: this.planName,
  138. price: this.paymentAmount,
  139. paymentMethod: method.type
  140. }
  141. const res = await purchaseVip(purchaseData)
  142. uni.hideLoading()
  143. this.isProcessing = false
  144. if (res && res.code === 200) {
  145. uni.showToast({
  146. title: '支付成功',
  147. icon: 'success'
  148. })
  149. // 更新本地用户信息中的VIP状态
  150. if (userInfo) {
  151. userInfo.isVip = true
  152. if (res.data && res.data.expireTime) {
  153. // 可以更新过期时间(如果返回了)
  154. }
  155. uni.setStorageSync('userInfo', userInfo)
  156. }
  157. // 支付成功后跳转
  158. setTimeout(() => {
  159. uni.navigateBack({
  160. delta: 2 // 返回VIP页面再返回上一页
  161. })
  162. }, 1500)
  163. } else {
  164. uni.showToast({
  165. title: res.message || '支付失败,请重试',
  166. icon: 'none'
  167. })
  168. }
  169. } catch (error) {
  170. uni.hideLoading()
  171. this.isProcessing = false
  172. console.error('支付失败:', error)
  173. uni.showToast({
  174. title: error.message || '支付失败,请重试',
  175. icon: 'none'
  176. })
  177. }
  178. }
  179. }
  180. }
  181. </script>
  182. <style scoped>
  183. .container {
  184. width: 100%;
  185. height: 100vh;
  186. min-height: 100vh;
  187. background-color: #F5F5F5;
  188. display: flex;
  189. padding-top: 30px;
  190. box-sizing: border-box;
  191. flex-direction: column;
  192. box-sizing: border-box;
  193. overflow: hidden;
  194. }
  195. .header {
  196. display: flex;
  197. align-items: center;
  198. justify-content: space-between;
  199. padding: 20rpx 30rpx;
  200. padding-top: calc(20rpx + env(safe-area-inset-top));
  201. background-color: #FFFFFF;
  202. border-bottom: 1rpx solid #F0F0F0;
  203. position: relative;
  204. flex-shrink: 0;
  205. box-sizing: border-box;
  206. }
  207. .back-btn {
  208. width: 60rpx;
  209. height: 60rpx;
  210. display: flex;
  211. align-items: center;
  212. justify-content: center;
  213. z-index: 10;
  214. }
  215. .back-icon {
  216. font-size: 40rpx;
  217. color: #333333;
  218. font-weight: bold;
  219. }
  220. .header-title {
  221. position: absolute;
  222. left: 50%;
  223. transform: translateX(-50%);
  224. font-size: 36rpx;
  225. font-weight: bold;
  226. color: #333333;
  227. }
  228. .header-right {
  229. width: 60rpx;
  230. }
  231. .scroll-content {
  232. flex: 1;
  233. width: 100%;
  234. height: 0;
  235. overflow: hidden;
  236. padding: 40rpx 30rpx;
  237. padding-bottom: calc(40rpx + env(safe-area-inset-bottom));
  238. box-sizing: border-box;
  239. }
  240. .amount-section {
  241. display: flex;
  242. flex-direction: column;
  243. align-items: center;
  244. justify-content: center;
  245. padding: 60rpx 0;
  246. margin-bottom: 40rpx;
  247. }
  248. .amount-label {
  249. font-size: 28rpx;
  250. color: #999999;
  251. margin-bottom: 20rpx;
  252. }
  253. .amount-value {
  254. font-size: 64rpx;
  255. font-weight: bold;
  256. color: #333333;
  257. }
  258. .payment-card {
  259. background-color: #FFFFFF;
  260. border-radius: 16rpx;
  261. overflow: hidden;
  262. }
  263. .payment-item {
  264. display: flex;
  265. align-items: center;
  266. justify-content: space-between;
  267. padding: 30rpx;
  268. border-bottom: 1rpx solid #F0F0F0;
  269. }
  270. .payment-item:last-child {
  271. border-bottom: none;
  272. }
  273. .payment-left {
  274. display: flex;
  275. align-items: center;
  276. flex: 1;
  277. }
  278. .payment-icon {
  279. width: 80rpx;
  280. height: 80rpx;
  281. border-radius: 50%;
  282. display: flex;
  283. align-items: center;
  284. justify-content: center;
  285. margin-right: 30rpx;
  286. }
  287. .payment-icon-text {
  288. font-size: 40rpx;
  289. }
  290. .payment-name {
  291. font-size: 30rpx;
  292. color: #333333;
  293. }
  294. .payment-radio {
  295. width: 40rpx;
  296. height: 40rpx;
  297. border-radius: 50%;
  298. border: 2rpx solid #CCCCCC;
  299. display: flex;
  300. align-items: center;
  301. justify-content: center;
  302. background-color: #FFFFFF;
  303. }
  304. .payment-radio.checked {
  305. border-color: #4FC3F7;
  306. background-color: #4FC3F7;
  307. }
  308. .radio-dot {
  309. width: 16rpx;
  310. height: 16rpx;
  311. border-radius: 50%;
  312. background-color: #FFFFFF;
  313. }
  314. .confirm-section {
  315. padding: 30rpx;
  316. background-color: #FFFFFF;
  317. border-top: 1rpx solid #F0F0F0;
  318. padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
  319. flex-shrink: 0;
  320. box-sizing: border-box;
  321. }
  322. .confirm-btn {
  323. width: 100%;
  324. height: 88rpx;
  325. background-color: #4FC3F7;
  326. color: #FFFFFF;
  327. font-size: 32rpx;
  328. font-weight: bold;
  329. border: none;
  330. border-radius: 44rpx;
  331. display: flex;
  332. align-items: center;
  333. justify-content: center;
  334. }
  335. .confirm-btn::after {
  336. border: none;
  337. }
  338. </style>