@gt6/sdk 1.0.8 → 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.
@@ -730,9 +730,272 @@ class SettingsAPI {
730
730
  }
731
731
  }
732
732
 
733
+ class FormsAPI {
734
+ constructor(client) {
735
+ this.client = client;
736
+ }
737
+ /**
738
+ * 通用表单提交方法
739
+ * @param aliases 表单别名(如 'CONTACT', 'FEEDBACK' 等)
740
+ * @param email 用户邮箱
741
+ * @param fieldValue 表单字段值
742
+ * @param options 提交选项
743
+ */
744
+ async submitForm(aliases, email, fieldValue, options) {
745
+ const config = this.client.getConfig();
746
+ const endpoint = options?.endpoint || '/api/platform/table';
747
+ const createSecret = options?.createSecret || 'Cmx349181!';
748
+ const submitData = {
749
+ platformId: config.platformId,
750
+ aliases,
751
+ email,
752
+ fieldValue,
753
+ createSecret
754
+ };
755
+ try {
756
+ const response = await fetch(`${config.baseUrl}${endpoint}`, {
757
+ method: 'POST',
758
+ headers: {
759
+ 'Content-Type': 'application/json',
760
+ },
761
+ body: JSON.stringify(submitData)
762
+ });
763
+ const result = await response.json();
764
+ return {
765
+ success: response.ok && result.success !== false,
766
+ message: result.message || (response.ok ? '提交成功' : '提交失败'),
767
+ data: result.data,
768
+ code: result.code
769
+ };
770
+ }
771
+ catch (error) {
772
+ return {
773
+ success: false,
774
+ message: error instanceof Error ? error.message : '网络错误',
775
+ data: null
776
+ };
777
+ }
778
+ }
779
+ /**
780
+ * 联系表单提交
781
+ * @param formData 联系表单数据
782
+ */
783
+ async submitContactForm(formData) {
784
+ return this.submitForm('CONTACT', formData.email, {
785
+ firstName: formData.firstName,
786
+ lastName: formData.lastName || '',
787
+ phone: formData.phone || '',
788
+ subject: formData.subject,
789
+ message: formData.message
790
+ });
791
+ }
792
+ /**
793
+ * 反馈表单提交
794
+ * @param formData 反馈表单数据
795
+ */
796
+ async submitFeedbackForm(formData) {
797
+ return this.submitForm('FEEDBACK', formData.email, {
798
+ name: formData.name,
799
+ type: formData.type,
800
+ message: formData.message,
801
+ rating: formData.rating || 0
802
+ });
803
+ }
804
+ /**
805
+ * 注册表单提交
806
+ * @param formData 注册表单数据
807
+ */
808
+ async submitRegistrationForm(formData) {
809
+ return this.submitForm('REGISTRATION', formData.email, {
810
+ username: formData.username,
811
+ password: formData.password,
812
+ confirmPassword: formData.confirmPassword,
813
+ agreeTerms: formData.agreeTerms
814
+ });
815
+ }
816
+ /**
817
+ * 自定义表单提交
818
+ * @param aliases 表单别名
819
+ * @param formData 表单数据对象
820
+ * @param emailField 邮箱字段名(默认为 'email')
821
+ */
822
+ async submitCustomForm(aliases, formData, emailField = 'email') {
823
+ const email = formData[emailField];
824
+ if (!email) {
825
+ return {
826
+ success: false,
827
+ message: '邮箱地址是必需的'
828
+ };
829
+ }
830
+ // 移除邮箱字段,避免重复
831
+ const { [emailField]: _, ...fieldValue } = formData;
832
+ return this.submitForm(aliases, email, fieldValue);
833
+ }
834
+ }
835
+
836
+ /**
837
+ * 用户管理API模块
838
+ * 提供用户登录、注册等功能
839
+ */
840
+ class UsersAPI {
841
+ constructor(client) {
842
+ this.client = client;
843
+ }
844
+ /**
845
+ * 用户登录
846
+ * @param loginData 登录数据
847
+ * @returns 登录响应
848
+ */
849
+ async login(loginData) {
850
+ try {
851
+ const response = await this.client.request('/web/user/login', {
852
+ method: 'POST',
853
+ headers: {
854
+ 'Content-Type': 'application/json',
855
+ },
856
+ body: JSON.stringify(loginData)
857
+ });
858
+ return response;
859
+ }
860
+ catch (error) {
861
+ return {
862
+ success: false,
863
+ message: error instanceof Error ? error.message : '登录请求失败'
864
+ };
865
+ }
866
+ }
867
+ /**
868
+ * 用户注册
869
+ * @param registerData 注册数据
870
+ * @returns 注册响应
871
+ */
872
+ async register(registerData) {
873
+ try {
874
+ const response = await this.client.request('/web/user/register', {
875
+ method: 'POST',
876
+ headers: {
877
+ 'Content-Type': 'application/json',
878
+ },
879
+ body: JSON.stringify(registerData)
880
+ });
881
+ return response;
882
+ }
883
+ catch (error) {
884
+ return {
885
+ success: false,
886
+ message: error instanceof Error ? error.message : '注册请求失败'
887
+ };
888
+ }
889
+ }
890
+ /**
891
+ * 保存用户数据到本地存储
892
+ * @param userData 用户数据
893
+ */
894
+ saveUserData(userData) {
895
+ if (typeof window !== 'undefined' && window.localStorage) {
896
+ localStorage.setItem('userData', JSON.stringify(userData));
897
+ }
898
+ }
899
+ /**
900
+ * 从本地存储获取用户数据
901
+ * @returns 用户数据或null
902
+ */
903
+ getUserData() {
904
+ if (typeof window !== 'undefined' && window.localStorage) {
905
+ const userDataStr = localStorage.getItem('userData');
906
+ if (userDataStr) {
907
+ try {
908
+ return JSON.parse(userDataStr);
909
+ }
910
+ catch (error) {
911
+ console.warn('解析用户数据失败:', error);
912
+ return null;
913
+ }
914
+ }
915
+ }
916
+ return null;
917
+ }
918
+ /**
919
+ * 清除本地存储的用户数据
920
+ */
921
+ clearUserData() {
922
+ if (typeof window !== 'undefined' && window.localStorage) {
923
+ localStorage.removeItem('userData');
924
+ }
925
+ }
926
+ /**
927
+ * 检查用户是否已登录
928
+ * @returns 是否已登录
929
+ */
930
+ isLoggedIn() {
931
+ const userData = this.getUserData();
932
+ return userData !== null && userData.token !== undefined;
933
+ }
934
+ /**
935
+ * 获取当前登录用户的token
936
+ * @returns token或null
937
+ */
938
+ getToken() {
939
+ const userData = this.getUserData();
940
+ return userData?.token || null;
941
+ }
942
+ /**
943
+ * 获取当前登录用户信息
944
+ * @returns 用户信息或null
945
+ */
946
+ getCurrentUser() {
947
+ const userData = this.getUserData();
948
+ if (userData) {
949
+ const { token, ...userInfo } = userData;
950
+ return userInfo;
951
+ }
952
+ return null;
953
+ }
954
+ /**
955
+ * 用户登出
956
+ */
957
+ logout() {
958
+ this.clearUserData();
959
+ }
960
+ /**
961
+ * 记住用户登录信息
962
+ * @param username 用户名
963
+ * @param password 密码
964
+ */
965
+ rememberLogin(username, password) {
966
+ if (typeof window !== 'undefined' && window.localStorage) {
967
+ localStorage.setItem('rememberedUsername', username);
968
+ localStorage.setItem('rememberedPassword', password);
969
+ }
970
+ }
971
+ /**
972
+ * 获取记住的登录信息
973
+ * @returns 记住的登录信息
974
+ */
975
+ getRememberedLogin() {
976
+ if (typeof window !== 'undefined' && window.localStorage) {
977
+ const username = localStorage.getItem('rememberedUsername');
978
+ const password = localStorage.getItem('rememberedPassword');
979
+ if (username && password) {
980
+ return { username, password };
981
+ }
982
+ }
983
+ return null;
984
+ }
985
+ /**
986
+ * 清除记住的登录信息
987
+ */
988
+ clearRememberedLogin() {
989
+ if (typeof window !== 'undefined' && window.localStorage) {
990
+ localStorage.removeItem('rememberedUsername');
991
+ localStorage.removeItem('rememberedPassword');
992
+ }
993
+ }
994
+ }
995
+
733
996
  /**
734
997
  * GT6 SDK 主类
735
- * 提供文章、产品和设置管理相关的所有功能
998
+ * 提供文章、产品、设置、表单和用户管理相关的所有功能
736
999
  */
737
1000
  class GT6SDK {
738
1001
  constructor(config) {
@@ -740,6 +1003,8 @@ class GT6SDK {
740
1003
  this.articles = new ArticlesAPI(client);
741
1004
  this.products = new ProductsAPI(client);
742
1005
  this.settings = new SettingsAPI(client);
1006
+ this.forms = new FormsAPI(client);
1007
+ this.users = new UsersAPI(client);
743
1008
  }
744
1009
  /**
745
1010
  * 获取客户端实例(用于高级用法)
@@ -897,13 +1162,105 @@ class GT6SDK {
897
1162
  async getPaymentMethodAttribute(methodId, attrName, paymentAlias) {
898
1163
  return this.products.getPaymentMethodAttribute(methodId, attrName, paymentAlias);
899
1164
  }
1165
+ /**
1166
+ * 24. 便捷方法:通用表单提交
1167
+ */
1168
+ async submitForm(aliases, email, fieldValue, options) {
1169
+ return this.forms.submitForm(aliases, email, fieldValue, options);
1170
+ }
1171
+ /**
1172
+ * 25. 便捷方法:联系表单提交
1173
+ */
1174
+ async submitContactForm(formData) {
1175
+ return this.forms.submitContactForm(formData);
1176
+ }
1177
+ /**
1178
+ * 26. 便捷方法:反馈表单提交
1179
+ */
1180
+ async submitFeedbackForm(formData) {
1181
+ return this.forms.submitFeedbackForm(formData);
1182
+ }
1183
+ /**
1184
+ * 27. 便捷方法:自定义表单提交
1185
+ */
1186
+ async submitCustomForm(aliases, formData, emailField) {
1187
+ return this.forms.submitCustomForm(aliases, formData, emailField);
1188
+ }
1189
+ /**
1190
+ * 28. 便捷方法:用户登录
1191
+ */
1192
+ async login(username, password, platformId) {
1193
+ return this.users.login({ username, password, platformId });
1194
+ }
1195
+ /**
1196
+ * 29. 便捷方法:用户注册
1197
+ */
1198
+ async register(username, password, platformId, ibcode) {
1199
+ return this.users.register({ username, password, platformId, ibcode });
1200
+ }
1201
+ /**
1202
+ * 30. 便捷方法:检查用户是否已登录
1203
+ */
1204
+ isLoggedIn() {
1205
+ return this.users.isLoggedIn();
1206
+ }
1207
+ /**
1208
+ * 31. 便捷方法:获取当前登录用户信息
1209
+ */
1210
+ getCurrentUser() {
1211
+ return this.users.getCurrentUser();
1212
+ }
1213
+ /**
1214
+ * 32. 便捷方法:获取当前登录用户的token
1215
+ */
1216
+ getToken() {
1217
+ return this.users.getToken();
1218
+ }
1219
+ /**
1220
+ * 33. 便捷方法:用户登出
1221
+ */
1222
+ logout() {
1223
+ return this.users.logout();
1224
+ }
1225
+ /**
1226
+ * 34. 便捷方法:保存用户数据
1227
+ */
1228
+ saveUserData(userData) {
1229
+ return this.users.saveUserData(userData);
1230
+ }
1231
+ /**
1232
+ * 35. 便捷方法:获取用户数据
1233
+ */
1234
+ getUserData() {
1235
+ return this.users.getUserData();
1236
+ }
1237
+ /**
1238
+ * 36. 便捷方法:记住登录信息
1239
+ */
1240
+ rememberLogin(username, password) {
1241
+ return this.users.rememberLogin(username, password);
1242
+ }
1243
+ /**
1244
+ * 37. 便捷方法:获取记住的登录信息
1245
+ */
1246
+ getRememberedLogin() {
1247
+ return this.users.getRememberedLogin();
1248
+ }
1249
+ /**
1250
+ * 38. 便捷方法:清除记住的登录信息
1251
+ */
1252
+ clearRememberedLogin() {
1253
+ return this.users.clearRememberedLogin();
1254
+ }
900
1255
  }
901
1256
 
902
1257
  exports.ArticlesAPI = ArticlesAPI;
1258
+ exports.FormsAPI = FormsAPI;
903
1259
  exports.GT6Client = GT6Client;
904
1260
  exports.GT6Error = GT6Error;
905
1261
  exports.GT6SDK = GT6SDK;
906
1262
  exports.ProductsAPI = ProductsAPI;
907
1263
  exports.SettingsAPI = SettingsAPI;
1264
+ exports.UsersAPI = UsersAPI;
908
1265
  exports.default = GT6SDK;
909
1266
  //# sourceMappingURL=gt6-sdk.cjs.js.map