webview.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view class="container">
  3. <!-- 加载提示 -->
  4. <view class="loading" v-if="loading">
  5. <text>加载中...</text>
  6. </view>
  7. <!-- web-view组件 -->
  8. <web-view
  9. :src="webviewUrl"
  10. @message="handleMessage"
  11. @load="onLoads"
  12. @error="onError"
  13. ></web-view>
  14. </view>
  15. </template>
  16. <script>
  17. import { getusername} from '@/api/courseLook'
  18. export default {
  19. data() {
  20. return {
  21. loading: true,
  22. // webviewUrl: '',//福本源
  23. // webviewUrl: 'https://vip.aishanghys.com/weixinOauth',//蜂巢快药
  24. webviewUrl: uni.getStorageSync('authUrl'),//中康
  25. // webviewUrl: 'https://vip.liangmiaoedu.com/weixinOauth',//良苗
  26. // webviewUrl: 'https://vips.beliyostore.com/weixinOauth',//倍力优
  27. // webviewUrl: 'https://userapp.drkzyy.cn/weixinOauth',//德瑞康
  28. // webviewUrl: 'https://companyapp.gjh2024.com/weixinOauth.html',//金慷建
  29. userInfo: {},
  30. userinfos:{
  31. nickname:"",
  32. avatar:""
  33. },
  34. }
  35. },
  36. onLoad(options) {
  37. console.log(options)
  38. // if(options.url&&!options.code){
  39. // this.webviewUrl=options.url
  40. // }
  41. if(options.code){
  42. // uni.$emit('us ercode', { code: options.code });
  43. this.loginweixin(options.code)
  44. }
  45. // 生成带参的H5授权页面URL
  46. // this.webviewUrl = this.generateAuthUrl()
  47. },
  48. methods: {
  49. loginweixin(datas){
  50. var data = {
  51. code: datas,
  52. }
  53. getusername(data).then(res => {
  54. this.res=res
  55. uni.hideLoading();
  56. if (res.code == 200) {
  57. console.log(res)
  58. uni.hideLoading();
  59. uni.showToast({
  60. icon:'none',
  61. title: "成功获取用户信息",
  62. });
  63. this.userinfos.nickname=res.nickname;
  64. this.userinfos.avatar=res.headImgUrl;
  65. uni.setStorageSync('userInfos', this.userinfos);
  66. uni.navigateBack({
  67. delta: 1
  68. });
  69. } else {
  70. uni.hideLoading();
  71. uni.showToast({
  72. title: res.msg || '获取用户信息失败',
  73. icon: 'none'
  74. })
  75. }
  76. },
  77. err => {}
  78. ).catch(err=>{
  79. uni.hideLoading();
  80. uni.showToast({
  81. icon:'none',
  82. title: "获取用户信息失败",
  83. });
  84. });
  85. },
  86. // 生成授权页面URL,附带小程序传递的参数
  87. generateAuthUrl() {
  88. // 获取当前小程序的场景值,用于后续业务处理
  89. const scene = uni.getLaunchOptionsSync().scene
  90. // 这里替换为你的uniapp H5项目域名
  91. // 拼接参数,可包含小程序特有的信息
  92. const params = {
  93. scene,
  94. appid: 'wx961fadab9bcb792b', // 公众号AppID
  95. redirect_uri: encodeURIComponent('https://h5.fbylive.com/weixinOauth'),
  96. scope: 'snsapi_userinfo',
  97. state: 'wechat_redirect'
  98. }
  99. // 微信公众号授权URL
  100. return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${params.appid}&redirect_uri=${params.redirect_uri}&response_type=code&scope=${params.scope}&state=${params.state}#wechat_redirect`
  101. },
  102. // 处理web-view向小程序发送的消息
  103. handleMessage(e) {
  104. console.log('收到web-view消息:', e.detail)
  105. console.log('收到web-view消息:', e)
  106. // 获取H5页面传递过来的用户信息
  107. if (e.detail && e.detail.type === 'user_info') {
  108. this.userInfo = e.detail.data
  109. this.token= e.detail.token
  110. // 存储用户信息到本地
  111. uni.setStorageSync('userInfo', this.userInfo)
  112. uni.setStorageSync('TOKEN_WEXIN', this.userInfo)
  113. // 返回上一页或跳转到首页
  114. uni.showToast({
  115. title: '登录成功',
  116. icon: 'success'
  117. })
  118. setTimeout(() => {
  119. uni.navigateBack()
  120. }, 1500)
  121. }
  122. },
  123. // web-view加载完成
  124. onLoads() {
  125. this.loading = false
  126. console.log('web-view加载完成')
  127. },
  128. // web-view加载失败
  129. onError(e) {
  130. this.loading = false
  131. console.error('web-view加载失败:', e)
  132. uni.showToast({
  133. title: '页面加载失败',
  134. icon: 'none'
  135. })
  136. }
  137. }
  138. }
  139. </script>
  140. <style>
  141. .container {
  142. width: 100%;
  143. height: 100%;
  144. position: relative;
  145. }
  146. .loading {
  147. position: absolute;
  148. top: 0;
  149. left: 0;
  150. width: 100%;
  151. height: 100%;
  152. display: flex;
  153. justify-content: center;
  154. align-items: center;
  155. background-color: #fff;
  156. z-index: 100;
  157. }
  158. web-view {
  159. width: 100%;
  160. height: 100%;
  161. }
  162. </style>