index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <template>
  2. <view class="page">
  3. <!-- 顶部导航栏 -->
  4. <view class="nav-bar">
  5. <view class="nav-content">
  6. <view class="nav-left" @click="goBack">
  7. <text class="back-icon">←</text>
  8. </view>
  9. <text class="page-title">{{ isWechatPay ? '微信扫码支付' : '支付宝扫码支付' }}</text>
  10. </view>
  11. </view>
  12. <!-- 支付内容 -->
  13. <view class="content">
  14. <!-- 订单信息 -->
  15. <view class="order-card">
  16. <text class="order-title">订单信息</text>
  17. <view class="order-info">
  18. <text class="info-label">商品</text>
  19. <text class="info-value">{{ orderInfo.planName || '-' }}</text>
  20. </view>
  21. <view class="order-info">
  22. <text class="info-label">订单号</text>
  23. <text class="info-value">{{ orderInfo.orderNo || '-' }}</text>
  24. </view>
  25. <view class="order-info highlight">
  26. <text class="info-label">应付金额</text>
  27. <text class="info-value price">¥{{ orderInfo.amount || 0 }}</text>
  28. </view>
  29. </view>
  30. <!-- 二维码 -->
  31. <view class="qrcode-section" v-if="qrcodeUrl">
  32. <text class="qrcode-hint">{{ isWechatPay ? '请使用微信扫码支付' : '请使用支付宝扫码支付' }}</text>
  33. <view class="qrcode-container">
  34. <image class="qrcode" :src="qrcodeImageUrl" mode="aspectFit" show-menu-by-longpress />
  35. </view>
  36. <text class="qrcode-tip">支付成功后页面将自动更新</text>
  37. </view>
  38. <!-- 加载中 -->
  39. <view class="loading-section" v-else-if="loading">
  40. <text class="loading-text">正在生成支付二维码...</text>
  41. </view>
  42. <!-- 错误 -->
  43. <view class="error-section" v-else-if="error">
  44. <text class="error-icon">!</text>
  45. <text class="error-text">{{ error }}</text>
  46. <button class="retry-btn" @click="generateQrcode">重新生成</button>
  47. </view>
  48. <!-- 操作按钮 -->
  49. <view class="actions">
  50. <button class="btn-secondary" @click="goBack">取消支付</button>
  51. <button class="btn-primary" @click="checkPaymentStatus">我已支付</button>
  52. </view>
  53. <!-- 提示 -->
  54. <view class="tips">
  55. <text class="tips-title">温馨提示</text>
  56. <text class="tips-text">• 请在24小时内完成支付</text>
  57. <text class="tips-text">• 支付成功后权益将自动到账</text>
  58. <text class="tips-text">• 如支付遇到问题,请联系客服</text>
  59. </view>
  60. </view>
  61. </view>
  62. </template>
  63. <script setup lang="ts">
  64. import { ref, onMounted, watch, nextTick } from 'vue';
  65. import { post, get } from '../../utils/request';
  66. import QRCode from 'qrcode';
  67. const orderInfo = ref<{
  68. orderNo: string;
  69. amount: number;
  70. planName: string;
  71. }>({
  72. orderNo: '',
  73. amount: 0,
  74. planName: ''
  75. });
  76. const qrcodeUrl = ref('');
  77. const qrcodeImageUrl = ref('');
  78. const isWechatPay = ref(false);
  79. const loading = ref(false);
  80. const error = ref('');
  81. let checkTimer: ReturnType<typeof setInterval> | null = null;
  82. // 本地生成二维码
  83. async function generateQRCodeImage(text: string) {
  84. try {
  85. qrcodeImageUrl.value = await QRCode.toDataURL(text, {
  86. width: 300,
  87. margin: 2,
  88. color: { dark: '#000000', light: '#ffffff' }
  89. });
  90. } catch (e) {
  91. console.error('生成二维码失败:', e);
  92. }
  93. }
  94. watch(qrcodeUrl, (url) => {
  95. if (url) {
  96. generateQRCodeImage(url);
  97. }
  98. });
  99. onMounted(async () => {
  100. // 获取 URL 参数
  101. const pages = getCurrentPages();
  102. const currentPage = pages[pages.length - 1] as any;
  103. // #ifdef APP-PLUS
  104. const options = currentPage?.$page?.options || {};
  105. // #endif
  106. // #ifndef APP-PLUS
  107. const options = currentPage?.options || {};
  108. // #endif
  109. // 检测支付方式
  110. isWechatPay.value = options.qrcode && (options.qrcode.includes('weixin') || options.qrcode.includes('wxpay'));
  111. if (options.orderNo) {
  112. orderInfo.value.orderNo = options.orderNo;
  113. orderInfo.value.amount = parseFloat(options.amount) || 0;
  114. orderInfo.value.planName = decodeURIComponent(options.planName || '会员订阅');
  115. qrcodeUrl.value = decodeURIComponent(options.qrcode || '');
  116. } else if (options.planId) {
  117. // 需要先创建订单
  118. await generateOrderAndQrcode(parseInt(options.planId), options.method === 'wechat' ? 'wechat' : 'alipay');
  119. }
  120. // 开始轮询支付状态
  121. if (qrcodeUrl.value) {
  122. startPaymentCheck();
  123. }
  124. });
  125. async function generateOrderAndQrcode(planId: number, method: 'alipay' | 'wechat' = 'alipay') {
  126. loading.value = true;
  127. error.value = '';
  128. isWechatPay.value = method === 'wechat';
  129. try {
  130. const result = await post<{
  131. orderNo: string;
  132. amount: number;
  133. planName: string;
  134. qrcode: string;
  135. }>('/payment/create', {
  136. planId,
  137. paymentMethod: method
  138. });
  139. orderInfo.value = {
  140. orderNo: result.orderNo,
  141. amount: result.amount,
  142. planName: result.planName
  143. };
  144. qrcodeUrl.value = result.qrcode || '';
  145. // 开始轮询
  146. if (qrcodeUrl.value) {
  147. startPaymentCheck();
  148. }
  149. } catch (e: any) {
  150. error.value = e.message || '生成支付二维码失败';
  151. } finally {
  152. loading.value = false;
  153. }
  154. }
  155. async function generateQrcode() {
  156. if (orderInfo.value.orderNo) {
  157. // 已有订单号,重新获取二维码
  158. await generateOrderAndQrcode(0);
  159. }
  160. }
  161. function startPaymentCheck() {
  162. if (checkTimer) return;
  163. checkTimer = setInterval(async () => {
  164. try {
  165. const result = await get<{ status: string }>(`/payment/orders/${orderInfo.value.orderNo}`);
  166. if (result.status === 'paid') {
  167. clearInterval(checkTimer!);
  168. uni.showModal({
  169. title: '支付成功',
  170. content: '恭喜您,订阅已生效!',
  171. showCancel: false,
  172. success: () => {
  173. uni.switchTab({ url: '/pages/mine/index' });
  174. }
  175. });
  176. }
  177. } catch (e) {
  178. // 忽略错误,继续轮询
  179. }
  180. }, 3000);
  181. }
  182. function stopPaymentCheck() {
  183. if (checkTimer) {
  184. clearInterval(checkTimer);
  185. checkTimer = null;
  186. }
  187. }
  188. async function checkPaymentStatus() {
  189. try {
  190. const result = await get<{ status: string }>(`/payment/orders/${orderInfo.value.orderNo}`);
  191. if (result.status === 'paid') {
  192. uni.showModal({
  193. title: '支付成功',
  194. content: '恭喜您,订阅已生效!',
  195. showCancel: false,
  196. success: () => {
  197. stopPaymentCheck();
  198. uni.switchTab({ url: '/pages/mine/index' });
  199. }
  200. });
  201. } else {
  202. uni.showToast({ title: '暂未收到支付结果,请稍后', icon: 'none' });
  203. }
  204. } catch (e) {
  205. uni.showToast({ title: '查询失败,请稍后重试', icon: 'none' });
  206. }
  207. }
  208. function goBack() {
  209. stopPaymentCheck();
  210. const pages = getCurrentPages();
  211. if (pages.length > 1) {
  212. uni.navigateBack();
  213. } else {
  214. uni.switchTab({ url: '/pages/member/index' });
  215. }
  216. }
  217. </script>
  218. <style scoped>
  219. .page {
  220. min-height: 100vh;
  221. background: #f5f5f5;
  222. }
  223. .nav-bar {
  224. position: fixed;
  225. top: 0;
  226. left: 0;
  227. right: 0;
  228. height: 88rpx;
  229. padding-top: env(safe-area-inset-top);
  230. background: #ffffff;
  231. border-bottom: 1px solid #e5e7eb;
  232. z-index: 100;
  233. }
  234. .nav-content {
  235. height: 100%;
  236. display: flex;
  237. align-items: center;
  238. padding: 0 24rpx;
  239. }
  240. .nav-left {
  241. width: 80rpx;
  242. display: flex;
  243. align-items: center;
  244. }
  245. .back-icon {
  246. font-size: 36rpx;
  247. color: #333;
  248. }
  249. .page-title {
  250. flex: 1;
  251. text-align: center;
  252. font-size: 32rpx;
  253. font-weight: 500;
  254. color: #333;
  255. margin-right: 80rpx;
  256. }
  257. .content {
  258. padding-top: calc(120rpx + env(safe-area-inset-top));
  259. padding-left: 32rpx;
  260. padding-right: 32rpx;
  261. }
  262. .order-card {
  263. background: #ffffff;
  264. border-radius: 16rpx;
  265. padding: 32rpx;
  266. margin-bottom: 32rpx;
  267. }
  268. .order-title {
  269. font-size: 28rpx;
  270. font-weight: 600;
  271. color: #1f2937;
  272. margin-bottom: 24rpx;
  273. display: block;
  274. }
  275. .order-info {
  276. display: flex;
  277. justify-content: space-between;
  278. align-items: center;
  279. padding: 16rpx 0;
  280. border-bottom: 1px solid #f3f4f6;
  281. }
  282. .order-info:last-child {
  283. border-bottom: none;
  284. }
  285. .order-info.highlight {
  286. margin-top: 16rpx;
  287. padding-top: 24rpx;
  288. border-top: 2rpx dashed #e5e7eb;
  289. border-bottom: none;
  290. }
  291. .info-label {
  292. font-size: 26rpx;
  293. color: #9ca3af;
  294. }
  295. .info-value {
  296. font-size: 26rpx;
  297. color: #1f2937;
  298. }
  299. .info-value.price {
  300. font-size: 36rpx;
  301. font-weight: 700;
  302. color: #ef4444;
  303. }
  304. .qrcode-section {
  305. background: #ffffff;
  306. border-radius: 16rpx;
  307. padding: 40rpx;
  308. text-align: center;
  309. margin-bottom: 32rpx;
  310. }
  311. .qrcode-hint {
  312. font-size: 28rpx;
  313. color: #1f2937;
  314. margin-bottom: 32rpx;
  315. display: block;
  316. }
  317. .qrcode-container {
  318. width: 400rpx;
  319. height: 400rpx;
  320. margin: 0 auto 32rpx;
  321. background: #f9fafb;
  322. border-radius: 16rpx;
  323. display: flex;
  324. align-items: center;
  325. justify-content: center;
  326. }
  327. .qrcode {
  328. width: 360rpx;
  329. height: 360rpx;
  330. }
  331. .qrcode-tip {
  332. font-size: 24rpx;
  333. color: #9ca3af;
  334. }
  335. .loading-section {
  336. background: #ffffff;
  337. border-radius: 16rpx;
  338. padding: 80rpx 40rpx;
  339. text-align: center;
  340. margin-bottom: 32rpx;
  341. }
  342. .loading-text {
  343. font-size: 28rpx;
  344. color: #6b7280;
  345. }
  346. .error-section {
  347. background: #ffffff;
  348. border-radius: 16rpx;
  349. padding: 80rpx 40rpx;
  350. text-align: center;
  351. margin-bottom: 32rpx;
  352. }
  353. .error-icon {
  354. width: 80rpx;
  355. height: 80rpx;
  356. background: #fef2f2;
  357. color: #ef4444;
  358. border-radius: 50%;
  359. display: flex;
  360. align-items: center;
  361. justify-content: center;
  362. font-size: 40rpx;
  363. font-weight: bold;
  364. margin: 0 auto 24rpx;
  365. }
  366. .error-text {
  367. font-size: 28rpx;
  368. color: #6b7280;
  369. display: block;
  370. margin-bottom: 32rpx;
  371. }
  372. .retry-btn {
  373. padding: 16rpx 48rpx;
  374. background: #4f46e5;
  375. color: #ffffff;
  376. border-radius: 40rpx;
  377. font-size: 28rpx;
  378. border: none;
  379. }
  380. .actions {
  381. display: flex;
  382. gap: 24rpx;
  383. margin-bottom: 32rpx;
  384. }
  385. .btn-primary,
  386. .btn-secondary {
  387. flex: 1;
  388. height: 88rpx;
  389. border-radius: 44rpx;
  390. display: flex;
  391. align-items: center;
  392. justify-content: center;
  393. font-size: 30rpx;
  394. border: none;
  395. }
  396. .btn-primary {
  397. background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  398. color: #ffffff;
  399. }
  400. .btn-secondary {
  401. background: #ffffff;
  402. color: #6b7280;
  403. border: 2rpx solid #e5e7eb;
  404. }
  405. .tips {
  406. background: #fef3c7;
  407. border-radius: 16rpx;
  408. padding: 24rpx;
  409. margin-bottom: 48rpx;
  410. }
  411. .tips-title {
  412. font-size: 24rpx;
  413. font-weight: 600;
  414. color: #92400e;
  415. display: block;
  416. margin-bottom: 12rpx;
  417. }
  418. .tips-text {
  419. font-size: 22rpx;
  420. color: #78350f;
  421. display: block;
  422. margin-bottom: 6rpx;
  423. }
  424. </style>