Commit 496de6de authored by 林生雨's avatar 林生雨

commit

parent 0d25bc98
sdk.dir=/Users/apple/Library/Android/sdk
flutter.sdk=/Users/apple/Downloads/flutter
flutter.buildMode=release
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1
\ No newline at end of file
......@@ -31,21 +31,29 @@ class ServiceGenerator extends GeneratorForAnnotation<ServiceCenter> {
final metadata = annometadata.computeConstantValue();
if (metadata.type.name == "Post" || metadata.type.name == "Get") {
if (!differentList.contains(methodElement.returnType.name)) {
var pathSegments = buildStep.inputId.pathSegments;
StringBuffer path = new StringBuffer();
for (int i = 0; i < pathSegments.length; i++) {
if (i < pathSegments.length - 2&& pathSegments[i] != "lib") {
path.write("${pathSegments[i]}/");
if(methodElement.returnType.name=="SimpleResponce"){
improtBuffer.write(
"import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';\n");
}else {
var pathSegments = buildStep.inputId.pathSegments;
StringBuffer path = new StringBuffer();
for (int i = 0; i < pathSegments.length; i++) {
if (i < pathSegments.length - 2 && pathSegments[i] != "lib") {
path.write("${pathSegments[i]}/");
}
}
improtBuffer.write(
"import 'package:${buildStep.inputId.package}/${path
.toString()}entity/${methodElement.returnType
.name}.dart\';\n");
}
improtBuffer.write(
"import 'package:${buildStep.inputId.package}/${path.toString()}entity/${methodElement.returnType.name}.dart\';\n");
differentList.add(methodElement.returnType.name);
}
String tempParams;
String sendType = metadata.type.name == "Post" ? "post" : "get";
mapBuffer.write("""
return Observable.fromFuture(DioUtil().${sendType}(\'${metadata.getField("sufUrl").toStringValue()}\',data:{
return Observable.fromFuture(DioUtil().${sendType}(\'${metadata.getField("sufUrl").toStringValue()}\'
""");
bool needMap = false;
for (int i = 0; i < methodElement.parameters.length; i++) {
......@@ -60,9 +68,17 @@ class ServiceGenerator extends GeneratorForAnnotation<ServiceCenter> {
} else {
tempParams = "${tempParams},${paramsMeta}";
}
if (i == methodElement.parameters.length - 1) {
if(i==0){
mapBuffer.write(
"\'${queryAnno.getField("params").toStringValue()}\':${paramsMeta.name}");
",data:{\'${queryAnno.getField("params").toStringValue()}\':${paramsMeta.name}");
if(methodElement.parameters.length>1){
mapBuffer.write(",");
}else{
mapBuffer.write("}");
}
}else if (i == methodElement.parameters.length - 1) {
mapBuffer.write(
"\'${queryAnno.getField("params").toStringValue()}\':${paramsMeta.name}}");
} else {
mapBuffer.write(
"\'${queryAnno.getField("params").toStringValue()}\':${paramsMeta.name},");
......@@ -70,7 +86,7 @@ class ServiceGenerator extends GeneratorForAnnotation<ServiceCenter> {
}
}
}
mapBuffer.write("}))");
mapBuffer.write("))");
mapBuffer.write("""
.map((value){
if(value.statusCode==200){
......@@ -98,6 +114,20 @@ class ServiceGenerator extends GeneratorForAnnotation<ServiceCenter> {
${improtBuffer.toString()}
import 'package:gmalpha_flutter/commonModel/net/DioUtil.dart';\n
class ${element.displayName}Impl{
factory ${element.displayName}Impl() => _sharedInstance();
static ${element.displayName}Impl _instance;
${element.displayName}Impl._() {
}
static ${element.displayName}Impl _sharedInstance() {
if (_instance == null) {
_instance = ${element.displayName}Impl._();
}
return _instance;
}
${methodBuffer.toString()}
}
""";
......
/*
* @author lsy
* @date 2019-09-05
**/
import 'dart:async';
import 'package:rxdart/rxdart.dart';
abstract class BaseModel {
//TODO
void dispose();
}
/*
* @author lsy
* @date 2019-09-05
**/
import 'dart:async';
class LiveData<T> {
StreamController<T> _controller;
T data;
LiveData() {
this._controller = new StreamController<T>();
}
get stream => _controller.stream;
get controller => _controller;
void notifyView(T t) {
this.data = t;
_controller.sink.add(t);
}
void dispost() {
_controller.close();
}
}
......@@ -255,12 +255,12 @@ class DioUtil {
/*
* post请求
*/
post(url, {data, options, cancelToken}) async {
Future<Response> post(url, {data, options, cancelToken}) async {
Response response;
try {
response = await _dio.post(url,
queryParameters: data, options: options, cancelToken: cancelToken);
print('post success---------${response.data}');
print('post success---------${response.statusCode} ${response.data}');
} on DioError catch (e) {
print('post error---------$e');
formatError(e);
......
/*
* @author lsy
* @date 2019-09-05
**/
class SimpleResponce {
Null data;
int errorCode;
String errorMsg;
SimpleResponce({this.data, this.errorCode, this.errorMsg});
SimpleResponce.fromJson(Map<String, dynamic> json) {
data = json['data'];
errorCode = json['errorCode'];
errorMsg = json['errorMsg'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['data'] = this.data;
data['errorCode'] = this.errorCode;
data['errorMsg'] = this.errorMsg;
return data;
}
}
\ No newline at end of file
......@@ -56,11 +56,16 @@ class _MyAppState extends State<MyApp> {
routes: {
// '/': (context) => CommentSuggest({"Cookie":" _gm_token=7e48641558699683; sessionid=nb3ze4ur7ucosln8sd8pzwojddenv9ym; _gtid=a1bc0a387e1911e996b9525400fa516d4094"}),
'/': (context) {
var findMessageRouter = RouterCenterImpl().findMessageRouter();
if (findMessageRouter == null) {
return null;
}
return findMessageRouter.getMessagePage({});
// var findMessageRouter = RouterCenterImpl().findMessageRouter();
// if (findMessageRouter == null) {
// return null;
// }
// return findMessageRouter.getMessagePage({});
var findUserRouter = RouterCenterImpl().findUserRouter();
if(findUserRouter==null){
return null;
}
return findUserRouter.getUserPage();
},
},
//调试的时候可以打开
......
......@@ -3,6 +3,7 @@
* @date 2019-09-03
**/
import 'package:flutter/material.dart';
import 'package:gmalpha_flutter/Annotations/RouterBaser.dart';
import 'package:gmalpha_flutter/Annotations/anno/Router.dart';
import 'package:gmalpha_flutter/userModel/UserRouterImpl.dart';
......@@ -10,4 +11,5 @@ import 'package:gmalpha_flutter/userModel/UserRouterImpl.dart';
@Router("userModel",UserRouterImpl)
abstract class UserRouter implements RouterBaser{
Widget getUserPage();
}
\ No newline at end of file
......@@ -2,8 +2,13 @@
* @author lsy
* @date 2019-09-03
**/
import 'package:flutter/src/widgets/framework.dart';
import 'package:gmalpha_flutter/userModel/UserRouter.dart';
import 'package:gmalpha_flutter/userModel/page/user/UserPage.dart';
class UserRouterImpl implements UserRouter{
}
\ No newline at end of file
class UserRouterImpl implements UserRouter {
@override
Widget getUserPage() {
return UserWidget();
}
}
......@@ -2,8 +2,12 @@
* @author lsy
* @date 2019-09-04
**/
import 'package:flutter/material.dart';
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';
import 'package:gmalpha_flutter/userModel/page/user/UserPageModel.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';
class UserWidget extends StatefulWidget {
@override
......@@ -13,20 +17,121 @@ class UserWidget extends StatefulWidget {
class UserState extends State<UserWidget> {
UserPageModel _model;
TextEditingController _nameController = new TextEditingController();
TextEditingController _wordController = new TextEditingController();
@override
void initState() {
super.initState();
_model = new UserPageModel();
}
@override
void dispose() {
super.dispose();
_model.dispose();
_wordController.dispose();
_nameController.dispose();
}
@override
Widget build(BuildContext context) {
// return Scaffold(
// body: ALRootInheritWideget(
// model: _model,
// child: Center(
// child: Text(_model.text),
// ),
// ));
return Scaffold(
appBar: AppBar(
title: Text(
"User",
style: TextStyle(fontSize: 16),
),
centerTitle: true,
),
body: Column(
children: <Widget>[
Expanded(
child: StreamBuilder<SimpleResponce>(
stream: _model.logoutLive.stream,
initialData: _model.logoutLive.data,
builder: (BuildContext context,
AsyncSnapshot<SimpleResponce> snapshot) {
if (snapshot.data == null) {
return Center(child: Text("没有退出"));
} else {
return Center(child: Text("退出接口回掉成功!!"));
}
},
),
),
Container(
margin: EdgeInsets.fromLTRB(16, 10, 16, 20),
child: TextField(
cursorColor: Colors.black,
controller: _nameController,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(10.0),
icon: Icon(Icons.text_fields, color: Colors.black),
labelText: 'name',
labelStyle: TextStyle(color: Colors.black)),
autofocus: false,
),
),
Container(
margin: EdgeInsets.fromLTRB(16, 0, 16, 20),
child: TextField(
cursorColor: Colors.black,
controller: _wordController,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(10.0),
icon: Icon(
Icons.text_fields,
color: Colors.black,
),
labelText: 'word',
labelStyle: TextStyle(color: Colors.black)),
autofocus: false,
),
),
Container(
width: double.maxFinite,
margin: EdgeInsets.fromLTRB(16, 0, 16, 0),
child: OutlineButton(
onPressed: () => _model.resignUser(
context, _nameController.text, _wordController.text),
child: Text("注册"),
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(3),
),
)),
Container(
width: double.maxFinite,
margin: EdgeInsets.fromLTRB(16, 0, 16, 0),
child: OutlineButton(
onPressed: () => _model.logout(context),
child: Text("退出"),
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(3),
),
)),
Expanded(
child: StreamBuilder<TestUserEntity>(
stream: _model.resignLive.stream,
initialData: _model.resignLive.data,
builder: (BuildContext context,
AsyncSnapshot<TestUserEntity> snapshot) {
if (snapshot.data != null) {
return Center(
child: snapshot.data.data == null
? Text("错误 ${snapshot.data.errorMsg}")
: Text("登入回掉 用户id: ${snapshot.data.data.id}"),
);
} else {
return Center(
child: Text("初始化~"),
);
}
},
),
)
],
),
);
}
}
......@@ -3,11 +3,46 @@
* @date 2019-09-04
**/
class UserPageModel {
import 'package:flutter/cupertino.dart';
import 'package:gmalpha_flutter/commonModel/live/BaseModel.dart';
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';
import 'package:gmalpha_flutter/commonModel/toast/toast.dart';
import 'package:gmalpha_flutter/userModel/service/UserRepository.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';
String text;
import '../../../commonModel/live/LiveData.dart';
class UserPageModel extends BaseModel {
LiveData<SimpleResponce> logoutLive = new LiveData();
LiveData<TestUserEntity> resignLive = new LiveData();
// LiveData<String> userTokenLive = new LiveData();
UserPageModel() {}
void logout(BuildContext context) {
UserRepository.getInstance().logout().listen((data) {
logoutLive.notifyView(data);
}).onError((error) {
Toast.show(context, "error ${error}");
print(error);
});
}
void resignUser(BuildContext context, String name, String word) {
UserRepository.getInstance().resignUser(name, word, word).listen((data) {
print("data !! :${data}");
resignLive.notifyView(data);
}).onError((error) {
Toast.show(context, "error :${error}");
print(error);
});
}
@override
void dispose() {
logoutLive.dispost();
resignLive.dispost();
}
}
......@@ -2,8 +2,12 @@
* @author lsy
* @date 2019-09-04
**/
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';
import 'package:gmalpha_flutter/userModel/service/local/UserLocal.dart';
import 'package:gmalpha_flutter/userModel/service/remote/UserRemote.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';
import 'package:rxdart/rxdart.dart';
class UserRepository {
UserRemote _remote;
......@@ -23,5 +27,31 @@ class UserRepository {
return _userRepository;
}
Observable<UserEntity> getUserInfo(String token) {
_remote.getUserInfo(token).map((value) {
if (value != null) {
_local.saveUserInfo(value);
}
return value;
});
}
Observable<TestUserEntity> resignUser(
String name, String word, String reword) {
return _remote.resignUser(name, word, reword).map((value) {
if (value != null) {
_local.saveUser(value);
}
return value;
});
}
Observable<SimpleResponce> logout() {
return _remote.logout().map((value) {
if (value != null) {
_local.logout();
}
return value;
});
}
}
......@@ -2,6 +2,9 @@
* @author lsy
* @date 2019-09-04
**/
import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';
class UserLocal {
static UserLocal _userRemote;
......@@ -13,4 +16,16 @@ class UserLocal {
}
return _userRemote;
}
void saveUserInfo(UserEntity value) {
//TODO
}
void saveUser(TestUserEntity value) {
//TODO
}
void logout() {
//TODO
}
}
......@@ -2,6 +2,12 @@
* @author lsy
* @date 2019-09-04
**/
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';
import 'package:gmalpha_flutter/userModel/service/remote/api/UserLoginApi.serv.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';
import 'package:rxdart/rxdart.dart';
class UserRemote {
static UserRemote _userRemote;
......@@ -14,5 +20,16 @@ class UserRemote {
return _userRemote;
}
Observable<UserEntity> getUserInfo(String token) {
return UserLoginApiImpl().getUserInfo(token);
}
Observable<TestUserEntity> resignUser(
String name, String word, String reWord) {
return UserLoginApiImpl().resign(name, word, reWord);
}
Observable<SimpleResponce> logout() {
return UserLoginApiImpl().logout();
}
}
......@@ -6,13 +6,19 @@ import 'package:gmalpha_flutter/Annotations/anno/Get.dart';
import 'package:gmalpha_flutter/Annotations/anno/Post.dart';
import 'package:gmalpha_flutter/Annotations/anno/Query.dart';
import 'package:gmalpha_flutter/Annotations/anno/ServiceCenter.dart';
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';
@ServiceCenter()
abstract class UserLoginApi {
@Post("user/login")
UserEntity login(@Query("name") String name, @Query("word") String word);
@Post("user/register")
TestUserEntity resign(@Query("username") String name,
@Query("password") String word, @Query("repassword") String repassword);
@Get("user/info")
UserEntity getUserInfo(@Query("token") String token);
@Get("user/logout/json")
SimpleResponce logout();
}
......@@ -10,18 +10,36 @@ import 'dart:io';
import 'package:rxdart/rxdart.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';
import 'package:gmalpha_flutter/commonModel/net/DioUtil.dart';
class UserLoginApiImpl {
Observable<UserEntity> login(String name, String word) {
return Observable.fromFuture(
DioUtil().post('user/login', data: {'name': name, 'word': word}))
.map((value) {
factory UserLoginApiImpl() => _sharedInstance();
static UserLoginApiImpl _instance;
UserLoginApiImpl._() {}
static UserLoginApiImpl _sharedInstance() {
if (_instance == null) {
_instance = UserLoginApiImpl._();
}
return _instance;
}
Observable<TestUserEntity> resign(
String name, String word, String repassword) {
return Observable.fromFuture(DioUtil().post('user/register', data: {
'username': name,
'password': word,
'repassword': repassword
})).map((value) {
if (value.statusCode == 200) {
Map map = json.decode(value.toString());
return UserEntity.fromJson(map);
return TestUserEntity.fromJson(map);
} else {
throw HttpException("statusCode error :${value.statusCode}");
}
......@@ -39,4 +57,16 @@ class UserLoginApiImpl {
}
});
}
Observable<SimpleResponce> logout() {
return Observable.fromFuture(DioUtil().get('user/logout/json'))
.map((value) {
if (value.statusCode == 200) {
Map map = json.decode(value.toString());
return SimpleResponce.fromJson(map);
} else {
throw HttpException("statusCode error :${value.statusCode}");
}
});
}
}
/*
* @author lsy
* @date 2019-09-05
**/
class TestUserEntity {
Data data;
int errorCode;
String errorMsg;
TestUserEntity({this.data, this.errorCode, this.errorMsg});
TestUserEntity.fromJson(Map<String, dynamic> json) {
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
errorCode = json['errorCode'];
errorMsg = json['errorMsg'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.data != null) {
data['data'] = this.data.toJson();
}
data['errorCode'] = this.errorCode;
data['errorMsg'] = this.errorMsg;
return data;
}
}
class Data {
bool admin;
List<String> chapterTops;
List<String> collectIds;
String email;
String icon;
int id;
String nickname;
String password;
String token;
int type;
String username;
Data(
{this.admin,
this.chapterTops,
this.collectIds,
this.email,
this.icon,
this.id,
this.nickname,
this.password,
this.token,
this.type,
this.username});
Data.fromJson(Map<String, dynamic> json) {
admin = json['admin'];
chapterTops = json['chapterTops'].cast<String>();
collectIds = json['collectIds'].cast<String>();
email = json['email'];
icon = json['icon'];
id = json['id'];
nickname = json['nickname'];
password = json['password'];
token = json['token'];
type = json['type'];
username = json['username'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['admin'] = this.admin;
data['chapterTops'] = this.chapterTops;
data['collectIds'] = this.collectIds;
data['email'] = this.email;
data['icon'] = this.icon;
data['id'] = this.id;
data['nickname'] = this.nickname;
data['password'] = this.password;
data['token'] = this.token;
data['type'] = this.type;
data['username'] = this.username;
return data;
}
}
\ No newline at end of file
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