You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
9.8 KiB
Dart

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:hyzp_ybqx/components/UserAuthority.dart';
import '../../components/commonFun.dart';
import '../../widget/JdText.dart';
import '../../widget/JdButton.dart';
import '../../config/Config.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../../services/Storage.dart';
import 'dart:convert';
import '../../components/EncryptUtil.dart';
import '../../services/EventBus.dart';
import '../../config/service_url.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class LoginByName extends StatefulWidget {
LoginByName({Key key}) : super(key: key);
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginByName> {
//监听登录页面销毁的事件
dispose() {
super.dispose();
eventBus.fire(new UserEvent('登录成功...'));
}
bool bRemmberPW = false;
@override
void initState() {
super.initState();
// g_userInfo.username = 'the_user_03';
// g_userInfo.password = '123456';
// g_userInfo.password = 'ybhb1234';
g_userInfo.username = '';
g_userInfo.password = '';
doInit();
}
doInit() async {
bRemmberPW = await Storage.getBool('bRemmberPW');
bRemmberPW = (null == bRemmberPW) ? false : bRemmberPW;
print('bRemmberPW = $bRemmberPW');
if (bRemmberPW) {
//取出后需解密
g_userInfo.username = await Storage.getString('username');
g_userInfo.username = EncryptUtil.aesDecode(g_userInfo.username);
g_userInfo.password = await Storage.getString('password');
g_userInfo.password = EncryptUtil.aesDecode(g_userInfo.password);
}
setState(() {});
}
doLogin() async {
//测试用,临时绕过登录处理
// Navigator.pushNamed(context, '/tabs', arguments: g_iIndex);
// return;
if (bRemmberPW) {
Storage.setBool('bRemmberPW', bRemmberPW);
//加密保存
Storage.setString('username', EncryptUtil.aesEncode(g_userInfo.username));
Storage.setString('password', EncryptUtil.aesEncode(g_userInfo.password));
} else {
Storage.remove('username');
Storage.remove('password');
}
var api = ServicePath.loginUrl;
print(api);
try {
print('开始处理登录请求...');
print('username = ${g_userInfo.username}');
print('password = ${g_userInfo.password}');
Response response;
Dio dio = Dio();
String random = RandomBit(6); //flutter dart生成N位随机数
response = await dio.post(api, data: {
"username": g_userInfo.username,
"password": g_userInfo.password,
"sign": GenerateMd5(APPkey + random),
"random": random,
});
print('response = ${response.toString()}');
//I/flutter ( 5242): {"ret":200,"data":{"is_login":true,"user_id":3,"token":"32EE57A0109A3D1D6590CFD3DEBA71820F77AB654093C1DE750347C88D1A41CF"},"msg":""}
if (response.statusCode == 200) {
// Storage.setString('userInfo', json.encode(response.data["userinfo"]));
// //Navigator.pop(context);
// Navigator.pushNamed(context, '/tabs', arguments: g_iIndex);
//print('response.data["data"]["is_login"] = ${response.data["data"]["is_login"]}');
//I/flutter ( 5242): response.data["data"]["is_login"] = true
//{
// "ret": 200,
// "data": {
// "is_login": true,
// "user_id": 1,
// "token": "B93EC91FA2FE293B7077162D4527FC4BB228CD6C0A4F24A882B9A8BBE6C3FB47"
// },
// "msg": ""
// }
print('response = ${response}');
//response = {"ret":406,"data":{},"msg":"非法请求:签名错误"}
if (true == response.data["data"]["is_login"]) {
print('登录成功');
print('response.data = ${response.data}');
//保存用户信息
Storage.setString('userInfo', json.encode(response.data["data"]));
g_userInfo.setUserInfo(theMapUserInfoRet: await getMapFromJson(response.data));
//获取用户所属分组和相应权限
getUserGroupAll().then((value) {
//Navigator.pop(context);
Navigator.pushNamed(context, '/tabs', arguments: g_iIndex);
});
} else {
print('登录失败:${response.data["data"]}');
Fluttertoast.showToast(
msg: '登录失败:用户名或密码不正确。}',
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
);
print('登录失败:${response.data["data"]}');
}
print('登录过程正常完成');
} else {
throw Exception('后端接口出现异常,请检测代码和服务器情况.........');
}
} catch (e) {
print('登录过程异常...');
print('ERROR:======>${e}');
Fluttertoast.showToast(
msg: 'ERROR:======>${e}',
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
);
}
return;
}
@override
Widget build(BuildContext context) {
//FocusScope.of(context).requestFocus(FocusNode());
//FocusScope.of(context).unfocus();
return Scaffold(
body: Container(
padding: EdgeInsets.only(top: 20, bottom: 20, left: 20, right: 20),
child: ListView(
children: <Widget>[
Center(
child: Container(
margin: EdgeInsets.only(top: 30),
height: ScreenUtil().setWidth(160),
width: ScreenUtil().setWidth(160),
//child: Image.asset('assets/images/user.png', fit: BoxFit.cover),
child: Image.asset('assets/images/ybsthbj.png', fit: BoxFit.fitHeight),
// child: Image.network(
// 'https://www.itying.com/images/flutter/list5.jpg',
// fit: BoxFit.cover),
),
),
SizedBox(height: 30),
JdText(
height: ScreenUtil().setHeight(300),
title: '账号:',
text: "请输入账号",
onChanged: (String value) {
// print(value);
g_userInfo.username = value;
},
endBtn: 'ClearBtn',
controller: TextEditingController(text: g_userInfo.username),
),
SizedBox(height: 10),
JdText(
height: ScreenUtil().setHeight(300),
title: '密码:',
text: "请输入密码",
password: true,
onChanged: (String value) {
// print(value);
g_userInfo.password = value;
},
endBtn: 'ShowHiddenBtn',
controller: TextEditingController(text: g_userInfo.password),
),
SizedBox(height: 10),
Container(
padding: EdgeInsets.all(ScreenUtil().setWidth(20)),
child: Stack(
children: <Widget>[
// Align(
// alignment: Alignment.centerLeft,
// child: InkWell(
// onTap: () {
// Navigator.pushNamed(context, '/forgotPassword');
// },
// child: Text('忘记密码'),
// ),
// ),
Align(
alignment: Alignment.topRight,
child: InkWell(
onTap: () {
Navigator.pushNamed(context, '/registerFirst');
},
//child: Text('记住密码'),
child: Container(
alignment: Alignment(1, -1),
width: 150,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('记住密码:'),
Container(
alignment: Alignment(1, -1),
height: 22,
width: 22,
child: Checkbox(
value: bRemmberPW,
activeColor: Colors.blue,
onChanged: (bool val) {
this.setState(() {
bRemmberPW = !bRemmberPW;
});
Storage.setBool('bRemmberPW', bRemmberPW);
},
),
),
],
),
),
),
),
// Align(
// alignment: Alignment.centerRight,
// child: InkWell(
// onTap: () {
// Navigator.pushNamed(context, '/registerFirst');
// },
// child: Text(
// '新用户注册',
// style: TextStyle(
// // 创建 paint 对象,设置 color 属性为想要的颜色
// background: Paint()..color = Color.fromRGBO(218, 218, 218, 1)),
// ),
// ),
// ),
],
),
),
SizedBox(height: 20),
JdButton(
height: ScreenUtil().setHeight(350),
text: "登录",
color: Colors.blueAccent,
onTop: doLogin,
)
],
),
),
);
}
}