pay.vue 14 KB

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