index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import request from '@/config/axios'
  2. import { getRefreshToken } from '@/utils/auth'
  3. import type { UserLoginVO } from './types'
  4. import { service } from '@/config/axios/service'
  5. export interface CodeImgResult {
  6. captchaOnOff: boolean
  7. img: string
  8. uuid: string
  9. }
  10. export interface SmsCodeVO {
  11. mobile: string
  12. scene: number
  13. }
  14. export interface SmsLoginVO {
  15. mobile: string
  16. code: string
  17. }
  18. // 登录
  19. export const loginApi = (data: UserLoginVO) => {
  20. return request.post({ url: '/system/auth/login', data })
  21. }
  22. // 刷新访问令牌
  23. export const refreshToken = () => {
  24. return request.post({ url: '/system/auth/refresh-token?refreshToken=' + getRefreshToken() })
  25. }
  26. // 使用租户名,获得租户编号
  27. export const getTenantIdByNameApi = (name: string) => {
  28. return request.get({ url: '/system/tenant/get-id-by-name?name=' + name })
  29. }
  30. // 登出
  31. export const loginOutApi = () => {
  32. return request.post({ url: '/system/auth/logout' })
  33. }
  34. // 获取用户权限信息
  35. export const getInfoApi = () => {
  36. return request.get({ url: '/system/auth/get-permission-info' })
  37. }
  38. // 路由
  39. export const getAsyncRoutesApi = () => {
  40. return request.get({ url: '/system/auth/list-menus' })
  41. }
  42. //获取登录验证码
  43. export const sendSmsCodeApi = (data: SmsCodeVO) => {
  44. return request.post({ url: '/system/auth/send-sms-code', data })
  45. }
  46. // 短信验证码登录
  47. export const smsLoginApi = (data: SmsLoginVO) => {
  48. return request.post({ url: '/system/auth/sms-login', data })
  49. }
  50. // 社交授权的跳转
  51. export const socialAuthRedirectApi = (type: number, redirectUri: string) => {
  52. return request.get({
  53. url: '/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + redirectUri
  54. })
  55. }
  56. // 获取验证图片 以及token
  57. export const getCodeApi = (data) => {
  58. return request.postOriginal({ url: 'system/captcha/get', data })
  59. }
  60. // 滑动或者点选验证
  61. export const reqCheckApi = (data) => {
  62. return request.postOriginal({ url: 'system/captcha/check', data })
  63. }
  64. // ========== OAUTH 2.0 相关 ==========
  65. export type scopesType = string[]
  66. export interface paramsType {
  67. responseType: string
  68. clientId: string
  69. redirectUri: string
  70. state: string
  71. scopes: scopesType
  72. }
  73. export const getAuthorize = (clientId) => {
  74. return request.get({ url: '/system/oauth2/authorize?clientId=' + clientId })
  75. }
  76. export function authorize(
  77. responseType: string,
  78. clientId: string,
  79. redirectUri: string,
  80. state: string,
  81. autoApprove: boolean,
  82. checkedScopes: scopesType,
  83. uncheckedScopes: scopesType
  84. ) {
  85. // 构建 scopes
  86. const scopes = {}
  87. for (const scope of checkedScopes) {
  88. scopes[scope] = true
  89. }
  90. for (const scope of uncheckedScopes) {
  91. scopes[scope] = false
  92. }
  93. // 发起请求
  94. return service({
  95. url: '/system/oauth2/authorize',
  96. headers: {
  97. 'Content-type': 'application/x-www-form-urlencoded'
  98. },
  99. params: {
  100. response_type: responseType,
  101. client_id: clientId,
  102. redirect_uri: redirectUri,
  103. state: state,
  104. auto_approve: autoApprove,
  105. scope: JSON.stringify(scopes)
  106. },
  107. method: 'post'
  108. })
  109. }