paymentOrder.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. <template>
  2. <view class="content">
  3. <view class="inner">
  4. <!-- 时间、价格 -->
  5. <view class="time-price">
  6. <text class="time">待支付</text>
  7. <view class="price-box">
  8. <text class="unit">¥</text>
  9. <text class="num">{{order ? (Number(order.payPrice) || 0).toFixed(2) : "0.00"}}</text>
  10. </view>
  11. </view>
  12. <!-- 支付方式 -->
  13. <view class="pay-type">
  14. <view class="title">支付方式</view>
  15. <radio-group @change="handlePayTypeChange">
  16. <view class="item">
  17. <view class="left">
  18. <image
  19. src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/userapp/images/wecha_pay.png"
  20. mode=""></image>
  21. <text class="text">微信支付</text>
  22. </view>
  23. <label>
  24. <radio :value="1" :checked="payType === 1" />
  25. </label>
  26. </view>
  27. <!-- #ifdef APP-PLUS||H5 -->
  28. <view class="item">
  29. <view class="left">
  30. <image src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/userapp/images/zfb.png"
  31. mode=""></image>
  32. <text class="text">支付宝</text>
  33. </view>
  34. <label>
  35. <radio :value="2" :checked="payType === 2" />
  36. </label>
  37. </view>
  38. <!-- #endif -->
  39. </radio-group>
  40. </view>
  41. <!-- 订单详情查看 -->
  42. <view class="order-info">
  43. <view class="title">订单信息</view>
  44. <view class="item">
  45. <text class="label">订单编号</text>
  46. <view class="sn-box">
  47. <view>
  48. <view class="text">{{order.orderCode}}</view>
  49. </view>
  50. </view>
  51. </view>
  52. <view class="item">
  53. <text class="label">下单时间</text>
  54. <text class="text">{{ formattedDate}} </text>
  55. </view>
  56. <view class="item">
  57. <text class="label">订单金额</text>
  58. <text class="text"
  59. v-if="order!=null">{{order ? (Number(order.payPrice) || 0).toFixed(2) : "0.00"}}</text>
  60. </view>
  61. <view class="item">
  62. <text class="label">优惠券</text>
  63. <text class="text">-¥{{order ? (Number(order.discountMoney) || 0).toFixed(2) : "0.00"}} </text>
  64. </view>
  65. </view>
  66. </view>
  67. <view class="btn-box">
  68. <view class="btn" @click="payOrder()">去支付</view>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. import {
  74. clearPayType
  75. } from '@/api/storeOrder'
  76. import dayjs from 'dayjs';
  77. import {
  78. weChatPayment
  79. } from '@/api/pay_new'
  80. import {
  81. zfbPayment,
  82. // weChatPayment
  83. } from '@/api/pay'
  84. import {
  85. payConfirmReward,
  86. } from '@/api/order'
  87. export default {
  88. data() {
  89. return {
  90. type: '',
  91. payPrice: null,
  92. newOrder: {},
  93. payType: 1, // 默认微信支付
  94. order: null,
  95. orderId: null,
  96. payDelivery: 0,
  97. payMoney: 0,
  98. config: null,
  99. user: null,
  100. couponUserId: null,
  101. payParams: null,
  102. userinfo: {}, // 添加用户信息存储
  103. isPaying: false // 防止重复支付
  104. }
  105. },
  106. computed: {
  107. formattedDate() {
  108. return this.order?.createTime ? dayjs(this.order.createTime).format('YYYY-MM-DD HH:mm:ss') : '';
  109. },
  110. payLimitTime() {
  111. return this.order?.updateTime ?
  112. dayjs(this.order.updateTime).add(2, 'day').format('YYYY-MM-DD HH:mm:ss') :
  113. '';
  114. }
  115. },
  116. onLoad(options) {
  117. this.getSafeUserInfo(); // 页面加载时获取用户信息
  118. if (options.couponUserId) {
  119. this.couponUserId = options.couponUserId;
  120. }
  121. console.log("支付订单是>>", options)
  122. this.type = options.type;
  123. if (options.orderList) {
  124. try {
  125. const decoded = decodeURIComponent(options.orderList);
  126. this.order = JSON.parse(decoded) || {}; // 默认空对象
  127. } catch (e) {
  128. console.error('参数解析失败:', e);
  129. this.order = {}; // 显式赋默认值
  130. }
  131. }
  132. },
  133. onUnload() {
  134. this.clearPayTypeFun()
  135. },
  136. methods: {
  137. getSafeUserInfo() {
  138. try {
  139. const userInfoStr = uni.getStorageSync('userInfo');
  140. if (!userInfoStr) {
  141. this.userinfo = {};
  142. return;
  143. }
  144. // 如果是字符串,解析为对象
  145. if (typeof userInfoStr === 'string') {
  146. this.userinfo = JSON.parse(userInfoStr);
  147. } else {
  148. // 如果是对象,直接使用
  149. this.userinfo = userInfoStr;
  150. }
  151. console.log('获取到的用户信息:', this.userinfo);
  152. } catch (error) {
  153. console.error('获取用户信息失败:', error);
  154. this.userinfo = {};
  155. }
  156. },
  157. redirectToLogin() {
  158. // 直接跳转到登录页
  159. uni.navigateTo({
  160. url: '/pages/auth/login'
  161. });
  162. },
  163. async clearPayTypeFun() {
  164. this.payParams && this.payParams.orderId && await clearPayType(this.payParams)
  165. },
  166. weixinPayOrder() {
  167. var data = {
  168. orderId: this.order.orderId,
  169. payType: 1
  170. };
  171. this.payParams = data
  172. var that = this;
  173. uni.showLoading();
  174. weChatPayment(data).then(
  175. res => {
  176. if (res.code == 200) {
  177. console.log(res);
  178. if (res.payType == 1 || res.payType == 2) {
  179. var result = res.result;
  180. uni.requestPayment({
  181. provider: 'wxpay',
  182. timeStamp: result.timeStamp,
  183. nonceStr: result.nonceStr,
  184. package: result.package,
  185. signType: result.signType,
  186. paySign: result.paySign,
  187. success: function(res) {
  188. uni.hideLoading();
  189. uni.redirectTo({
  190. url: "./success?order=" + JSON.stringify(that.newOrder)
  191. })
  192. },
  193. fail: function(err) {
  194. uni.showToast({
  195. icon: 'none',
  196. title: '支付失败',
  197. });
  198. uni.hideLoading();
  199. }
  200. });
  201. }
  202. } else {
  203. uni.showToast({
  204. icon: 'none',
  205. title: res.msg,
  206. });
  207. }
  208. },
  209. rej => {}
  210. );
  211. },
  212. // 修改后的支付方法
  213. payOrder() {
  214. // 1. 防止重复点击
  215. if (this.isPaying) return;
  216. // 2. 安全地获取用户信息
  217. this.getSafeUserInfo();
  218. // 3. 检查用户信息是否有效
  219. if (!this.userinfo || Object.keys(this.userinfo).length === 0) {
  220. uni.showToast({
  221. title: '用户信息异常,请重新登录',
  222. icon: 'none'
  223. });
  224. // 直接跳转到登录页
  225. this.redirectToLogin();
  226. return;
  227. }
  228. // 4. 检查 maOpenId,如果不存在则提示重新登录
  229. if (!this.userinfo.maOpenId) {
  230. uni.showModal({
  231. title: '提示',
  232. content: '用户信息不完整,需要重新登录',
  233. success: (res) => {
  234. if (res.confirm) {
  235. this.redirectToLogin();
  236. }
  237. }
  238. });
  239. return;
  240. }
  241. // 5. 检查登录状态
  242. this.utils.isLogin().then(res => {
  243. if (res) {
  244. // 设置支付状态防止重复点击
  245. this.isPaying = true;
  246. // 执行支付逻辑
  247. if (this.type == 'win') {
  248. this.toPayConfirmReward()
  249. } else {
  250. if (this.payType == 1) {
  251. console.log("这个order", this.order)
  252. const {
  253. itemJson,
  254. ...newOrder
  255. } = this.order;
  256. this.newOrder = newOrder;
  257. console.log("这个newOrder", this.newOrder)
  258. this.weixinPayOrder()
  259. } else {
  260. uni.showToast({
  261. title: "暂时无可用支付",
  262. icon: 'none'
  263. })
  264. }
  265. }
  266. // 支付完成后重置状态
  267. setTimeout(() => {
  268. this.isPaying = false;
  269. }, 2000);
  270. } else {
  271. // 未登录,直接跳转到登录页
  272. this.redirectToLogin();
  273. }
  274. }).catch(err => {
  275. console.error('检查登录状态失败:', err);
  276. uni.showToast({
  277. title: '登录状态异常',
  278. icon: 'none'
  279. });
  280. // 登录状态异常,也跳转到登录页
  281. this.redirectToLogin();
  282. });
  283. },
  284. // 选微信支付或者支付宝支付
  285. handlePayTypeChange(e) {
  286. this.payType = e.detail.value; // 获取选中的 value
  287. console.log('当前选中:', this.payType);
  288. },
  289. toPayConfirmReward() {
  290. let data = {
  291. orderId: this.order.orderId
  292. }
  293. payConfirmReward(data).then(
  294. res => {
  295. if (res.code == 200) {
  296. uni.showToast({
  297. icon: 'none',
  298. title: res.msg,
  299. });
  300. const {
  301. itemJson,
  302. ...newOrder
  303. } = this.order;
  304. uni.redirectTo({
  305. url: "./success?order=" + JSON.stringify(newOrder)
  306. })
  307. } else {
  308. uni.showToast({
  309. icon: 'none',
  310. title: "请求失败",
  311. });
  312. }
  313. },
  314. rej => {}
  315. );
  316. },
  317. getUserInfo() {
  318. getUserInfo().then(
  319. res => {
  320. if (res.code == 200) {
  321. if (res.user != null) {
  322. this.user = res.user;
  323. }
  324. } else {
  325. uni.showToast({
  326. icon: 'none',
  327. title: "请求失败",
  328. });
  329. }
  330. },
  331. rej => {}
  332. );
  333. },
  334. getStoreConfig() {
  335. getStoreConfig().then(
  336. res => {
  337. if (res.code == 200) {
  338. this.config = res.data
  339. console.log(this.config);
  340. }
  341. },
  342. rej => {}
  343. );
  344. },
  345. payTypeChange(e) {
  346. if (this.combinationOrderId) {
  347. this.editPayTypeByCombinationId(e.detail.value)
  348. } else {
  349. this.editPayType(e.detail.value)
  350. }
  351. },
  352. editPayType(payType) {
  353. var data = {
  354. orderId: this.orderId,
  355. payType: payType
  356. };
  357. this.payParams = data
  358. var that = this;
  359. uni.showLoading();
  360. editPayType(data).then(
  361. res => {
  362. if (res.code == 200) {
  363. console.log(res);
  364. uni.hideLoading();
  365. that.order = res.order;
  366. that.order.orderCodes = that.order.orderCode ? [that.order.orderCode] : []
  367. that.orderCode = that.order.orderCode
  368. // this.payType=this.order.payType
  369. this.payMoney = this.order.payMoney;
  370. this.payDelivery = this.order.payDelivery;
  371. } else {
  372. uni.showToast({
  373. icon: 'none',
  374. title: res.msg,
  375. });
  376. }
  377. },
  378. rej => {}
  379. );
  380. },
  381. }
  382. }
  383. </script>
  384. <style lang="scss">
  385. page {
  386. height: 100%;
  387. }
  388. .content {
  389. height: 100%;
  390. display: flex;
  391. flex-direction: column;
  392. justify-content: space-between;
  393. .inner {
  394. padding: 20upx;
  395. .time-price {
  396. box-sizing: border-box;
  397. padding: 50upx 0upx;
  398. background: #FFFFFF;
  399. border-radius: 16upx;
  400. display: flex;
  401. flex-direction: column;
  402. align-items: center;
  403. .time {
  404. font-size: 32upx;
  405. font-family: PingFang SC;
  406. font-weight: 500;
  407. color: #222222;
  408. line-height: 1;
  409. text-align: center;
  410. }
  411. .desc {
  412. margin: 30upx 0upx 15upx;
  413. font-size: 26upx;
  414. font-family: PingFang SC;
  415. color: #999999;
  416. line-height: 1;
  417. text-align: center;
  418. }
  419. .price-box {
  420. display: flex;
  421. align-items: flex-end;
  422. margin-top: 28upx;
  423. .unit {
  424. font-size: 32upx;
  425. font-family: PingFang SC;
  426. font-weight: bold;
  427. color: #FF6633;
  428. line-height: 1.3;
  429. margin-right: 10upx;
  430. }
  431. .num {
  432. font-size: 56upx;
  433. font-family: PingFang SC;
  434. font-weight: bold;
  435. color: #FF6633;
  436. line-height: 1;
  437. }
  438. }
  439. }
  440. .pay-type {
  441. box-sizing: border-box;
  442. background: #FFFFFF;
  443. border-radius: 16upx;
  444. margin-top: 20upx;
  445. padding: 40upx 30upx;
  446. display: flex;
  447. flex-direction: column;
  448. justify-content: space-between;
  449. .title {
  450. font-size: 28upx;
  451. font-family: PingFang SC;
  452. font-weight: 500;
  453. color: #999999;
  454. line-height: 1;
  455. margin-bottom: 10upx;
  456. }
  457. .item {
  458. padding: 15upx 0upx;
  459. display: flex;
  460. align-items: center;
  461. justify-content: space-between;
  462. .left {
  463. display: flex;
  464. align-items: center;
  465. image {
  466. width: 44upx;
  467. height: 44upx;
  468. margin-right: 20upx;
  469. }
  470. .text {
  471. font-size: 30upx;
  472. font-family: PingFang SC;
  473. font-weight: bold;
  474. color: #222222;
  475. line-height: 1;
  476. }
  477. }
  478. }
  479. }
  480. .order-info {
  481. margin-top: 20upx;
  482. background: #FFFFFF;
  483. border-radius: 16upx;
  484. padding: 40upx 30upx;
  485. .title {
  486. font-size: 30upx;
  487. font-family: PingFang SC;
  488. font-weight: bold;
  489. color: #222222;
  490. line-height: 1;
  491. }
  492. .item {
  493. margin-top: 40upx;
  494. display: flex;
  495. align-items: center;
  496. justify-content: space-between;
  497. .label {
  498. font-size: 26upx;
  499. font-family: PingFang SC;
  500. font-weight: 500;
  501. color: #666666;
  502. line-height: 1;
  503. }
  504. .text {
  505. font-size: 26upx;
  506. font-family: PingFang SC;
  507. font-weight: 500;
  508. color: #222222;
  509. line-height: 32upx;
  510. }
  511. .cont-text {
  512. font-size: 26upx;
  513. font-family: PingFang SC;
  514. font-weight: 500;
  515. color: #666666;
  516. .bold {
  517. color: #111111;
  518. }
  519. }
  520. .sn-box {
  521. display: flex;
  522. align-items: center;
  523. .copy-btn {
  524. width: 58upx;
  525. height: 32upx;
  526. line-height: 32upx;
  527. text-align: center;
  528. font-size: 22upx;
  529. font-weight: 500;
  530. color: #222222;
  531. background: #F5F5F5;
  532. border-radius: 4upx;
  533. margin-left: 24upx;
  534. }
  535. }
  536. }
  537. .line {
  538. width: 100%;
  539. height: 1px;
  540. background: #F0F0F0;
  541. margin-top: 30upx;
  542. }
  543. }
  544. }
  545. .btn-box {
  546. height: 242upx;
  547. background: #FFFFFF;
  548. display: flex;
  549. align-items: center;
  550. justify-content: center;
  551. flex-direction: column;
  552. .btn {
  553. width: 91.73%;
  554. height: 88upx;
  555. line-height: 88upx;
  556. font-size: 30upx;
  557. font-family: PingFang SC;
  558. font-weight: bold;
  559. color: #FFFFFF;
  560. text-align: center;
  561. background: #2BC7B9;
  562. border-radius: 44upx;
  563. margin-bottom: 10rpx;
  564. }
  565. .other-btn {
  566. width: 91.73%;
  567. height: 88upx;
  568. line-height: 88upx;
  569. font-size: 30upx;
  570. font-family: PingFang SC;
  571. font-weight: bold;
  572. color: #2BC7B9;
  573. border: 1rpx solid #2BC7B9;
  574. text-align: center;
  575. background: #FFFFFF;
  576. border-radius: 44upx;
  577. margin-bottom: 10rpx;
  578. position: relative;
  579. .share {
  580. display: inline-block;
  581. position: absolute;
  582. top: 0;
  583. left: 0;
  584. width: 100%;
  585. height: 100%;
  586. opacity: 0;
  587. }
  588. }
  589. }
  590. }
  591. </style>