webview.vue 5.6 KB

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