Commit c8823752d003988c7a4910c0d29df7f12c128979
1 parent
6d27ba43
perf: loginform code
Showing
3 changed files
with
56 additions
and
31 deletions
yudao-ui-admin-vue3/src/config/axios/index.ts
| ... | ... | @@ -124,7 +124,15 @@ service.interceptors.response.use( |
| 124 | 124 | }, |
| 125 | 125 | (error: AxiosError) => { |
| 126 | 126 | console.log('err' + error) // for debug |
| 127 | - ElMessage.error(error.message) | |
| 127 | + let { message } = error | |
| 128 | + if (message === 'Network Error') { | |
| 129 | + message = '后端接口连接异常' | |
| 130 | + } else if (message.includes('timeout')) { | |
| 131 | + message = '系统接口请求超时' | |
| 132 | + } else if (message.includes('Request failed with status code')) { | |
| 133 | + message = '系统接口' + message.substr(message.length - 3) + '异常' | |
| 134 | + } | |
| 135 | + ElMessage.error(message) | |
| 128 | 136 | return Promise.reject(error) |
| 129 | 137 | } |
| 130 | 138 | ) | ... | ... |
yudao-ui-admin-vue3/src/views/Login/components/LoginForm.vue
| ... | ... | @@ -13,7 +13,14 @@ import { |
| 13 | 13 | } from 'element-plus' |
| 14 | 14 | import { reactive, ref, unref, onMounted, computed, watch } from 'vue' |
| 15 | 15 | import * as LoginApi from '@/api/login' |
| 16 | -import { setToken, setTenantId } from '@/utils/auth' | |
| 16 | +import { | |
| 17 | + setToken, | |
| 18 | + setTenantId, | |
| 19 | + getUsername, | |
| 20 | + getRememberMe, | |
| 21 | + getPassword, | |
| 22 | + getTenantName | |
| 23 | +} from '@/utils/auth' | |
| 17 | 24 | import { useUserStoreWithOut } from '@/store/modules/user' |
| 18 | 25 | import { useCache } from '@/hooks/web/useCache' |
| 19 | 26 | import { usePermissionStore } from '@/store/modules/permission' |
| ... | ... | @@ -40,7 +47,6 @@ const iconHouse = useIcon({ icon: 'ep:house' }) |
| 40 | 47 | const iconAvatar = useIcon({ icon: 'ep:avatar' }) |
| 41 | 48 | const iconLock = useIcon({ icon: 'ep:lock' }) |
| 42 | 49 | const iconCircleCheck = useIcon({ icon: 'ep:circle-check' }) |
| 43 | -const remember = ref(false) | |
| 44 | 50 | const LoginRules = { |
| 45 | 51 | tenantName: [required], |
| 46 | 52 | username: [required], |
| ... | ... | @@ -61,6 +67,7 @@ const loginData = reactive({ |
| 61 | 67 | tenantName: '芋道源码', |
| 62 | 68 | username: 'admin', |
| 63 | 69 | password: 'admin123', |
| 70 | + rememberMe: false, | |
| 64 | 71 | code: '', |
| 65 | 72 | uuid: '' |
| 66 | 73 | } |
| ... | ... | @@ -77,6 +84,20 @@ const getTenantId = async () => { |
| 77 | 84 | const res = await LoginApi.getTenantIdByNameApi(loginData.loginForm.tenantName) |
| 78 | 85 | setTenantId(res) |
| 79 | 86 | } |
| 87 | +// 记住我 | |
| 88 | +const getCookie = () => { | |
| 89 | + const username = getUsername() | |
| 90 | + const password = getPassword() | |
| 91 | + const rememberMe = getRememberMe() | |
| 92 | + const tenantName = getTenantName() | |
| 93 | + loginData.loginForm = { | |
| 94 | + ...loginData.loginForm, | |
| 95 | + username: username ? username : loginData.loginForm.username, | |
| 96 | + password: password ? password : loginData.loginForm.password, | |
| 97 | + rememberMe: rememberMe ? getRememberMe() : false, | |
| 98 | + tenantName: tenantName ? tenantName : loginData.loginForm.tenantName | |
| 99 | + } | |
| 100 | +} | |
| 80 | 101 | // 登录 |
| 81 | 102 | const handleLogin = async () => { |
| 82 | 103 | await getTenantId() |
| ... | ... | @@ -103,7 +124,7 @@ const getRoutes = async () => { |
| 103 | 124 | // 后端过滤菜单 |
| 104 | 125 | const res = await LoginApi.getAsyncRoutesApi() |
| 105 | 126 | wsCache.set('roleRouters', res) |
| 106 | - await permissionStore.generateRoutes(res).catch(() => {}) | |
| 127 | + await permissionStore.generateRoutes(res) | |
| 107 | 128 | permissionStore.getAddRouters.forEach((route) => { |
| 108 | 129 | addRoute(route as RouteRecordRaw) // 动态添加可访问路由表 |
| 109 | 130 | }) |
| ... | ... | @@ -120,8 +141,9 @@ watch( |
| 120 | 141 | immediate: true |
| 121 | 142 | } |
| 122 | 143 | ) |
| 123 | -onMounted(() => { | |
| 124 | - getCode() | |
| 144 | +onMounted(async () => { | |
| 145 | + await getCode() | |
| 146 | + getCookie() | |
| 125 | 147 | }) |
| 126 | 148 | </script> |
| 127 | 149 | <template> |
| ... | ... | @@ -165,7 +187,7 @@ onMounted(() => { |
| 165 | 187 | <el-input |
| 166 | 188 | v-model="loginData.loginForm.password" |
| 167 | 189 | type="password" |
| 168 | - :placeholder="t('login.password')" | |
| 190 | + :placeholder="t('login.passwordPlaceholder')" | |
| 169 | 191 | show-password |
| 170 | 192 | @keyup.enter="handleLogin" |
| 171 | 193 | :prefix-icon="iconLock" |
| ... | ... | @@ -178,7 +200,7 @@ onMounted(() => { |
| 178 | 200 | <el-col :span="14"> |
| 179 | 201 | <el-input |
| 180 | 202 | v-model="loginData.loginForm.code" |
| 181 | - :placeholder="t('login.code')" | |
| 203 | + :placeholder="t('login.codePlaceholder')" | |
| 182 | 204 | @keyup.enter="handleLogin" |
| 183 | 205 | :prefix-icon="iconCircleCheck" |
| 184 | 206 | style="width: 90%" |
| ... | ... | @@ -199,7 +221,9 @@ onMounted(() => { |
| 199 | 221 | <el-form-item> |
| 200 | 222 | <el-row justify="space-between" style="width: 100%"> |
| 201 | 223 | <el-col :span="6"> |
| 202 | - <el-checkbox v-model="remember">{{ t('login.remember') }}</el-checkbox> | |
| 224 | + <el-checkbox v-model="loginData.loginForm.rememberMe"> | |
| 225 | + {{ t('login.remember') }} | |
| 226 | + </el-checkbox> | |
| 203 | 227 | </el-col> |
| 204 | 228 | <el-col :span="12" :offset="6"> |
| 205 | 229 | <el-link type="primary" style="float: right">{{ t('login.forgetPassword') }}</el-link> |
| ... | ... | @@ -254,7 +278,7 @@ onMounted(() => { |
| 254 | 278 | class="cursor-pointer anticon" |
| 255 | 279 | /> |
| 256 | 280 | <Icon |
| 257 | - icon="ant-design:weibo-circle-filled" | |
| 281 | + icon="ant-design:dingtalk-circle-filled" | |
| 258 | 282 | :size="iconSize" |
| 259 | 283 | :color="iconColor" |
| 260 | 284 | class="cursor-pointer anticon" | ... | ... |
yudao-ui-admin-vue3/src/views/Login/components/MobileForm.vue
| ... | ... | @@ -67,27 +67,21 @@ const redirect = ref<string>('') |
| 67 | 67 | const getSmsCode = async () => { |
| 68 | 68 | await getTenantId() |
| 69 | 69 | smsVO.smsCode.mobile = loginData.loginForm.mobileNumber |
| 70 | - console.log('getSmsCode begin:', smsVO.smsCode) | |
| 71 | - await sendSmsCodeApi(smsVO.smsCode) | |
| 72 | - .then(async (res) => { | |
| 73 | - // 提示验证码发送成功 | |
| 74 | - ElMessage({ | |
| 75 | - type: 'success', | |
| 76 | - message: t('login.SmsSendMsg') | |
| 77 | - }) | |
| 78 | - console.log('res', res) | |
| 79 | - // 设置倒计时 | |
| 80 | - mobileCodeTimer.value = 60 | |
| 81 | - let msgTimer = setInterval(() => { | |
| 82 | - mobileCodeTimer.value = mobileCodeTimer.value - 1 | |
| 83 | - if (mobileCodeTimer.value <= 0) { | |
| 84 | - clearInterval(msgTimer) | |
| 85 | - } | |
| 86 | - }, 1000) | |
| 87 | - }) | |
| 88 | - .catch(() => { | |
| 89 | - console.log('error') | |
| 70 | + await sendSmsCodeApi(smsVO.smsCode).then(async () => { | |
| 71 | + // 提示验证码发送成功 | |
| 72 | + ElMessage({ | |
| 73 | + type: 'success', | |
| 74 | + message: t('login.SmsSendMsg') | |
| 90 | 75 | }) |
| 76 | + // 设置倒计时 | |
| 77 | + mobileCodeTimer.value = 60 | |
| 78 | + let msgTimer = setInterval(() => { | |
| 79 | + mobileCodeTimer.value = mobileCodeTimer.value - 1 | |
| 80 | + if (mobileCodeTimer.value <= 0) { | |
| 81 | + clearInterval(msgTimer) | |
| 82 | + } | |
| 83 | + }, 1000) | |
| 84 | + }) | |
| 91 | 85 | } |
| 92 | 86 | watch( |
| 93 | 87 | () => currentRoute.value, |
| ... | ... | @@ -126,7 +120,6 @@ const signIn = async () => { |
| 126 | 120 | // 获取路由 |
| 127 | 121 | const getRoutes = async () => { |
| 128 | 122 | // 后端过滤菜单 |
| 129 | - // TODO @jinz:这块 getRoutes 的代码,是不是可以统一到 store 里,类似 ruoyi-vue 的做法,可能要找作者沟通下 | |
| 130 | 123 | const routers = await getAsyncRoutesApi() |
| 131 | 124 | wsCache.set('roleRouters', routers) |
| 132 | 125 | await permissionStore.generateRoutes(routers).catch(() => {}) | ... | ... |