iospay.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script>
  6. import http from "../library/http.js"
  7. export default {
  8. props:{
  9. type:{
  10. default:'recharge'
  11. },
  12. money_select:{
  13. default:0
  14. },
  15. money:{
  16. default:0
  17. }
  18. },
  19. data() {
  20. return {
  21. iapChannel:null,
  22. productId:'vip_month',//默认苹果内购商品ID
  23. productIds: ['vip_month'],
  24. user:uni.getStorageSync('userInfo'),
  25. system:uni.getStorageSync('system'),
  26. };
  27. },
  28. mounted() {
  29. this.plusReady();
  30. },
  31. methods:{
  32. errorMsg(title){
  33. uni.showToast({
  34. icon:'none',
  35. title:title
  36. })
  37. this.cancel_pay();
  38. },
  39. cancel_pay(){
  40. this.$emit('cancel_pay',false);
  41. },
  42. plusReady() {
  43. if(this.type=='recharge'){
  44. this.productIds=this.system.iospay_recharge.split('|');
  45. }else{
  46. this.productIds=this.system.iospay_vip.split('|');
  47. }
  48. this.productId=this.productIds[this.money_select];
  49. // console.log(this.productId);
  50. var that=this;
  51. plus.payment.getChannels((channels) => {
  52. for (var i in channels) {
  53. var channel = channels[i];
  54. //苹果支付
  55. if (channel.id === 'appleiap') {
  56. this.iapChannel = channel;
  57. this.requestOrder();//关键
  58. }
  59. }
  60. if(!this.iapChannel){
  61. that.errorMsg('未获取到ios内购通道')
  62. }
  63. else{
  64. var that1=that;
  65. // setTimeout(function(){ that1.pay_start();},300)
  66. }
  67. // console.log(this.iapChannel);
  68. }, (error) => {
  69. that.errorMsg('iOS内购通道获取失败')
  70. });
  71. } ,
  72. requestOrder() {
  73. uni.showLoading({
  74. title:'正在请求支付通道...'
  75. })
  76. this.iapChannel.requestOrder(this.productIds, (orderList) => { //必须调用此方法才能进行 iap 支付
  77. uni.hideLoading();
  78. this.pay_start()
  79. }, (e) => {
  80. uni.hideLoading();
  81. this.errorMsg('支付通道请求失败')
  82. });
  83. },
  84. pay_start(){
  85. let that = this;
  86. uni.showLoading({
  87. title: '正在调起支付...'
  88. });
  89. uni.requestPayment({
  90. provider:'appleiap',
  91. orderInfo:{
  92. productid: that.productId,
  93. username: that.user.id
  94. },
  95. success: (e) => {
  96. this.pay_ok(e);
  97. },
  98. fail: (e) => {
  99. uni.hideLoading();
  100. this.errorMsg('支付失败,请重试')
  101. },
  102. complete: (e) => {
  103. uni.hideLoading();
  104. }
  105. })
  106. },
  107. pay_ok(e){
  108. var postdata={type:this.type,money:this.money,userid:this.user.id,money_select:this.money_select}
  109. http.setWait(false).post('pay.php?act=iospay',postdata).then(res=>{
  110. uni.showToast({
  111. icon:'none',
  112. title:res.message
  113. })
  114. this.getuserinfo();
  115. if(this.type=='recharge'){
  116. uni.showModal({
  117. title: '提示',
  118. content: "充值已到账",
  119. showCancel: true,
  120. cancelText: '关闭',
  121. confirmText: '查看账单',
  122. success: res => {
  123. if(res.confirm) {
  124. uni.redirectTo({
  125. url:"/pages/mine/order"
  126. })
  127. }else{
  128. this.cancel_pay();
  129. }
  130. }
  131. });
  132. }
  133. else{
  134. setTimeout(function(){
  135. uni.navigateBack();
  136. },800)
  137. }
  138. })
  139. },
  140. getuserinfo(){
  141. var postdata={
  142. id: uni.getStorageSync('access_token')
  143. };
  144. http.setWait(false).post('user.php?act=userinfo',postdata).then(res=>{
  145. this.user=res.data;
  146. uni.setStorageSync('userInfo',this.user)
  147. })
  148. },
  149. }
  150. }
  151. </script>
  152. <style lang="scss">
  153. </style>