YSNetWorking.dart 7.0 KB

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