webview.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. console.log(res)
  88. uni.hideLoading();
  89. uni.showToast({
  90. icon:'none',
  91. title: "成功获取用户信息",
  92. });
  93. this.userinfos.nickname=res.user.nickname
  94. this.userinfos.avatar=res.user.avatar
  95. uni.setStorageSync("userinfos",this.userinfos)
  96. uni.setStorageSync('userInfo', res.user);
  97. uni.setStorageSync('TOKEN_WEXIN', res.token);
  98. this.usercode.code=datas
  99. this.usercode.userId=res.user.userId
  100. uni.$emit('usercode',this.usercode)
  101. uni.navigateBack({
  102. delta: 1
  103. });
  104. } else {
  105. uni.hideLoading();
  106. uni.showToast({
  107. title: res.msg || '获取用户信息失败',
  108. icon: 'none'
  109. })
  110. setTimeout(()=>{
  111. uni.$emit('vipMsg',res.msg)
  112. uni.navigateBack({
  113. delta: 1
  114. });
  115. },2000)
  116. }
  117. },
  118. err => {}
  119. ).catch(err=>{
  120. uni.hideLoading();
  121. uni.showToast({
  122. icon:'none',
  123. title: "获取用户信息失败",
  124. });
  125. });
  126. },
  127. // 生成授权页面URL,附带小程序传递的参数
  128. generateAuthUrl() {
  129. // 获取当前小程序的场景值,用于后续业务处理
  130. const scene = uni.getLaunchOptionsSync().scene
  131. // 这里替换为你的uniapp H5项目域名
  132. // 拼接参数,可包含小程序特有的信息
  133. this.h5Appid = this.getUrlParam().appid
  134. this.redirect_uri = this.webviewUrl
  135. const params = {
  136. scene,
  137. appid: this.h5Appid, // 公众号AppID
  138. redirect_uri: encodeURIComponent(this.redirect_uri),
  139. scope: 'snsapi_userinfo',
  140. state: 'wechat_redirect'
  141. }
  142. // 微信公众号授权URL
  143. 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`
  144. },
  145. // 处理web-view向小程序发送的消息
  146. handleMessage(e) {
  147. console.log('收到web-view消息:', e.detail)
  148. console.log('收到web-view消息:', e)
  149. // 获取H5页面传递过来的用户信息
  150. if (e.detail && e.detail.type === 'user_info') {
  151. this.userInfo = e.detail.data
  152. this.token= e.detail.token
  153. // 存储用户信息到本地
  154. uni.setStorageSync('userInfo', this.userInfo)
  155. uni.setStorageSync('TOKEN_WEXIN', this.userInfo)
  156. // 返回上一页或跳转到首页
  157. uni.showToast({
  158. title: '登录成功',
  159. icon: 'success'
  160. })
  161. setTimeout(() => {
  162. uni.navigateBack()
  163. }, 1500)
  164. }
  165. },
  166. // web-view加载完成
  167. onLoads() {
  168. this.loading = false
  169. console.log('web-view加载完成')
  170. },
  171. // web-view加载失败
  172. onError(e) {
  173. this.loading = false
  174. console.error('web-view加载失败:', e)
  175. uni.showToast({
  176. title: '页面加载失败',
  177. icon: 'none'
  178. })
  179. }
  180. }
  181. }
  182. </script>
  183. <style lang="scss" scoped>
  184. .container {
  185. width: 100%;
  186. height: 100%;
  187. position: relative;
  188. }
  189. .loading2 {
  190. position: absolute;
  191. top: 0;
  192. left: 0;
  193. width: 100%;
  194. height: 100%;
  195. display: flex;
  196. justify-content: center;
  197. align-items: center;
  198. background-color: #fff;
  199. z-index: 100;
  200. }
  201. web-view {
  202. width: 100%;
  203. height: 100%;
  204. }
  205. </style>