pay.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <template>
  2. <view class="">
  3. <u-navbar
  4. title="收银台"
  5. :placeholder="true"
  6. @leftClick="leftClick"
  7. :autoBack="false"
  8. :safeAreaInsetTop="true"
  9. >
  10. </u-navbar>
  11. <view class="page-wrap">
  12. <view class="pay-way page-wrap">
  13. <u-radio-group v-model="params.paymentMode" placement="column" @change="groupChange">
  14. <view class="pay-way-item u-flex u-row-between" @click="selectPayment('wx')">
  15. <view class="left u-flex">
  16. <image class="icon" :src="staticUrl+'/img/payway-wx.png'" mode="aspectFill"></image>
  17. <view class="name">微信支付</view>
  18. </view>
  19. <u-radio name="wx" activeColor="#1677FF" size="36rpx"></u-radio>
  20. </view>
  21. <view :class="{disabled:balanceInfo.balance<params.payAmount}" @click="selectPayment('yue')" class="pay-way-item u-flex u-row-between">
  22. <view class="left u-flex">
  23. <image class="icon" :src="staticUrl+'/img/pyway-yue.png'" mode="aspectFill"></image>
  24. <view class="name">账户余额</view>
  25. ¥ {{balanceInfo.balance}}
  26. </view>
  27. <u-radio name="yue" :disabled="balanceInfo.balance<params.payAmount" activeColor="#1677FF" size="36rpx"></u-radio>
  28. </view>
  29. <view :class="{disabled:balanceInfo.grantSurplus<params.payAmount}" @click="selectPayment('shouxin')" class="pay-way-item u-flex u-row-between">
  30. <view class="left u-flex">
  31. <image class="icon" :src="staticUrl+'/img/pyway-shouxin.png'" mode="aspectFill"></image>
  32. <view class="name">授信余额</view>
  33. ¥ {{balanceInfo.grantSurplus}}
  34. </view>
  35. <u-radio name="shouxin" :disabled="balanceInfo.grantSurplus<params.payAmount" activeColor="#1677FF" size="36rpx"></u-radio>
  36. </view>
  37. <view class="repayment-time pay-way-item u-flex u-row-between" @click="showDate = true" v-if="params.paymentMode === 'shouxin'">
  38. <view class="label">账期</view>
  39. <view class="value">{{ params.rePaymentTime||'请选择账期' }}</view>
  40. </view>
  41. </u-radio-group>
  42. </view>
  43. <u-datetime-picker
  44. :show="showDate"
  45. :value="params.rePaymentTime"
  46. mode="date"
  47. @confirm="confirmDateChange"
  48. @cancel="showDate = false"
  49. :minDate="minDate"
  50. :maxDate="maxDate"
  51. ></u-datetime-picker>
  52. <view class="full-btn" v-if="params.paymentMode&&cansubmit" @click="payRoute">去支付<text>¥ {{params.payAmount}}</text></view>
  53. <view class="full-btn gray" v-else>去支付<text>¥ {{params.payAmount}}</text></view>
  54. <u-popup :show="checkPassShow" @close="checkPassClose" mode="center">
  55. <view class="check-pass">
  56. <view class="title">请输入支付密码</view>
  57. <!-- <view class="name">商城</view> -->
  58. <view class="number">¥{{params.payAmount}}</view>
  59. <view class="the-way u-flex u-row-between">
  60. <view class="left">支付方式</view>
  61. <view class="right u-flex">
  62. <u--image class="image" :src="staticUrl+'/img/qian.png'" width="32rpx" height="32rpx"></u--image>
  63. <text style="margin-left: 10px;">余额</text>
  64. </view>
  65. </view>
  66. <u-code-input v-model="paypass"
  67. :space="0"
  68. :size="40"
  69. @finish="checkPayPassword"
  70. :maxlength="6"
  71. :adjustPosition="true"
  72. :dot="true"
  73. :focus="true"
  74. :disabled-keyboard="false" >
  75. </u-code-input>
  76. <!-- <view class="btn-wrap u-flex">
  77. <u-button text="取消" @click="checkPassShow=false"></u-button>
  78. <u-button text="确认" @click="checkPassShow=false"></u-button>
  79. </view> -->
  80. </view>
  81. </u-popup>
  82. </view>
  83. </view>
  84. </template>
  85. <script>
  86. // #ifdef H5
  87. import wxH5 from "weixin-jsapi";
  88. // #endif
  89. export default {
  90. data() {
  91. return {
  92. staticUrl:this.$commonConfig.staticUrl,
  93. fromPage:'',
  94. customBalance:'',
  95. credit:'',
  96. params:{
  97. paymentMode:null,
  98. orderId:'',
  99. openid:'',
  100. payAmount:'',
  101. payResult:{},
  102. rePaymentTime: '',
  103. },
  104. checkPassShow:false,
  105. paypass:'',
  106. noPaypass:true,
  107. balanceInfo:{
  108. balance:0,// 用户余额
  109. grantQuota:0,// 授信额度
  110. grantUsed:0,//已使用的额度
  111. grantSurplus:0,//剩余额度
  112. isSetPayPassword:0,//是否设置支付密码 0-否 1-是
  113. },
  114. cansubmit:true,
  115. needPayQuery:0,
  116. payerId:'',
  117. showDate: false,
  118. minDate: new Date().getTime(),
  119. maxDate: new Date(new Date().getFullYear() + 1, 11, 31).getTime(),
  120. }
  121. },
  122. onShow(page) {
  123. // console.log('onShow page',page);
  124. let allPages = getCurrentPages(); //获取当前页面栈的实例;
  125. // console.log(allPages);
  126. let lastPages = allPages.length - 1; // 获得倒数第二个元素的索引;
  127. console.log(lastPages);
  128. let option = allPages[lastPages].options; // 获得上个页面传递的参数;
  129. console.log(option);
  130. if(option.needPayQuery){
  131. this.cansubmit = false;
  132. this.payQuery();
  133. }
  134. this.getBalanceInfo();
  135. },
  136. onLoad(page) {
  137. console.log('page',page);
  138. this.fromPage = page.fromPage;
  139. this.params.orderId = page.orderId;
  140. this.params.openid = this.vuex_wechatOpenid;
  141. this.params.payAmount = page.payAmount;
  142. this.params.paymentMode = page.paymentMode;
  143. this.needPayQuery = page.needPayQuery;
  144. this.payerId = page.payerId;
  145. if(page.needPayQuery){
  146. this.cansubmit = false;
  147. this.payQuery();
  148. }
  149. },
  150. methods: {
  151. leftClick(){
  152. uni.showModal({
  153. title: '提示',
  154. content: '是否放弃本次付款!',
  155. success: res => {
  156. if (res.confirm) {
  157. uni.navigateBack()
  158. } else if (res.cancel) {
  159. console.log('用户点击取消');
  160. }
  161. }
  162. });
  163. },
  164. getBalanceInfo(){
  165. this.$u.api.balanceInfo({id:this.vuex_user_info.userid,payerId:this.payerId}).then(res=>{
  166. this.balanceInfo= res.data;
  167. }).catch(err=>{
  168. console.log('balanceInfo',err.data);
  169. })
  170. },
  171. checkPassClose(){
  172. this.checkPassShow = false;
  173. },
  174. payRoute(){
  175. if(this.params.paymentMode!='wx'){
  176. if(this.params.paymentMode=='shouxin'&&!this.params.rePaymentTime){
  177. uni.showToast({
  178. title: '请选择账期',
  179. icon: 'none'
  180. });
  181. return;
  182. }
  183. this.checkExistPayPassword();
  184. }else{
  185. this.gotoPay();
  186. }
  187. this.cansubmit = false;
  188. },
  189. checkExistPayPassword(){
  190. if(this.balanceInfo.isSetPayPassword==0){
  191. this.noPaypass = true;
  192. uni.showModal({
  193. title: '还未设置支付密码',
  194. content: '请先设置支付密码!',
  195. success: res => {
  196. if (res.confirm) {
  197. uni.$u.route('/center/setPaypass', {
  198. from: 'pay',
  199. backUrl:'/shopping/order'
  200. });
  201. }
  202. }
  203. })
  204. }else{
  205. this.noPaypass = false;
  206. this.checkPassShow = true
  207. }
  208. },
  209. checkPayPassword(){
  210. this.$u.api.checkPayPassword({payPassword:this.paypass}).then(res=>{
  211. // console.log('res',res.data);
  212. if(res.msg=='操作成功'){//密码正确
  213. if(this.params.paymentMode=='wx'){
  214. this.gotoPay();
  215. }else if(this.params.paymentMode=='yue'){
  216. this.gotoBalancePay()
  217. }else if(this.params.paymentMode=='shouxin'){
  218. this.gotoQuotaPay()
  219. }
  220. }else{
  221. this.paypass = '';
  222. uni.showToast({
  223. title:'密码错误',
  224. icon:'error'
  225. });
  226. }
  227. }).catch(err=>{
  228. this.paypass = '';
  229. uni.showToast({
  230. title:'密码错误',
  231. icon:'error'
  232. });
  233. console.log('checkPayPassword',err);
  234. })
  235. },
  236. gotoPay() {
  237. this.$u.api.gotoPay({...this.params,payerId:this.payerId}).then(res => {
  238. // this.payResult = res.data.payInfo;
  239. // this.payResult.package = res.data.payInfo.packageValue;
  240. // ygh
  241. if(!res.data.needPay){
  242. //this.payQuery();
  243. uni.$u.route('/center/orderdetails', {
  244. type:'redirectTo',
  245. id: this.params.orderId
  246. });
  247. }else {
  248. window.location.href = res.data.payInfo;
  249. return
  250. // 以下未微信原生支付
  251. this.payResult = res.data.payInfo;
  252. this.payResult.package = res.data.payInfo.packageValue;
  253. // #ifdef H5
  254. this.initConfig(this.payResult)
  255. // #endif
  256. // #ifdef MP
  257. this.wxPay()
  258. // #endif
  259. }
  260. // if(this.params.paymentMode==1||this.params.paymentMode==4){
  261. // this.wxPay()
  262. // }else{
  263. // uni.$u.route('/shopping/paysuccess');
  264. // }
  265. // console.log('gotoPayres', res.data);
  266. }).catch(err => {
  267. this.cansubmit = true;
  268. // this.paypass = '';
  269. // this.checkPassShow = false;
  270. console.log('gotoPay', err);
  271. })
  272. },
  273. gotoBalancePay(){
  274. let that = this;
  275. this.$u.api.balancePay({orderId:this.params.orderId,payerId:this.payerId}).then(res=>{
  276. // console.log('res',res);
  277. uni.showToast({
  278. title:res.msg,
  279. icon:'none'
  280. })
  281. setTimeout(()=>{
  282. uni.reLaunch({
  283. url: `/center/orderdetails?type=redirectTo&id=${that.params.orderId}`
  284. });
  285. },1500)
  286. }).catch(err=>{
  287. console.log('balancePay',err.data);
  288. }).finally(()=>{
  289. this.cansubmit = true;
  290. })
  291. },
  292. gotoQuotaPay(){
  293. let that = this;
  294. this.$u.api.quotaPay({orderId:this.params.orderId,payerId:this.payerId,rePaymentTime:this.params.rePaymentTime}).then(res=>{
  295. // console.log('res',res);
  296. uni.showToast({
  297. title:res.msg,
  298. icon:'none'
  299. })
  300. setTimeout(()=>{
  301. uni.reLaunch({
  302. url: `/center/orderdetails?type=redirectTo&id=${that.params.orderId}`
  303. });
  304. },1500)
  305. }).catch(err=>{
  306. console.log('quotaPay',err.data);
  307. }).finally(()=>{
  308. this.cansubmit = true;
  309. })
  310. },
  311. wxPay(){
  312. uni.requestPayment({
  313. ... this.payResult,
  314. "provider": "wxpay",
  315. "orderInfo": {
  316. // "appid": "wx499********7c70e", // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  317. // "noncestr": "c5sEwbaNPiXAF3iv", // 随机字符串
  318. // "package": "Sign=WXPay", // 固定值
  319. // "partnerid": "148*****52", // 微信支付商户号
  320. // "prepayid": "wx202254********************fbe90000", // 统一下单订单号
  321. // "timestamp": 1597935292, // 时间戳(单位:秒)
  322. // "sign": "A842B45937F6EFF60DEC7A2EAA52D5A0" // 签名,这里用的 MD5/RSA 签名
  323. },
  324. success(res) {
  325. uni.$u.route('/shopping/paysuccess');
  326. },
  327. fail(e) {
  328. console.log('wxPayfail',e);
  329. }
  330. })
  331. },
  332. /**
  333. * 公众号微信支付
  334. */
  335. initConfig() {
  336. // #ifdef H5
  337. let that = this
  338. wxH5.config({
  339. debug: false, // 这里一般在测试阶段先用ture,等打包给后台的时候就改回false,
  340. appId: that.payResult.appId, // 必填,公众号的唯一标识
  341. timestamp: that.payResult.timeStamp, // 必填,生成签名的时间戳
  342. nonceStr: that.payResult.nonceStr, // 必填,生成签名的随机串
  343. signature: that.payResult.paySign, // 必填,签名
  344. jsApiList: ['chooseWXPay', 'checkJsApi'] // 必填,需要使用的JS接口列表
  345. })
  346. wxH5.ready(() => {
  347. wxH5.chooseWXPay({
  348. timestamp: that.payResult.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  349. nonceStr: that.payResult.nonceStr, // 支付签名随机串,不长于 32 位
  350. package: that.payResult.packageValue, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  351. signType: 'SHA1', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  352. paySign: that.payResult.paySign, // 支付签名
  353. success: () => {
  354. // setTimeout(()=>{
  355. // uni.$u.route('/center/orderdetails', {
  356. // type:'redirectTo',
  357. // id: that.params.orderId
  358. // });
  359. // },1500)
  360. that.payQuery();
  361. },
  362. fail: (e) => {
  363. uni.$u.route('/center/order', {
  364. status: 0
  365. });
  366. console.log('wxPayfail', e);
  367. },
  368. cancel: () => {
  369. uni.$u.route('/center/order', {
  370. status: 0
  371. });
  372. that.cansubmit = true;
  373. }
  374. })
  375. })
  376. // #endif
  377. },
  378. payQuery(){
  379. let that = this;
  380. let retryCount = 0;
  381. let maxRetryCount = 5; // 设置最大重试次数
  382. let interval = 2000; // 设置间隔时间为2秒
  383. let timeout = 10000; // 设置超时时间为10秒
  384. let timer;
  385. uni.showLoading({
  386. title:'支付结果查询中'
  387. })
  388. timer = setInterval(() => {
  389. retryCount++;
  390. if (retryCount > maxRetryCount || retryCount * interval > timeout) {
  391. clearInterval(timer);
  392. uni.hideLoading();
  393. console.log("支付查询超时或达到最大重试次数");
  394. // 在这里添加超时或达到最大重试次数的处理逻辑
  395. this.reloadList()
  396. } else {
  397. console.log("第" + retryCount + "次查询");
  398. // 调用查询支付状态的方法
  399. // 如果支付状态为成功,则清除定时器并处理成功的逻辑
  400. // 如果支付状态为失败,则清除定时器并处理失败的逻辑
  401. this.$u.api.payQuery({orderId:this.params.orderId}).then(res=>{
  402. // 0-未支付 1-已支付 2-支付中 3-支付失败 4-支付退款
  403. let payStatus = res.data.payStatus;
  404. if(payStatus===1){
  405. uni.reLaunch({
  406. url: `/center/orderdetails?type=redirectTo&id=${that.params.orderId}`
  407. });
  408. }else if(payStatus===0||payStatus===2){
  409. this.payQuery()
  410. }else if(payStatus===3){
  411. that.cansubmit = true;
  412. uni.toast('支付失败')
  413. }
  414. clearInterval(timer);
  415. }).catch(err=>{
  416. that.cansubmit = true;
  417. console.log('payQuery',err);
  418. }).finally(()=>{
  419. uni.hideLoading()
  420. })
  421. }
  422. }, interval);
  423. },
  424. groupChange(e){
  425. console.log('groupChange',e);
  426. },
  427. selectPayment(paymentType) {
  428. this.params.paymentMode = paymentType;
  429. },
  430. confirmDateChange(e) {
  431. console.log('confirmDateChange',e);
  432. this.params.rePaymentTime = this.$u.timeFormat(e.value, 'yyyy-mm-dd');
  433. console.log('this.params.rePaymentTime',this.params.rePaymentTime);
  434. this.showDate = false;
  435. },
  436. }
  437. }
  438. </script>
  439. <style>
  440. page{
  441. background-color: #F5F5F5;
  442. }
  443. </style>
  444. <style lang="scss" scoped>
  445. .pay-way{
  446. background-color: #fff;
  447. border-radius: 8rpx;
  448. .pay-way-item {
  449. padding: 30rpx 10rpx;
  450. .left{
  451. .icon{
  452. width: 40rpx;
  453. height: 40rpx;
  454. margin-right: 16rpx;
  455. }
  456. .name{
  457. font-weight: bold;
  458. margin-right: 40rpx;
  459. }
  460. }
  461. &:not(:last-child){
  462. border-bottom: 0.5px solid #eee;
  463. }
  464. &.disabled{
  465. .left{
  466. color: #D8D8D8;
  467. }
  468. }
  469. &.repayment-time{
  470. padding-bottom: 0;
  471. }
  472. }
  473. .left{
  474. font-size: 30rpx;
  475. font-weight: 500;
  476. color: #333333;
  477. line-height: 42rpx;
  478. }
  479. }
  480. .full-btn{
  481. margin-top: 100rpx;
  482. }
  483. /deep/ .u-popup__content{
  484. overflow: hidden;
  485. border-radius: 8rpx;
  486. }
  487. .check-pass{
  488. padding: 24rpx 40rpx 40rpx;
  489. .title{
  490. margin-bottom: 30rpx;
  491. font-size: 36rpx;
  492. font-weight: 600;
  493. color: #333333;
  494. line-height: 50rpx;
  495. text-align: center;
  496. }
  497. .name{
  498. text-align: center;
  499. font-size: 30rpx;
  500. font-weight: 400;
  501. color: #666666;
  502. line-height: 42rpx;
  503. margin-bottom: 10rpx;
  504. }
  505. .number{
  506. text-align: center;
  507. font-size: 58rpx;
  508. font-weight: 600;
  509. color: #FF3538;
  510. line-height: 81rpx;
  511. margin-bottom: 30rpx;
  512. }
  513. .the-way{
  514. font-size: 30rpx;
  515. font-weight: 400;
  516. color: #333333;
  517. line-height: 42rpx;
  518. margin-bottom: 25rpx;
  519. }
  520. .btn-wrap{
  521. }
  522. }
  523. .repayment-time {
  524. .label {
  525. font-size: 30rpx;
  526. font-weight: 500;
  527. color: #333333;
  528. line-height: 42rpx;
  529. margin-bottom: 10rpx;
  530. }
  531. }
  532. </style>