webview.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view class="container">
  3. <!-- 加载提示 -->
  4. <view class="loading" 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 { getusername,editUser} from '@/api/user'
  18. export default {
  19. data() {
  20. return {
  21. loading: true,
  22. // webviewUrl: '',//福本源
  23. // webviewUrl: 'https://vip.aishanghys.com/weixinOauth',//蜂巢快药
  24. webviewUrl: uni.getStorageSync('authUrl'),//中康
  25. // webviewUrl: 'https://vip.liangmiaoedu.com/weixinOauth',//良苗
  26. // webviewUrl: 'https://vips.beliyostore.com/weixinOauth',//倍力优
  27. // webviewUrl: 'https://userapp.drkzyy.cn/weixinOauth',//德瑞康
  28. // webviewUrl: 'https://companyapp.gjh2024.com/weixinOauth.html',//金慷建
  29. userInfo: {},
  30. userinfos:{
  31. nickname:"",
  32. avatar:""
  33. },
  34. }
  35. },
  36. onLoad(options) {
  37. console.log(options)
  38. // if(options.url&&!options.code){
  39. // this.webviewUrl=options.url
  40. // }
  41. if(options.code){
  42. // uni.$emit('us ercode', { code: options.code });
  43. this.loginweixin(options.code)
  44. }
  45. // 生成带参的H5授权页面URL
  46. // this.webviewUrl = this.generateAuthUrl()
  47. },
  48. methods: {
  49. editUserA(){
  50. const data={
  51. nickname:this.userinfos.nickname,
  52. avatar:this.userinfos.avatar,
  53. }
  54. editUser(data).then(res=>{
  55. if(res.code==200){
  56. setTimeout(()=>{
  57. uni.navigateBack({
  58. delta: 1
  59. });
  60. },200)
  61. }else{
  62. uni.showToast({
  63. icon:'none',
  64. title:res.msg,
  65. });
  66. }
  67. })
  68. },
  69. loginweixin(datas){
  70. var data = {
  71. code: datas,
  72. }
  73. getusername(data).then(res => {
  74. this.res=res
  75. uni.hideLoading();
  76. if (res.code == 200) {
  77. console.log(res)
  78. uni.hideLoading();
  79. uni.showToast({
  80. icon:'none',
  81. title: "成功获取用户信息",
  82. });
  83. this.userinfos.nickname=res.nickname;
  84. this.userinfos.avatar=res.headImgUrl;
  85. uni.setStorageSync('userInfos', this.userinfos);
  86. uni.navigateBack({
  87. delta: 1
  88. });
  89. } else {
  90. uni.hideLoading();
  91. uni.showToast({
  92. title: res.msg || '获取用户信息失败',
  93. icon: 'none'
  94. })
  95. }
  96. },
  97. err => {}
  98. ).catch(err=>{
  99. uni.hideLoading();
  100. uni.showToast({
  101. icon:'none',
  102. title: "获取用户信息失败",
  103. });
  104. });
  105. },
  106. // 生成授权页面URL,附带小程序传递的参数
  107. generateAuthUrl() {
  108. // 获取当前小程序的场景值,用于后续业务处理
  109. const scene = uni.getLaunchOptionsSync().scene
  110. // 这里替换为你的uniapp H5项目域名
  111. // 拼接参数,可包含小程序特有的信息
  112. const params = {
  113. scene,
  114. appid: 'wx961fadab9bcb792b', // 公众号AppID
  115. redirect_uri: encodeURIComponent('https://h5.fbylive.com/weixinOauth'),
  116. scope: 'snsapi_userinfo',
  117. state: 'wechat_redirect'
  118. }
  119. // 微信公众号授权URL
  120. 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`
  121. },
  122. // 处理web-view向小程序发送的消息
  123. handleMessage(e) {
  124. console.log('收到web-view消息:', e.detail)
  125. console.log('收到web-view消息:', e)
  126. // 获取H5页面传递过来的用户信息
  127. if (e.detail && e.detail.type === 'user_info') {
  128. this.userInfo = e.detail.data
  129. this.token= e.detail.token
  130. // 存储用户信息到本地
  131. uni.setStorageSync('userInfo', this.userInfo)
  132. uni.setStorageSync('TOKEN_WEXIN', this.userInfo)
  133. // 返回上一页或跳转到首页
  134. uni.showToast({
  135. title: '登录成功',
  136. icon: 'success'
  137. })
  138. setTimeout(() => {
  139. uni.navigateBack()
  140. }, 1500)
  141. }
  142. },
  143. // web-view加载完成
  144. onLoads() {
  145. this.loading = false
  146. console.log('web-view加载完成')
  147. },
  148. // web-view加载失败
  149. onError(e) {
  150. this.loading = false
  151. console.error('web-view加载失败:', e)
  152. uni.showToast({
  153. title: '页面加载失败',
  154. icon: 'none'
  155. })
  156. }
  157. }
  158. }
  159. </script>
  160. <style>
  161. .container {
  162. width: 100%;
  163. height: 100%;
  164. position: relative;
  165. }
  166. .loading {
  167. position: absolute;
  168. top: 0;
  169. left: 0;
  170. width: 100%;
  171. height: 100%;
  172. display: flex;
  173. justify-content: center;
  174. align-items: center;
  175. background-color: #fff;
  176. z-index: 100;
  177. }
  178. web-view {
  179. width: 100%;
  180. height: 100%;
  181. }
  182. </style>