http.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:dio/adapter.dart';
  4. import 'package:dio/dio.dart';
  5. import 'constants.dart';
  6. import '../utils/index.dart';
  7. import '../utils/cache.dart';
  8. import '../models/index.dart';
  9. class Http {
  10. static Dio _dio = new Dio(BaseOptions(
  11. sendTimeout: 3500,
  12. ));
  13. static init() {
  14. var host = Cache.get("server");
  15. var type = Cache.get("server_type");
  16. if (type == null || host == null) return;
  17. _dio.options.baseUrl = type + host + "/";
  18. print("current server: ${_dio.options.baseUrl}");
  19. (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
  20. (client) {
  21. client.badCertificateCallback =
  22. (X509Certificate cert, String host, int port) {
  23. return true;
  24. };
  25. };
  26. }
  27. static Future get(String api) async {
  28. try {
  29. return await _process(await _dio.get(api));
  30. } on DioError catch (err) {
  31. print(err.message);
  32. throw NetError(err.message);
  33. }
  34. }
  35. static Future post(String api, { Map<String, dynamic> data }) async {
  36. try {
  37. return await _process(await _dio.post(api, data: FormData.fromMap(data)));
  38. } on DioError catch (err) {
  39. print(err.message);
  40. throw NetError(err.message);
  41. }
  42. }
  43. static Future<dynamic> _process(dynamic response) async {
  44. var resp = HttpResponse.fromJson(response.data);
  45. if (resp.code != 200) {
  46. throw NetError(resp.msg);
  47. }
  48. return resp.result;
  49. }
  50. static Future<UserInfo> login(Map<String, dynamic> data) async {
  51. data["type"] = "kefu";
  52. var response = await post("check", data: data);
  53. String token = response["token"];
  54. String refToken = response["refToken"];
  55. _dio.options.headers["token"] = token;
  56. Cache.set("token", token);
  57. Cache.set("refToken", refToken);
  58. print("login ok");
  59. return await auth();
  60. }
  61. static Future<UserInfo> autoLogin() async {
  62. var token = Cache.get("token");
  63. if (token == null) {
  64. throw NetError("no token");
  65. }
  66. print("auto login");
  67. _dio.options.headers["token"] = token;
  68. return await auth();
  69. }
  70. static Future<UserInfo> auth() async {
  71. await post("check_auth");
  72. print("auth ok");
  73. return await info();
  74. }
  75. static Future<UserInfo> info() async {
  76. var response = await get("kefuinfo");
  77. var customer = UserInfo.fromJson(response);
  78. AppData.set("info", customer);
  79. Cache.set("config", json.encode(customer));
  80. print("info ok");
  81. return customer;
  82. }
  83. static Future<List<HttpMessage>> getHistory(String id) async {
  84. var response = await get("messages?visitorId=$id&kefuId=${Cache.get("username")}");
  85. List<HttpMessage> messages = new List<HttpMessage>();
  86. response = response as List;
  87. for(var msg in response) {
  88. messages.add(HttpMessage.fromJson(msg));
  89. }
  90. return messages;
  91. }
  92. static Future sendMsg(String to, String msg) async {
  93. var data = {
  94. "type": "kefu",
  95. "from_id": AppData.get("info").id,
  96. "to_id": to,
  97. "content": msg,
  98. };
  99. return await post("message", data: data);
  100. }
  101. }