paymentOrder.vue 14 KB

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