UserView.dart 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:get_it/get_it.dart';
  6. import 'package:sprintf/sprintf.dart';
  7. import 'package:ustest/ConsultationList.dart';
  8. import 'package:ustest/Services/UserService.dart';
  9. import 'package:http/http.dart' as http;
  10. class UserView extends StatelessWidget {
  11. const UserView();
  12. @override
  13. Widget build(BuildContext context) {
  14. var service = GetIt.instance.get<UserService>();
  15. var user = service.getCurrentUser();
  16. if (user == null) {
  17. print("user is null");
  18. _showSignInScreen(context);
  19. }
  20. var userName = user == null ? "" : user.userName;
  21. var accessToken = user == null ? "" : user.accessToken;
  22. return Scaffold(
  23. body: Center(
  24. child: Column(
  25. children: [
  26. Padding(
  27. padding: EdgeInsets.all(8.0),
  28. child: Row(
  29. children: [
  30. user == null
  31. ? TextButton(
  32. child: const Text("Sign in"),
  33. onPressed: () => _showSignInScreen(context))
  34. : Text(sprintf(
  35. 'Name:%s, Token:%s', [userName, accessToken])),
  36. TextButton.icon(
  37. onPressed: () => onLogout(context),
  38. icon: Icon(Icons.logout),
  39. label: Text("注銷"))
  40. ],
  41. ),
  42. ),
  43. Row(
  44. children: [
  45. TextButton.icon(
  46. onPressed: () => onApplyConsultation(context),
  47. icon: Icon(Icons.add),
  48. label: Text("Apply")),
  49. ],
  50. ),
  51. Expanded(
  52. child: Center(
  53. child: FutureBuilder<List<Consultation>>(
  54. future: fetchPatients(),
  55. builder: (context, snapshot) {
  56. if (snapshot.hasError) {
  57. return const Center(
  58. child: Text('An error has occurred!'),
  59. );
  60. } else if (snapshot.hasData) {
  61. return ConsultationList(
  62. token: accessToken, patients: snapshot.data!);
  63. } else {
  64. return const Center(
  65. child: CircularProgressIndicator(),
  66. );
  67. }
  68. },
  69. ),
  70. )),
  71. StreamBuilder(
  72. stream: service.Channel.stream,
  73. builder: (context, snapshot) {
  74. var message = "";
  75. // var message =
  76. // snapshot.hasData ? '${snapshot.data}' : 'no message';
  77. if (snapshot.hasData) {
  78. var uint8Array =
  79. Uint8List.fromList(snapshot.data as List<int>);
  80. var byteData = uint8Array.buffer.asByteData();
  81. var byteDataLength = byteData.lengthInBytes;
  82. var messageConentList =
  83. Uint8List.view(uint8Array.buffer, 0, byteDataLength);
  84. var messageConentLength = messageConentList.length;
  85. var messageConent = Uint8List.fromList(messageConentList);
  86. var messageText = Utf8Decoder().convert(messageConent);
  87. Map<String, dynamic> messageObject = jsonDecode(messageText);
  88. //通知类型区分
  89. if (messageObject["NotificationType"] as int ==
  90. NotificationTypeEnum.ConnectionNotification.index) {
  91. var connectionNotification =
  92. ConnectionNotification.fromJson(messageObject);
  93. print(
  94. "connectionNotification.NotificationType:${connectionNotification.notificationType}");
  95. } else if (messageObject["NotificationType"] as int ==
  96. NotificationTypeEnum.DisconnectNotification.index) {
  97. var disconnectNotification =
  98. DisconnectNotification.fromJson(messageObject);
  99. print(
  100. "disconnectNotification.NotificationType:${disconnectNotification.notificationType}");
  101. } else if (messageObject["NotificationType"] as int ==
  102. NotificationTypeEnum.FinishNotifyRecordsMessage.index) {
  103. var disconnectNotification =
  104. FinishNotifyRecordsMessage.fromJson(messageObject);
  105. print(
  106. "FinishNotifyRecordsMessage.NotificationType:${disconnectNotification.notificationType}, ${disconnectNotification.codes}");
  107. }
  108. message = messageText;
  109. }
  110. return new Padding(
  111. padding: const EdgeInsets.symmetric(vertical: 24.0),
  112. child: new Text(message),
  113. );
  114. },
  115. )
  116. ],
  117. ),
  118. ),
  119. );
  120. }
  121. void _showSignInScreen(BuildContext context) {
  122. Future.delayed(Duration.zero, () {
  123. Navigator.of(context).pushNamed('/signin');
  124. });
  125. }
  126. Future<List<Consultation>> fetchPatients() async {
  127. var list = <Consultation>[];
  128. try {
  129. var service = GetIt.instance.get<UserService>();
  130. var user = service.getCurrentUser() as User;
  131. var client = http.Client();
  132. var body = sprintf(
  133. '{"jsonrpc": "2.0", "method": "FindPatients", "params": [{"Token": "%s", "PageIndex": 1, "PageSize": 10}], "id": 1 }',
  134. [user.accessToken]);
  135. print('QueryExam http.Client()' + body);
  136. final response = await client.post(
  137. Uri.parse('http://192.168.6.80:8303/IPatientService'),
  138. body: body);
  139. print('QueryExam response.body' + response.body);
  140. final parsed = jsonDecode(response.body);
  141. var datas = parsed['result']['PageData'];
  142. var list = datas
  143. .map<Consultation>((json) => Consultation.fromJson(json))
  144. .toList();
  145. return list;
  146. } catch (ex) {
  147. print('QueryExam.to ex' + ex.toString());
  148. return list;
  149. }
  150. }
  151. void onLogout(context) {
  152. var service = GetIt.instance.get<UserService>();
  153. service.logout();
  154. Navigator.of(context).pushNamed('/signin');
  155. }
  156. void onApplyConsultation(context) {
  157. print("onApplyConsultation click");
  158. Navigator.of(context).pushNamed('/applyconsultation');
  159. }
  160. }
  161. enum NotificationTypeEnum {
  162. Daiding,
  163. /// <summary>
  164. /// ChatMsgNotification|1| 聊天通知
  165. /// </summary>
  166. ChatMsgNotification,
  167. /// <summary>
  168. /// UpgradeVersionNotification|2| 版本更新通知
  169. /// </summary>
  170. UpgradeVersionNotification,
  171. /// <summary>
  172. /// LogoffNotification|3| 登出通知
  173. /// </summary>
  174. LogoffNotification,
  175. /// <summary>
  176. /// DisconnectNotification| 4|与服务器断开连接通知
  177. /// </summary>
  178. DisconnectNotification,
  179. /// <summary>
  180. /// ConnectionNotification| 5| 与服务器已连接通知
  181. /// </summary>
  182. ConnectionNotification,
  183. /// <summary>
  184. ///FinishNotifyRecordsMessage| 6 | 检查记录完成消息类型
  185. /// </summary>
  186. FinishNotifyRecordsMessage,
  187. /// <summary>
  188. ///InvitedEnterRoomNotification| 7 | 邀请用户加入房间通知
  189. /// </summary>
  190. InvitedEnterRoomNotification,
  191. /// <summary>
  192. ///CancelInvitedEnterRoomNotification| 8 |取消邀请用户通知
  193. /// </summary>
  194. CancelInvitedEnterRoomNotification,
  195. }
  196. class ConnectionNotification {
  197. NotificationTypeEnum notificationType;
  198. ConnectionNotification({
  199. this.notificationType = NotificationTypeEnum.Daiding,
  200. });
  201. factory ConnectionNotification.fromJson(Map<String, dynamic> map) {
  202. return ConnectionNotification(
  203. notificationType: NotificationTypeEnum.values
  204. .firstWhere((e) => e.index == map['NotificationType']),
  205. );
  206. }
  207. }
  208. class DisconnectNotification {
  209. NotificationTypeEnum notificationType;
  210. DisconnectNotification({
  211. this.notificationType = NotificationTypeEnum.Daiding,
  212. });
  213. factory DisconnectNotification.fromJson(Map<String, dynamic> map) {
  214. return DisconnectNotification(
  215. notificationType: NotificationTypeEnum.values
  216. .firstWhere((e) => e.index == map['NotificationType']),
  217. );
  218. }
  219. }
  220. class FinishNotifyRecordsMessage {
  221. NotificationTypeEnum notificationType;
  222. List<String>? codes;
  223. FinishNotifyRecordsMessage(
  224. {this.notificationType = NotificationTypeEnum.Daiding, this.codes});
  225. factory FinishNotifyRecordsMessage.fromJson(Map<String, dynamic> map) {
  226. return FinishNotifyRecordsMessage(
  227. notificationType: NotificationTypeEnum.values
  228. .firstWhere((e) => e.index == map['NotificationType']),
  229. codes: map['Codes'] != null ? map['Codes'].cast<String>().toList() : null,
  230. );
  231. }
  232. }