webview.vue 5.4 KB

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