paymentOrder.vue 14 KB

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