pay.vue 13 KB

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