paymentOrder.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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()" :disabled="isPaying" :style="isPaying ? 'background: #ccc; color: #999; border: none;' : ''">去支付</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. liveId:null,
  91. type: '',
  92. payPrice: null,
  93. newOrder: {},
  94. payType: 1, // 默认微信支付
  95. order: null,
  96. orderId: null,
  97. payDelivery: 0,
  98. payMoney: 0,
  99. config: null,
  100. user: null,
  101. couponUserId: null,
  102. payParams: null,
  103. userinfo: {}, // 添加用户信息存储
  104. isPaying: false // 防止重复支付
  105. }
  106. },
  107. computed: {
  108. formattedDate() {
  109. return this.order?.createTime ? dayjs(this.order.createTime).format('YYYY-MM-DD HH:mm:ss') : '';
  110. },
  111. payLimitTime() {
  112. return this.order?.updateTime ?
  113. dayjs(this.order.updateTime).add(2, 'day').format('YYYY-MM-DD HH:mm:ss') :
  114. '';
  115. }
  116. },
  117. onLoad(options) {
  118. if(options.liveId){
  119. this.liveId=options.liveId
  120. }
  121. this.getSafeUserInfo(); // 页面加载时获取用户信息
  122. if (options.couponUserId) {
  123. this.couponUserId = options.couponUserId;
  124. }
  125. console.log("支付订单是>>", options)
  126. this.type = options.type;
  127. if (options.orderList) {
  128. try {
  129. const decoded = decodeURIComponent(options.orderList);
  130. this.order = JSON.parse(decoded) || {}; // 默认空对象
  131. console.log(this.order,'---')
  132. } catch (e) {
  133. console.error('参数解析失败:', e);
  134. this.order = {}; // 显式赋默认值
  135. }
  136. }
  137. },
  138. onShow() {
  139. // 页面显示时重置支付状态,防止从其他页面返回时状态未重置
  140. this.isPaying = false;
  141. },
  142. onUnload() {
  143. // 页面卸载时重置支付状态
  144. this.isPaying = false;
  145. this.clearPayTypeFun()
  146. },
  147. methods: {
  148. getSafeUserInfo() {
  149. try {
  150. const userInfoStr = uni.getStorageSync('userInfo');
  151. if (!userInfoStr) {
  152. this.userinfo = {};
  153. return;
  154. }
  155. // 如果是字符串,解析为对象
  156. if (typeof userInfoStr === 'string') {
  157. this.userinfo = JSON.parse(userInfoStr);
  158. } else {
  159. // 如果是对象,直接使用
  160. this.userinfo = userInfoStr;
  161. }
  162. console.log('获取到的用户信息:', this.userinfo);
  163. } catch (error) {
  164. console.error('获取用户信息失败:', error);
  165. this.userinfo = {};
  166. }
  167. },
  168. redirectToLogin() {
  169. // 直接跳转到登录页
  170. uni.navigateTo({
  171. url: '/pages/auth/login'
  172. });
  173. },
  174. async clearPayTypeFun() {
  175. this.payParams && this.payParams.orderId && await clearPayType(this.payParams)
  176. },
  177. weixinPayOrder() {
  178. // 防止重复调用支付接口
  179. if (this.isPaying && this.payParams && this.payParams.orderId === this.order.orderId) {
  180. uni.showToast({
  181. icon: 'none',
  182. title: '支付请求正在处理中,请勿重复点击',
  183. });
  184. return;
  185. }
  186. var data = {
  187. orderId: this.order.orderId,
  188. payType: 1,
  189. appId: wx.getAccountInfoSync().miniProgram.appId
  190. };
  191. console.log('当前小程序信息id',data)
  192. this.payParams = data
  193. var that = this;
  194. uni.showLoading();
  195. weChatPayment(data).then(
  196. res => {
  197. if (res.code == 501) {
  198. that.isPaying = false; // 重置支付状态
  199. uni.hideLoading();
  200. return uni.navigateBack()
  201. }
  202. if (res.code == 200) {
  203. console.log(res);
  204. if (res.payType == 1 || res.payType == 2) {
  205. var result = res.result;
  206. uni.requestPayment({
  207. provider: 'wxpay',
  208. timeStamp: result.timeStamp,
  209. nonceStr: result.nonceStr,
  210. package: result.package,
  211. signType: result.signType,
  212. paySign: result.paySign,
  213. success: function(res) {
  214. uni.hideLoading();
  215. that.isPaying = false; // 支付成功,重置状态
  216. uni.redirectTo({
  217. url: "./success?order=" + JSON.stringify(that.newOrder)+"&liveId="+that.liveId
  218. })
  219. },
  220. fail: function(err) {
  221. that.isPaying = false; // 支付失败,重置状态
  222. uni.showToast({
  223. icon: 'none',
  224. title: '支付失败',
  225. });
  226. uni.hideLoading();
  227. }
  228. });
  229. } else {
  230. that.isPaying = false; // 重置支付状态
  231. }
  232. } else {
  233. that.isPaying = false; // 接口返回错误,重置支付状态
  234. uni.hideLoading();
  235. uni.showToast({
  236. icon: 'none',
  237. title: res.msg,
  238. });
  239. }
  240. },
  241. rej => {
  242. that.isPaying = false; // 接口调用失败,重置支付状态
  243. uni.hideLoading();
  244. console.error('支付接口调用失败:', rej);
  245. uni.showToast({
  246. icon: 'none',
  247. title: '支付接口调用失败',
  248. });
  249. }
  250. );
  251. },
  252. // 修改后的支付方法
  253. payOrder() {
  254. // 1. 防止重复点击
  255. if (this.isPaying) {
  256. uni.showToast({
  257. icon: 'none',
  258. title: '支付正在进行中,请勿重复点击',
  259. });
  260. return;
  261. }
  262. // 2. 安全地获取用户信息
  263. this.getSafeUserInfo();
  264. // 3. 检查用户信息是否有效
  265. if (!this.userinfo || Object.keys(this.userinfo).length === 0) {
  266. // uni.showToast({
  267. // title: '用户信息异常,请重新登录',
  268. // icon: 'none'
  269. // });
  270. // 直接跳转到登录页
  271. this.redirectToLogin();
  272. return;
  273. }
  274. // 4. 检查 maOpenId,如果不存在则提示重新登录
  275. if (!this.userinfo.maOpenId) {
  276. // uni.showModal({
  277. // title: '提示',
  278. // content: '用户信息不完整,需要重新登录',
  279. // success: (res) => {
  280. // if (res.confirm) {
  281. // this.redirectToLogin();
  282. // }
  283. // }
  284. // });
  285. this.redirectToLogin();
  286. return;
  287. }
  288. // 5. 检查登录状态
  289. this.utils.isLogin().then(res => {
  290. if (res) {
  291. // 设置支付状态防止重复点击
  292. this.isPaying = true;
  293. // 执行支付逻辑
  294. if (this.type == 'win') {
  295. this.toPayConfirmReward()
  296. } else {
  297. if (this.payType == 1) {
  298. // console.log("这个order", this.order)
  299. const {
  300. itemJson,
  301. ...newOrder
  302. } = this.order;
  303. this.newOrder = newOrder;
  304. this.weixinPayOrder()
  305. } else {
  306. this.isPaying = false; // 无可用支付方式,重置状态
  307. uni.showToast({
  308. title: "暂时无可用支付",
  309. icon: 'none'
  310. })
  311. }
  312. }
  313. // 注意:isPaying 的重置应该在支付成功/失败的回调中进行,而不是使用 setTimeout
  314. } else {
  315. // 未登录,直接跳转到登录页
  316. this.isPaying = false; // 重置状态
  317. this.redirectToLogin();
  318. }
  319. }).catch(err => {
  320. console.error('检查登录状态失败:', err);
  321. this.isPaying = false; // 检查登录失败,重置状态
  322. uni.showToast({
  323. title: '登录状态异常',
  324. icon: 'none'
  325. });
  326. // 登录状态异常,也跳转到登录页
  327. this.redirectToLogin();
  328. });
  329. },
  330. // 选微信支付或者支付宝支付
  331. handlePayTypeChange(e) {
  332. this.payType = e.detail.value; // 获取选中的 value
  333. console.log('当前选中:', this.payType);
  334. },
  335. toPayConfirmReward() {
  336. // 防止重复调用支付接口
  337. if (this.isPaying && this.payParams && this.payParams.orderId === this.order.orderId) {
  338. uni.showToast({
  339. icon: 'none',
  340. title: '中奖订单支付请求正在处理中,请勿重复点击',
  341. });
  342. return;
  343. }
  344. let data = {
  345. orderId: this.order.orderId,
  346. appId: wx.getAccountInfoSync().miniProgram.appId
  347. }
  348. console.log('当前小程序信息id',data)
  349. this.payParams = data;
  350. var that = this;
  351. payConfirmReward(data).then(
  352. res => {
  353. if (res.code == 200) {
  354. that.isPaying = false; // 支付成功,重置状态
  355. uni.showToast({
  356. icon: 'none',
  357. title: res.msg,
  358. });
  359. const {
  360. itemJson,
  361. ...newOrder
  362. } = this.order;
  363. uni.redirectTo({
  364. url: "./success?order=" + JSON.stringify(newOrder)
  365. })
  366. } else {
  367. that.isPaying = false; // 支付失败,重置状态
  368. uni.showToast({
  369. icon: 'none',
  370. title: res.msg || "请求失败",
  371. });
  372. }
  373. },
  374. rej => {
  375. that.isPaying = false; // 接口调用失败,重置状态
  376. console.error('中奖订单支付接口调用失败:', rej);
  377. uni.showToast({
  378. icon: 'none',
  379. title: "支付接口调用失败",
  380. });
  381. }
  382. );
  383. },
  384. getUserInfo() {
  385. getUserInfo().then(
  386. res => {
  387. if (res.code == 200) {
  388. if (res.user != null) {
  389. this.user = res.user;
  390. }
  391. } else {
  392. uni.showToast({
  393. icon: 'none',
  394. title: "请求失败",
  395. });
  396. }
  397. },
  398. rej => {}
  399. );
  400. },
  401. getStoreConfig() {
  402. getStoreConfig().then(
  403. res => {
  404. if (res.code == 200) {
  405. this.config = res.data
  406. console.log(this.config);
  407. }
  408. },
  409. rej => {}
  410. );
  411. },
  412. payTypeChange(e) {
  413. if (this.combinationOrderId) {
  414. this.editPayTypeByCombinationId(e.detail.value)
  415. } else {
  416. this.editPayType(e.detail.value)
  417. }
  418. },
  419. editPayType(payType) {
  420. var data = {
  421. orderId: this.orderId,
  422. payType: payType
  423. };
  424. this.payParams = data
  425. var that = this;
  426. uni.showLoading();
  427. editPayType(data).then(
  428. res => {
  429. if (res.code == 200) {
  430. console.log(res);
  431. uni.hideLoading();
  432. that.order = res.order;
  433. that.order.orderCodes = that.order.orderCode ? [that.order.orderCode] : []
  434. that.orderCode = that.order.orderCode
  435. // this.payType=this.order.payType
  436. this.payMoney = this.order.payMoney;
  437. this.payDelivery = this.order.payDelivery;
  438. } else {
  439. uni.showToast({
  440. icon: 'none',
  441. title: res.msg,
  442. });
  443. }
  444. },
  445. rej => {}
  446. );
  447. },
  448. }
  449. }
  450. </script>
  451. <style lang="scss">
  452. page {
  453. height: 100%;
  454. }
  455. .content {
  456. height: 100%;
  457. display: flex;
  458. flex-direction: column;
  459. justify-content: space-between;
  460. .inner {
  461. padding: 20upx;
  462. .time-price {
  463. box-sizing: border-box;
  464. padding: 50upx 0upx;
  465. background: #FFFFFF;
  466. border-radius: 16upx;
  467. display: flex;
  468. flex-direction: column;
  469. align-items: center;
  470. .time {
  471. font-size: 32upx;
  472. font-family: PingFang SC;
  473. font-weight: 500;
  474. color: #222222;
  475. line-height: 1;
  476. text-align: center;
  477. }
  478. .desc {
  479. margin: 30upx 0upx 15upx;
  480. font-size: 26upx;
  481. font-family: PingFang SC;
  482. color: #999999;
  483. line-height: 1;
  484. text-align: center;
  485. }
  486. .price-box {
  487. display: flex;
  488. align-items: flex-end;
  489. margin-top: 28upx;
  490. .unit {
  491. font-size: 32upx;
  492. font-family: PingFang SC;
  493. font-weight: bold;
  494. color: #FF6633;
  495. line-height: 1.3;
  496. margin-right: 10upx;
  497. }
  498. .num {
  499. font-size: 56upx;
  500. font-family: PingFang SC;
  501. font-weight: bold;
  502. color: #FF6633;
  503. line-height: 1;
  504. }
  505. }
  506. }
  507. .pay-type {
  508. box-sizing: border-box;
  509. background: #FFFFFF;
  510. border-radius: 16upx;
  511. margin-top: 20upx;
  512. padding: 40upx 30upx;
  513. display: flex;
  514. flex-direction: column;
  515. justify-content: space-between;
  516. .title {
  517. font-size: 28upx;
  518. font-family: PingFang SC;
  519. font-weight: 500;
  520. color: #999999;
  521. line-height: 1;
  522. margin-bottom: 10upx;
  523. }
  524. .item {
  525. padding: 15upx 0upx;
  526. display: flex;
  527. align-items: center;
  528. justify-content: space-between;
  529. .left {
  530. display: flex;
  531. align-items: center;
  532. image {
  533. width: 44upx;
  534. height: 44upx;
  535. margin-right: 20upx;
  536. }
  537. .text {
  538. font-size: 30upx;
  539. font-family: PingFang SC;
  540. font-weight: bold;
  541. color: #222222;
  542. line-height: 1;
  543. }
  544. }
  545. }
  546. }
  547. .order-info {
  548. margin-top: 20upx;
  549. background: #FFFFFF;
  550. border-radius: 16upx;
  551. padding: 40upx 30upx;
  552. .title {
  553. font-size: 30upx;
  554. font-family: PingFang SC;
  555. font-weight: bold;
  556. color: #222222;
  557. line-height: 1;
  558. }
  559. .item {
  560. margin-top: 40upx;
  561. display: flex;
  562. align-items: center;
  563. justify-content: space-between;
  564. .label {
  565. font-size: 26upx;
  566. font-family: PingFang SC;
  567. font-weight: 500;
  568. color: #666666;
  569. line-height: 1;
  570. }
  571. .text {
  572. font-size: 26upx;
  573. font-family: PingFang SC;
  574. font-weight: 500;
  575. color: #222222;
  576. line-height: 32upx;
  577. }
  578. .cont-text {
  579. font-size: 26upx;
  580. font-family: PingFang SC;
  581. font-weight: 500;
  582. color: #666666;
  583. .bold {
  584. color: #111111;
  585. }
  586. }
  587. .sn-box {
  588. display: flex;
  589. align-items: center;
  590. .copy-btn {
  591. width: 58upx;
  592. height: 32upx;
  593. line-height: 32upx;
  594. text-align: center;
  595. font-size: 22upx;
  596. font-weight: 500;
  597. color: #222222;
  598. background: #F5F5F5;
  599. border-radius: 4upx;
  600. margin-left: 24upx;
  601. }
  602. }
  603. }
  604. .line {
  605. width: 100%;
  606. height: 1px;
  607. background: #F0F0F0;
  608. margin-top: 30upx;
  609. }
  610. }
  611. }
  612. .btn-box {
  613. height: 242upx;
  614. background: #FFFFFF;
  615. display: flex;
  616. align-items: center;
  617. justify-content: center;
  618. flex-direction: column;
  619. .btn {
  620. width: 91.73%;
  621. height: 88upx;
  622. line-height: 88upx;
  623. font-size: 30upx;
  624. font-family: PingFang SC;
  625. font-weight: bold;
  626. color: #FFFFFF;
  627. text-align: center;
  628. background: #2BC7B9;
  629. border-radius: 44upx;
  630. margin-bottom: 10rpx;
  631. }
  632. .other-btn {
  633. width: 91.73%;
  634. height: 88upx;
  635. line-height: 88upx;
  636. font-size: 30upx;
  637. font-family: PingFang SC;
  638. font-weight: bold;
  639. color: #2BC7B9;
  640. border: 1rpx solid #2BC7B9;
  641. text-align: center;
  642. background: #FFFFFF;
  643. border-radius: 44upx;
  644. margin-bottom: 10rpx;
  645. position: relative;
  646. .share {
  647. display: inline-block;
  648. position: absolute;
  649. top: 0;
  650. left: 0;
  651. width: 100%;
  652. height: 100%;
  653. opacity: 0;
  654. }
  655. }
  656. }
  657. }
  658. </style>