UserService.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. logout() {
  39. this.storage.deleteItem(UserStroageKey);
  40. }
  41. Future<bool> signInAsync(
  42. String host, String userName, String password) async {
  43. AppSettings.host = host;
  44. var client = http.Client();
  45. var body = sprintf(
  46. '{"jsonrpc": "2.0", "method": "CommonLoginAsync", "params": [{"AnyAccount": "%s", "AnyCode": "", "Password": "%s" }], "id": 1 }',
  47. [userName, password]);
  48. var response = await client
  49. .post(Uri.parse(AppSettings.host + '/ILoginService'), body: body);
  50. print('response.body' + response.body);
  51. var parsed = jsonDecode(response.body);
  52. var token = parsed['result']['Token'];
  53. if (token != null) {
  54. body = sprintf(
  55. '{"jsonrpc": "2.0", "method": "GetUserInfoAsync", "params": [{"Token": "%s" }], "id": 1 }',
  56. [token]);
  57. response = await client
  58. .post(Uri.parse(AppSettings.host + '/IUserService'), body: body);
  59. print('GetUserInfoAsync response.body' + response.body);
  60. var parsed = jsonDecode(response.body);
  61. if (parsed != null) {
  62. var organizationCode = parsed['result']['OrganizationCode'];
  63. var user = new User(
  64. userName: userName,
  65. accessToken: token,
  66. organizationCode: organizationCode);
  67. var jsonUser = user.toJson();
  68. this.storage.setItem(UserStroageKey, jsonUser);
  69. var userAgenter = UserAgentClient(client, token);
  70. print('jsonUser:' + jsonUser.toString());
  71. var url = Uri.parse(AppSettings.host);
  72. print("object" + url.host);
  73. Channel = WebSocketChannel.connect(
  74. Uri.parse('ws://192.168.6.80:9301?token=${token}'),
  75. );
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. dynamic post(
  82. http.Client client, String interface, String method, String args) async {
  83. final body = sprintf(
  84. '{"jsonrpc": "2.0", "method": "GetUserInfoAsync", "params": %s, "id": 1 }',
  85. args);
  86. final response = await client
  87. .post(Uri.parse(AppSettings.host + '/$interface'), body: body);
  88. print('GetUserInfoAsync response.body' + response.body);
  89. var parsed = jsonDecode(response.body);
  90. }
  91. }
  92. class UserAgentClient extends http.BaseClient {
  93. //final String userAgent;
  94. final http.Client _inner;
  95. final String token;
  96. late Timer? _timer;
  97. UserAgentClient(this._inner, this.token) {
  98. //cancelTimer();
  99. final Duration duration = Duration(seconds: 300);
  100. _timer = Timer(duration, () => Run());
  101. }
  102. Future<http.StreamedResponse> send(http.BaseRequest request) {
  103. //request.headers['user-agent'] = userAgent;
  104. return _inner.send(request);
  105. }
  106. void Run() {
  107. //cancelTimer();
  108. var request =
  109. http.Request('POST', Uri.parse(AppSettings.host + '/IUserService'));
  110. request.body =
  111. '{"jsonrpc": "2.0", "method": "HeartRateAsync", "params": [{"Token": "$token" }], "id": 1 }';
  112. var response = send(request);
  113. response.asStream().listen((event) {
  114. print('heartrate response:${event.toString()}');
  115. });
  116. response.then(
  117. (value) => {print('heartrate result:' + value.stream.toString())});
  118. //var parsed = jsonDecode(response.then((value) => {print(value)}));
  119. //return parsed['result'] as bool;
  120. }
  121. void cancelTimer() {
  122. _timer?.cancel();
  123. }
  124. }
  125. class JsonRpcResult {}
  126. class User {
  127. final String userName;
  128. final String accessToken;
  129. final String organizationCode;
  130. User(
  131. {required this.userName,
  132. required this.accessToken,
  133. required this.organizationCode});
  134. factory User.fromJson(Map<String, dynamic> json) {
  135. return User(
  136. userName: json['userName'] as String,
  137. accessToken: json['accessToken'] as String,
  138. organizationCode: json['organizationCode'] as String,
  139. );
  140. }
  141. Map<String, dynamic> toJson() => {
  142. "userName": userName,
  143. "accessToken": accessToken,
  144. "organizationCode": organizationCode,
  145. };
  146. }