274 lines
6.1 KiB
JavaScript
274 lines
6.1 KiB
JavaScript
import { appConfig } from '@/config';
|
|
import { HttpImClient } from './httpImClient';
|
|
import axios from 'axios';
|
|
import {
|
|
appServerGet,
|
|
appServerPost,
|
|
clearStoredAppAuth,
|
|
getStoredAppAuth,
|
|
persistAppAuth
|
|
} from './appServerClient';
|
|
|
|
function inferAccountType(account) {
|
|
return String(account || '').includes('@') ? 2 : 1;
|
|
}
|
|
|
|
function unwrapData(response) {
|
|
return response.data?.data ?? response.data?.result ?? null;
|
|
}
|
|
|
|
function unwrapMessage(response, fallback) {
|
|
return response.data?.message || response.data?.msg || fallback;
|
|
}
|
|
|
|
export class DocumentedHttpImClient extends HttpImClient {
|
|
unsupported(feature) {
|
|
throw new Error(`当前对接文档未提供“${feature}”接口,已禁用旧本地代理链路`);
|
|
}
|
|
|
|
async loginWithPassword(account, password, type = inferAccountType(account)) {
|
|
const response = await appServerPost(`${appConfig.appServer}/auth/login_pwd`, {
|
|
type,
|
|
number: account,
|
|
password,
|
|
platform: appConfig.platform,
|
|
clientId: this.getClientId()
|
|
}, {
|
|
skipAuth: true
|
|
});
|
|
|
|
if (response.data?.code !== 0) {
|
|
throw new Error(unwrapMessage(response, '登录失败'));
|
|
}
|
|
|
|
persistAppAuth(unwrapData(response));
|
|
return unwrapData(response);
|
|
}
|
|
|
|
async requestAuthCode(email) {
|
|
const response = await appServerPost(`${appConfig.appServer}/auth/sendEmailCode`, {
|
|
email
|
|
}, {
|
|
skipAuth: true
|
|
});
|
|
|
|
if (response.data?.code !== 0) {
|
|
throw new Error(unwrapMessage(response, '验证码发送失败'));
|
|
}
|
|
|
|
return unwrapData(response);
|
|
}
|
|
|
|
async loginWithCode(account, code) {
|
|
void account;
|
|
void code;
|
|
throw new Error('接口文档未提供验证码登录接口,请使用密码登录');
|
|
}
|
|
|
|
async register({ type, number, code = '', password, invitationCode = '' }) {
|
|
const response = await appServerPost(`${appConfig.appServer}/auth/register`, {
|
|
type,
|
|
number,
|
|
code,
|
|
password,
|
|
clientId: this.getClientId(),
|
|
platform: appConfig.platform,
|
|
invitationCode
|
|
}, {
|
|
skipAuth: true
|
|
});
|
|
|
|
if (response.data?.code !== 0) {
|
|
throw new Error(unwrapMessage(response, '注册失败'));
|
|
}
|
|
|
|
persistAppAuth(unwrapData(response));
|
|
return unwrapData(response);
|
|
}
|
|
|
|
async createPcSession(userId = '') {
|
|
const response = await axios.post(`${appConfig.qrAppServer}/pc_session`, {
|
|
flag: 1,
|
|
device_name: 'Web IM',
|
|
userId,
|
|
clientId: this.getClientId(),
|
|
platform: appConfig.platform
|
|
}, { withCredentials: false });
|
|
|
|
if (response.data?.code !== 0) {
|
|
throw new Error(unwrapMessage(response, '二维码登录会话创建失败'));
|
|
}
|
|
|
|
return unwrapData(response) || {};
|
|
}
|
|
|
|
async loginWithPcSession(token) {
|
|
const url = `${appConfig.qrAppServer}/session_login/${encodeURIComponent(token)}`;
|
|
let response;
|
|
try {
|
|
response = await axios.post(url, null, { withCredentials: false });
|
|
} catch (postError) {
|
|
response = await axios.get(url, { withCredentials: false });
|
|
}
|
|
|
|
this.persistAppToken(url, response);
|
|
|
|
const payload = response.data || {};
|
|
const result = payload.result ?? payload.data ?? null;
|
|
|
|
if (Number(payload.code) === 0 && result) {
|
|
persistAppAuth(result);
|
|
}
|
|
|
|
if ([0, 8, 9, 10, 13, 18, 20042].includes(Number(payload.code))) {
|
|
return {
|
|
...payload,
|
|
data: result
|
|
};
|
|
}
|
|
|
|
throw new Error(payload.message || '扫码登录失败');
|
|
}
|
|
|
|
async changePassword({ oldPassword, newPassword }) {
|
|
const response = await appServerPost(appConfig.changePasswordApi, {
|
|
oldPassword,
|
|
newPassword
|
|
});
|
|
|
|
if (response.data?.code !== 0) {
|
|
throw new Error(unwrapMessage(response, '密码修改失败'));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
async resetPasswordWithCode({ mobile, resetCode, newPassword }) {
|
|
void mobile;
|
|
void resetCode;
|
|
void newPassword;
|
|
throw new Error('接口文档未提供重置密码接口,请联系服务端确认流程');
|
|
}
|
|
|
|
async logout() {
|
|
const auth = getStoredAppAuth();
|
|
if (!auth?.refreshToken) {
|
|
clearStoredAppAuth();
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
const response = await appServerPost(appConfig.logoutApi, {
|
|
refreshToken: auth.refreshToken
|
|
});
|
|
if (response.data?.code !== 0) {
|
|
throw new Error(unwrapMessage(response, '退出登录失败'));
|
|
}
|
|
} finally {
|
|
clearStoredAppAuth();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
async fetchRemoteConversations() {
|
|
return [];
|
|
}
|
|
|
|
async fetchRemoteMessages() {
|
|
return [];
|
|
}
|
|
|
|
async getServerContactList() {
|
|
return [];
|
|
}
|
|
|
|
async getProfile() {
|
|
return null;
|
|
}
|
|
|
|
async sendTextMessage() {
|
|
this.unsupported('发送消息');
|
|
}
|
|
|
|
async sendAttachmentMessage() {
|
|
this.unsupported('发送附件消息');
|
|
}
|
|
|
|
async addContact() {
|
|
this.unsupported('添加联系人');
|
|
}
|
|
|
|
async updateContactAlias() {
|
|
this.unsupported('联系人备注');
|
|
}
|
|
|
|
async createGroup() {
|
|
this.unsupported('创建群组');
|
|
}
|
|
|
|
async joinGroup() {
|
|
this.unsupported('加入群组');
|
|
}
|
|
|
|
async broadcastMessage() {
|
|
this.unsupported('群发消息');
|
|
}
|
|
|
|
async getGroupDetail() {
|
|
this.unsupported('群详情');
|
|
}
|
|
|
|
async getGroupMembers() {
|
|
this.unsupported('群成员');
|
|
}
|
|
|
|
async updateGroupAlias() {
|
|
this.unsupported('群备注');
|
|
}
|
|
|
|
async leaveGroup() {
|
|
this.unsupported('退出群组');
|
|
}
|
|
|
|
async dismissGroup() {
|
|
this.unsupported('解散群组');
|
|
}
|
|
|
|
async transferGroup() {
|
|
this.unsupported('转让群组');
|
|
}
|
|
|
|
async setGroupAdmins() {
|
|
this.unsupported('设置群管理员');
|
|
}
|
|
|
|
async updateGroupProfile() {
|
|
this.unsupported('更新群资料');
|
|
}
|
|
|
|
async kickGroupMembers() {
|
|
this.unsupported('移除群成员');
|
|
}
|
|
|
|
async updateGroupMuteSetting() {
|
|
this.unsupported('群禁言设置');
|
|
}
|
|
|
|
async updateProfile() {
|
|
this.unsupported('更新个人资料');
|
|
}
|
|
|
|
async recallMessage() {
|
|
this.unsupported('撤回消息');
|
|
}
|
|
|
|
async editMessage() {
|
|
this.unsupported('编辑消息');
|
|
}
|
|
|
|
async getMessageDetail() {
|
|
this.unsupported('消息详情');
|
|
}
|
|
}
|