|
@@ -0,0 +1,197 @@
|
|
|
+import 'package:dio/dio.dart';
|
|
|
+import 'package:flutter/cupertino.dart';
|
|
|
+import 'package:flutter/material.dart';
|
|
|
+import 'package:flutterairplane/code/YSLogin.dart';
|
|
|
+import 'package:fluttertoast/fluttertoast.dart';
|
|
|
+import 'package:shared_preferences/shared_preferences.dart';
|
|
|
+
|
|
|
+String base = 'http://fy.rungyun.cn/api/';
|
|
|
+enum requestType{
|
|
|
+ put,
|
|
|
+ delete,
|
|
|
+ get,
|
|
|
+ post
|
|
|
+}
|
|
|
+
|
|
|
+ysRequestHttp(BuildContext context,requestType type,String api,var parameter) async {
|
|
|
+ showDialog(
|
|
|
+ context: context,
|
|
|
+ barrierDismissible: false,
|
|
|
+ builder: (BuildContext context) {return LoadingDialog(text: "正在请求",);}
|
|
|
+ );
|
|
|
+ try {
|
|
|
+ SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
+ String token = prefs.getString('token');
|
|
|
+ Map<String, dynamic> httpHeaders = {
|
|
|
+ 'Accept': 'application/json,*/*',
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'Authorization': token!=null?'Bearer '+token:''
|
|
|
+ };
|
|
|
+ String url = base+api;
|
|
|
+ print('请求网址:$url');
|
|
|
+ print('token:$token');
|
|
|
+ print('请求参数:$parameter');
|
|
|
+ Response response;
|
|
|
+ if(type==requestType.put){
|
|
|
+ response = await Dio().put(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
|
|
|
+ }else if(type==requestType.post){
|
|
|
+ response = await Dio().post(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
|
|
|
+ }else if(type==requestType.delete){
|
|
|
+ response = await Dio().delete(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
|
|
|
+ }else if(type==requestType.get){
|
|
|
+ response = await Dio().get(url,queryParameters: Map<String, dynamic>.from(parameter),options: Options(receiveTimeout: 10000,headers: httpHeaders));
|
|
|
+ }
|
|
|
+ Navigator.of(context,rootNavigator: true).pop();
|
|
|
+ print('请求结果:${response.data}');
|
|
|
+ if(response.data is List){
|
|
|
+ List list = response.data;
|
|
|
+ return list;
|
|
|
+ }else{
|
|
|
+ Map dict = response.data;
|
|
|
+ if(dict['msg']!=null){
|
|
|
+ ysFlutterToast(context,dict['msg'].toString());
|
|
|
+ }else if(dict['message']!=null){
|
|
|
+ ysFlutterToast(context,dict['message'].toString());
|
|
|
+ }
|
|
|
+ if(api=='chapter/list' || api=='user/info' || api=='policy/list' || api=='policy/info' || api=='policy/doctor' || api=='policy/doctorInfo'){
|
|
|
+ return dict;
|
|
|
+ }else if(dict['code']==200 || dict['status']=='0000'){
|
|
|
+ return dict;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ Navigator.of(context,rootNavigator: true).pop();
|
|
|
+ print('网络错误:$error');
|
|
|
+ if(error.message=='Http status error [401]'){
|
|
|
+ ysFlutterToast(context,'登录失效,请重新登录');
|
|
|
+ Navigator.of(context).pushAndRemoveUntil(
|
|
|
+ MaterialPageRoute(
|
|
|
+ builder: (context){
|
|
|
+ Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
|
|
|
+ _prefer.then((value){
|
|
|
+ value.remove('token');
|
|
|
+ });
|
|
|
+ return YSLogin();
|
|
|
+ }
|
|
|
+ ), (route) => false);
|
|
|
+ }else{
|
|
|
+ ysFlutterToast(context,'网络错误');
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+ysRequestHttpNoLoading(BuildContext context,requestType type,String api,var parameter) async {
|
|
|
+ try {
|
|
|
+ SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
+ String token = prefs.getString('token');
|
|
|
+ Map<String, dynamic> httpHeaders = {
|
|
|
+ 'Accept': 'application/json,*/*',
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'Authorization': token!=null?'Bearer '+token:''
|
|
|
+ };
|
|
|
+ String url = base+api;
|
|
|
+ print('请求网址:$url');
|
|
|
+ print('token:$token');
|
|
|
+ print('请求参数:$parameter');
|
|
|
+ Response response;
|
|
|
+ if(type==requestType.put){
|
|
|
+ response = await Dio().put(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
|
|
|
+ }else if(type==requestType.post){
|
|
|
+ response = await Dio().post(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
|
|
|
+ }else if(type==requestType.delete){
|
|
|
+ response = await Dio().delete(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
|
|
|
+ }else if(type==requestType.get){
|
|
|
+ response = await Dio().get(url,queryParameters: Map<String, dynamic>.from(parameter),options: Options(receiveTimeout: 10000,headers: httpHeaders));
|
|
|
+ }
|
|
|
+ print('请求结果:${response.data}');
|
|
|
+ if(response.data is List){
|
|
|
+ List list = response.data;
|
|
|
+ return list;
|
|
|
+ }else{
|
|
|
+ Map dict = response.data;
|
|
|
+ if(api=='chapter/list' || api=='wike/list' || api=='upQiniuToken' || api=='basic/BodyStandard' || api=='basic/info' || api=='wike/favoriteList' || api=='policy/list'){
|
|
|
+ return dict;
|
|
|
+ }else if(dict['code']==200 || dict['status']=='0000'){
|
|
|
+ return dict;
|
|
|
+ }else{
|
|
|
+ if(dict['msg']!=null){
|
|
|
+ ysFlutterToast(context,dict['msg'].toString());
|
|
|
+ }else if(dict['message']!=null){
|
|
|
+ ysFlutterToast(context,dict['message'].toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ print('网络错误:$error');
|
|
|
+ if(error.message=='Http status error [401]'){
|
|
|
+ ysFlutterToast(context,'登录失效,请重新登录');
|
|
|
+ Navigator.of(context).pushAndRemoveUntil(
|
|
|
+ MaterialPageRoute(
|
|
|
+ builder: (context){
|
|
|
+ Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
|
|
|
+ _prefer.then((value){
|
|
|
+ value.remove('token');
|
|
|
+ });
|
|
|
+ return YSLogin();
|
|
|
+ }
|
|
|
+ ), (route) => false);
|
|
|
+ }else{
|
|
|
+ ysFlutterToast(context,'网络错误');
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class LoadingDialog extends Dialog {
|
|
|
+ final String text;
|
|
|
+
|
|
|
+ LoadingDialog({Key key, @required this.text}) : super(key: key);
|
|
|
+
|
|
|
+ @override
|
|
|
+ Widget build(BuildContext context) {
|
|
|
+ return Material(
|
|
|
+ type: MaterialType.transparency,
|
|
|
+ child: Center(
|
|
|
+ child: SizedBox(
|
|
|
+ width: 120.0,
|
|
|
+ height: 120.0,
|
|
|
+ child: Container(
|
|
|
+ decoration: ShapeDecoration(
|
|
|
+ color: Color(0xFFDB5278),
|
|
|
+ shape: RoundedRectangleBorder(
|
|
|
+ borderRadius: BorderRadius.all(
|
|
|
+ Radius.circular(8.0),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ child: Column(
|
|
|
+ mainAxisAlignment: MainAxisAlignment.center,
|
|
|
+ crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
+ children: <Widget>[
|
|
|
+ CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.white),),
|
|
|
+ Padding(
|
|
|
+ padding: EdgeInsets.only(top: 20.0,),
|
|
|
+ child: Text(text,style: TextStyle(color: Colors.white),),
|
|
|
+ ),
|
|
|
+ ],
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void ysFlutterToast(BuildContext context,String msg){
|
|
|
+ FlutterToast(context).showToast(
|
|
|
+ child: Container(
|
|
|
+ padding: EdgeInsets.only(left: 20,right: 20,top: 5,bottom: 5),
|
|
|
+ decoration: BoxDecoration(
|
|
|
+ color: Color(0xFFDB5278),
|
|
|
+ borderRadius: BorderRadius.all(Radius.circular(25))
|
|
|
+ ),
|
|
|
+ child: Text(msg,style: TextStyle(fontSize: 14,color: Colors.white),),
|
|
|
+ ),
|
|
|
+ gravity: ToastGravity.BOTTOM,
|
|
|
+ toastDuration: Duration(seconds: 2),
|
|
|
+ );
|
|
|
+}
|