callback.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <view class="container">
  3. <view class="loading">
  4. <text>正在获取用户信息...</text>
  5. <view>{{userInfo}}000</view>
  6. <view>{{token}}{{res}}</view>
  7. <view @click="copy">复制res</view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uni from "@/utils/uni.webview.1.5.4.js";
  13. import { loginByMp} from '@/api/user'
  14. export default {
  15. data() {
  16. return {
  17. code: '',
  18. userInfo: null,
  19. token:null,
  20. res:{}
  21. }
  22. },
  23. onLoad(options) {
  24. // 获取URL中的code参数(微信授权返回的)
  25. this.code = this.getUrlParam('code')
  26. if (!this.code) {
  27. uni.showToast({
  28. title: '授权失败,未获取到code',
  29. icon: 'none'
  30. })
  31. return
  32. }
  33. // 通过code获取用户信息
  34. this.getUserInfoByCode(this.code)
  35. },
  36. methods: {
  37. copy(){
  38. uni.setClipboardData({
  39. data: this.res, //data是需要复制的数据
  40. showToast: true, //配置是否弹出提示,默认弹出提示
  41. success: function () {
  42. console.log('success'); //复制成功的回调函数
  43. },
  44. fail: function () {
  45. console.log('fail'); //复制失败的回调函数
  46. },
  47. complete: function () {
  48. console.log('complete'); //接口调用结束的回调函数(调用成功、失败都会执行)
  49. },
  50. });
  51. },
  52. // 获取URL参数
  53. getUrlParam(name) {
  54. const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  55. const r = window.location.search.substr(1).match(reg)
  56. if (r != null) return decodeURIComponent(r[2])
  57. return null
  58. },
  59. // 通过code获取用户信息
  60. getUserInfoByCode(code) {
  61. // 调用后端API,通过code换取用户信息
  62. uni.showLoading({
  63. title: "处理中..."
  64. });
  65. var data = {
  66. code: this.code,
  67. videoId: this.videoId,
  68. companyId: this.companyId,
  69. companyUserId: this.companyUserId
  70. }
  71. loginByMp(data).then(res => {
  72. this.res=res
  73. uni.hideLoading();
  74. if (res.code == 200) {
  75. uni.setStorageSync('TOKEN_WEXIN', res.token);
  76. this.userInfo = res.user
  77. this.token= res.token
  78. // 将用户信息传递给小程序
  79. setTimeout(() => {
  80. this.postMessageToMiniProgram()
  81. }, 200)
  82. } else {
  83. console.error('获取用户信息失败:', res.msg)
  84. uni.showToast({
  85. title: res.msg || '获取用户信息失败',
  86. icon: 'none'
  87. })
  88. }
  89. },
  90. err => {}
  91. ).catch(err=>{
  92. uni.hideLoading();
  93. });
  94. },
  95. // 向小程序发送消息
  96. postMessageToMiniProgram() {
  97. // 判断是否在小程序环境中
  98. uni.showToast({
  99. title:'获取用户信息失败22',
  100. icon: 'none'
  101. })
  102. // if (wx && wx.miniProgram) {
  103. // wx.miniProgram.postMessage({ data: ['这是从H5页面发送的消息', 'wdwdsdsf'] });
  104. // console.log(uni.postMessage);
  105. // console.log(111111);
  106. // } else {
  107. // console.error('当前环境不支持微信小程序API');
  108. // }
  109. // uni.showToast({
  110. // title:'获取用户信息失败123',
  111. // icon: 'none'
  112. // })
  113. // setTimeout(() => {
  114. // uni.navigateBack()
  115. // }, 500)
  116. if (window.__wxjs_environment === 'miniprogram') {
  117. // 向小程序发送用户信息
  118. uni.postMessage({
  119. data: {
  120. type: 'user_info',
  121. data: this.userInfo,
  122. token: this.token,
  123. }
  124. })
  125. uni.showToast({
  126. title:'获取用户信息失败123',
  127. icon: 'none'
  128. })
  129. // 延迟关闭当前页面,确保消息发送成功
  130. setTimeout(() => {
  131. uni.navigateBack()
  132. }, 500)
  133. } else {
  134. uni.showToast({
  135. title:'获取用户信息失败456',
  136. icon: 'none'
  137. })
  138. // 在浏览器环境中,可以做H5版本的处理
  139. console.log('非小程序环境,H5版本处理用户信息:', this.userInfo)
  140. uni.setStorageSync('userInfo', this.userInfo)
  141. uni.setStorageSync('TOKEN_WEXIN', this.token)
  142. console.error('TOKEN_WEXIN:', this.token)
  143. uni.navigateTo({
  144. url: '/pages/index/index'
  145. })
  146. }
  147. }
  148. }
  149. }
  150. </script>
  151. <style>
  152. .container {
  153. width: 100%;
  154. height: 100vh;
  155. display: flex;
  156. justify-content: center;
  157. align-items: center;
  158. background-color: #fff;
  159. }
  160. .loading {
  161. display: flex;
  162. flex-direction: column;
  163. align-items: center;
  164. }
  165. </style>