webview.vue 5.8 KB

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