login.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import request from "@/utils/request";
  2. import { getRefreshToken } from "@/utils/auth";
  3. import service from "@/utils/request";
  4. // 登录方法
  5. export function login(
  6. username,
  7. password,
  8. captchaVerification,
  9. socialType,
  10. socialCode,
  11. socialState
  12. ) {
  13. const data = {
  14. username,
  15. password,
  16. captchaVerification,
  17. // 社交相关
  18. socialType,
  19. socialCode,
  20. socialState,
  21. };
  22. return request({
  23. url: "/system/auth/login",
  24. method: "post",
  25. data: data,
  26. });
  27. }
  28. // 获取用户详细信息
  29. export function getInfo() {
  30. return request({
  31. url: "/system/auth/get-permission-info",
  32. method: "get",
  33. });
  34. }
  35. // 退出方法
  36. export function logout() {
  37. return request({
  38. url: "/system/auth/logout",
  39. method: "post",
  40. });
  41. }
  42. // 社交授权的跳转
  43. export function socialAuthRedirect(type, redirectUri) {
  44. return request({
  45. url:
  46. "/system/auth/social-auth-redirect?type=" +
  47. type +
  48. "&redirectUri=" +
  49. redirectUri,
  50. method: "get",
  51. });
  52. }
  53. // 社交快捷登录,使用 code 授权码
  54. export function socialLogin(type, code, state) {
  55. return request({
  56. url: "/system/auth/social-login",
  57. method: "post",
  58. data: {
  59. type,
  60. code,
  61. state,
  62. },
  63. });
  64. }
  65. // 获取登录验证码
  66. export function sendSmsCode(mobile, scene) {
  67. return request({
  68. url: "/system/auth/send-sms-code",
  69. method: "post",
  70. data: {
  71. mobile,
  72. scene,
  73. },
  74. });
  75. }
  76. // 短信验证码登录
  77. export function smsLogin(mobile, code) {
  78. return request({
  79. url: "/system/auth/sms-login",
  80. method: "post",
  81. data: {
  82. mobile,
  83. code,
  84. },
  85. });
  86. }
  87. // 刷新访问令牌
  88. export function refreshToken() {
  89. return service({
  90. url: "/system/auth/refresh-token?refreshToken=" + getRefreshToken(),
  91. method: "post",
  92. });
  93. }
  94. // ========== OAUTH 2.0 相关 ==========
  95. export function getAuthorize(clientId) {
  96. return request({
  97. url: "/system/oauth2/authorize?clientId=" + clientId,
  98. method: "get",
  99. });
  100. }
  101. export function authorize(
  102. responseType,
  103. clientId,
  104. redirectUri,
  105. state,
  106. autoApprove,
  107. checkedScopes,
  108. uncheckedScopes
  109. ) {
  110. // 构建 scopes
  111. const scopes = {};
  112. for (const scope of checkedScopes) {
  113. scopes[scope] = true;
  114. }
  115. for (const scope of uncheckedScopes) {
  116. scopes[scope] = false;
  117. }
  118. // 发起请求
  119. return service({
  120. url: "/system/oauth2/authorize",
  121. headers: {
  122. "Content-type": "application/x-www-form-urlencoded",
  123. },
  124. params: {
  125. response_type: responseType,
  126. client_id: clientId,
  127. redirect_uri: redirectUri,
  128. state: state,
  129. auto_approve: autoApprove,
  130. scope: JSON.stringify(scopes),
  131. },
  132. method: "post",
  133. });
  134. }
  135. export class socialBindLogin {}