UserService.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:js_util';
  4. import 'package:get_it/get_it.dart';
  5. import 'package:http/http.dart' as http;
  6. import 'package:localstorage/localstorage.dart';
  7. import 'package:sprintf/sprintf.dart';
  8. import 'package:ustest/Services/LocalStorageService.dart';
  9. import 'dart:typed_data';
  10. import 'package:web_socket_channel/web_socket_channel.dart';
  11. import 'AppSettings.dart';
  12. class UserService {
  13. late User currentUser;
  14. final String UserStroageKey = "CurrentUser";
  15. final LocalStorage storage = new LocalStorage('UserStroage');
  16. late WebSocketChannel? Channel = WebSocketChannel.connect(
  17. Uri.parse('ws://192.168.6.80:9301?token=${currentUser.accessToken}'),
  18. ); //TODO
  19. User? getCurrentUser() {
  20. //if (currentUser != null) //TODO workaround
  21. //{
  22. // if (currentUser?.userName != 'notlogin') {
  23. // return currentUser;
  24. // }
  25. //}
  26. // this.storage.deleteItem(UserStroageKey);
  27. var value = this.storage.getItem(UserStroageKey);
  28. if (value != null) {
  29. print('getCurrentUser value:' + value.toString());
  30. currentUser = User.fromJson(value);
  31. } else {
  32. print('getCurrentUser value: null');
  33. currentUser =
  34. new User(userName: 'notlogin', accessToken: '', organizationCode: '');
  35. }
  36. return currentUser;
  37. }
  38. void UpdateCurrentUserDetail() async {
  39. var user = getCurrentUser();
  40. if (user != null) {
  41. var token = user.accessToken;
  42. var body = sprintf(
  43. '{"jsonrpc": "2.0", "method": "GetUserInfoAsync", "params": [{"Token": "%s" }], "id": 1 }',
  44. [token]);
  45. var client = http.Client();
  46. var response = await client
  47. .post(Uri.parse(AppSettings.host + '/IUserService'), body: body);
  48. print('GetUserInfoAsync response.body' + response.body);
  49. var parsed = jsonDecode(response.body);
  50. if (parsed != null) {
  51. user.organizationCode = parsed['result']['OrganizationCode'];
  52. var jsonUser = user.toJson();
  53. this.storage.setItem(UserStroageKey, jsonUser);
  54. var userAgenter = UserAgentClient(client, token);
  55. print('jsonUser:' + jsonUser.toString());
  56. var url = Uri.parse(AppSettings.host);
  57. var wsUrl = 'ws://${url.host}:9301?token=$token';
  58. print("wsUrl---" + wsUrl);
  59. Channel = WebSocketChannel.connect(
  60. Uri.parse(wsUrl),
  61. );
  62. }
  63. }
  64. }
  65. logout() {
  66. this.storage.deleteItem(UserStroageKey);
  67. }
  68. Future<bool> signInAsync(
  69. String host, String userName, String password) async {
  70. AppSettings.host = host;
  71. var client = http.Client();
  72. var body = sprintf(
  73. '{"jsonrpc": "2.0", "method": "CommonLoginAsync", "params": [{"AnyAccount": "%s", "AnyCode": "", "Password": "%s" }], "id": 1 }',
  74. [userName, password]);
  75. var response = await client
  76. .post(Uri.parse(AppSettings.host + '/ILoginService'), body: body);
  77. print('response.body' + response.body);
  78. var parsed = jsonDecode(response.body);
  79. var token = parsed['result']['Token'];
  80. if (token != null) {
  81. var user = new User(
  82. userName: userName, accessToken: token, organizationCode: '');
  83. var jsonUser = user.toJson();
  84. this.storage.setItem(UserStroageKey, jsonUser);
  85. return true;
  86. }
  87. return false;
  88. }
  89. dynamic post(
  90. http.Client client, String interface, String method, String args) async {
  91. final body = sprintf(
  92. '{"jsonrpc": "2.0", "method": "GetUserInfoAsync", "params": %s, "id": 1 }',
  93. args);
  94. final response = await client
  95. .post(Uri.parse(AppSettings.host + '/$interface'), body: body);
  96. print('GetUserInfoAsync response.body' + response.body);
  97. var parsed = jsonDecode(response.body);
  98. }
  99. }
  100. class UserAgentClient extends http.BaseClient {
  101. //final String userAgent;
  102. final http.Client _inner;
  103. final String token;
  104. late Timer? _timer;
  105. UserAgentClient(this._inner, this.token) {
  106. print('heartrate client');
  107. //cancelTimer();
  108. final Duration duration = Duration(seconds: 300);
  109. _timer = Timer(duration, () => Run());
  110. Run();
  111. }
  112. Future<http.StreamedResponse> send(http.BaseRequest request) {
  113. //request.headers['user-agent'] = userAgent;
  114. return _inner.send(request);
  115. }
  116. void Run() {
  117. //cancelTimer();
  118. // var request =
  119. // http.Request('POST', Uri.parse(AppSettings.host + '/IUserService'));
  120. // request.body =
  121. // '{"jsonrpc": "2.0", "method": "HeartRateAsync", "params": [{"Token": "$token" }], "id": 1 }';
  122. print('heartrate send');
  123. //Future<http.StreamedResponse> streamedResponse = send(request);
  124. // Future.sync(() => send(request).then((value) =>
  125. // http.Response.fromStream(value)
  126. // .then((response) => print('heartrate response:${response.body}'))));
  127. Future.sync(() => _inner
  128. .post(Uri.parse(AppSettings.host + '/IUserService'),
  129. body:
  130. '{"jsonrpc": "2.0", "method": "HeartRateAsync", "params": [{"Token": "$token" }], "id": 1 }')
  131. .then((response) => print('heartrate response:${response.body}')));
  132. //var parsed = jsonDecode(response.then((value) => {print(value)}));
  133. //return parsed['result'] as bool;
  134. print('heartrate send done');
  135. }
  136. void cancelTimer() {
  137. _timer?.cancel();
  138. }
  139. }
  140. class JsonRpcResult {}
  141. class User {
  142. final String userName;
  143. final String accessToken;
  144. String organizationCode;
  145. User(
  146. {required this.userName,
  147. required this.accessToken,
  148. required this.organizationCode});
  149. factory User.fromJson(Map<String, dynamic> json) {
  150. return User(
  151. userName: json['userName'] as String,
  152. accessToken: json['accessToken'] as String,
  153. organizationCode: json['organizationCode'] as String,
  154. );
  155. }
  156. Map<String, dynamic> toJson() => {
  157. "userName": userName,
  158. "accessToken": accessToken,
  159. "organizationCode": organizationCode,
  160. };
  161. }