pay.vue 13 KB

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