|
|
@ -38,6 +38,9 @@ |
|
|
|
|
|
|
|
|
<script> |
|
|
<script> |
|
|
import Icp from '../components/common/Icp.vue' |
|
|
import Icp from '../components/common/Icp.vue' |
|
|
|
|
|
import useFriendStore from '../store/friendStore' |
|
|
|
|
|
import useChatStore from '../store/chatStore' |
|
|
|
|
|
|
|
|
export default { |
|
|
export default { |
|
|
name: "login", |
|
|
name: "login", |
|
|
components: { |
|
|
components: { |
|
|
@ -55,7 +58,6 @@ export default { |
|
|
callback(new Error('请输入密码')); |
|
|
callback(new Error('请输入密码')); |
|
|
} |
|
|
} |
|
|
callback(); |
|
|
callback(); |
|
|
|
|
|
|
|
|
}; |
|
|
}; |
|
|
return { |
|
|
return { |
|
|
loginForm: { |
|
|
loginForm: { |
|
|
@ -76,28 +78,65 @@ export default { |
|
|
}; |
|
|
}; |
|
|
}, |
|
|
}, |
|
|
methods: { |
|
|
methods: { |
|
|
submitForm(formName) { |
|
|
async submitForm(formName) { |
|
|
this.$refs[formName].validate((valid) => { |
|
|
const valid = await this.$refs[formName].validate().catch(() => false); |
|
|
if (valid) { |
|
|
if (!valid) return; |
|
|
this.$http({ |
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
const data = await this.$http({ |
|
|
url: "/loginCustom", |
|
|
url: "/loginCustom", |
|
|
method: 'post', |
|
|
method: 'post', |
|
|
data: this.loginForm |
|
|
data: this.loginForm |
|
|
}) |
|
|
}); |
|
|
.then((data) => { |
|
|
|
|
|
// 保存密码到cookie(不安全) |
|
|
// 保存cookie |
|
|
this.setCookie('username', this.loginForm.userName); |
|
|
this.setCookie('username', this.loginForm.userName); |
|
|
this.setCookie('password', this.loginForm.password); |
|
|
this.setCookie('password', this.loginForm.password); |
|
|
|
|
|
|
|
|
// 保存token |
|
|
// 保存token |
|
|
sessionStorage.setItem("accessToken", data.accessToken); |
|
|
sessionStorage.setItem("accessToken", data.accessToken); |
|
|
sessionStorage.setItem("refreshToken", data.refreshToken); |
|
|
sessionStorage.setItem("refreshToken", data.refreshToken); |
|
|
|
|
|
|
|
|
this.$message.success("登录成功"); |
|
|
this.$message.success("登录成功"); |
|
|
|
|
|
|
|
|
|
|
|
// 先跳转,再异步初始化 |
|
|
this.$router.push("/home/chat"); |
|
|
this.$router.push("/home/chat"); |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
// 延迟执行初始化,确保页面已渲染 |
|
|
|
|
|
this.$nextTick(() => { |
|
|
|
|
|
this.initAfterLogin(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error('登录失败:', error); |
|
|
|
|
|
this.$message.error('登录失败'); |
|
|
|
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
async initAfterLogin() { |
|
|
|
|
|
const friendStore = useFriendStore(); |
|
|
|
|
|
const chatStore = useChatStore(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await friendStore.loadFriend(); |
|
|
|
|
|
|
|
|
|
|
|
await chatStore.loadChat(); |
|
|
|
|
|
|
|
|
|
|
|
chatStore.refreshChats(); |
|
|
|
|
|
|
|
|
|
|
|
chatStore.setLoading(false); |
|
|
|
|
|
|
|
|
|
|
|
const removedCount = chatStore.cleanOfflineChats(); |
|
|
|
|
|
|
|
|
|
|
|
console.log(`=== 初始化完成: 清理了 ${removedCount} 个离线会话 ===`); |
|
|
|
|
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error('初始化失败:', error); |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
resetForm(formName) { |
|
|
resetForm(formName) { |
|
|
this.$refs[formName].resetFields(); |
|
|
this.$refs[formName].resetFields(); |
|
|
}, |
|
|
}, |
|
|
@ -112,11 +151,9 @@ export default { |
|
|
setCookie(name, value) { |
|
|
setCookie(name, value) { |
|
|
document.cookie = name + "=" + escape(value); |
|
|
document.cookie = name + "=" + escape(value); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
}, |
|
|
}, |
|
|
mounted() { |
|
|
mounted() { |
|
|
this.loginForm.userName = this.getCookie("username"); |
|
|
this.loginForm.userName = this.getCookie("username"); |
|
|
// cookie存密码并不安全,暂时是为了方便 |
|
|
|
|
|
this.loginForm.password = this.getCookie("password"); |
|
|
this.loginForm.password = this.getCookie("password"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|