autoLogin.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view>
  3. 自动登录中...
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. code:"",
  11. state: ''
  12. }
  13. },
  14. mounted() {
  15. this. getCode()
  16. },
  17. methods: {
  18. getCode() { // 非静默授权,第一次有弹框
  19. let appid = 'wxac0afae9104e5e4d'
  20. // let local = 'http://192.168.1.16:8080'
  21. let local = 'https://xiaoyi.xunsoftware.com'
  22. let that = this
  23. this.code = ''
  24. this.state = 'index'
  25. this.code = this.getUrlCode().code // 截取code
  26. this.state = this.getUrlCode().state
  27. console.error('code is :>>>', this.code)
  28. if (this.code == null || this.code === '') { // 如果没有code,则去请求
  29. let redirectType = this.getUrlCode().type || 'index'
  30. window.location.href =
  31. // `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local1)}&response_type=code&scope=snsapi_userinfo&state=${redirectType}#wechat_redirect`
  32. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=${redirectType}#wechat_redirect`
  33. } else {
  34. that.$request({
  35. url: that.$api.auth.getToken,
  36. data: {
  37. code: that.code,
  38. grant_type: 'social_school_doctor_user'
  39. },
  40. method: 'POST'
  41. }).then(res=>{
  42. if(res.access_token) {
  43. localStorage.setItem('tenant_id', res.tenant_id) // 获取登录之后的租户id
  44. this.$storage.setToken(res.access_token)
  45. this.$storage.setAuthUser(JSON.stringify(res))
  46. // 登录成功后跳转至/pages/index/index
  47. window.location.href = local + '/pages/index/index'
  48. // TODO
  49. } else {
  50. // console.error(res)
  51. }
  52. }).catch(err=>{
  53. if(err.error_description == '未绑定微信,请先绑定' || err.error_description == 'social grant failure, user is null'){
  54. uni.showToast({
  55. title: '当前微信未绑定,请先通过账号登录',
  56. icon:'none'
  57. })
  58. setTimeout(()=>{
  59. window.location.href = local + '/pages/signIn/logOn?type=1'
  60. },2000)
  61. }
  62. })
  63. }
  64. },
  65. getUrlCode() { // 截取url中的code方法
  66. var url = location.search
  67. this.winUrl = url
  68. var theRequest = new Object()
  69. if (url.indexOf("?") != -1) {
  70. var str = url.substr(1)
  71. var strs = str.split("&")
  72. for (var i = 0; i < strs.length; i++) {
  73. theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
  74. }
  75. }
  76. return theRequest
  77. },
  78. }
  79. }
  80. </script>
  81. <style>
  82. </style>