koishi-plugin-ets2-tools-tmp 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/command/ets-app/resetPassword.js +72 -152
- package/package.json +1 -1
|
@@ -3,172 +3,92 @@ module.exports = async (ctx, cfg, session, targetTeamId) => {
|
|
|
3
3
|
const { adminUsers } = cfg.resetPassword;
|
|
4
4
|
const currentUserQQ = session.userId;
|
|
5
5
|
const isAdmin = adminUsers.includes(currentUserQQ);
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
const isPrivateChat = session.channelId === `private:${currentUserQQ}`;
|
|
7
|
+
const log = (msg) => logOutput && ctx.logger.info(msg);
|
|
8
|
+
const fetchData = async (apiUrl) => {
|
|
8
9
|
try {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (qq.startsWith("<at ")) {
|
|
13
|
-
if (qq.startsWith('<at ')) {
|
|
14
|
-
qq = qq.replace('<at ', '');
|
|
15
|
-
}
|
|
16
|
-
let id = '';
|
|
17
|
-
const idStart = qq.indexOf('id="');
|
|
18
|
-
if (idStart !== -1) {
|
|
19
|
-
const valueStart = idStart + 4;
|
|
20
|
-
const valueEnd = qq.indexOf('"', valueStart);
|
|
21
|
-
if (valueEnd !== -1) {
|
|
22
|
-
id = qq.substring(valueStart, valueEnd);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
queryQQ = id;
|
|
26
|
-
if (!/^\d+$/.test(queryQQ)) {
|
|
27
|
-
return "获取qq号错误,请使用车队编号重置";
|
|
28
|
-
}
|
|
29
|
-
if (logOutput) {
|
|
30
|
-
ctx.logger.info(`管理员 ${currentUserQQ} 请求重置 qq号 ${queryQQ} 的密码`);
|
|
31
|
-
}
|
|
32
|
-
const verifyUrl = `https://${url}/api/user/info/list?token=${token}&page=0&limit=1&tmpId=&tmpName=&teamId=&qq=${queryQQ}&state=0&teamRole=`;
|
|
33
|
-
const verifyResponse = await ctx.http.post(verifyUrl);
|
|
34
|
-
if (verifyResponse.code !== 0 || !verifyResponse.page?.list?.length) {
|
|
35
|
-
return `未找到qq号为 ${queryQQ} 的用户信息`;
|
|
36
|
-
}
|
|
37
|
-
const userInfo = verifyResponse.page.list[0];
|
|
38
|
-
if (logOutput) {
|
|
39
|
-
ctx.logger.info(`目标用户信息: ${userInfo.tmpName} (QQ: ${userInfo.qq})`);
|
|
40
|
-
}
|
|
41
|
-
const teamId = userInfo.teamId;
|
|
42
|
-
const resetPasswordUrl = `https://${url}/api/user/info/resetPasswordWithTeamId?token=${token}&teamId=${teamId}`;
|
|
43
|
-
if (logOutput) {
|
|
44
|
-
ctx.logger.info(`请求重置密码: ${resetPasswordUrl}`);
|
|
45
|
-
}
|
|
46
|
-
const resetResponse = await ctx.http.post(resetPasswordUrl);
|
|
47
|
-
if (logOutput) {
|
|
48
|
-
ctx.logger.info(`重置密码响应: ${JSON.stringify(resetResponse)}`);
|
|
49
|
-
}
|
|
50
|
-
if (resetResponse.code === 0) {
|
|
51
|
-
if (targetTeamId) {
|
|
52
|
-
return `管理员操作:车队编号 ${teamId} 的密码重置成功!新密码已发送到用户邮箱。`;
|
|
53
|
-
} else {
|
|
54
|
-
return "密码重置成功!新密码已发送到您的邮箱,请查收。";
|
|
55
|
-
}
|
|
56
|
-
} else {
|
|
57
|
-
return `密码重置失败: ${resetResponse.msg || "未知错误"}`;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
let teamId = qq;
|
|
61
|
-
if (logOutput) {
|
|
62
|
-
ctx.logger.info(`管理员 ${currentUserQQ} 请求重置 车队编号 ${teamId} 的密码`);
|
|
63
|
-
}
|
|
64
|
-
const verifyUrl = `https://${url}/api/user/info/list?token=${token}&page=0&limit=1&tmpId=&tmpName=&teamId=${teamId}&qq=&state=0&teamRole=`;
|
|
65
|
-
const verifyResponse = await ctx.http.post(verifyUrl);
|
|
66
|
-
if (verifyResponse.code !== 0 || !verifyResponse.page?.list?.length) {
|
|
67
|
-
return `未找到车队编号为 ${teamId} 的用户信息`;
|
|
68
|
-
}
|
|
69
|
-
const userInfo = verifyResponse.page.list[0];
|
|
70
|
-
if (logOutput) {
|
|
71
|
-
ctx.logger.info(`目标用户信息: ${userInfo.tmpName} (QQ: ${userInfo.qq})`);
|
|
72
|
-
}
|
|
73
|
-
const resetPasswordUrl = `https://${url}/api/user/info/resetPasswordWithTeamId?token=${token}&teamId=${teamId}`;
|
|
74
|
-
if (logOutput) {
|
|
75
|
-
ctx.logger.info(`请求重置密码: ${resetPasswordUrl}`);
|
|
76
|
-
}
|
|
77
|
-
const resetResponse = await ctx.http.post(resetPasswordUrl);
|
|
78
|
-
if (logOutput) {
|
|
79
|
-
ctx.logger.info(`重置密码响应: ${JSON.stringify(resetResponse)}`);
|
|
80
|
-
}
|
|
81
|
-
if (resetResponse.code === 0) {
|
|
82
|
-
if (targetTeamId) {
|
|
83
|
-
return `管理员操作:车队编号 ${teamId} 的密码重置成功!新密码已发送到用户邮箱。`;
|
|
84
|
-
} else {
|
|
85
|
-
return "密码重置成功!新密码已发送到您的邮箱,请查收。";
|
|
86
|
-
}
|
|
87
|
-
} else {
|
|
88
|
-
return `密码重置失败: ${resetResponse.msg || "未知错误"}`;
|
|
89
|
-
}
|
|
10
|
+
const response = await ctx.http.post(apiUrl);
|
|
11
|
+
log(`请求响应: ${JSON.stringify(response)}`);
|
|
12
|
+
return response;
|
|
90
13
|
} catch (error) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return `请求失败: ${error.response.status} ${error.response.statusText}`;
|
|
94
|
-
} else if (error.code) {
|
|
95
|
-
return `网络错误: ${error.code}`;
|
|
96
|
-
} else {
|
|
97
|
-
return "系统错误,请稍后重试";
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
try {
|
|
102
|
-
let teamId;
|
|
103
|
-
if (!targetTeamId) {
|
|
104
|
-
if (logOutput) {
|
|
105
|
-
ctx.logger.info(`开始处理用户 ${currentUserQQ} 的密码重置请求`);
|
|
106
|
-
}
|
|
107
|
-
const userInfoUrl = `https://${url}/api/user/info/list?token=${token}&page=0&limit=7&tmpId=&tmpName=&teamId=&qq=${currentUserQQ}&state=0&teamRole=`;
|
|
108
|
-
if (logOutput) {
|
|
109
|
-
ctx.logger.info(`请求用户信息: ${userInfoUrl}`);
|
|
110
|
-
}
|
|
111
|
-
const userInfoResponse = await ctx.http.post(userInfoUrl);
|
|
112
|
-
if (logOutput) {
|
|
113
|
-
ctx.logger.info(`用户信息响应: ${JSON.stringify(userInfoResponse)}`);
|
|
114
|
-
}
|
|
115
|
-
if (userInfoResponse.code !== 0) {
|
|
116
|
-
return `获取用户信息失败: ${userInfoResponse.msg || "未知错误"}`;
|
|
117
|
-
}
|
|
118
|
-
const userList = userInfoResponse.page?.list || [];
|
|
119
|
-
if (userList.length === 0) {
|
|
120
|
-
return "未找到与该QQ号关联的用户信息";
|
|
121
|
-
}
|
|
122
|
-
teamId = userList[0].teamId;
|
|
123
|
-
if (!teamId) {
|
|
124
|
-
return "在平台中未找到您的信息,请联系管理员重置密码";
|
|
125
|
-
}
|
|
126
|
-
if (logOutput) {
|
|
127
|
-
ctx.logger.info(`找到用户 车队编号: ${teamId}`);
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
if (!isAdmin) {
|
|
131
|
-
return "您没有权限重置其他成员的密码,请联系管理员";
|
|
132
|
-
}
|
|
133
|
-
teamId = targetTeamId;
|
|
134
|
-
if (logOutput) {
|
|
135
|
-
ctx.logger.info(`管理员 ${currentUserQQ} 请求重置 车队编号 ${teamId} 的密码`);
|
|
136
|
-
}
|
|
137
|
-
const verifyUrl = `https://${url}/api/user/info/list?token=${token}&page=0&limit=1&tmpId=&tmpName=&teamId=${teamId}&qq=&state=0&teamRole=`;
|
|
138
|
-
const verifyResponse = await ctx.http.post(verifyUrl);
|
|
139
|
-
if (verifyResponse.code !== 0 || !verifyResponse.page?.list?.length) {
|
|
140
|
-
return `未找到车队编号为 ${teamId} 的用户信息`;
|
|
141
|
-
}
|
|
142
|
-
const userInfo = verifyResponse.page.list[0];
|
|
143
|
-
if (logOutput) {
|
|
144
|
-
ctx.logger.info(`目标用户信息: ${userInfo.tmpName} (QQ: ${userInfo.qq})`);
|
|
145
|
-
}
|
|
14
|
+
log(`请求错误: ${error}`);
|
|
15
|
+
throw error;
|
|
146
16
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const getUserInfo = async (params) => {
|
|
20
|
+
const query = new URLSearchParams({ token, page: 0, limit: 1, ...params }).toString();
|
|
21
|
+
const userInfoUrl = `https://${url}/api/user/info/list?${query}`;
|
|
22
|
+
log(`获取用户信息: ${userInfoUrl}`);
|
|
23
|
+
const response = await fetchData(userInfoUrl);
|
|
24
|
+
|
|
25
|
+
if (response.code !== 0 || !response.page?.list?.length) {
|
|
26
|
+
throw new Error(`未找到用户信息`);
|
|
150
27
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
28
|
+
return response.page.list[0];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const resetPassword = async (teamId) => {
|
|
32
|
+
const resetUrl = `https://${url}/api/user/info/resetPasswordWithTeamId?token=${token}&teamId=${teamId}`;
|
|
33
|
+
log(`重置密码请求: ${resetUrl}`);
|
|
34
|
+
const response = await fetchData(resetUrl);
|
|
35
|
+
|
|
36
|
+
if (response.code !== 0) {
|
|
37
|
+
throw new Error(response.msg || "未知错误");
|
|
154
38
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
39
|
+
return teamId;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
let teamId, targetQQ;
|
|
44
|
+
if (targetTeamId?.startsWith("<at ")) {
|
|
45
|
+
if (!isAdmin) return "您没有权限重置其他用户的密码,请联系管理员";
|
|
46
|
+
const idStart = targetTeamId.indexOf('id="');
|
|
47
|
+
if (idStart === -1) return "获取QQ号错误,请使用车队编号重置";
|
|
48
|
+
|
|
49
|
+
targetQQ = targetTeamId.substring(idStart + 4, targetTeamId.indexOf('"', idStart + 4));
|
|
50
|
+
if (!/^\d+$/.test(targetQQ)) return "获取QQ号错误,请使用车队编号重置";
|
|
51
|
+
|
|
52
|
+
log(`管理员 ${currentUserQQ} 重置QQ: ${targetQQ} 密码`);
|
|
53
|
+
const userInfo = await getUserInfo({ qq: targetQQ });
|
|
54
|
+
teamId = userInfo.teamId;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
if (isPrivateChat) {
|
|
58
|
+
if (!targetTeamId) {
|
|
59
|
+
log(`用户 ${currentUserQQ} 发起密码重置`);
|
|
60
|
+
const userInfo = await getUserInfo({ qq: currentUserQQ });
|
|
61
|
+
teamId = userInfo.teamId;
|
|
62
|
+
if (!teamId) return "未找到您的信息,请联系管理员";
|
|
63
|
+
} else {
|
|
64
|
+
const userInfo = await getUserInfo({ teamId: targetTeamId });
|
|
65
|
+
if (!isAdmin && userInfo.qq !== currentUserQQ) {
|
|
66
|
+
return "您没有权限重置其他成员的密码,请联系管理员";
|
|
67
|
+
}
|
|
68
|
+
teamId = targetTeamId;
|
|
69
|
+
}
|
|
158
70
|
} else {
|
|
159
|
-
return "
|
|
71
|
+
if (!isAdmin) return "您没有权限重置其他用户的密码,请联系管理员重置,或私聊机器人重置";
|
|
72
|
+
|
|
73
|
+
teamId = targetTeamId;
|
|
74
|
+
log(`管理员 ${currentUserQQ} 重置车队: ${teamId} 密码`);
|
|
75
|
+
await getUserInfo({ teamId });
|
|
160
76
|
}
|
|
161
|
-
} else {
|
|
162
|
-
return `密码重置失败: ${resetResponse.msg || "未知错误"}`;
|
|
163
77
|
}
|
|
78
|
+
const resetTeamId = await resetPassword(teamId);
|
|
79
|
+
const isAdminOp = isAdmin && (targetTeamId || targetQQ);
|
|
80
|
+
return isAdminOp
|
|
81
|
+
? `管理员操作:车队编号 ${resetTeamId} 的密码重置成功!新密码已发送到用户邮箱。`
|
|
82
|
+
: "密码重置成功!新密码已发送到您的邮箱,请查收。";
|
|
83
|
+
|
|
164
84
|
} catch (error) {
|
|
165
|
-
ctx.logger.error(
|
|
85
|
+
ctx.logger.error(`密码重置错误: ${error.message}`);
|
|
166
86
|
if (error.response) {
|
|
167
87
|
return `请求失败: ${error.response.status} ${error.response.statusText}`;
|
|
168
88
|
} else if (error.code) {
|
|
169
89
|
return `网络错误: ${error.code}`;
|
|
170
90
|
} else {
|
|
171
|
-
return "系统错误,请稍后重试";
|
|
91
|
+
return error.message.includes("未找到") ? error.message : "系统错误,请稍后重试";
|
|
172
92
|
}
|
|
173
93
|
}
|
|
174
94
|
};
|