@gt6/sdk 1.0.9 → 1.0.10

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.
@@ -829,9 +829,169 @@ 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
+ return {
858
+ success: false,
859
+ message: error instanceof Error ? error.message : '登录请求失败'
860
+ };
861
+ }
862
+ }
863
+ /**
864
+ * 用户注册
865
+ * @param registerData 注册数据
866
+ * @returns 注册响应
867
+ */
868
+ async register(registerData) {
869
+ try {
870
+ const response = await this.client.request('/web/user/register', {
871
+ method: 'POST',
872
+ headers: {
873
+ 'Content-Type': 'application/json',
874
+ },
875
+ body: JSON.stringify(registerData)
876
+ });
877
+ return response;
878
+ }
879
+ catch (error) {
880
+ return {
881
+ success: false,
882
+ message: error instanceof Error ? error.message : '注册请求失败'
883
+ };
884
+ }
885
+ }
886
+ /**
887
+ * 保存用户数据到本地存储
888
+ * @param userData 用户数据
889
+ */
890
+ saveUserData(userData) {
891
+ if (typeof window !== 'undefined' && window.localStorage) {
892
+ localStorage.setItem('userData', JSON.stringify(userData));
893
+ }
894
+ }
895
+ /**
896
+ * 从本地存储获取用户数据
897
+ * @returns 用户数据或null
898
+ */
899
+ getUserData() {
900
+ if (typeof window !== 'undefined' && window.localStorage) {
901
+ const userDataStr = localStorage.getItem('userData');
902
+ if (userDataStr) {
903
+ try {
904
+ return JSON.parse(userDataStr);
905
+ }
906
+ catch (error) {
907
+ console.warn('解析用户数据失败:', error);
908
+ return null;
909
+ }
910
+ }
911
+ }
912
+ return null;
913
+ }
914
+ /**
915
+ * 清除本地存储的用户数据
916
+ */
917
+ clearUserData() {
918
+ if (typeof window !== 'undefined' && window.localStorage) {
919
+ localStorage.removeItem('userData');
920
+ }
921
+ }
922
+ /**
923
+ * 检查用户是否已登录
924
+ * @returns 是否已登录
925
+ */
926
+ isLoggedIn() {
927
+ const userData = this.getUserData();
928
+ return userData !== null && userData.token !== undefined;
929
+ }
930
+ /**
931
+ * 获取当前登录用户的token
932
+ * @returns token或null
933
+ */
934
+ getToken() {
935
+ const userData = this.getUserData();
936
+ return userData?.token || null;
937
+ }
938
+ /**
939
+ * 获取当前登录用户信息
940
+ * @returns 用户信息或null
941
+ */
942
+ getCurrentUser() {
943
+ const userData = this.getUserData();
944
+ if (userData) {
945
+ const { token, ...userInfo } = userData;
946
+ return userInfo;
947
+ }
948
+ return null;
949
+ }
950
+ /**
951
+ * 用户登出
952
+ */
953
+ logout() {
954
+ this.clearUserData();
955
+ }
956
+ /**
957
+ * 记住用户登录信息
958
+ * @param username 用户名
959
+ * @param password 密码
960
+ */
961
+ rememberLogin(username, password) {
962
+ if (typeof window !== 'undefined' && window.localStorage) {
963
+ localStorage.setItem('rememberedUsername', username);
964
+ localStorage.setItem('rememberedPassword', password);
965
+ }
966
+ }
967
+ /**
968
+ * 获取记住的登录信息
969
+ * @returns 记住的登录信息
970
+ */
971
+ getRememberedLogin() {
972
+ if (typeof window !== 'undefined' && window.localStorage) {
973
+ const username = localStorage.getItem('rememberedUsername');
974
+ const password = localStorage.getItem('rememberedPassword');
975
+ if (username && password) {
976
+ return { username, password };
977
+ }
978
+ }
979
+ return null;
980
+ }
981
+ /**
982
+ * 清除记住的登录信息
983
+ */
984
+ clearRememberedLogin() {
985
+ if (typeof window !== 'undefined' && window.localStorage) {
986
+ localStorage.removeItem('rememberedUsername');
987
+ localStorage.removeItem('rememberedPassword');
988
+ }
989
+ }
990
+ }
991
+
832
992
  /**
833
993
  * GT6 SDK 主类
834
- * 提供文章、产品和设置管理相关的所有功能
994
+ * 提供文章、产品、设置、表单和用户管理相关的所有功能
835
995
  */
836
996
  class GT6SDK {
837
997
  constructor(config) {
@@ -840,6 +1000,7 @@ class GT6SDK {
840
1000
  this.products = new ProductsAPI(client);
841
1001
  this.settings = new SettingsAPI(client);
842
1002
  this.forms = new FormsAPI(client);
1003
+ this.users = new UsersAPI(client);
843
1004
  }
844
1005
  /**
845
1006
  * 获取客户端实例(用于高级用法)
@@ -1021,7 +1182,73 @@ class GT6SDK {
1021
1182
  async submitCustomForm(aliases, formData, emailField) {
1022
1183
  return this.forms.submitCustomForm(aliases, formData, emailField);
1023
1184
  }
1185
+ /**
1186
+ * 28. 便捷方法:用户登录
1187
+ */
1188
+ async login(username, password, platformId) {
1189
+ return this.users.login({ username, password, platformId });
1190
+ }
1191
+ /**
1192
+ * 29. 便捷方法:用户注册
1193
+ */
1194
+ async register(username, password, platformId, ibcode) {
1195
+ return this.users.register({ username, password, platformId, ibcode });
1196
+ }
1197
+ /**
1198
+ * 30. 便捷方法:检查用户是否已登录
1199
+ */
1200
+ isLoggedIn() {
1201
+ return this.users.isLoggedIn();
1202
+ }
1203
+ /**
1204
+ * 31. 便捷方法:获取当前登录用户信息
1205
+ */
1206
+ getCurrentUser() {
1207
+ return this.users.getCurrentUser();
1208
+ }
1209
+ /**
1210
+ * 32. 便捷方法:获取当前登录用户的token
1211
+ */
1212
+ getToken() {
1213
+ return this.users.getToken();
1214
+ }
1215
+ /**
1216
+ * 33. 便捷方法:用户登出
1217
+ */
1218
+ logout() {
1219
+ return this.users.logout();
1220
+ }
1221
+ /**
1222
+ * 34. 便捷方法:保存用户数据
1223
+ */
1224
+ saveUserData(userData) {
1225
+ return this.users.saveUserData(userData);
1226
+ }
1227
+ /**
1228
+ * 35. 便捷方法:获取用户数据
1229
+ */
1230
+ getUserData() {
1231
+ return this.users.getUserData();
1232
+ }
1233
+ /**
1234
+ * 36. 便捷方法:记住登录信息
1235
+ */
1236
+ rememberLogin(username, password) {
1237
+ return this.users.rememberLogin(username, password);
1238
+ }
1239
+ /**
1240
+ * 37. 便捷方法:获取记住的登录信息
1241
+ */
1242
+ getRememberedLogin() {
1243
+ return this.users.getRememberedLogin();
1244
+ }
1245
+ /**
1246
+ * 38. 便捷方法:清除记住的登录信息
1247
+ */
1248
+ clearRememberedLogin() {
1249
+ return this.users.clearRememberedLogin();
1250
+ }
1024
1251
  }
1025
1252
 
1026
- export { ArticlesAPI, FormsAPI, GT6Client, GT6Error, GT6SDK, ProductsAPI, SettingsAPI, GT6SDK as default };
1253
+ export { ArticlesAPI, FormsAPI, GT6Client, GT6Error, GT6SDK, ProductsAPI, SettingsAPI, UsersAPI, GT6SDK as default };
1027
1254
  //# sourceMappingURL=gt6-sdk.esm.js.map