api.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {AxiosPromise} from 'axios'
  2. import axios from "axios"
  3. //import qs from "qs"
  4. import * as CryptoJS from "crypto-js"
  5. import CONSTS from "../shared/index"
  6. import Cookies from "js-cookie"
  7. export const baseUrl = 'http://127.0.0.1:9527/sign'
  8. axios.defaults.baseURL = baseUrl
  9. axios.defaults.headers.post['Content-Type'] = 'application/json';
  10. export const api ={
  11. redirect_uri:'',
  12. //授权
  13. login(data:any):AxiosPromise{
  14. return axios.post('/login/signin?_allow_anonymous=true',
  15. {...data},
  16. )
  17. },
  18. //获取登录信息
  19. get(params:any):AxiosPromise{
  20. return axios.get('/login/get?_allow_anonymous=true',
  21. {params}
  22. )
  23. },
  24. //设置回调地址
  25. setRedirectUri(uri: string){
  26. this.redirect_uri = CryptoJS.enc.Base64url.parse(uri).toString(CryptoJS.enc.Utf8)
  27. console.log(`uri:${this.redirect_uri}`)
  28. localStorage.setItem(CONSTS.REDIRECT_URI,this.redirect_uri)
  29. },
  30. //使用复用信息
  31. congress(authParam:any):AxiosPromise{
  32. return axios.post('/login/congress?_allow_anonymous=true',
  33. {...authParam}
  34. )
  35. },
  36. //验证
  37. auth(authJwt: any){
  38. let user ={
  39. name: `${authJwt.displayName}(${authJwt.username})`,
  40. displayName: authJwt.displayName,
  41. username: authJwt.username,
  42. userId: authJwt.id,
  43. avatar: './assets/svg/avatar.svg',
  44. email: authJwt.email,
  45. passwordSetType: authJwt.passwordSetType
  46. }
  47. let hostsnames = window.location.hostname.split('.')
  48. let subHostName = window.location.hostname
  49. if(hostsnames.length>=2){
  50. subHostName=`${hostsnames[hostsnames.length-2]}.${hostsnames[hostsnames.length-1]}`;
  51. }
  52. Cookies.set(CONSTS.CONGRESS,authJwt.token,{path:'/'})
  53. Cookies.set(CONSTS.ONLINE_TICKET,authJwt.ticket,{domain:subHostName,path:'/'})
  54. if(authJwt.remeberMe){
  55. localStorage.setItem(CONSTS.REMEMBER,authJwt.remeberMe)
  56. }
  57. localStorage.setItem('user',JSON.stringify(user))
  58. localStorage.setItem('token',JSON.stringify(authJwt))
  59. },
  60. jwtAuth(authParam: any){
  61. return axios.get(`/login/jwt/trust?_allow_anonymous=true`, authParam)
  62. },
  63. //重定向地址
  64. navigate(authJwt:any){
  65. if(localStorage.getItem(CONSTS.REDIRECT_URI) != null){
  66. this.redirect_uri =`${localStorage.getItem(CONSTS.REDIRECT_URI)}`
  67. localStorage.removeItem(CONSTS.REDIRECT_URI)
  68. }
  69. if(this.redirect_uri != ''){
  70. location.href=this.redirect_uri
  71. }
  72. },
  73. //获取图片验证码
  74. getImageCaptcha(params:any){
  75. return axios.get('/captcha?_allow_anonymous=true',
  76. {params}
  77. )
  78. },
  79. //获取用户列表
  80. appList(){
  81. return axios.get('/appList',
  82. {headers:{
  83. 'Authorization':`Bearer ${Cookies.get(CONSTS.CONGRESS)}`,
  84. 'token': Cookies.get(CONSTS.CONGRESS) as string | number | boolean
  85. }
  86. }
  87. )
  88. },
  89. //忘记密码时产生验证码
  90. produceOtp(params:any) {
  91. return axios.get('/forgotpassword/produceOtp?_allow_anonymous=true', {params});
  92. },
  93. //邮箱验证
  94. produceEmailOtp(params:any){
  95. return axios.get('/forgotpassword/produceEmailOtp?_allow_anonymous=true',{params});
  96. },
  97. //修改密码
  98. setPassword(params:any){
  99. return axios.get('/forgotpassword/setpassword?_allow_anonymous=true', {params})
  100. },
  101. //验证登录账号
  102. authorize(provider:string){
  103. return axios.get(`/logon/oauth20/authorize/${provider}?_allow_anonymous=true`)
  104. },
  105. scanqrcode(provider:string){
  106. return axios.get(`/logon/oauth20/scanqrcode/${provider}?_allow_anonymous=true`)
  107. },
  108. //获取个人信息
  109. getProfile(){
  110. return axios.get('/config/profile/get',
  111. {
  112. headers:{
  113. 'Authorization':`Bearer ${Cookies.get(CONSTS.CONGRESS)}`,
  114. 'token': Cookies.get(CONSTS.CONGRESS) as string | number | boolean
  115. }
  116. }
  117. )
  118. },
  119. //更新个人信息
  120. updateProfile(body:any){
  121. return axios.put('/config/profile/update',
  122. {...body},
  123. {
  124. headers:{
  125. 'Authorization':`Bearer ${Cookies.get(CONSTS.CONGRESS)}`,
  126. 'token': Cookies.get(CONSTS.CONGRESS) as string | number | boolean
  127. }
  128. }
  129. )
  130. },
  131. logout(){
  132. Cookies.remove(CONSTS.CONGRESS)
  133. return axios.get('/login/logout')
  134. },
  135. //清除token
  136. clear(){
  137. Cookies.set('token','')
  138. localStorage.setItem(CONSTS.REMEMBER,'')
  139. },
  140. }