fix(mobile): 修复登录逻辑对齐现有系统
This commit is contained in:
1
healthlink-his-mobile/.gitignore
vendored
1
healthlink-his-mobile/.gitignore
vendored
@@ -3,3 +3,4 @@ dist/
|
||||
.env.local
|
||||
.env.*.local
|
||||
*.log
|
||||
package-lock.json
|
||||
|
||||
@@ -7,17 +7,17 @@ const request = axios.create({
|
||||
})
|
||||
|
||||
request.interceptors.request.use(config => {
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) config.headers.Authorization = `Bearer ${token}`
|
||||
const tenantId = localStorage.getItem('tenantId')
|
||||
if (tenantId) config.headers['tenant-id'] = tenantId
|
||||
const token = localStorage.getItem('Admin-Token')
|
||||
if (token && !(config.headers && config.headers.isToken === false)) {
|
||||
config.headers.Authorization = 'Bearer ' + token
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
request.interceptors.response.use(
|
||||
res => {
|
||||
if (res.data?.code === 401) {
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('Admin-Token')
|
||||
window.location.href = '/login'
|
||||
return Promise.reject(new Error('登录已过期'))
|
||||
}
|
||||
@@ -25,18 +25,16 @@ request.interceptors.response.use(
|
||||
},
|
||||
err => {
|
||||
if (err.response?.status === 401) {
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('Admin-Token')
|
||||
window.location.href = '/login'
|
||||
}
|
||||
ElMessage.error(err.response?.data?.msg || '请求失败')
|
||||
return Promise.reject(err)
|
||||
}
|
||||
)
|
||||
|
||||
export const authApi = {
|
||||
login: (data) => request.post('/login', data),
|
||||
getTenants: () => request.get('/tenant/list'),
|
||||
logout: () => request.post('/logout')
|
||||
login: (data) => request.post('/login', data, { headers: { isToken: false } }),
|
||||
getTenants: (username) => request.get('/system/tenant/user-bind/' + username, { headers: { isToken: false } })
|
||||
}
|
||||
|
||||
export const nursingApi = {
|
||||
|
||||
@@ -17,7 +17,7 @@ const router = createRouter({ history: createWebHistory(), routes })
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (to.meta.requiresAuth) {
|
||||
const token = localStorage.getItem('token')
|
||||
const token = localStorage.getItem('Admin-Token')
|
||||
if (!token) { next('/login'); return }
|
||||
}
|
||||
next()
|
||||
|
||||
@@ -7,78 +7,78 @@
|
||||
</div>
|
||||
<div class="login-form">
|
||||
<div class="form-item">
|
||||
<label>用户名</label>
|
||||
<input v-model="form.username" type="text" placeholder="请输入用户名" class="input" @blur="loadTenants" />
|
||||
</div>
|
||||
<div class="form-item" v-if="tenants.length > 0">
|
||||
<label>租户</label>
|
||||
<select v-model="form.tenantId" class="input">
|
||||
<option value="">请选择租户</option>
|
||||
<option v-for="t in tenants" :key="t.tenantId" :value="t.tenantId">{{ t.tenantName }}</option>
|
||||
<option v-for="t in tenants" :key="t.id" :value="t.id">{{ t.tenantName }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-item">
|
||||
<label>用户名</label>
|
||||
<input v-model="form.username" type="text" placeholder="请输入用户名" class="input" />
|
||||
</div>
|
||||
<div class="form-item">
|
||||
<label>密码</label>
|
||||
<input v-model="form.password" type="password" placeholder="请输入密码" class="input" />
|
||||
<input v-model="form.password" type="password" placeholder="请输入密码" class="input" @keyup.enter="handleLogin" />
|
||||
</div>
|
||||
<button class="login-btn" @click="handleLogin" :disabled="loading">
|
||||
{{ loading ? '登录中...' : '登 录' }}
|
||||
</button>
|
||||
<div v-if="errorMsg" class="error-msg">{{ errorMsg }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import axios from 'axios'
|
||||
import { authApi } from '../api'
|
||||
|
||||
const router = useRouter()
|
||||
const loading = ref(false)
|
||||
const errorMsg = ref('')
|
||||
const tenants = ref([])
|
||||
const form = ref({ tenantId: '', username: '', password: '' })
|
||||
const form = ref({ username: '', password: '', tenantId: null })
|
||||
|
||||
const loadTenants = async () => {
|
||||
if (!form.value.username) return
|
||||
try {
|
||||
const res = await axios.get('/dev-api/tenant/list')
|
||||
tenants.value = res.data?.data || []
|
||||
const res = await authApi.getTenants(form.value.username)
|
||||
if (res.code === 200 && res.data) {
|
||||
tenants.value = res.data
|
||||
if (res.data.length === 1) form.value.tenantId = res.data[0].id
|
||||
}
|
||||
} catch (e) { console.error(e) }
|
||||
}
|
||||
|
||||
const handleLogin = async () => {
|
||||
if (!form.value.tenantId || !form.value.username || !form.value.password) {
|
||||
ElMessage.warning('请填写完整信息')
|
||||
return
|
||||
}
|
||||
if (!form.value.username) { errorMsg.value = '请输入用户名'; return }
|
||||
if (!form.value.password) { errorMsg.value = '请输入密码'; return }
|
||||
if (!form.value.tenantId && tenants.value.length > 0) { errorMsg.value = '请选择租户'; return }
|
||||
|
||||
loading.value = true
|
||||
errorMsg.value = ''
|
||||
try {
|
||||
const res = await axios.post('/dev-api/login', {
|
||||
const res = await authApi.login({
|
||||
username: form.value.username,
|
||||
password: form.value.password,
|
||||
tenantId: form.value.tenantId
|
||||
tenantId: form.value.tenantId,
|
||||
code: '',
|
||||
uuid: ''
|
||||
})
|
||||
if (res.data?.code === 200) {
|
||||
localStorage.setItem('token', res.data.token)
|
||||
localStorage.setItem('userInfo', JSON.stringify(res.data.userInfo))
|
||||
localStorage.setItem('tenantId', form.value.tenantId)
|
||||
if (res.code === 200 && res.token) {
|
||||
localStorage.setItem('Admin-Token', res.token)
|
||||
ElMessage.success('登录成功')
|
||||
router.push('/mobile/tasks')
|
||||
} else {
|
||||
ElMessage.error(res.data?.msg || '登录失败')
|
||||
errorMsg.value = res.msg || '登录失败'
|
||||
}
|
||||
} catch (e) {
|
||||
ElMessage.error('登录失败: ' + (e.response?.data?.msg || e.message))
|
||||
errorMsg.value = e.response?.data?.msg || '登录失败,请检查网络'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) router.push('/mobile/tasks')
|
||||
else loadTenants()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
@@ -95,4 +95,5 @@ onMounted(() => {
|
||||
select.input { appearance: none; background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23999' d='M6 8L1 3h10z'/%3E%3C/svg%3E") no-repeat right 12px center; }
|
||||
.login-btn { width: 100%; padding: 14px; background: #1890ff; color: #fff; border: none; border-radius: 8px; font-size: 18px; font-weight: 600; cursor: pointer; }
|
||||
.login-btn:disabled { background: #91d5ff; }
|
||||
.error-msg { color: #f5222d; text-align: center; margin-top: 12px; font-size: 14px; }
|
||||
</style>
|
||||
|
||||
@@ -22,7 +22,7 @@ import { ElMessageBox } from 'element-plus'
|
||||
const logout = async () => {
|
||||
try {
|
||||
await ElMessageBox.confirm('确认退出登录?', '提示', { confirmButtonText: '确认', cancelButtonText: '取消' })
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('Admin-Token')
|
||||
window.location.href = '/login'
|
||||
} catch {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user