webview.vue 3.7 KB

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