webview.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <view class="container">
  3. <!-- 加载提示 -->
  4. <view class="loading2" 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/course.js'
  18. export default {
  19. data() {
  20. return {
  21. loading: false,
  22. // webviewUrl: uni.getStorageSync('authUrl'),//
  23. // webviewUrl: 'https://company.h5.test.ylrztop.com/avatarAuth/weixinOauth.html',//云联融智
  24. // webviewUrl: 'https://authdrk.ylrztop.com/weixinOauth',//青岛德瑞康
  25. // webviewUrl: 'https://wxmpauth.zkwlyf.com/weixinOauth',//中康智慧
  26. // webviewUrl:'https://userappkyt.ylrzcloud.com/weixinOauth',
  27. // webviewUrl:'https://uviewui.com/components/popup.html',
  28. userinfos:{
  29. nickname:"",
  30. avatar:""
  31. },
  32. usercode:{},
  33. h5Appid:'',
  34. redirect_uri: '',
  35. appid:'',
  36. webviewUrl:''
  37. }
  38. },
  39. onLoad(options) {
  40. this.appid = getApp().globalData.appId
  41. this.webviewUrl = uni.getStorageSync('weixinOauth')
  42. if(options.code){
  43. this.loginweixin(options.code)
  44. }
  45. console.log(this.webviewUrl)
  46. // 生成带参的H5授权页面URL
  47. // this.webviewUrl = this.generateAuthUrl()
  48. },
  49. methods: {
  50. // 截取url中的参数方法
  51. getUrlParam() {
  52. var url = this.webviewUrl;
  53. var theRequest = new Object();
  54. if (url.indexOf("?") != -1) {
  55. var str = url.substr(1);
  56. var strs = str.split("&");
  57. for (var i = 0; i < strs.length; i++) {
  58. theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
  59. }
  60. }
  61. return theRequest;
  62. },
  63. loginweixin(datas){
  64. const h5course=uni.getStorageSync('H5course')
  65. var data = {
  66. code: datas,
  67. appId:this.appid,
  68. companyId:h5course.companyId,
  69. companyUserId:h5course.companyUserId,
  70. }
  71. H5logoinApp(data).then(res => {
  72. uni.hideLoading();
  73. if (res.code == 200) {
  74. this.$store.commit('setCoureLogin', 1);
  75. uni.setStorageSync('web_userInfo', res.user);
  76. uni.setStorageSync('TOKEN_WEXIN', res.token);
  77. this.usercode.code=datas
  78. this.usercode.userId=res.user.userId
  79. uni.$emit('usercode',this.usercode)
  80. uni.navigateBack({
  81. delta: 1
  82. });
  83. } else {
  84. uni.showToast({
  85. title: res.msg || '获取用户信息失败',
  86. icon: 'none'
  87. })
  88. setTimeout(()=>{
  89. uni.navigateBack({
  90. delta: 1
  91. });
  92. },2000)
  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. this.h5Appid = this.getUrlParam().appid
  111. this.redirect_uri = this.webviewUrl
  112. const params = {
  113. scene,
  114. appid: this.h5Appid, // 公众号AppID
  115. redirect_uri: encodeURIComponent(this.redirect_uri),
  116. scope: 'snsapi_userinfo',
  117. state: 'wechat_redirect'
  118. }
  119. // 微信公众号授权URL
  120. 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`
  121. },
  122. // 处理web-view向小程序发送的消息
  123. handleMessage(e) {
  124. console.log('收到web-view消息:', e.detail)
  125. console.log('收到web-view消息:', e)
  126. // 获取H5页面传递过来的用户信息
  127. if (e.detail && e.detail.type === 'user_info') {
  128. this.userInfo = e.detail.data
  129. this.token= e.detail.token
  130. // 存储用户信息到本地
  131. uni.setStorageSync('userInfo', this.userInfo)
  132. uni.setStorageSync('TOKEN_WEXIN', this.userInfo)
  133. // 返回上一页或跳转到首页
  134. uni.showToast({
  135. title: '登录成功',
  136. icon: 'success'
  137. })
  138. setTimeout(() => {
  139. uni.navigateBack()
  140. }, 1500)
  141. }
  142. },
  143. // web-view加载完成
  144. onLoads() {
  145. this.loading = false
  146. console.log('web-view加载完成')
  147. },
  148. // web-view加载失败
  149. onError(e) {
  150. this.loading = false
  151. console.error('web-view加载失败:', e)
  152. uni.showToast({
  153. title: '页面加载失败',
  154. icon: 'none'
  155. })
  156. }
  157. }
  158. }
  159. </script>
  160. <style lang="scss" scoped>
  161. .container {
  162. width: 100%;
  163. height: 100%;
  164. position: relative;
  165. }
  166. .loading2 {
  167. position: absolute;
  168. top: 0;
  169. left: 0;
  170. width: 100%;
  171. height: 100%;
  172. display: flex;
  173. justify-content: center;
  174. align-items: center;
  175. background-color: #fff;
  176. z-index: 100;
  177. }
  178. web-view {
  179. width: 100%;
  180. height: 100%;
  181. }
  182. </style>