webview.vue 4.7 KB

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