select_clinic.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import 'dart:io';
  2. import 'package:eitc_erm_app/utils/Component.dart';
  3. import 'package:eitc_erm_app/utils/Constants.dart';
  4. import 'package:eitc_erm_app/utils/Utils.dart';
  5. import 'package:eitc_erm_app/utils/logger.dart';
  6. import 'package:eitc_erm_app/widget/line.dart';
  7. import 'package:eitc_erm_app/widget/loading.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:http/http.dart' as http;
  10. import 'bean/clinic_list.dart';
  11. import 'bean/synchronize_data.dart';
  12. /*void main() => runApp(DoctorTalkList());*/
  13. class SelectClinic extends StatelessWidget {
  14. DateTime now = DateTime.now();
  15. ValueNotifier<dynamic> result = ValueNotifier(null);
  16. late Future<ClinicList?> _future = fetchData();
  17. String mFrom = "";
  18. SelectClinic(String from) {
  19. mFrom = from;
  20. }
  21. Future<ClinicList?> fetchData() async {
  22. String url = '${Global.BaseUrl}api/clinicList';
  23. final response = await http.get(Uri.parse(url), headers: jsonHeaders());
  24. logd(url);
  25. if (response.statusCode == 200) {
  26. ClinicList mClinicList =
  27. ClinicList.fromJson(decodeBodyToJson(response.bodyBytes));
  28. if (mClinicList.code == Global.responseSuccessCode) {
  29. } else {
  30. Component.toast(mClinicList.msg.toString(), 0);
  31. }
  32. return mClinicList;
  33. } else {
  34. Component.toast("出错了,请稍后再试!", 0);
  35. }
  36. }
  37. Future<SynchronizeData?> synchronizeData(BuildContext context) async {
  38. String url =
  39. '${Global.BaseUrl}api/synchronizeData?id=${Global.userId}&identificationCard=${Global.userIdentificationCard}&phoneNumber=${Global.loginPhoneNo}';
  40. final response = await http.get(Uri.parse(url), headers: jsonHeaders());
  41. logd(url);
  42. if (response.statusCode == 200) {
  43. final json = decodeBodyToJson(response.bodyBytes);
  44. logd("同步数据结果=$json");
  45. SynchronizeData mSynchronizeData = SynchronizeData.fromJson(json);
  46. if (mSynchronizeData.code == Global.responseSuccessCode) {
  47. Global.token = mSynchronizeData.data!.token.toString();
  48. await Utils.saveData("token", Global.token);
  49. Navigator.pushReplacementNamed(context, '/home');
  50. } else {
  51. Component.toast(mSynchronizeData.msg.toString(), 0);
  52. }
  53. return mSynchronizeData;
  54. } else {
  55. Component.toast("出错了,请稍后再试!", 0);
  56. }
  57. }
  58. @override
  59. Widget build(BuildContext context) {
  60. return Scaffold(
  61. appBar: new AppBar(
  62. title: new Text('请选择您需要挂号的医院/诊所',
  63. style: TextStyle(
  64. color: Colors.white,
  65. )),
  66. centerTitle: true,
  67. elevation: 0.5,
  68. backgroundColor: Global.StatusBarColor,
  69. ),
  70. body: Column(children: [
  71. Expanded(
  72. child: FutureBuilder<ClinicList?>(
  73. future: _future,
  74. builder: (context, snapshot) {
  75. if (snapshot.hasData) {
  76. ClinicList? data = snapshot.data;
  77. return ListView.builder(
  78. shrinkWrap: true,
  79. itemCount: data?.data?.length,
  80. itemBuilder: (context, index) {
  81. return Center(
  82. child: ListTile(
  83. shape: const RoundedRectangleBorder(
  84. borderRadius: BorderRadius.only(
  85. topLeft: Radius.circular(5),
  86. topRight: Radius.circular(5),
  87. bottomRight: Radius.circular(5),
  88. bottomLeft: Radius.circular(5))),
  89. tileColor: Colors.white,
  90. iconColor: Colors.white,
  91. title: Text(
  92. '${data!.data?[index].clinicName}',
  93. textAlign: TextAlign.center,
  94. style:
  95. const TextStyle(fontSize: 18, color: Colors.black),
  96. ),
  97. subtitle: Line(),
  98. onTap: () async {
  99. Global.clinic = data.data![index].clinicName!;
  100. Global.RootUrl = data.data![index].clinicIp!;
  101. Global.Port = data.data![index].serverPost!;
  102. Global.ChatPort = data.data![index].webSocketPost!;
  103. //诊所ID
  104. String clinicId = await Utils.readData("clinicId");
  105. Utils.saveData("clinicId", data.data![index].id!);
  106. String token = await Utils.readData("token");
  107. if (mFrom == "fromHome") {
  108. synchronizeData(context);
  109. } else if (token.isNotEmpty) {
  110. Global.token = token;
  111. Global.userId = await Utils.readData("userId");
  112. //ID与保存的ID不同就同步数据
  113. if (clinicId != data.data![index].id!) {
  114. Global.userIdentificationCard =
  115. await Utils.readData("userIdentificationCard");
  116. Global.loginPhoneNo =
  117. await Utils.readData("loginPhoneNo");
  118. //保存的身份证和手机号为空跳转到登录
  119. if (Global.userIdentificationCard.isEmpty ||
  120. Global.loginPhoneNo.isEmpty) {
  121. Navigator.pushReplacementNamed(context, '/login');
  122. return;
  123. }
  124. synchronizeData(context);
  125. return;
  126. }
  127. logd("2222 " + Global.userId);
  128. Navigator.pushReplacementNamed(context, '/home');
  129. }
  130. //医生没有同步数据的逻辑,所以不保存token每次都登录
  131. /*else if ((await Utils.readData("doctorToken"))
  132. .isNotEmpty) {
  133. Global.token = await Utils.readData("doctorToken");
  134. Navigator.pushReplacementNamed(context, '/doctor');
  135. }*/
  136. else
  137. Navigator.pushReplacementNamed(context, '/login');
  138. },
  139. ));
  140. },
  141. );
  142. } else if (snapshot.hasError) {
  143. return Text('Error: ${snapshot.error}');
  144. }
  145. return const ColorLoader();
  146. },
  147. ),
  148. ),
  149. ]),
  150. );
  151. }
  152. }