WxPay.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <view class="pay-container">
  3. <!-- 加载状态 -->
  4. <view v-if="loading" class="loading-wrap flex-center">
  5. <uni-load-more type="loading" color="#f8c008"></uni-load-more>
  6. <text class="loading-text">正在初始化支付...</text>
  7. </view>
  8. <!-- 订单信息 & 支付区域 -->
  9. <view v-else-if="orderInfo" class="pay-content">
  10. <view class="order-card">
  11. <view class="order-item price-highlight">
  12. <text class="label">支付金额:</text>
  13. <view class="value price">
  14. <text class="currency">¥</text>
  15. <text class="amount-text">{{ orderInfo.amount }}</text>
  16. <text class="unit">元</text>
  17. </view>
  18. </view>
  19. <view class="order-item" v-if="orderInfo.description">
  20. <text class="label">订单描述:</text>
  21. <text class="value">{{ orderInfo.description }}</text>
  22. </view>
  23. <view class="order-item" v-if="orderInfo.orderNo">
  24. <text class="label">订单号:</text>
  25. <text class="value">{{ orderInfo.orderNo }}</text>
  26. </view>
  27. </view>
  28. <!-- 支付按钮 -->
  29. <view class="pay-btn-wrap flex-center" @click="handlePay">
  30. <button class="pay-btn" :disabled="paying">
  31. <text v-if="!paying" class="btn-text">立即支付</text>
  32. <text v-else class="btn-text">支付中...</text>
  33. </button>
  34. </view>
  35. </view>
  36. <!-- 异常提示 -->
  37. <view v-else class="error-wrap flex-center">
  38. <text class="error-text">{{ errorMsg }}</text>
  39. <button class="back-btn" @click="goBackToWebview">返回上一页</button>
  40. </view>
  41. <!-- 微信支付组件-->
  42. <WechatPayment ref="wechatPaymentRef" />
  43. </view>
  44. </template>
  45. <script setup>
  46. import { ref } from "vue";
  47. import { onLoad } from "@dcloudio/uni-app";
  48. import { useAppStore } from "@/stores/app";
  49. import { getUserOpenId } from "@/api/user";
  50. import { Calc } from "@/utils/util";
  51. import { goldPrincipalCompleteOrder } from "@/api/payment";
  52. import { recyclePreOrderSuccessAPI } from "@/api/functions";
  53. import WechatPayment from "@/components/payment/WechatPayment.vue";
  54. // 状态管理
  55. const appStore = useAppStore();
  56. const wechatPaymentRef = ref(null);
  57. // 页面核心数据
  58. const loading = ref(true);
  59. const paying = ref(false);
  60. const amount = ref(0);
  61. const orderNo = ref(0);
  62. const description = ref("");
  63. const returnUrl = ref("");
  64. const orderPrefix = ref("");
  65. const orderInfo = ref(null);
  66. const errorMsg = ref("");
  67. onLoad(async (options) => {
  68. try {
  69. amount.value = Number(options.amount) || 0.01;
  70. description.value = decodeURIComponent(options.description || "");
  71. orderPrefix.value = options.orderPrefix || "H5";
  72. orderNo.value = options.orderNo || "";
  73. returnUrl.value = options.returnUrl || "";
  74. if (isNaN(amount.value) || amount.value <= 0) {
  75. throw new Error("支付金额异常(需大于0分)");
  76. }
  77. if (!description.value && !orderNo.value) {
  78. throw new Error("订单描述或订单号其中一项不能为空");
  79. }
  80. if (!returnUrl.value) {
  81. throw new Error("支付完成返回地址不能为空");
  82. }
  83. orderInfo.value = {
  84. amount: Calc.div(amount.value, 100).truncate().valueOf(),
  85. description: description.value,
  86. orderNo: orderNo.value,
  87. };
  88. loading.value = false;
  89. } catch (err) {
  90. loading.value = false;
  91. errorMsg.value = err.message || "页面参数异常";
  92. console.error("页面初始化失败:", err);
  93. }
  94. });
  95. const handlePay = async () => {
  96. if (paying.value) return;
  97. if (!appStore.isLogin) {
  98. uni.showToast({ title: "请先登录", icon: "none" });
  99. return;
  100. }
  101. const userOpenId = appStore.$userInfo?.openId;
  102. if (!userOpenId) {
  103. uni.showToast({ title: "用户信息异常,请重新登录", icon: "none" });
  104. return;
  105. }
  106. paying.value = true;
  107. try {
  108. wechatPaymentRef.value.createUniPay({
  109. amount: amount.value,
  110. description: description.value,
  111. openId: userOpenId,
  112. orderPrefix: orderPrefix.value,
  113. orderNo: orderNo.value,
  114. onUniPayCreate: async (payRes) => {
  115. console.log("支付订单创建成功:", payRes);
  116. },
  117. onUniPaySuccess: async (payStatusRes) => {
  118. console.log("支付成功:", payStatusRes);
  119. await processOrderBizChange(payStatusRes);
  120. paying.value = false;
  121. uni.showToast({
  122. title: "您已支付成功,将返回订单列表",
  123. icon: "success",
  124. });
  125. setTimeout(() => {
  126. goBackToWebview();
  127. }, 1300);
  128. },
  129. onUniPayCancel: () => {
  130. paying.value = false;
  131. uni.showToast({ title: "你已取消支付", icon: "none" });
  132. },
  133. onUniPayFail: (err) => {
  134. paying.value = false;
  135. const errMsg = err || "支付失败,请重试";
  136. uni.showToast({ title: errMsg, icon: "none" });
  137. console.error("支付失败:", err);
  138. },
  139. });
  140. } catch (err) {
  141. paying.value = false;
  142. uni.showToast({ title: "支付接口调用失败", icon: "none" });
  143. console.error("支付调用异常:", err);
  144. }
  145. };
  146. const processOrderBizChange = async (payStatusRes) => {
  147. try {
  148. switch (payStatusRes.orderType) {
  149. case "recyle":
  150. await recyclePreOrderSuccessAPI({
  151. orderNo: payStatusRes.outTradeNo,
  152. payType: "routine",
  153. });
  154. break;
  155. case "tl":
  156. await goldPrincipalCompleteOrder({
  157. orderNo: payStatusRes.outTradeNo,
  158. });
  159. break;
  160. case "svip":
  161. break;
  162. }
  163. } catch (error) {
  164. uni.showToast({ title: error || "业务订单状态更改失败", icon: "none" });
  165. console.error("业务订单状态更改失败:", error);
  166. }
  167. };
  168. const goBackToWebview = () => {
  169. if (!returnUrl.value) {
  170. uni.showToast({ title: "返回地址异常", icon: "none" });
  171. return;
  172. }
  173. let finalUrl = returnUrl.value;
  174. try {
  175. if (finalUrl.includes("%")) {
  176. finalUrl = decodeURIComponent(finalUrl);
  177. }
  178. } catch (e) {
  179. console.warn("URL解码失败,使用原始URL:", e);
  180. }
  181. uni.redirectTo({
  182. url: `/pages/webview/index?path=${returnUrl.value}`,
  183. });
  184. };
  185. </script>
  186. <style scoped lang="scss">
  187. $primary: #f8c008;
  188. $primary-dark: #e6b007;
  189. $primary-light: #fff5cc;
  190. $primary-disabled: #fad966;
  191. .pay-container {
  192. min-height: 100vh;
  193. background-color: #fafafa;
  194. padding: 30rpx;
  195. }
  196. // 加载状态
  197. .loading-wrap {
  198. flex-direction: column;
  199. height: 60vh;
  200. .loading-text {
  201. margin-top: 20rpx;
  202. font-size: 28rpx;
  203. color: #666;
  204. }
  205. }
  206. // 订单信息卡片
  207. .pay-content {
  208. .order-card {
  209. background-color: #fff;
  210. border-radius: 20rpx;
  211. padding: 40rpx 32rpx;
  212. margin-bottom: 80rpx;
  213. box-shadow: 0 6rpx 30rpx rgba(0, 0, 0, 0.04);
  214. border: 1px solid $primary-light;
  215. .order-item {
  216. display: flex;
  217. align-items: center;
  218. margin-bottom: 28rpx;
  219. &:last-child {
  220. margin-bottom: 0;
  221. }
  222. .label {
  223. font-size: 28rpx;
  224. color: #777;
  225. width: 160rpx;
  226. font-weight: 500;
  227. }
  228. .value {
  229. font-size: 28rpx;
  230. color: #333;
  231. flex: 1;
  232. word-break: break-all;
  233. }
  234. }
  235. // 🔥 高亮金额区域
  236. .price-highlight {
  237. padding: 12rpx 0;
  238. margin: 12rpx 0 32rpx;
  239. .value.price {
  240. display: flex;
  241. align-items: baseline;
  242. gap: 6rpx;
  243. .currency {
  244. font-size: 32rpx;
  245. color: $primary;
  246. font-weight: 600;
  247. }
  248. // 核心金额数字(最大、最亮)
  249. .amount-text {
  250. font-size: 48rpx;
  251. font-weight: 700;
  252. color: $primary;
  253. letter-spacing: 1rpx;
  254. }
  255. .unit {
  256. font-size: 28rpx;
  257. color: $primary;
  258. font-weight: 500;
  259. margin-left: 4rpx;
  260. }
  261. }
  262. }
  263. }
  264. }
  265. // 支付按钮
  266. .pay-btn-wrap {
  267. .pay-btn {
  268. width: 100%;
  269. height: 96rpx;
  270. line-height: 96rpx;
  271. background-color: $primary;
  272. border: none;
  273. border-radius: 48rpx;
  274. box-shadow: 0 8rpx 24rpx rgba($primary, 0.25);
  275. transition: all 0.2s ease;
  276. .btn-text {
  277. font-size: 34rpx;
  278. color: #fff;
  279. font-weight: 600;
  280. letter-spacing: 2rpx;
  281. }
  282. &:not(:disabled):active {
  283. background-color: $primary-dark;
  284. box-shadow: 0 4rpx 12rpx rgba($primary, 0.2);
  285. transform: translateY(2rpx);
  286. }
  287. &:disabled {
  288. background-color: $primary-disabled;
  289. box-shadow: none;
  290. }
  291. }
  292. }
  293. // 异常提示
  294. .error-wrap {
  295. flex-direction: column;
  296. height: 60vh;
  297. .error-text {
  298. font-size: 30rpx;
  299. color: #e64340;
  300. margin-bottom: 40rpx;
  301. text-align: center;
  302. padding: 0 30rpx;
  303. line-height: 44rpx;
  304. }
  305. .back-btn {
  306. width: 280rpx;
  307. height: 76rpx;
  308. line-height: 76rpx;
  309. background-color: #fff;
  310. border: 1px solid $primary;
  311. border-radius: 38rpx;
  312. font-size: 28rpx;
  313. color: $primary;
  314. transition: all 0.2s ease;
  315. &:active {
  316. background-color: $primary-light;
  317. }
  318. }
  319. }
  320. .flex-center {
  321. display: flex;
  322. justify-content: center;
  323. align-items: center;
  324. }
  325. </style>