pay.vue 14 KB

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