对请求的结构、报错提示在接口部分做统一处理
This commit is contained in:
@@ -320,14 +320,14 @@ export default {
|
||||
getCurrentUser() {
|
||||
// 获取当前登录用户信息
|
||||
getUserInfo()
|
||||
.then((response) => {
|
||||
this.currentUser = response.data;
|
||||
.then((data) => {
|
||||
this.currentUser = data;
|
||||
this.getRecentMessageList();
|
||||
this.getContactList();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
this.$message.error("获取用户信息失败,请重新登录");
|
||||
this.$message.error(error.message);
|
||||
});
|
||||
},
|
||||
|
||||
@@ -335,13 +335,13 @@ export default {
|
||||
getRecentMessageList() {
|
||||
if (!this.currentUser) return;
|
||||
getRecentMessages()
|
||||
.then((response) => {
|
||||
this.recentChats = response.data.recentChats || [];
|
||||
.then((data) => {
|
||||
this.recentChats = data.recentChats || [];
|
||||
this.selectFirstContact();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
this.$message.error("获取消息列表失败");
|
||||
this.$message.error(error.message);
|
||||
});
|
||||
},
|
||||
|
||||
@@ -376,14 +376,14 @@ export default {
|
||||
async getContactMessageList(contactName, limit, offset) {
|
||||
try {
|
||||
// 获取当前联系人的消息列表
|
||||
const response = await getContactMessages({
|
||||
const data = await getContactMessages({
|
||||
contactName: contactName,
|
||||
limit: limit,
|
||||
offset: offset,
|
||||
});
|
||||
|
||||
const contactMessages = [];
|
||||
const messages = response.data.messages || [];
|
||||
const messages = data.messages || [];
|
||||
messages.forEach((item) => {
|
||||
contactMessages.push({
|
||||
...item,
|
||||
@@ -399,22 +399,22 @@ export default {
|
||||
console.log("contactMessages", contactMessages);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
this.$message.error("获取消息列表失败");
|
||||
this.$message.error(error.message);
|
||||
}
|
||||
},
|
||||
|
||||
// 获取联系人列表
|
||||
getContactList() {
|
||||
getContactInfo()
|
||||
.then((response) => {
|
||||
this.contactList = response.data.contacts.map((contact) => ({
|
||||
.then((data) => {
|
||||
this.contactList = data.contacts.map((contact) => ({
|
||||
...contact,
|
||||
unreadCount: contact.unreadCount || 0,
|
||||
}));
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
this.$message.error("获取联系人信息失败");
|
||||
this.$message.error(error.message);
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
@@ -54,14 +54,14 @@ export default {
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
})
|
||||
.then((response) => {
|
||||
const token = response.data.token;
|
||||
.then((data) => {
|
||||
const token = data.token;
|
||||
this.$store.commit("SET_TOKEN", token);
|
||||
this.$router.push("/chat");
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
this.$message.error("账号或密码错误");
|
||||
this.$message.error(error.message);
|
||||
});
|
||||
} else {
|
||||
this.$message.error("请输入账号和密码");
|
||||
|
||||
@@ -69,7 +69,7 @@ export default {
|
||||
this.$router.push('/login')
|
||||
}).catch((error) => {
|
||||
console.error(error)
|
||||
this.$message.error('注册失败,请重试')
|
||||
this.$message.error(error.message)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+29
-3
@@ -47,15 +47,41 @@ request.interceptors.request.use(
|
||||
|
||||
request.interceptors.response.use(
|
||||
(response) => {
|
||||
return response.data;
|
||||
const result = response.data;
|
||||
|
||||
// HTTP 请求成功,但业务失败
|
||||
if (result.code !== 200) {
|
||||
return Promise.reject({
|
||||
type: "business",
|
||||
message: result.message
|
||||
});
|
||||
}
|
||||
return result.data;
|
||||
},
|
||||
(error) => {
|
||||
if (!error.response) {
|
||||
// 网络错误
|
||||
return Promise.reject({
|
||||
type: "network",
|
||||
message: "网络连接失败,请检查网络"
|
||||
});
|
||||
}
|
||||
|
||||
if (error.response && error.response.status === 401) {
|
||||
// 如果返回401,说明token过期或无效,清除token并跳转到登录页
|
||||
store.commit('SET_TOKEN', '');
|
||||
redirectToLogin();
|
||||
}
|
||||
return Promise.reject(error);
|
||||
|
||||
return Promise.reject({
|
||||
type: "auth",
|
||||
message: "登录已过期,请重新登录"
|
||||
});
|
||||
};
|
||||
|
||||
return Promise.reject({
|
||||
type: "server",
|
||||
message: "服务器异常,请稍后重试"
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user