paymentOrder.vue 14 KB

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