webview.vue 5.5 KB

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