webview.vue 3.7 KB

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