paymentOrder.vue 14 KB

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