choosePayment.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <view class="pages">
  3. <view class="" :style="{height: navHeight+'px' }"></view>
  4. <u-navbar
  5. title="订单详情"
  6. :safeAreaInsetTop="true"
  7. @leftClick="leftClick"
  8. :titleStyle="{color:'#fff'}"
  9. leftIconColor="#fff"
  10. bgColor="#ED0000">
  11. </u-navbar>
  12. <view class="page-wrap">
  13. <view class="pay-list">
  14. <radio-group @change="payRadioChange">
  15. <view class="pay-list-item u-flex u-row-between" @click="payRadioClick('weixin')">
  16. <view class="left u-flex">
  17. <image class="icon" :src="staticUrl+'/img/share-ico.png'" ></image>
  18. <view class="name">微信支付</view>
  19. </view>
  20. <radio color="#2DCF8C" value="weixin" :checked="'weixin' === radioCurrent" />
  21. </view>
  22. <view class="pay-list-item u-flex u-row-between" @click="payRadioClick('yue')">
  23. <view class="left u-flex">
  24. <image class="icon" :src="staticUrl+'/img/share-ico.png'" ></image>
  25. <view class="name">账户余额</view>
  26. <view class="num">剩余¥:2000</view>
  27. </view>
  28. <radio color="#2DCF8C" value="yue" :checked="'yue' === radioCurrent" />
  29. </view>
  30. <view class="pay-list-item u-flex u-row-between" @click="payRadioClick('shouxin')">
  31. <view class="left u-flex">
  32. <image class="icon" :src="staticUrl+'/img/share-ico.png'" ></image>
  33. <view class="name">授信余额</view>
  34. <view class="num">剩余¥:2000</view>
  35. </view>
  36. <radio color="#2DCF8C" value="shouxin" :checked="'shouxin' === radioCurrent" />
  37. </view>
  38. </radio-group>
  39. </view>
  40. <view class="full-btn" @click="submit">支付</view>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import { systemInfo } from "@/mixin.js";
  46. // #ifdef H5
  47. import wxH5 from "weixin-jsapi";
  48. // #endif
  49. export default{
  50. mixins:[systemInfo],
  51. name:'',
  52. components:{
  53. },
  54. props:{
  55. },
  56. data(){
  57. return{
  58. staticUrl:this.$commonConfig.staticUrl,
  59. // 支付方式选择
  60. radioCurrent: 'weixin',
  61. }
  62. },
  63. onLoad(){
  64. this.getSystemInfo();
  65. },
  66. methods:{
  67. leftClick(e){
  68. let pages = getCurrentPages();
  69. if(pages.length==1){
  70. uni.$u.route('/pages/index/index')
  71. }else{
  72. uni.navigateBack()
  73. };
  74. },
  75. payRadioChange({ detail }) {
  76. this.radioCurrent = detail.value;
  77. },
  78. payRadioClick(value) {
  79. this.radioCurrent = value;
  80. },
  81. submit(){
  82. console.log('this.radioCurrent', this.radioCurrent);
  83. },
  84. getTemplateIdList(){
  85. this.$u.api.templateIdList({templateLabel:'order_pay'}).then(res=>{
  86. console.log('getTemplateIdList',res.data);
  87. this.templateIdList = res.data.list.map(item=>{
  88. return item.templateId
  89. });
  90. // if(this.templateIdList.length>0){
  91. // this.templateEven();
  92. // }
  93. }).catch(err=>{
  94. console.log('getTemplateIdList',err);
  95. })
  96. },
  97. pay(item){
  98. // #ifdef H5
  99. this.gotoPay(this.orderDetails.id);
  100. // #endif
  101. // #ifdef MP
  102. this.setTemplate(this.orderDetails.id);
  103. // #endif
  104. },
  105. gotoPay(orderId){
  106. this.$u.api.gotoPay({orderId:orderId,openid:''}).then(res=>{
  107. this.payResult = res.data.payInfo;
  108. this.payResult.package = res.data.payInfo.packageValue;
  109. // #ifdef H5
  110. this.initConfig(this.payResult)
  111. // #endif
  112. // #ifdef MP
  113. this.wxPay()
  114. // #endif
  115. // if(this.params.paymentMode==1||this.params.paymentMode==4){
  116. // this.wxPay()
  117. // }else{
  118. // uni.$u.route('/shopping/paysuccess');
  119. // }
  120. console.log('gotoPayres',res.data);
  121. }).catch(err=>{
  122. this.paypass = '';
  123. this.checkPassShow = false;
  124. console.log('gotoPay',err);
  125. })
  126. },
  127. wxPay(){
  128. let that = this;
  129. uni.requestPayment({
  130. ... this.payResult,
  131. "provider": "wxpay",
  132. "orderInfo": {
  133. // "appid": "wx499********7c70e", // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  134. // "noncestr": "c5sEwbaNPiXAF3iv", // 随机字符串
  135. // "package": "Sign=WXPay", // 固定值
  136. // "partnerid": "148*****52", // 微信支付商户号
  137. // "prepayid": "wx202254********************fbe90000", // 统一下单订单号
  138. // "timestamp": 1597935292, // 时间戳(单位:秒)
  139. // "sign": "A842B45937F6EFF60DEC7A2EAA52D5A0" // 签名,这里用的 MD5/RSA 签名
  140. },
  141. success(res) {
  142. that.payQuery();
  143. },
  144. fail(e) {
  145. console.log('wxPayfail',e);
  146. }
  147. })
  148. },
  149. payQuery(){
  150. let that = this;
  151. let retryCount = 0;
  152. let maxRetryCount = 5; // 设置最大重试次数
  153. let interval = 2000; // 设置间隔时间为2秒
  154. let timeout = 10000; // 设置超时时间为10秒
  155. let timer;
  156. uni.showLoading({
  157. title:'支付结果查询中'
  158. })
  159. timer = setInterval(() => {
  160. retryCount++;
  161. if (retryCount > maxRetryCount || retryCount * interval > timeout) {
  162. clearInterval(timer);
  163. uni.hideLoading();
  164. console.log("支付查询超时或达到最大重试次数");
  165. // 在这里添加超时或达到最大重试次数的处理逻辑
  166. this.reloadList()
  167. } else {
  168. console.log("第" + retryCount + "次查询");
  169. // 调用查询支付状态的方法
  170. // 如果支付状态为成功,则清除定时器并处理成功的逻辑
  171. // 如果支付状态为失败,则清除定时器并处理失败的逻辑
  172. this.$u.api.payQuery({orderId:this.orderId}).then(res=>{
  173. // 0-未支付 1-已支付 2-支付中 3-支付失败 4-支付退款
  174. let payStatus = res.data.payStatus;
  175. if(payStatus===1){
  176. uni.$u.route('/center/paysuccess');
  177. }else if(payStatus===0||payStatus===2){
  178. this.payQuery()
  179. }else if(payStatus===3){
  180. uni.toast('支付失败')
  181. }
  182. clearInterval(timer);
  183. }).catch(err=>{
  184. console.log('payQuery',err);
  185. }).finally(()=>{
  186. uni.hideLoading()
  187. })
  188. }
  189. }, interval);
  190. },
  191. /**
  192. * 公众号微信支付
  193. */
  194. initConfig() {
  195. // #ifdef H5
  196. let that = this
  197. wxH5.config({
  198. debug: false, // 这里一般在测试阶段先用ture,等打包给后台的时候就改回false,
  199. appId: that.payResult.appId, // 必填,公众号的唯一标识
  200. timestamp: that.payResult.timeStamp, // 必填,生成签名的时间戳
  201. nonceStr: that.payResult.nonceStr, // 必填,生成签名的随机串
  202. signature: that.payResult.paySign, // 必填,签名
  203. jsApiList: ['chooseWXPay', 'checkJsApi'] // 必填,需要使用的JS接口列表
  204. })
  205. wxH5.ready(() => {
  206. wxH5.chooseWXPay({
  207. timestamp: that.payResult.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  208. nonceStr: that.payResult.nonceStr, // 支付签名随机串,不长于 32 位
  209. package: that.payResult.packageValue, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  210. signType: 'SHA1', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  211. paySign: that.payResult.paySign, // 支付签名
  212. success: () => {
  213. that.payQuery();
  214. },
  215. fail: (e) => {
  216. uni.$u.route('/center/order', {
  217. status: 0
  218. });
  219. console.log('wxPayfail', e);
  220. },
  221. cancel: () => {
  222. uni.$u.route('/center/order', {
  223. status: 0
  224. });
  225. that.cansubmit = true;
  226. }
  227. })
  228. })
  229. // #endif
  230. },
  231. }
  232. }
  233. </script>
  234. <style lang="scss" scoped>
  235. .pay-list{
  236. .pay-list-item{
  237. margin-bottom: 24rpx;
  238. .icon{
  239. width: 40rpx;
  240. height: 40rpx;
  241. margin-right: 24rpx;
  242. }
  243. .name{
  244. margin-right: 24rpx;
  245. }
  246. .num{
  247. }
  248. }
  249. }
  250. </style>