From 2d2db0418bdacc82bc01a832c61e43518b4668e9 Mon Sep 17 00:00:00 2001 From: xingyu Date: Thu, 8 Jun 2023 17:33:44 +0800 Subject: [PATCH] feat: sms login --- src/api/base/login.ts | 11 +++ src/api/base/model/userModel.ts | 8 +++ src/api/base/user.ts | 20 +++--- src/store/modules/user.ts | 22 +++++- src/views/base/login/MobileForm.vue | 104 ++++++++++++++++++++++++++-- 5 files changed, 149 insertions(+), 16 deletions(-) diff --git a/src/api/base/login.ts b/src/api/base/login.ts index a1bf8547..6021c750 100644 --- a/src/api/base/login.ts +++ b/src/api/base/login.ts @@ -38,6 +38,17 @@ export function getAsyncRoutes() { return defHttp.get({ url: Api.GetAsyncRoutes }) } +// 获取登录验证码 +export function sendSmsCode(mobile, scene) { + return defHttp.post({ + url: '/system/auth/send-sms-code', + data: { + mobile, + scene + } + }) +} + // 获取验证图片 以及token export function getCaptcha(data) { return defHttp.post({ url: Api.GetCaptcha, data }, { isReturnNativeResponse: true }) diff --git a/src/api/base/model/userModel.ts b/src/api/base/model/userModel.ts index b3b4b65b..527d14e1 100644 --- a/src/api/base/model/userModel.ts +++ b/src/api/base/model/userModel.ts @@ -7,6 +7,14 @@ export interface LoginParams { captchaVerification: string } +/** + * @description: SmsLogin interface parameters + */ +export interface SmsLoginParams { + mobile: number + code: number +} + /** * @description: Login interface return value */ diff --git a/src/api/base/user.ts b/src/api/base/user.ts index bec1a3c8..ffd8ed72 100644 --- a/src/api/base/user.ts +++ b/src/api/base/user.ts @@ -1,11 +1,12 @@ import { defHttp } from '@/utils/http/axios' -import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel' +import { LoginParams, LoginResultModel, GetUserInfoModel, SmsLoginParams } from './model/userModel' import { ErrorMessageMode } from '@/types/axios' enum Api { Login = '/system/auth/login', Logout = '/system/auth/logout', + SmsLogin = '/system/auth/sms-login', GetUserInfo = '/system/auth/get-permission-info' } @@ -13,15 +14,14 @@ enum Api { * @description: user login api */ export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') { - return defHttp.post( - { - url: Api.Login, - params - }, - { - errorMessageMode: mode - } - ) + return defHttp.post({ url: Api.Login, params }, { errorMessageMode: mode }) +} + +/** + * @description: user smslogin api + */ +export function smsLogin(params: SmsLoginParams, mode: ErrorMessageMode = 'modal') { + return defHttp.post({ url: Api.SmsLogin, params }, { errorMessageMode: mode }) } /** diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index e782e3f8..a01e84fb 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -13,8 +13,8 @@ import { usePermissionStore } from '@/store/modules/permission' import { useI18n } from '@/hooks/web/useI18n' import { useMessage } from '@/hooks/web/useMessage' import { getAuthCache, setAuthCache } from '@/utils/auth' -import { doLogout, getUserInfo, loginApi } from '@/api/base/user' -import { GetUserInfoModel, LoginParams } from '@/api/base/model/userModel' +import { doLogout, getUserInfo, loginApi, smsLogin } from '@/api/base/user' +import { GetUserInfoModel, LoginParams, SmsLoginParams } from '@/api/base/model/userModel' import { isArray } from '@/utils/is' @@ -110,6 +110,24 @@ export const useUserStore = defineStore('app-user', { return Promise.reject(error) } }, + async smsLogin( + params: SmsLoginParams & { + goHome?: boolean + mode?: ErrorMessageMode + } + ): Promise { + try { + const { goHome = true, mode, ...smsLoginParams } = params + const data = await smsLogin(smsLoginParams, mode) + const { accessToken, refreshToken } = data + // save token + this.setAccessToken(accessToken) + this.setRefreshToken(refreshToken) + return this.afterLoginAction(goHome) + } catch (error) { + return Promise.reject(error) + } + }, async afterLoginAction(goHome?: boolean): Promise { if (!this.getAccessToken) return null // get user info diff --git a/src/views/base/login/MobileForm.vue b/src/views/base/login/MobileForm.vue index 41c600cc..a151f29c 100644 --- a/src/views/base/login/MobileForm.vue +++ b/src/views/base/login/MobileForm.vue @@ -2,15 +2,31 @@
+ + + - - + + - +