|
| 1 | +import 'dart:_http'; |
| 2 | + |
| 3 | +import 'package:dio/dio.dart'; |
| 4 | +import 'package:gsy_github_app_flutter/common/net/Code.dart'; |
| 5 | + |
| 6 | +import 'dart:collection'; |
| 7 | + |
| 8 | +import 'package:gsy_github_app_flutter/common/config/Config.dart'; |
| 9 | +import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart'; |
| 10 | +import 'package:gsy_github_app_flutter/common/net/ResultData.dart'; |
| 11 | + |
| 12 | +///http请求 |
| 13 | +class HttpManager { |
| 14 | + static const CONTENT_TYPE_JSON = "application/json"; |
| 15 | + static const CONTENT_TYPE_FORM = "application/x-www-form-urlencoded"; |
| 16 | + static Map optionParams = { |
| 17 | + "timeoutMs": 15000, |
| 18 | + "token": null, |
| 19 | + "authorizationCode": null, |
| 20 | + }; |
| 21 | + |
| 22 | + ///发起网络请求 |
| 23 | + ///[ url] 请求url |
| 24 | + ///[ params] 请求参数 |
| 25 | + ///[ header] 外加头 |
| 26 | + ///[ text] 是否text返回 |
| 27 | + ///[ option] 配置 |
| 28 | + static netFetch(url, params, Map<String, String> header, text, Options option) async { |
| 29 | + |
| 30 | + //todo 没有网络 |
| 31 | + /*var isConnected = await NetInfo.isConnected.fetch().done; |
| 32 | + if (!isConnected) { |
| 33 | + return { |
| 34 | + result: false, |
| 35 | + code: Code.NETWORK_ERROR, |
| 36 | + msg: I18n('netError') |
| 37 | + } |
| 38 | + }*/ |
| 39 | + |
| 40 | + Map<String, String> headers = new HashMap(); |
| 41 | + if (header != null) { |
| 42 | + headers.addAll(headers); |
| 43 | + } |
| 44 | + |
| 45 | + //授权码 |
| 46 | + if (optionParams["authorizationCode"] == null) { |
| 47 | + var authorizationCode = await getAuthorization(); |
| 48 | + if (authorizationCode != null) { |
| 49 | + optionParams["authorizationCode"] = authorizationCode; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + headers["Authorization"] = optionParams["authorizationCode"]; |
| 54 | + |
| 55 | + option.headers = headers; |
| 56 | + |
| 57 | + Dio dio = new Dio(); |
| 58 | + Response response = await dio.get(url, data: params, options: option); |
| 59 | + |
| 60 | + if (Config.DEBUG) { |
| 61 | + print('请求url: ' + url); |
| 62 | + if (params != null) { |
| 63 | + print('请求参数: ' + params.toString()); |
| 64 | + } |
| 65 | + if (response != null) { |
| 66 | + print('返回参数: ' + response.toString()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + if (option.contentType != null && option.contentType == ContentType.TEXT) { |
| 72 | + return new ResultData(response.data, true, Code.SUCCESS); |
| 73 | + } else { |
| 74 | + var responseJson = await response.data; |
| 75 | + if (response.statusCode == 201 && responseJson.token != null) { |
| 76 | + optionParams["authorizationCode"] = 'token ' + responseJson.token; |
| 77 | + await LocalStorage.save(Config.TOKEN_KEY, optionParams["authorizationCode"]); |
| 78 | + } |
| 79 | + } |
| 80 | + if (response.statusCode == 200 || response.statusCode == 201) { |
| 81 | + return new ResultData(response.data, true, Code.SUCCESS, headers: response.headers); |
| 82 | + } |
| 83 | + } catch (e) { |
| 84 | + print(e.toString() + url); |
| 85 | + return new ResultData(response.data, false, response.statusCode, headers: response.headers); |
| 86 | + } |
| 87 | + return new ResultData(Code.errorHandleFunction(response.statusCode), false, response.statusCode); |
| 88 | + } |
| 89 | + |
| 90 | + ///清除授权 |
| 91 | + static clearAuthorization() { |
| 92 | + optionParams["authorizationCode"] = null; |
| 93 | + LocalStorage.remove(Config.TOKEN_KEY); |
| 94 | + } |
| 95 | + |
| 96 | + ///获取授权token |
| 97 | + static getAuthorization() async { |
| 98 | + String token = await LocalStorage.get(Config.TOKEN_KEY); |
| 99 | + if (token == null) { |
| 100 | + String basic = await LocalStorage.get(Config.USER_BASIC_CODE); |
| 101 | + if (basic == null) { |
| 102 | + //提示输入账号密码 |
| 103 | + } else { |
| 104 | + //通过 basic 去获取token,获取到设置,返回token |
| 105 | + return "Basic $basic"; |
| 106 | + } |
| 107 | + } else { |
| 108 | + optionParams["authorizationCode"] = token; |
| 109 | + return token; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments