@gt6/sdk 1.0.9 → 1.0.11
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/README.md +388 -2
- package/dist/gt6-sdk.cjs.js +261 -1
- package/dist/gt6-sdk.cjs.js.map +1 -1
- package/dist/gt6-sdk.esm.js +261 -2
- package/dist/gt6-sdk.esm.js.map +1 -1
- package/dist/index.d.ts +51 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/modules/users.d.ts +115 -0
- package/dist/modules/users.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/gt6-sdk.esm.js
CHANGED
@@ -829,9 +829,201 @@ class FormsAPI {
|
|
829
829
|
}
|
830
830
|
}
|
831
831
|
|
832
|
+
/**
|
833
|
+
* 用户管理API模块
|
834
|
+
* 提供用户登录、注册等功能
|
835
|
+
*/
|
836
|
+
class UsersAPI {
|
837
|
+
constructor(client) {
|
838
|
+
this.client = client;
|
839
|
+
}
|
840
|
+
/**
|
841
|
+
* 用户登录
|
842
|
+
* @param loginData 登录数据
|
843
|
+
* @returns 登录响应
|
844
|
+
*/
|
845
|
+
async login(loginData) {
|
846
|
+
try {
|
847
|
+
const response = await this.client.request('/web/user/login', {
|
848
|
+
method: 'POST',
|
849
|
+
headers: {
|
850
|
+
'Content-Type': 'application/json',
|
851
|
+
},
|
852
|
+
body: JSON.stringify(loginData)
|
853
|
+
});
|
854
|
+
return response;
|
855
|
+
}
|
856
|
+
catch (error) {
|
857
|
+
// 如果是HTTP错误,尝试解析响应体
|
858
|
+
if (error.statusCode && error.message) {
|
859
|
+
// 对于401错误,返回特定的错误信息
|
860
|
+
if (error.statusCode === 401) {
|
861
|
+
return {
|
862
|
+
success: false,
|
863
|
+
message: '用户名或密码错误'
|
864
|
+
};
|
865
|
+
}
|
866
|
+
// 对于其他HTTP错误,返回状态码信息
|
867
|
+
return {
|
868
|
+
success: false,
|
869
|
+
message: error.message || `HTTP ${error.statusCode} 错误`
|
870
|
+
};
|
871
|
+
}
|
872
|
+
// 对于网络错误或其他错误
|
873
|
+
return {
|
874
|
+
success: false,
|
875
|
+
message: error instanceof Error ? error.message : '登录请求失败'
|
876
|
+
};
|
877
|
+
}
|
878
|
+
}
|
879
|
+
/**
|
880
|
+
* 用户注册
|
881
|
+
* @param registerData 注册数据
|
882
|
+
* @returns 注册响应
|
883
|
+
*/
|
884
|
+
async register(registerData) {
|
885
|
+
try {
|
886
|
+
const response = await this.client.request('/web/user/register', {
|
887
|
+
method: 'POST',
|
888
|
+
headers: {
|
889
|
+
'Content-Type': 'application/json',
|
890
|
+
},
|
891
|
+
body: JSON.stringify(registerData)
|
892
|
+
});
|
893
|
+
return response;
|
894
|
+
}
|
895
|
+
catch (error) {
|
896
|
+
// 如果是HTTP错误,尝试解析响应体
|
897
|
+
if (error.statusCode && error.message) {
|
898
|
+
// 对于400错误,可能是用户名已存在等验证错误
|
899
|
+
if (error.statusCode === 400) {
|
900
|
+
return {
|
901
|
+
success: false,
|
902
|
+
message: '注册信息有误,请检查输入'
|
903
|
+
};
|
904
|
+
}
|
905
|
+
// 对于其他HTTP错误,返回状态码信息
|
906
|
+
return {
|
907
|
+
success: false,
|
908
|
+
message: error.message || `HTTP ${error.statusCode} 错误`
|
909
|
+
};
|
910
|
+
}
|
911
|
+
// 对于网络错误或其他错误
|
912
|
+
return {
|
913
|
+
success: false,
|
914
|
+
message: error instanceof Error ? error.message : '注册请求失败'
|
915
|
+
};
|
916
|
+
}
|
917
|
+
}
|
918
|
+
/**
|
919
|
+
* 保存用户数据到本地存储
|
920
|
+
* @param userData 用户数据
|
921
|
+
*/
|
922
|
+
saveUserData(userData) {
|
923
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
924
|
+
localStorage.setItem('userData', JSON.stringify(userData));
|
925
|
+
}
|
926
|
+
}
|
927
|
+
/**
|
928
|
+
* 从本地存储获取用户数据
|
929
|
+
* @returns 用户数据或null
|
930
|
+
*/
|
931
|
+
getUserData() {
|
932
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
933
|
+
const userDataStr = localStorage.getItem('userData');
|
934
|
+
if (userDataStr) {
|
935
|
+
try {
|
936
|
+
return JSON.parse(userDataStr);
|
937
|
+
}
|
938
|
+
catch (error) {
|
939
|
+
console.warn('解析用户数据失败:', error);
|
940
|
+
return null;
|
941
|
+
}
|
942
|
+
}
|
943
|
+
}
|
944
|
+
return null;
|
945
|
+
}
|
946
|
+
/**
|
947
|
+
* 清除本地存储的用户数据
|
948
|
+
*/
|
949
|
+
clearUserData() {
|
950
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
951
|
+
localStorage.removeItem('userData');
|
952
|
+
}
|
953
|
+
}
|
954
|
+
/**
|
955
|
+
* 检查用户是否已登录
|
956
|
+
* @returns 是否已登录
|
957
|
+
*/
|
958
|
+
isLoggedIn() {
|
959
|
+
const userData = this.getUserData();
|
960
|
+
return userData !== null && userData.token !== undefined;
|
961
|
+
}
|
962
|
+
/**
|
963
|
+
* 获取当前登录用户的token
|
964
|
+
* @returns token或null
|
965
|
+
*/
|
966
|
+
getToken() {
|
967
|
+
const userData = this.getUserData();
|
968
|
+
return userData?.token || null;
|
969
|
+
}
|
970
|
+
/**
|
971
|
+
* 获取当前登录用户信息
|
972
|
+
* @returns 用户信息或null
|
973
|
+
*/
|
974
|
+
getCurrentUser() {
|
975
|
+
const userData = this.getUserData();
|
976
|
+
if (userData) {
|
977
|
+
const { token, ...userInfo } = userData;
|
978
|
+
return userInfo;
|
979
|
+
}
|
980
|
+
return null;
|
981
|
+
}
|
982
|
+
/**
|
983
|
+
* 用户登出
|
984
|
+
*/
|
985
|
+
logout() {
|
986
|
+
this.clearUserData();
|
987
|
+
}
|
988
|
+
/**
|
989
|
+
* 记住用户登录信息
|
990
|
+
* @param username 用户名
|
991
|
+
* @param password 密码
|
992
|
+
*/
|
993
|
+
rememberLogin(username, password) {
|
994
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
995
|
+
localStorage.setItem('rememberedUsername', username);
|
996
|
+
localStorage.setItem('rememberedPassword', password);
|
997
|
+
}
|
998
|
+
}
|
999
|
+
/**
|
1000
|
+
* 获取记住的登录信息
|
1001
|
+
* @returns 记住的登录信息
|
1002
|
+
*/
|
1003
|
+
getRememberedLogin() {
|
1004
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
1005
|
+
const username = localStorage.getItem('rememberedUsername');
|
1006
|
+
const password = localStorage.getItem('rememberedPassword');
|
1007
|
+
if (username && password) {
|
1008
|
+
return { username, password };
|
1009
|
+
}
|
1010
|
+
}
|
1011
|
+
return null;
|
1012
|
+
}
|
1013
|
+
/**
|
1014
|
+
* 清除记住的登录信息
|
1015
|
+
*/
|
1016
|
+
clearRememberedLogin() {
|
1017
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
1018
|
+
localStorage.removeItem('rememberedUsername');
|
1019
|
+
localStorage.removeItem('rememberedPassword');
|
1020
|
+
}
|
1021
|
+
}
|
1022
|
+
}
|
1023
|
+
|
832
1024
|
/**
|
833
1025
|
* GT6 SDK 主类
|
834
|
-
*
|
1026
|
+
* 提供文章、产品、设置、表单和用户管理相关的所有功能
|
835
1027
|
*/
|
836
1028
|
class GT6SDK {
|
837
1029
|
constructor(config) {
|
@@ -840,6 +1032,7 @@ class GT6SDK {
|
|
840
1032
|
this.products = new ProductsAPI(client);
|
841
1033
|
this.settings = new SettingsAPI(client);
|
842
1034
|
this.forms = new FormsAPI(client);
|
1035
|
+
this.users = new UsersAPI(client);
|
843
1036
|
}
|
844
1037
|
/**
|
845
1038
|
* 获取客户端实例(用于高级用法)
|
@@ -1021,7 +1214,73 @@ class GT6SDK {
|
|
1021
1214
|
async submitCustomForm(aliases, formData, emailField) {
|
1022
1215
|
return this.forms.submitCustomForm(aliases, formData, emailField);
|
1023
1216
|
}
|
1217
|
+
/**
|
1218
|
+
* 28. 便捷方法:用户登录
|
1219
|
+
*/
|
1220
|
+
async login(username, password, platformId) {
|
1221
|
+
return this.users.login({ username, password, platformId });
|
1222
|
+
}
|
1223
|
+
/**
|
1224
|
+
* 29. 便捷方法:用户注册
|
1225
|
+
*/
|
1226
|
+
async register(username, password, platformId, ibcode) {
|
1227
|
+
return this.users.register({ username, password, platformId, ibcode });
|
1228
|
+
}
|
1229
|
+
/**
|
1230
|
+
* 30. 便捷方法:检查用户是否已登录
|
1231
|
+
*/
|
1232
|
+
isLoggedIn() {
|
1233
|
+
return this.users.isLoggedIn();
|
1234
|
+
}
|
1235
|
+
/**
|
1236
|
+
* 31. 便捷方法:获取当前登录用户信息
|
1237
|
+
*/
|
1238
|
+
getCurrentUser() {
|
1239
|
+
return this.users.getCurrentUser();
|
1240
|
+
}
|
1241
|
+
/**
|
1242
|
+
* 32. 便捷方法:获取当前登录用户的token
|
1243
|
+
*/
|
1244
|
+
getToken() {
|
1245
|
+
return this.users.getToken();
|
1246
|
+
}
|
1247
|
+
/**
|
1248
|
+
* 33. 便捷方法:用户登出
|
1249
|
+
*/
|
1250
|
+
logout() {
|
1251
|
+
return this.users.logout();
|
1252
|
+
}
|
1253
|
+
/**
|
1254
|
+
* 34. 便捷方法:保存用户数据
|
1255
|
+
*/
|
1256
|
+
saveUserData(userData) {
|
1257
|
+
return this.users.saveUserData(userData);
|
1258
|
+
}
|
1259
|
+
/**
|
1260
|
+
* 35. 便捷方法:获取用户数据
|
1261
|
+
*/
|
1262
|
+
getUserData() {
|
1263
|
+
return this.users.getUserData();
|
1264
|
+
}
|
1265
|
+
/**
|
1266
|
+
* 36. 便捷方法:记住登录信息
|
1267
|
+
*/
|
1268
|
+
rememberLogin(username, password) {
|
1269
|
+
return this.users.rememberLogin(username, password);
|
1270
|
+
}
|
1271
|
+
/**
|
1272
|
+
* 37. 便捷方法:获取记住的登录信息
|
1273
|
+
*/
|
1274
|
+
getRememberedLogin() {
|
1275
|
+
return this.users.getRememberedLogin();
|
1276
|
+
}
|
1277
|
+
/**
|
1278
|
+
* 38. 便捷方法:清除记住的登录信息
|
1279
|
+
*/
|
1280
|
+
clearRememberedLogin() {
|
1281
|
+
return this.users.clearRememberedLogin();
|
1282
|
+
}
|
1024
1283
|
}
|
1025
1284
|
|
1026
|
-
export { ArticlesAPI, FormsAPI, GT6Client, GT6Error, GT6SDK, ProductsAPI, SettingsAPI, GT6SDK as default };
|
1285
|
+
export { ArticlesAPI, FormsAPI, GT6Client, GT6Error, GT6SDK, ProductsAPI, SettingsAPI, UsersAPI, GT6SDK as default };
|
1027
1286
|
//# sourceMappingURL=gt6-sdk.esm.js.map
|