paymentOrder.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. <template>
  2. <view class="content">
  3. <view class="inner">
  4. <!-- 时间、价格 -->
  5. <view class="time-price">
  6. <text class="time">请在{{payLimitTime}}前完成支付</text>
  7. <view class="price-box">
  8. <text class="unit">¥</text>
  9. <text class="num">{{payMoney.toFixed(2)}}</text>
  10. </view>
  11. <!-- <text class="desc" v-if="payType==2">代收金额{{payDelivery.toFixed(2)}},请您在收到快递后支付尾款给快递人员。</text> -->
  12. <!-- <text class="desc" v-if="payType==3">货到付款金额{{payDelivery.toFixed(2)}},请您在收到快递后支付给快递人员。</text> -->
  13. </view>
  14. <!-- 支付方式 -->
  15. <view class="pay-type">
  16. <view class="title">支付方式</view>
  17. <!-- 改价订单只能选择微信支付和物流代收 -->
  18. <radio-group @change="payTypeChange" v-if="order.isEditMoney!=null&&order.isEditMoney==1">
  19. <view class="item">
  20. <view class="left">
  21. <image
  22. src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/wecha_pay.png"
  23. mode=""></image>
  24. <text class="text">微信支付</text>
  25. </view>
  26. <label>
  27. <radio :value="1" :checked="order.payType=='1'" />
  28. </label>
  29. </view>
  30. <!-- <view class="item" >
  31. <view class="left">
  32. <image src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/pay_de.png" mode=""></image>
  33. <text class="text">物流代收</text>
  34. </view>
  35. <label>
  36. <radio :value="2" :checked="order.payType=='2'" />
  37. </label>
  38. </view> -->
  39. </radio-group>
  40. <radio-group @change="payTypeChange" v-else-if="order.orderCreateType!=null&& order.orderCreateType==3">
  41. <view class="item">
  42. <view class="left">
  43. <image
  44. src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/wecha_pay.png"
  45. mode=""></image>
  46. <text class="text">微信支付</text>
  47. </view>
  48. <label>
  49. <radio :value="1" :checked="order.payType=='1'" />
  50. </label>
  51. </view>
  52. <!-- <view class="item" >
  53. <view class="left">
  54. <image src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/pay_de.png" mode=""></image>
  55. <text class="text">物流代收</text>
  56. </view>
  57. <label>
  58. <radio :value="2" :checked="order.payType=='2'" />
  59. </label>
  60. </view> -->
  61. <!-- <view class="item" v-if="user!=null&&user.level==1 "> -->
  62. <!-- <view class="item" >
  63. <view class="left">
  64. <image src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/pay_1.png" mode=""></image>
  65. <text class="text">货到付款</text>
  66. </view>
  67. <label>
  68. <radio :value="3" :checked="order.payType=='3'" />
  69. </label>
  70. </view> -->
  71. </radio-group>
  72. <radio-group @change="payTypeChange" v-else-if="order.orderCreateType!=null&& order.orderCreateType==2">
  73. <view class="item" v-if="payType==1||payType==4">
  74. <view class="left">
  75. <image
  76. src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/wecha_pay.png"
  77. mode=""></image>
  78. <text class="text">微信支付</text>
  79. </view>
  80. <label>
  81. <radio :value="1" :checked="order.payType=='1'" />
  82. </label>
  83. </view>
  84. <!-- <view class="item" v-if="payType==2||payType==4">
  85. <view class="left" >
  86. <image src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/pay_de.png" mode=""></image>
  87. <text class="text">物流代收</text>
  88. </view>
  89. <label>
  90. <radio :value="2" :checked="order.payType=='2'" />
  91. </label>
  92. </view> -->
  93. <!-- <view class="item" v-if="user!=null&&user.level==1 "> -->
  94. <!-- <view class="item" v-if="payType==1||payType==4" >
  95. <view class="left">
  96. <image src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/pay_1.png" mode=""></image>
  97. <text class="text">货到付款</text>
  98. </view>
  99. <label>
  100. <radio :value="3" :checked="order.payType=='3'" />
  101. </label>
  102. </view> -->
  103. </radio-group>
  104. <radio-group @change="payTypeChange"
  105. v-else-if="order.orderCreateType!=null&&(order.orderCreateType==1)">
  106. <view class="item">
  107. <view class="left">
  108. <image
  109. src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/wecha_pay.png"
  110. mode=""></image>
  111. <text class="text">微信支付</text>
  112. </view>
  113. <label>
  114. <radio :value="1" checked />
  115. </label>
  116. </view>
  117. </radio-group>
  118. </view>
  119. <!-- 订单详情查看 -->
  120. <view class="order-info">
  121. <view class="title">订单信息</view>
  122. <view class="item">
  123. <text class="label">订单编号</text>
  124. <view class="sn-box">
  125. <text class="text">{{order.orderCode}}</text>
  126. <view class="copy-btn" @click="copyOrderSn(order.orderCode)">复制</view>
  127. </view>
  128. </view>
  129. <view class="item">
  130. <text class="label">下单时间</text>
  131. <text class="text">{{order.createTime}}</text>
  132. </view>
  133. <view class="item">
  134. <text class="label">订单金额</text>
  135. <text class="text" v-if="order!=null">{{order.payPrice.toFixed(2)}}</text>
  136. </view>
  137. <!-- <view class="item">
  138. <text class="label">支付方式</text>
  139. <text class="text">微信支付</text>
  140. </view> -->
  141. </view>
  142. </view>
  143. <view class="btn-box">
  144. <view class="btn" @click="payOrder()">去支付</view>
  145. <!-- <view class="other-btn" >
  146. 亲友代付
  147. <button class="share" data-name="shareBtn" open-type="share">分享</button>
  148. </view> -->
  149. </view>
  150. </view>
  151. </template>
  152. <script>
  153. import {
  154. getUserInfo
  155. } from '@/api/user'
  156. import {
  157. getStoreConfig
  158. } from '@/api/common'
  159. import {
  160. editPayType,
  161. pay,
  162. getStoreOrderById,
  163. orderBindUser,
  164. clearPayType
  165. } from '@/api/storeOrder'
  166. export default {
  167. data() {
  168. return {
  169. orderId: null,
  170. payDelivery: 0,
  171. payMoney: 0,
  172. config: null,
  173. payType: 1,
  174. payLimitTime: null,
  175. order: null,
  176. user: null,
  177. payParams: null,
  178. userinfo: {},
  179. isPaying: false, // 防止重复支付
  180. payDebounceTimer: null // 防抖定时器
  181. }
  182. },
  183. onLoad(option) {
  184. this.getSafeUserInfo();
  185. this.orderId = JSON.parse(option.orderId);
  186. // this.orderBindUser(this.orderId)
  187. this.getStoreOrderById();
  188. this.getStoreConfig();
  189. // this.getUserInfo();
  190. },
  191. onShow() {
  192. this.getUserInfo();
  193. },
  194. onUnload() {
  195. this.clearPayTypeFun()
  196. },
  197. methods: {
  198. getSafeUserInfo() {
  199. try {
  200. const userInfoStr = uni.getStorageSync('userInfo');
  201. if (!userInfoStr) {
  202. this.userinfo = {};
  203. return;
  204. }
  205. // 如果是字符串,解析为对象
  206. if (typeof userInfoStr === 'string') {
  207. this.userinfo = JSON.parse(userInfoStr);
  208. } else {
  209. // 如果是对象,直接使用
  210. this.userinfo = userInfoStr;
  211. }
  212. console.log('获取到的用户信息:', this.userinfo);
  213. } catch (error) {
  214. console.error('获取用户信息失败:', error);
  215. this.userinfo = {};
  216. }
  217. },
  218. redirectToLogin() {
  219. // 直接跳转到登录页,不删除token和userInfo
  220. uni.navigateTo({
  221. url: '/pages/auth/login'
  222. });
  223. },
  224. async clearPayTypeFun() {
  225. this.payParams && this.payParams.orderId && await clearPayType(this.payParams)
  226. },
  227. // 防抖函数
  228. debounce(func, wait = 1000) {
  229. return (...args) => {
  230. // 清除之前的定时器
  231. if (this.payDebounceTimer) {
  232. clearTimeout(this.payDebounceTimer);
  233. }
  234. // 设置新的定时器
  235. this.payDebounceTimer = setTimeout(() => {
  236. func.apply(this, args);
  237. this.payDebounceTimer = null;
  238. }, wait);
  239. };
  240. },
  241. orderBindUser(orderId) {
  242. uni.showLoading({
  243. title: '加载中...'
  244. })
  245. orderBindUser({
  246. orderId: orderId
  247. }).then(res => {
  248. uni.hideLoading()
  249. if (res.code == 200) {
  250. this.getStoreOrderById();
  251. this.getStoreConfig();
  252. this.getUserInfo();
  253. } else {
  254. uni.showToast({
  255. icon: 'none',
  256. title: res.msg,
  257. });
  258. }
  259. }).catch(() => {
  260. uni.hideLoading()
  261. });
  262. },
  263. getUserInfo() {
  264. getUserInfo().then(
  265. res => {
  266. if (res.code == 200) {
  267. if (res.user != null) {
  268. this.user = res.user;
  269. }
  270. } else {
  271. uni.showToast({
  272. icon: 'none',
  273. title: "请求失败",
  274. });
  275. }
  276. },
  277. rej => {}
  278. );
  279. },
  280. getStoreConfig() {
  281. getStoreConfig().then(
  282. res => {
  283. if (res.code == 200) {
  284. this.config = res.data
  285. console.log(this.config);
  286. }
  287. },
  288. rej => {}
  289. );
  290. },
  291. payTypeChange(e) {
  292. this.editPayType(e.detail.value)
  293. },
  294. copyOrderSn(text) {
  295. // 复制方法
  296. uni.setClipboardData({
  297. data: text,
  298. success: () => {
  299. uni.showToast({
  300. title: '内容已成功复制到剪切板',
  301. icon: 'none'
  302. })
  303. }
  304. });
  305. },
  306. getStoreOrderById() {
  307. var data = {
  308. orderId: this.orderId
  309. };
  310. var that = this;
  311. uni.showLoading();
  312. getStoreOrderById(data).then(
  313. res => {
  314. if (res.code == 200) {
  315. console.log(res);
  316. uni.hideLoading();
  317. that.order = res.order;
  318. that.payLimitTime = res.payLimitTime;
  319. //套餐订单处理
  320. if (res.productPackage != null) {
  321. this.payType = res.productPackage.payType;
  322. console.log(this.payType)
  323. if (this.order.payType == 4) {
  324. this.order.payType = 1;
  325. }
  326. }
  327. this.editPayType(this.order.payType)
  328. } else {
  329. uni.showToast({
  330. icon: 'none',
  331. title: res.msg,
  332. });
  333. }
  334. },
  335. rej => {}
  336. );
  337. },
  338. editPayType(payType) {
  339. var data = {
  340. orderId: this.orderId,
  341. payType: payType
  342. };
  343. this.payParams = data
  344. var that = this;
  345. uni.showLoading();
  346. editPayType(data).then(
  347. res => {
  348. if (res.code == 200) {
  349. console.log(res);
  350. uni.hideLoading();
  351. that.order = res.order;
  352. //this.payType=this.order.payType
  353. this.payMoney = this.order.payMoney;
  354. this.payDelivery = this.order.payDelivery;
  355. } else {
  356. uni.showToast({
  357. icon: 'none',
  358. title: res.msg,
  359. });
  360. }
  361. },
  362. rej => {}
  363. );
  364. },
  365. otherPayOrder() {
  366. uni.navigateTo({
  367. url: '/pages_user/user/otherPaymentOrder?orderId=' + this.orderId
  368. })
  369. },
  370. // 防抖后的支付方法
  371. // 修复后的支付方法
  372. payOrder() {
  373. // 1. 安全地获取用户信息
  374. this.getSafeUserInfo();
  375. // 2. 检查 maOpenId,但不要直接修改字符串
  376. // 3. 检查用户信息是否有效
  377. if (!this.userinfo || Object.keys(this.userinfo).length === 0) {
  378. uni.showToast({
  379. title: '用户信息异常,请重新登录',
  380. icon: 'none'
  381. });
  382. // 直接跳转到登录页
  383. this.redirectToLogin();
  384. return;
  385. }
  386. // 4. 检查 maOpenId,如果不存在则提示重新登录
  387. if (!this.userinfo.maOpenId) {
  388. uni.showModal({
  389. title: '提示',
  390. content: '用户信息不完整,需要重新登录',
  391. success: (res) => {
  392. if (res.confirm) {
  393. this.redirectToLogin();
  394. }
  395. }
  396. });
  397. return;
  398. }
  399. // 5. 检查登录状态
  400. this.utils.isLogin().then(res => {
  401. if (res) {
  402. // 创建防抖函数实例
  403. const debouncedPay = this.debounce(function() {
  404. this.executePay();
  405. }, 1000); // 1秒防抖时间
  406. // 执行防抖函数
  407. debouncedPay();
  408. } else {
  409. // 未登录,直接跳转到登录页
  410. this.redirectToLogin();
  411. }
  412. }).catch(err => {
  413. console.error('检查登录状态失败:', err);
  414. uni.showToast({
  415. title: '登录状态异常',
  416. icon: 'none'
  417. });
  418. // 登录状态异常,也跳转到登录页
  419. this.redirectToLogin();
  420. });
  421. },
  422. // 实际的支付执行逻辑
  423. executePay() {
  424. var data = {
  425. orderId: this.order.id,
  426. payType: this.order.payType
  427. };
  428. var that = this;
  429. // 防止重复点击,可以添加加载状态
  430. if (this.isPaying) return;
  431. this.isPaying = true;
  432. uni.showLoading();
  433. pay(data).then(
  434. res => {
  435. this.isPaying = false;
  436. if (res.code == 200) {
  437. console.log(res);
  438. if (res.payType == 1 || res.payType == 2 || res.payType == 3) {
  439. uni.requestPayment({
  440. provider: 'wxpay',
  441. timeStamp: res.result.timeStamp,
  442. nonceStr: res.result.nonceStr,
  443. package: res.result.packageValue,
  444. signType: res.result.signType,
  445. paySign: res.result.paySign,
  446. success: function(res) {
  447. console.log('yess:' + JSON.stringify(res));
  448. uni.hideLoading();
  449. uni.redirectTo({
  450. url: "success?order=" + JSON.stringify(that.order)
  451. })
  452. },
  453. fail: function(err) {
  454. uni.showToast({
  455. icon: 'none',
  456. title: '支付失败',
  457. });
  458. console.log('fail:' + JSON.stringify(err));
  459. uni.hideLoading();
  460. },
  461. complete: (err) => {
  462. console.log('fail:' + JSON.stringify(err));
  463. }
  464. });
  465. }
  466. } else {
  467. uni.showToast({
  468. icon: 'none',
  469. title: res.msg,
  470. });
  471. }
  472. },
  473. rej => {
  474. this.isPaying = false;
  475. uni.hideLoading();
  476. uni.showToast({
  477. icon: 'none',
  478. title: '网络请求失败',
  479. });
  480. }
  481. );
  482. }
  483. }
  484. }
  485. </script>
  486. <style lang="scss">
  487. page {
  488. height: 100%;
  489. }
  490. .content {
  491. height: 100%;
  492. display: flex;
  493. flex-direction: column;
  494. justify-content: space-between;
  495. .inner {
  496. padding: 20upx;
  497. .time-price {
  498. box-sizing: border-box;
  499. padding: 50upx 0upx;
  500. background: #FFFFFF;
  501. border-radius: 16upx;
  502. display: flex;
  503. flex-direction: column;
  504. align-items: center;
  505. .time {
  506. font-size: 32upx;
  507. font-family: PingFang SC;
  508. font-weight: 500;
  509. color: #222222;
  510. line-height: 1;
  511. text-align: center;
  512. }
  513. .desc {
  514. margin: 30upx 0upx 15upx;
  515. font-size: 26upx;
  516. font-family: PingFang SC;
  517. color: #999999;
  518. line-height: 1;
  519. text-align: center;
  520. }
  521. .price-box {
  522. display: flex;
  523. align-items: flex-end;
  524. margin-top: 28upx;
  525. .unit {
  526. font-size: 32upx;
  527. font-family: PingFang SC;
  528. font-weight: bold;
  529. color: #FF6633;
  530. line-height: 1.3;
  531. margin-right: 10upx;
  532. }
  533. .num {
  534. font-size: 56upx;
  535. font-family: PingFang SC;
  536. font-weight: bold;
  537. color: #FF6633;
  538. line-height: 1;
  539. }
  540. }
  541. }
  542. .pay-type {
  543. box-sizing: border-box;
  544. background: #FFFFFF;
  545. border-radius: 16upx;
  546. margin-top: 20upx;
  547. padding: 40upx 30upx;
  548. display: flex;
  549. flex-direction: column;
  550. justify-content: space-between;
  551. .title {
  552. font-size: 28upx;
  553. font-family: PingFang SC;
  554. font-weight: 500;
  555. color: #999999;
  556. line-height: 1;
  557. margin-bottom: 10upx;
  558. }
  559. .item {
  560. padding: 15upx 0upx;
  561. display: flex;
  562. align-items: center;
  563. justify-content: space-between;
  564. .left {
  565. display: flex;
  566. align-items: center;
  567. image {
  568. width: 44upx;
  569. height: 44upx;
  570. margin-right: 20upx;
  571. }
  572. .text {
  573. font-size: 30upx;
  574. font-family: PingFang SC;
  575. font-weight: bold;
  576. color: #222222;
  577. line-height: 1;
  578. }
  579. }
  580. }
  581. }
  582. .order-info {
  583. margin-top: 20upx;
  584. background: #FFFFFF;
  585. border-radius: 16upx;
  586. padding: 40upx 30upx;
  587. .title {
  588. font-size: 30upx;
  589. font-family: PingFang SC;
  590. font-weight: bold;
  591. color: #222222;
  592. line-height: 1;
  593. }
  594. .item {
  595. margin-top: 40upx;
  596. display: flex;
  597. align-items: center;
  598. justify-content: space-between;
  599. .label {
  600. font-size: 26upx;
  601. font-family: PingFang SC;
  602. font-weight: 500;
  603. color: #666666;
  604. line-height: 1;
  605. }
  606. .text {
  607. font-size: 26upx;
  608. font-family: PingFang SC;
  609. font-weight: 500;
  610. color: #222222;
  611. line-height: 32upx;
  612. }
  613. .cont-text {
  614. font-size: 26upx;
  615. font-family: PingFang SC;
  616. font-weight: 500;
  617. color: #666666;
  618. .bold {
  619. color: #111111;
  620. }
  621. }
  622. .sn-box {
  623. display: flex;
  624. align-items: center;
  625. .copy-btn {
  626. width: 58upx;
  627. height: 32upx;
  628. line-height: 32upx;
  629. text-align: center;
  630. font-size: 22upx;
  631. font-family: PingFang SC;
  632. font-weight: 500;
  633. color: #222222;
  634. background: #F5F5F5;
  635. border-radius: 4upx;
  636. margin-left: 24upx;
  637. }
  638. }
  639. }
  640. .line {
  641. width: 100%;
  642. height: 1px;
  643. background: #F0F0F0;
  644. margin-top: 30upx;
  645. }
  646. }
  647. }
  648. .btn-box {
  649. height: 242upx;
  650. background: #FFFFFF;
  651. display: flex;
  652. align-items: center;
  653. justify-content: center;
  654. flex-direction: column;
  655. .btn {
  656. width: 91.73%;
  657. height: 88upx;
  658. line-height: 88upx;
  659. font-size: 30upx;
  660. font-family: PingFang SC;
  661. font-weight: bold;
  662. color: #FFFFFF;
  663. text-align: center;
  664. background: #2BC7B9;
  665. border-radius: 44upx;
  666. margin-bottom: 10rpx;
  667. }
  668. .other-btn {
  669. width: 91.73%;
  670. height: 88upx;
  671. line-height: 88upx;
  672. font-size: 30upx;
  673. font-family: PingFang SC;
  674. font-weight: bold;
  675. color: #2BC7B9;
  676. border: 1rpx solid #2BC7B9;
  677. text-align: center;
  678. background: #FFFFFF;
  679. border-radius: 44upx;
  680. margin-bottom: 10rpx;
  681. position: relative;
  682. .share {
  683. display: inline-block;
  684. position: absolute;
  685. top: 0;
  686. left: 0;
  687. width: 100%;
  688. height: 100%;
  689. opacity: 0;
  690. }
  691. }
  692. }
  693. }
  694. </style>