paymentOrder.vue 14 KB

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