pay.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <view class="page">
  3. <view class="p-top flex-col align-center justify-center">
  4. <view class="key">待支付金额</view>
  5. <view class="price">¥151.00</view>
  6. </view>
  7. <view class="p-type">
  8. <view class="line align-center" @tap="tapType(1)">
  9. <view class="key">
  10. 余额支付
  11. </view>
  12. <text class="money">{{userInfo.money}}</text>
  13. <image :src="payType === 1? '/static/check-do.png' : '/static/check-un.png'" mode="widthFix" class="ic"></image>
  14. </view>
  15. <view class="line align-center" @tap="tapType(2)">
  16. <view class="key">
  17. 微信支付
  18. </view>
  19. <image :src="payType === 2? '/static/check-do.png' : '/static/check-un.png'" mode="widthFix" class="ic"></image>
  20. </view>
  21. </view>
  22. <submit-btn @submit="submit" ref="submitRef">
  23. <template v-slot:btns="{isSubmitted}">
  24. <view class="p-btn" :class="{'disabled':isSubmitted}">
  25. 立即支付
  26. </view>
  27. </template>
  28. </submit-btn>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. openid:'',
  36. orderId:'',
  37. payType:'', //1=余额支付,2微信支付
  38. price:0,
  39. };
  40. },
  41. onLoad(e){
  42. this.orderId = e.orderId;
  43. this.getOpenid();
  44. },
  45. methods:{
  46. async getOpenid(){
  47. let [lErr,lRes] = await uni.login({
  48. provider: 'weixin'
  49. });
  50. if (lRes.hasOwnProperty('code')) {
  51. let res = await this.$request('user.getopenid',{code:lRes.code});
  52. this.openid = res.data.openid;
  53. }else {
  54. this.$tools.msg('登录失败,请重启应用');
  55. }
  56. },
  57. tapType(type){
  58. if(type === this.payType){
  59. return;
  60. }
  61. if(type===1 && parseFloat(this.price || 0) > parseFloat(this.userInfo.money || 0) ){
  62. this.$tools.msg('您的余额不足')
  63. return;
  64. }
  65. this.payType = type;
  66. },
  67. //支付
  68. async submit(){
  69. if(!this.openid){
  70. let [mErr,mRes] = await uni.showModal({
  71. title: '微信错误,请刷新',
  72. showCancel:false,
  73. confirmText:'刷新'
  74. })
  75. if(mRes.confirm){
  76. this.getOpenid();
  77. }
  78. return;
  79. }else if(!this.payType){
  80. this.$tools.msg('请选择支付方式')
  81. return;
  82. }else if(this.payType===1 && parseFloat(this.price || 0) > parseFloat(this.userInfo.money || 0) ){
  83. this.$tools.msg('您的余额不足')
  84. return;
  85. }
  86. let sendData={
  87. id:this.orderId,
  88. pay_type:this.payType,
  89. openid: this.openid,
  90. }
  91. let res=await this.$request('order.pay',sendData);
  92. let data = res.data;
  93. if(this.payType == 1){
  94. this.$refs.submitRef.chgeStatus();
  95. this.navTo('/pages/public/paySuc',{},{type:'replace'})
  96. }else if (this.payType == 2) {
  97. uni.requestPayment({
  98. provider: 'wxpay',
  99. timeStamp: data.timeStamp,
  100. nonceStr: data.nonce_str,
  101. package: data.prepay_id,
  102. signType: data.signType,
  103. paySign: data.paySign,
  104. success: (res) => {
  105. this.$refs.submitRef.chgeStatus();
  106. this.navTo('/pages/public/paySuc',{},{type:'replace'})
  107. },
  108. fail: function(err) {
  109. //console.log('fail:' + JSON.stringify(err));
  110. //that.$api.msg('fail:' + JSON.stringify(err))
  111. that.$tools.msg('支付失败');
  112. }
  113. });
  114. }
  115. },
  116. }
  117. }
  118. </script>
  119. <style lang="scss">
  120. page{
  121. background-color: #f8f8f8;
  122. }
  123. .page{
  124. padding: 30rpx;
  125. }
  126. .p-top{
  127. width: 690rpx;
  128. height: 194rpx;
  129. background: #FFFFFF;
  130. box-shadow: 0rpx 6rpx 10rpx 0rpx rgba(42,42,42,0.08);
  131. border-radius: 20rpx;
  132. .key{
  133. font-size: 24rpx;
  134. font-family: PingFang SC;
  135. font-weight: 500;
  136. color: #999999;
  137. line-height: 37rpx;
  138. }
  139. .price{
  140. margin-top: 28rpx;
  141. font-size: 28rpx;
  142. font-family: PingFang SC;
  143. font-weight: 500;
  144. color: #FF3E3E;
  145. line-height: 37rpx;
  146. }
  147. }
  148. .p-type{
  149. margin-top: 24rpx;
  150. width: 690rpx;
  151. background: #FFFFFF;
  152. box-shadow: 0rpx 6rpx 10rpx 0rpx rgba(42,42,42,0.08);
  153. border-radius: 20rpx;
  154. padding: 0 20rpx;
  155. margin-bottom: 154rpx;
  156. .line{
  157. height: 108rpx;
  158. border-bottom: 2rpx solid #eee;
  159. &:last-child{
  160. border: none;
  161. }
  162. .key{
  163. font-size: 30rpx;
  164. font-family: PingFang SC;
  165. font-weight: 500;
  166. color: #333333;
  167. }
  168. .money{
  169. margin-left: 20rpx;
  170. color: #333;
  171. font-size: 28rpx;
  172. }
  173. .ic{
  174. margin-left: auto;
  175. width: 36rpx;
  176. height: 36rpx;
  177. }
  178. }
  179. }
  180. .p-btn{
  181. width: 690rpx;
  182. height: 94rpx;
  183. background: #92B99C;
  184. border-radius: 47rpx;
  185. font-size: 34rpx;
  186. font-family: PingFang SC;
  187. font-weight: 500;
  188. color: #FFFFFF;
  189. line-height: 94rpx;
  190. text-align: center;
  191. &.disabled{
  192. background-color: #eee;
  193. }
  194. }
  195. </style>