paymentOrder.vue 14 KB

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