import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; import 'package:encrypt/encrypt.dart' as encrpyt; import 'package:flutter/material.dart'; import '../bean/doctor_list.dart'; import '../bean/login_entity.dart'; import '../bean/user_list.dart'; class Global { static const BackgroundColor = Color.fromARGB(0xff, 0xef, 0xef, 0xef); static const StatusBarColor = Colors.blueAccent; static String RootUrl = '39.105.121.97'; // static const String RootUrl = '192.168.3.142'; static String Port = '8080'; static String get BaseUrl => 'http://$RootUrl:$Port/api/app/'; static String get ImageUrl => 'http://$RootUrl:$Port/api'; static String ChatPort = '8002'; static String get SocketUrl => 'ws://$RootUrl:$ChatPort/ws'; static const int responseSuccessCode = 200; static String token = ""; static String clinic = ""; static String loginPhoneNo = ""; static String appointmentPersonId = ""; static String userId = ""; static String userIdentificationCard = ""; static UserListEntity patient = UserListEntity(); static int selectPatient = -1; static DoctorListEntity doctor = DoctorListEntity(); static int selectDoctor = -1; static LoginEntity user = LoginEntity(); } ///json请求头 Map jsonHeaders( {bool withToken = false, Map? subHeaders}) { Map headers = { HttpHeaders.contentTypeHeader: "application/json; charset=utf-8" }; if (withToken) { headers["token"] = Global.token; } if (subHeaders != null) { headers.addAll(subHeaders); } return headers; } final _key = encrpyt.Key.fromUtf8("1234567890123456"); final _iv = encrpyt.IV.fromUtf8("1234567890123456"); final _encrypter = encrpyt.Encrypter( encrpyt.AES(_key, mode: encrpyt.AESMode.cbc, padding: "PKCS7")); ///AES加密,返回base64 String aesEncrypt(String str) { var encrypted = _encrypter.encrypt(str, iv: _iv); return encrypted.base64; } ///AES解密,使用base64 String aesDecrypt(String str) { var encrptyed = encrpyt.Encrypted.from64(str); return _encrypter.decrypt(encrptyed, iv: _iv); } ///解密响应body,只支持字符串和Uint8List类型 String decodeBody(dynamic body) { assert((body is String || body is Uint8List), "只支持字符串和Uint8List类型"); String decodeStr(String toDecode) { if (toDecode.startsWith('"') && toDecode.endsWith('"')) { return aesDecrypt(jsonDecode(toDecode)); } return aesDecrypt(toDecode); } if (body is String) { return decodeStr(body); } if (body is Uint8List) { return decodeStr(utf8.decode(body as List)); } return ""; } ///解密响应body到json对象,只支持字符串和Uint8List类型 Map decodeBodyToJson(dynamic body) { String decoded = decodeBody(body); //服务器返回的内容解密出来会多点东西,导致jsondecode异常,这里处理掉 decoded = decoded.replaceAll(RegExp(r'\0'), ""); return jsonDecode(decoded); } ///加密请求body String encodeBody(dynamic body) { if (body is String) { return aesEncrypt(body); } else if (body is Map) { return aesEncrypt(jsonEncode(body)); } return body; }