x_organization_assemble_authentication.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import 'package:get/get.dart';
  2. import '../models/index.dart';
  3. import '../utils/index.dart';
  4. ///
  5. ///认证服务
  6. ///
  7. class OrgAuthenticationService extends GetxService {
  8. static OrgAuthenticationService get to => Get.find();
  9. String baseUrl() {
  10. return O2ApiManager.instance.getModuleBaseUrl(
  11. O2DistributeModuleEnum.x_organization_assemble_authentication) ?? '';
  12. }
  13. ///
  14. /// 扫码登录pc
  15. ///
  16. Future<O2Person?> login2Pc(String meta) async {
  17. try {
  18. ApiResponse response = await O2HttpClient.instance.post('${baseUrl()}jaxrs/authentication/bind/meta/$meta', {});
  19. return O2Person.fromJson(response.data);
  20. } catch (err, stackTrace) {
  21. OLogger.e('扫码登录pc失败', err, stackTrace);
  22. }
  23. return null;
  24. }
  25. ///
  26. /// 获取服务器开启的登录模式
  27. ///
  28. Future<LoginModeData?> getLoginMode() async {
  29. try {
  30. ApiResponse response = await O2HttpClient.instance.get('${baseUrl()}jaxrs/authentication/mode', needToken: false);
  31. LoginModeData mode = LoginModeData.fromJson(response.data);
  32. return mode;
  33. } catch (err, stackTrace) {
  34. OLogger.e('获取登录模式失败', err, stackTrace);
  35. }
  36. return null;
  37. }
  38. ///
  39. ///获取图片验证码
  40. ///
  41. Future<CaptchaImgData?> getLoginCaptchaImgData(int width, int height) async {
  42. try {
  43. ApiResponse response = await O2HttpClient.instance.get('${baseUrl()}jaxrs/authentication/captcha/width/$width/height/$height', needToken: false);
  44. CaptchaImgData mode = CaptchaImgData.fromJson(response.data);
  45. return mode;
  46. } catch (err, stackTrace) {
  47. OLogger.e('获取图片验证码失败', err, stackTrace);
  48. }
  49. return null;
  50. }
  51. ///
  52. /// 获取加密的公钥
  53. ///
  54. Future<RSAPublicKeyData?> getRSAPublicKey() async {
  55. try {
  56. ApiResponse response = await O2HttpClient.instance.get('${baseUrl()}jaxrs/authentication/captchaRSAPublicKey', needToken: false);
  57. RSAPublicKeyData mode = RSAPublicKeyData.fromJson(response.data);
  58. return mode;
  59. } catch (err, stackTrace) {
  60. OLogger.e('获取加密的公钥失败', err, stackTrace);
  61. }
  62. return null;
  63. }
  64. ///
  65. /// 登录
  66. ///
  67. Future<O2Person?> login(LoginForm form) async {
  68. try {
  69. ApiResponse response = await O2HttpClient.instance
  70. .post('${baseUrl()}jaxrs/authentication/captcha', form.toJson(), needToken: false);
  71. //更新用户信息和token
  72. O2Person person = O2Person.fromJson(response.data);
  73. await O2ApiManager.instance.setupUser(person);
  74. return person;
  75. } catch (err, stackTrace) {
  76. OLogger.e('登录失败', err, stackTrace);
  77. }
  78. return null;
  79. }
  80. /// 双因素认证第一步
  81. Future<TwoFactorStepOneResponse?> loginTwofactorStepOne(LoginForm form) async {
  82. try {
  83. ApiResponse response = await O2HttpClient.instance
  84. .post('${baseUrl()}jaxrs/authentication/two/factory/login', form.toJson(), needToken: false);
  85. TwoFactorStepOneResponse person = TwoFactorStepOneResponse.fromJson(response.data);
  86. return person;
  87. } catch (err, stackTrace) {
  88. OLogger.e('双因素认证第一步失败', err, stackTrace);
  89. }
  90. return null;
  91. }
  92. ///
  93. ///获取短信验证码
  94. ///
  95. Future<CollectCodeData?> getPhoneCode(String credential) async {
  96. try {
  97. ApiResponse response = await O2HttpClient.instance.get('${baseUrl()}jaxrs/authentication/code/credential/$credential', needToken: false);
  98. CollectCodeData data = CollectCodeData.fromJson(response.data);
  99. return data;
  100. } catch (err, stackTrace) {
  101. OLogger.e('获取验证码失败', err, stackTrace);
  102. }
  103. return null;
  104. }
  105. ///
  106. ///手机验证码登录
  107. ///
  108. Future<O2Person?> loginWithCode(String credential, String codeAnswer) async {
  109. try {
  110. Map<String, String> data = {};
  111. data['credential'] = credential;
  112. data['codeAnswer'] = codeAnswer;
  113. ApiResponse response = await O2HttpClient.instance
  114. .post('${baseUrl()}jaxrs/authentication/code', data, needToken: false);
  115. //更新用户信息和token
  116. O2Person person = O2Person.fromJson(response.data);
  117. await O2ApiManager.instance.setupUser(person);
  118. return person;
  119. } catch (err, stackTrace) {
  120. OLogger.e('登录失败', err, stackTrace);
  121. }
  122. return null;
  123. }
  124. ///
  125. ///检测当前登录状态
  126. Future<O2Person?> who() async {
  127. try {
  128. ApiResponse response =
  129. await O2HttpClient.instance.get('${baseUrl()}jaxrs/authentication');
  130. String token = response.data['token'] as String;
  131. if (token.isNotEmpty) {
  132. //更新用户信息和token
  133. O2Person person = O2Person.fromJson(response.data);
  134. await O2ApiManager.instance.setupUser(person);
  135. return person;
  136. } else {
  137. OLogger.i('错误,token过期!');
  138. return null;
  139. }
  140. } catch (err, stackTrace) {
  141. OLogger.e('当前登录状态已过期', err, stackTrace);
  142. return null;
  143. }
  144. }
  145. Future<IdData?> logout() async {
  146. try {
  147. ApiResponse response = await O2HttpClient.instance.delete('${baseUrl()}jaxrs/authentication');
  148. IdData data = IdData.fromJson(response.data);
  149. return data;
  150. } catch (err, stackTrace) {
  151. OLogger.e('登出失败', err, stackTrace);
  152. return null;
  153. }
  154. }
  155. }