Constants.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'dart:typed_data';
  4. import 'package:encrypt/encrypt.dart' as encrpyt;
  5. import 'package:flutter/material.dart';
  6. import '../bean/doctor_list.dart';
  7. import '../bean/login_entity.dart';
  8. import '../bean/user_list.dart';
  9. class Global {
  10. static const BackgroundColor = Color.fromARGB(0xff, 0xef, 0xef, 0xef);
  11. static const StatusBarColor = Colors.blueAccent;
  12. static String RootUrl = '39.105.121.97';
  13. // static const String RootUrl = '192.168.3.142';
  14. static String Port = '8080';
  15. static String get BaseUrl => 'http://$RootUrl:$Port/api/app/';
  16. static String get ImageUrl => 'http://$RootUrl:$Port/api';
  17. static String ChatPort = '8002';
  18. static String get SocketUrl => 'ws://$RootUrl:$ChatPort/ws';
  19. static const int responseSuccessCode = 200;
  20. static String token = "";
  21. static String clinic = "";
  22. static String loginPhoneNo = "";
  23. static String appointmentPersonId = "";
  24. static String userId = "";
  25. static String userIdentificationCard = "";
  26. static UserListEntity patient = UserListEntity();
  27. static int selectPatient = -1;
  28. static DoctorListEntity doctor = DoctorListEntity();
  29. static int selectDoctor = -1;
  30. static LoginEntity user = LoginEntity();
  31. }
  32. ///json请求头
  33. Map<String, String> jsonHeaders(
  34. {bool withToken = false, Map<String, String>? subHeaders}) {
  35. Map<String, String> headers = {
  36. HttpHeaders.contentTypeHeader: "application/json; charset=utf-8"
  37. };
  38. if (withToken) {
  39. headers["token"] = Global.token;
  40. }
  41. if (subHeaders != null) {
  42. headers.addAll(subHeaders);
  43. }
  44. return headers;
  45. }
  46. final _key = encrpyt.Key.fromUtf8("1234567890123456");
  47. final _iv = encrpyt.IV.fromUtf8("1234567890123456");
  48. final _encrypter = encrpyt.Encrypter(
  49. encrpyt.AES(_key, mode: encrpyt.AESMode.cbc, padding: "PKCS7"));
  50. ///AES加密,返回base64
  51. String aesEncrypt(String str) {
  52. var encrypted = _encrypter.encrypt(str, iv: _iv);
  53. return encrypted.base64;
  54. }
  55. ///AES解密,使用base64
  56. String aesDecrypt(String str) {
  57. var encrptyed = encrpyt.Encrypted.from64(str);
  58. return _encrypter.decrypt(encrptyed, iv: _iv);
  59. }
  60. ///解密响应body,只支持字符串和Uint8List类型
  61. String decodeBody(dynamic body) {
  62. assert((body is String || body is Uint8List), "只支持字符串和Uint8List类型");
  63. String decodeStr(String toDecode) {
  64. if (toDecode.startsWith('"') && toDecode.endsWith('"')) {
  65. return aesDecrypt(jsonDecode(toDecode));
  66. }
  67. return aesDecrypt(toDecode);
  68. }
  69. if (body is String) {
  70. return decodeStr(body);
  71. }
  72. if (body is Uint8List) {
  73. return decodeStr(utf8.decode(body as List<int>));
  74. }
  75. return "";
  76. }
  77. ///解密响应body到json对象,只支持字符串和Uint8List类型
  78. Map<String, dynamic> decodeBodyToJson(dynamic body) {
  79. String decoded = decodeBody(body);
  80. //服务器返回的内容解密出来会多点东西,导致jsondecode异常,这里处理掉
  81. decoded = decoded.replaceAll(RegExp(r'\0'), "");
  82. return jsonDecode(decoded);
  83. }
  84. ///加密请求body
  85. String encodeBody(dynamic body) {
  86. if (body is String) {
  87. return aesEncrypt(body);
  88. } else if (body is Map<String, dynamic>) {
  89. return aesEncrypt(jsonEncode(body));
  90. }
  91. return body;
  92. }