YSNetWorking.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import 'package:dio/dio.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutterappfuyou/code/base/YSTools.dart';
  4. import 'package:fluttertoast/fluttertoast.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6. import '../YSlogin.dart';
  7. //https://v2fy.niwoshenghuo.com
  8. //http://114.115.251.233:30009
  9. //https://v3fy.niwoshenghuo.com
  10. //https://v3fy.api.niwoshenghuo.com
  11. //https://v3fy.niwoshenghuo.com/v3api
  12. //http://192.168.1.33:8088
  13. String base = 'https://v3fy.niwoshenghuo.com/v3api/api/';
  14. // ignore: camel_case_types
  15. enum requestType{
  16. put,
  17. delete,
  18. get,
  19. post
  20. }
  21. ysRequestHttp(BuildContext context,requestType type,String api,var parameter) async {
  22. showDialog(
  23. context: context,
  24. barrierDismissible: false,
  25. builder: (BuildContext context) {return LoadingDialog(text: "正在请求",);}
  26. );
  27. try {
  28. SharedPreferences prefs = await SharedPreferences.getInstance();
  29. String token = prefs.getString('token');
  30. Map<String, dynamic> httpHeaders = {
  31. 'Accept': 'application/json,*/*',
  32. // 'Content-Type': 'application/json',
  33. 'Authorization':'Bearer $token'
  34. };
  35. String url = base+api;
  36. print('请求网址:$url');
  37. print('token:$token');
  38. print('请求参数:$parameter');
  39. Response response;
  40. if(type==requestType.put){
  41. response = await Dio().put(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
  42. }else if(type==requestType.post){
  43. response = await Dio().post(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
  44. }else if(type==requestType.delete){
  45. response = await Dio().delete(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
  46. }else if(type==requestType.get){
  47. response = await Dio().get(url,queryParameters: Map<String, dynamic>.from(parameter),options: Options(receiveTimeout: 10000,headers: httpHeaders));
  48. }
  49. Navigator.of(context,rootNavigator: true).pop();
  50. LogUtil.d('请求结果:$url ${response.data}');
  51. if(response.data is List){
  52. List list = response.data;
  53. return list;
  54. }else{
  55. Map dict = response.data;
  56. if(api=='user/info'&&type==requestType.get){
  57. return dict['data'];
  58. }else if(api=='chapter/list' || api=='policy/list' || api=='policy/info' || api=='policy/doctor' || api=='policy/doctorInfo'){
  59. return dict;
  60. }else if(dict['code']==200 || dict['status']=='0000'){
  61. return dict;
  62. }else if(dict['datas']!=null||dict['id']!=null){
  63. return dict;
  64. }else if(dict['code']==401){
  65. clearAndToLogin(context);
  66. }else{
  67. if(dict['msg']!=null){
  68. ysFlutterToast(context,dict['msg'].toString());
  69. }else if(dict['message']!=null){
  70. ysFlutterToast(context,dict['message'].toString());
  71. }
  72. }
  73. }
  74. } catch (error) {
  75. Navigator.of(context,rootNavigator: true).pop();
  76. print('网络错误:$error');
  77. if(error.message=='Http status error [401]'){
  78. clearAndToLogin(context);
  79. }else{
  80. ysFlutterToast(context,'系统繁忙,请稍后重试');
  81. }
  82. }
  83. }
  84. clearAndToLogin(BuildContext context) {
  85. ysFlutterToast(context,'登录失效,请重新登录');
  86. Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
  87. _prefer.then((value){
  88. value.remove('token');
  89. });
  90. Navigator.of(context,rootNavigator: true).pushAndRemoveUntil(
  91. MaterialPageRoute(
  92. builder: (context){
  93. return YSLogin(isCheck: true,);
  94. }
  95. ), (route) => false);
  96. }
  97. ysRequestHttpNoLoading(BuildContext context,requestType type,String api,var parameter) async {
  98. try {
  99. SharedPreferences prefs = await SharedPreferences.getInstance();
  100. String token = prefs.getString('token');
  101. Map<String, dynamic> httpHeaders = {
  102. 'Accept': 'application/json,*/*',
  103. // 'Content-Type': 'application/json',
  104. 'Authorization':'Bearer $token'
  105. };
  106. LogUtil.d(httpHeaders);
  107. String url = base+api;
  108. print('请求网址:$url');
  109. print('token:$token');
  110. print('请求参数:$parameter');
  111. Response response;
  112. if(type==requestType.put){
  113. response = await Dio().put(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
  114. }else if(type==requestType.post){
  115. response = await Dio().post(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
  116. }else if(type==requestType.delete){
  117. response = await Dio().delete(url,data: parameter,options: Options(receiveTimeout: 10000,headers: httpHeaders));
  118. }else if(type==requestType.get){
  119. response = await Dio().get(url,queryParameters: Map<String, dynamic>.from(parameter),options: Options(receiveTimeout: 10000,headers: httpHeaders));
  120. }
  121. LogUtil.d('请求结果:$url ${response.data}');
  122. // LogUtil.d('请求结果: ${response.data}');
  123. if(response.data is List){
  124. List list = response.data;
  125. return list;
  126. }else if(response.data is Map){
  127. Map dict = response.data;
  128. if(api=='basic/BodyStandard'|| api=='upQiniuToken' || api=='app/version'){
  129. return dict;
  130. }else if(api=='wike/list' || api=='wike/favoriteList' || api=='policy/list'){//|| api=='basic/info'
  131. if(dict['code']==200){
  132. List datas = dict['data']['datas']??[];
  133. dict['data'] = datas;
  134. return dict;
  135. }
  136. }else if(api=='tool/list'||api=='policy/doctor'||api=='user/info'){
  137. return dict['data'];
  138. }else if(dict['code']==200 || dict['status']=='0000'){
  139. return dict;
  140. }else if(dict['datas']!=null||dict['id']!=null){
  141. return dict;
  142. }else if(dict['code']==401){
  143. clearAndToLogin(context);
  144. }else{
  145. if(dict['msg']!=null){
  146. ysFlutterToast(context,dict['msg']??'');
  147. }else if(dict['message']!=null){
  148. ysFlutterToast(context,dict['message']??'');
  149. }
  150. }
  151. }
  152. } catch (error) {
  153. LogUtil.d('网络错误:$error');
  154. if(error.message=='Http status error [401]'){
  155. clearAndToLogin(context);
  156. }else{
  157. ysFlutterToast(context,'系统繁忙,请稍后重试');
  158. }
  159. }
  160. }
  161. class LoadingDialog extends Dialog {
  162. final String text;
  163. LoadingDialog({Key key, @required this.text}) : super(key: key);
  164. @override
  165. Widget build(BuildContext context) {
  166. return Material(
  167. type: MaterialType.transparency,
  168. child: Center(
  169. child: SizedBox(
  170. width: 120.0,
  171. height: 120.0,
  172. child: Container(
  173. decoration: ShapeDecoration(
  174. color: Color(0xFFDB5278),
  175. shape: RoundedRectangleBorder(
  176. borderRadius: BorderRadius.all(
  177. Radius.circular(8.0),
  178. ),
  179. ),
  180. ),
  181. child: Column(
  182. mainAxisAlignment: MainAxisAlignment.center,
  183. crossAxisAlignment: CrossAxisAlignment.center,
  184. children: <Widget>[
  185. CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.white),),
  186. Padding(
  187. padding: EdgeInsets.only(top: 20.0,),
  188. child: Text(text,style: TextStyle(color: Colors.white),),
  189. ),
  190. ],
  191. ),
  192. ),
  193. ),
  194. ),
  195. );
  196. }
  197. }
  198. void ysFlutterToast(BuildContext context,String msg){
  199. FlutterToast(context).showToast(
  200. child: Padding(
  201. padding: EdgeInsets.only(top: 100),
  202. child: Container(
  203. padding: EdgeInsets.only(left: 20,right: 20,top: 5,bottom: 5),
  204. decoration: BoxDecoration(
  205. color: Color(0xFFDB5278),
  206. borderRadius: BorderRadius.all(Radius.circular(25))
  207. ),
  208. child: Text(msg,style: TextStyle(fontSize: 14,color: Colors.white),),
  209. ),
  210. ),
  211. gravity: ToastGravity.CENTER,
  212. toastDuration: Duration(seconds: 2),
  213. );
  214. }