Commit 9c0e86b5 authored by 窦文's avatar 窦文

http

parent bb56d287
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:netrain_flutter_app/douwen/custom/BaseView.dart';
import 'package:netrain_flutter_app/douwen/custom/BaseViewModel.dart';
import 'package:netrain_flutter_app/douwen/custom/http/AdService.dart';
import 'custom/http/base/IEntity.dart';
class Demo2 extends BaseView {
......@@ -30,8 +35,8 @@ class Demo2 extends BaseView {
builder: (context, value, child) =>
Text("点击了${viewModel.num.value}次")),
ElevatedButton(
onPressed: () {
viewModel.click();
onPressed: () async {
viewModel.login();
},
child: Text("点击"))
],
......
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:netrain_flutter_app/douwen/custom/http/AdRepository.dart';
import 'ViewState.dart';
......@@ -7,6 +9,7 @@ class BaseViewModel extends ChangeNotifier {
var num = ValueNotifier<int>(0);
set viewState(ViewState viewState) => setViewState(viewState);
get viewState => _viewState;
void click() {
......@@ -37,4 +40,12 @@ class BaseViewModel extends ChangeNotifier {
_viewState = viewState;
notifyListeners();
}
void login() {
AdRepository.instance.login((str) {
Fluttertoast.showToast(msg: str);
}, (iEntity) {
Fluttertoast.showToast(msg: iEntity.msg);
});
}
}
import 'package:netrain_flutter_app/douwen/custom/http/AdService.dart';
import 'base/IEntity.dart';
import 'base/IRepository.dart';
class AdRepository with IRepository{
AdRepository._privateConstructor(this._service);
static AdRepository _instance;
static AdRepository get instance {
if (_instance == null) {
_instance = AdRepository._privateConstructor(AdService());
}
return _instance;
}
final AdService _service;
void login(void onSuccess(String str),void onError(IEntity iEntity)) => getResult<String>(_service.login(),analysis: (data) => data.toString(),onSuccess: (str) => onSuccess(str),onError:(iEntity) => onError(iEntity));
}
import 'package:dio/dio.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/IService.dart';
import 'base/IEntity.dart';
class AdService with IService {
@override
String getServicePath() {
return "/ad";
}
Future<IEntity> login() => post("/login/validLogin", {
"phoneNum": "15321350450",
"verifyCode": "123456",
"deviceSN": "123456"
});
}
import 'IEntity.dart';
class ApiException implements Exception {
IEntity _iEntity;
get iEntity => _iEntity;
ApiException(this._iEntity){}
}
\ No newline at end of file
import 'package:dio/dio.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/HttpCode.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/IEntity.dart';
import 'ApiException.dart';
class ErrorInterceptor extends Interceptor{
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
IEntity iEntity = IEntity.fromJson(response.data);
if (HttpCode.SUCCESS != iEntity.code) {
throw ApiException(iEntity);
}
response.data = iEntity;
return super.onResponse(response, handler);
}
}
\ No newline at end of file
class HttpCode{
static int SUCCESS = 0;
static int NORMAL_ERROR = -999;//非逻辑错误码(网络错误、超时等)
}
\ No newline at end of file
import 'package:dio/dio.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/ErrorInterceptor.dart';
class HttpUtil {
static String baseUrl = "https://api.hkmeinian-hlw.cn";
Dio getDio(String baseUrl){
return Dio(BaseOptions(
baseUrl: baseUrl,
))
..interceptors.add(ErrorInterceptor());
}
}
import 'package:netrain_flutter_app/douwen/custom/http/base/HttpCode.dart';
/// code : -1
/// msg : "系统繁忙!"
/// data : null
/// timestamp : "2021-07-21 15:29:05"
class IEntity<T> {
int _code;
String _msg;
dynamic _data;
String _timestamp;
int get code => _code;
set code(int code) => _code = code;
String get msg => _msg;
set msg(String msg) => _msg = msg;
dynamic get data => _data;
String get timestamp => _timestamp;
set timestamp(String msg) => _timestamp = msg;
IEntity({int code, String msg, dynamic data, String timestamp}) {
_code = code;
_msg = msg;
_data = data;
_timestamp = timestamp;
}
bool isSuccess(){
if (code == HttpCode.SUCCESS) {
return true;
} else {
return false;
}
}
IEntity.fromJson(dynamic json) {
_code = json["code"];
_msg = json["msg"];
_data = json["data"];
_timestamp = json["timestamp"];
}
Map<String, dynamic> toJson() {
var map = <String, dynamic>{};
map["code"] = _code;
map["msg"] = _msg;
map["data"] = _data;
map["timestamp"] = _timestamp;
return map;
}
}
import 'package:flutter/foundation.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/IEntity.dart';
mixin IRepository {
void getResult<T>(Future<IEntity> future,{@required T analysis(dynamic data),@required void onSuccess(T t),void onError(IEntity iEntity),void onFinally()}) async {
IEntity iEntity = await future;
if (iEntity.isSuccess()) {
onSuccess(analysis(iEntity.data));
} else {
onError?.call(iEntity);
}
onFinally?.call();
}
}
\ No newline at end of file
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/ApiException.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/HttpCode.dart';
import 'package:netrain_flutter_app/douwen/custom/http/base/HttpUtil.dart';
import 'IEntity.dart';
mixin IService {
String getServicePath();
///获取请求对象
Dio _getRequest() {
return HttpUtil().getDio(HttpUtil.baseUrl + getServicePath() ?? "");
}
Future<IEntity> post(String path, data) => _getRequest()
.post(path, data: data)
.then((value) => _analysisData(value))
.catchError((error) => _analysisError(error));
IEntity _analysisData(Response<IEntity> value) {
IEntity iEntity = value.data;
return iEntity;
}
///这里处理公共逻辑异常
_analysisApiException(IEntity iEntity) {
}
IEntity _analysisError(var error) {
IEntity iEntity;
if (error is DioError) {
dynamic e = error.error;
if (e is ApiException) {
iEntity = e.iEntity;
_analysisApiException(iEntity);
}else{
iEntity = new IEntity(code: HttpCode.NORMAL_ERROR,msg:e.toString());
}
}
return iEntity;
}
}
......@@ -101,7 +101,7 @@ class _DoctorHomePageState extends State<DoctorHomePage> {
Positioned(
child: Container(
width: 68,
height: 20,
height: 10,
decoration: BoxDecoration(
color: Colors.white30,
borderRadius: BorderRadius.circular(10)),
......@@ -117,7 +117,8 @@ class _DoctorHomePageState extends State<DoctorHomePage> {
// ),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
// crossAxisAlignment: CrossAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"主任医师",
......@@ -228,7 +229,6 @@ class _DoctorHomePageState extends State<DoctorHomePage> {
),
key: Key("${list[index]}"),
onDismissed: (direction) {
setState(() {
print(
"删除成功? ${list.remove(list[index])}, ${list.toString()}");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment