webview.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 { H5logoinApp} from '@/api/courseLook.js'
  18. export default {
  19. data() {
  20. return {
  21. loading: true,
  22. webviewUrl: uni.getStorageSync('setWebviewUrl'),//动态
  23. userinfos:{
  24. nickname:"",
  25. avatar:""
  26. },
  27. usercode:{},
  28. }
  29. },
  30. onLoad(options) {
  31. console.log(this.webviewUrl)
  32. if(options.code){
  33. // uni.$emit('us ercode', { code: options.code });
  34. this.loginweixin(options.code)
  35. }
  36. // 生成带参的H5授权页面URL
  37. // this.webviewUrl = this.generateAuthUrl()
  38. },
  39. computed:{
  40. appid() {
  41. return this.$store.state.appid
  42. },
  43. },
  44. methods: {
  45. loginweixin(datas){
  46. console.log(uni.getStorageSync('H5course'))
  47. const h5course=uni.getStorageSync('H5course')
  48. var data = {
  49. code: datas,
  50. appId:this.appId,
  51. companyId:h5course.companyId,
  52. companyUserId:h5course.companyUserId,
  53. projectId:h5course.projectId
  54. }
  55. H5logoinApp(data).then(res => {
  56. this.res=res
  57. uni.hideLoading();
  58. if (res.code == 200) {
  59. console.log(res)
  60. uni.hideLoading();
  61. uni.showToast({
  62. icon:'none',
  63. title: "成功获取用户信息",
  64. });
  65. this.userinfos.nickname=res.user.nickname
  66. this.userinfos.avatar=res.user.avatar
  67. uni.setStorageSync("userinfos",this.userinfos)
  68. uni.setStorageSync('userInfo', res.user);
  69. uni.setStorageSync('TOKEN_WEXIN', res.token);
  70. this.usercode.code=datas
  71. this.usercode.userId=res.user.userId
  72. setTimeout(()=>{
  73. uni.$emit('usercode',this.usercode)
  74. uni.navigateBack({
  75. delta: 1
  76. });
  77. },200)
  78. } else {
  79. uni.hideLoading();
  80. uni.showToast({
  81. title: res.msg || '获取用户信息失败',
  82. icon: 'none'
  83. })
  84. uni.setStorageSync('vipMsg',res.msg)
  85. setTimeout(()=>{
  86. uni.$emit('vipMsg',res.msg)
  87. uni.navigateBack({
  88. delta: 1
  89. });
  90. },200)
  91. }
  92. },
  93. err => {}
  94. ).catch(err=>{
  95. uni.hideLoading();
  96. uni.showToast({
  97. icon:'none',
  98. title: "获取用户信息失败",
  99. });
  100. });
  101. },
  102. // 生成授权页面URL,附带小程序传递的参数
  103. generateAuthUrl() {
  104. // 获取当前小程序的场景值,用于后续业务处理
  105. const scene = uni.getLaunchOptionsSync().scene
  106. // 这里替换为你的uniapp H5项目域名
  107. // 拼接参数,可包含小程序特有的信息
  108. const params = {
  109. scene,
  110. appid: 'wx961fadab9bcb792b', // 公众号AppID
  111. redirect_uri: encodeURIComponent('https://h5.fbylive.com/weixinOauth'),
  112. scope: 'snsapi_userinfo',
  113. state: 'wechat_redirect'
  114. }
  115. // 微信公众号授权URL
  116. 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`
  117. },
  118. // 处理web-view向小程序发送的消息
  119. handleMessage(e) {
  120. console.log('收到web-view消息:', e.detail)
  121. console.log('收到web-view消息:', e)
  122. // 获取H5页面传递过来的用户信息
  123. if (e.detail && e.detail.type === 'user_info') {
  124. this.userInfo = e.detail.data
  125. this.token= e.detail.token
  126. // 存储用户信息到本地
  127. uni.setStorageSync('userInfo', this.userInfo)
  128. uni.setStorageSync('TOKEN_WEXIN', this.userInfo)
  129. // 返回上一页或跳转到首页
  130. uni.showToast({
  131. title: '登录成功',
  132. icon: 'success'
  133. })
  134. setTimeout(() => {
  135. uni.navigateBack()
  136. }, 1500)
  137. }
  138. },
  139. // web-view加载完成
  140. onLoads() {
  141. this.loading = false
  142. console.log('web-view加载完成')
  143. },
  144. // web-view加载失败
  145. onError(e) {
  146. this.loading = false
  147. console.error('web-view加载失败:', e)
  148. uni.showToast({
  149. title: '页面加载失败',
  150. icon: 'none'
  151. })
  152. }
  153. }
  154. }
  155. </script>
  156. <style>
  157. .container {
  158. width: 100%;
  159. height: 100%;
  160. position: relative;
  161. }
  162. .loading {
  163. position: absolute;
  164. top: 0;
  165. left: 0;
  166. width: 100%;
  167. height: 100%;
  168. display: flex;
  169. justify-content: center;
  170. align-items: center;
  171. background-color: #fff;
  172. z-index: 100;
  173. }
  174. web-view {
  175. width: 100%;
  176. height: 100%;
  177. }
  178. </style>