webview.vue 5.5 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. 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. this.$store.commit('setCoureLogin', 1);
  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('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('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>