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