first commit

This commit is contained in:
编码猿
2024-09-27 01:32:49 +08:00
commit 28ebe05a64
7399 changed files with 1174529 additions and 0 deletions

View File

@@ -0,0 +1 @@
6237ef09-050e-435b-85f4-d9966c31c525

View File

@@ -0,0 +1,6 @@
// ignore: camel_case_types
class config {
static const String BASE_URL = "http://192.168.0.104:3000";
//static const String BASE_URL = "https://www.geekhelp.cn";
static const String testUrl = "/index";
}

View File

@@ -0,0 +1,5 @@
# 基于dio的HTTP请求封装
- 1: config.dart 网络请求的基本配置
- 2: dio的主体封装

View File

@@ -0,0 +1,89 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/http/config.dart';
import 'package:bot_toast/bot_toast.dart';
class Http {
Dio _dio;
Http() {
this._dio = new Dio(new BaseOptions(
baseUrl: config.BASE_URL,
connectTimeout: 3000,
receiveTimeout: 3000,
));
this._dio.interceptors.add(InterceptorsWrapper(
onRequest:(RequestOptions options){
/// 请求发送前拦截,对 queryParameters 参数进行RSA公钥加密放到token中发送给后端
/// 后端私钥解密,查询数据,然后通过私钥加密数据再返回给前端
///options.headers = options.queryParameters;
///print(options.headers);
return options;
},
onResponse:(Response response) {
/// 响应拦截器,将数据返回给前端之前先使用公钥解密后端的数据,然后将解密后明文给前端
// 在返回响应数据之前做一些预处理
return response;
},
onError: (DioError e) {
return e;
}
));
}
/***
* get 请求
* url 请求地址
* params 请求参数
*/
get (String url, Object params) async {
Response response;
try {
response = await this._dio.get(url, queryParameters: params);
return response.data;
} on DioError catch (e) {
if (CancelToken.isCancel(e)) {
BotToast.showSimpleNotification(
title: 'GET请求取消',
subTitle: e.message,
hideCloseButton: true
);
}
BotToast.showSimpleNotification(
title: 'GET请求错误',
subTitle: e.message,
hideCloseButton: true
);
print(e.message);
}
}
/***
* post 请求
* url 请求地址
* params 请求参数
*/
post (String url, Object params) async {
Response response;
try {
response = await this._dio.post(url, data: params);
return response.data;
} on DioError catch (e) {
if (CancelToken.isCancel(e)) {
BotToast.showSimpleNotification(
title: 'POST请求取消',
subTitle: e.message,
hideCloseButton: true
);
}
BotToast.showSimpleNotification(
title: 'POST请求错误',
subTitle: e.message,
hideCloseButton: true
);
}
}
}