LoginSecond.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import 'dart:async';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import '../styles/colors.dart';
  6. import 'dart:ui';
  7. import '../styles/totast.dart';
  8. import '../widget/ITextInput.dart';
  9. import 'package:flutter_redux/flutter_redux.dart';
  10. import '../net/HttpManager.dart';
  11. import '../net/Result.dart';
  12. import 'HomePage.dart';
  13. import '../redux/AppState.dart';
  14. import '../redux/UserRedux.dart';
  15. import '../model/UserInfo.dart';
  16. import '../widget/LinearButton.dart';
  17. import '../widget/Dialog.dart';
  18. import '../model/CustomerService.dart';
  19. import 'Rule.dart';
  20. class LoginSecond extends StatefulWidget {
  21. LoginSecond({Key key, this.phone, this.userId = 0}) : super(key: key);
  22. final String phone; // 用来储存传递过来的值
  23. final int userId;
  24. @override
  25. LoginSecondState createState() => LoginSecondState();
  26. }
  27. class LoginSecondState extends State<LoginSecond> {
  28. String _sessionID;
  29. bool isSend = false; //是否发送
  30. int sendTime = 0;
  31. String password;
  32. String surePass;
  33. String useToken;
  34. Timer timer;
  35. String _qq;
  36. @override
  37. void initState() {
  38. super.initState();
  39. Future.delayed(Duration.zero, () async {
  40. // sendMsg();
  41. Result res2 = await HttpManager.get('customerService/random');
  42. if (res2.success) {
  43. setState(() {
  44. _qq = CustomerService.fromJson(res2.data).qq;
  45. });
  46. }
  47. });
  48. }
  49. @override
  50. Widget build(BuildContext context) {
  51. return WillPopScope(
  52. child: Container(
  53. color: BG_SUB_COLOR,
  54. child: Scaffold(
  55. appBar: AppBar(
  56. // backgroundColor: BG_SUB_COLOR,
  57. centerTitle: true,
  58. elevation: 0,
  59. ),
  60. body: Container(
  61. color: BG_SUB_COLOR,
  62. child: CustomScrollView(
  63. slivers: <Widget>[
  64. SliverToBoxAdapter(
  65. child: Container(
  66. color: BG_SUB_COLOR,
  67. padding: EdgeInsets.symmetric(vertical: 30, horizontal: 15),
  68. child: Column(
  69. crossAxisAlignment: CrossAxisAlignment.start,
  70. children: <Widget>[
  71. Text(
  72. '请输入密码',
  73. style: TextStyle(color: Colors.white, fontSize: 28, fontWeight: FontWeight.w600),
  74. ),
  75. Container(
  76. height: widget.userId == 0 ? 5 : 20,
  77. ),
  78. widget.userId == 0
  79. ? Text(
  80. '您当前为首次登录,请设置您的密码',
  81. style: TextStyle(color: Colors.white, fontSize: 14),
  82. )
  83. : Container(),
  84. Container(
  85. margin: EdgeInsets.only(top: 10),
  86. child: ITextField(
  87. keyboardType: ITextInputType.password,
  88. prefixIcon: Container(
  89. padding: EdgeInsets.all(10),
  90. // color: PRIMARY_COLOR,
  91. child: Image.asset(
  92. 'images/list_icon_yanzhengma.png',
  93. width: 20,
  94. ),
  95. ),
  96. hintText: widget.userId == 0 ? '请设置您的密码' : '请输入您的密码',
  97. hintStyle: TextStyle(
  98. fontSize: 16,
  99. color: Color(0xFF727785),
  100. ),
  101. textStyle: TextStyle(color: Colors.white),
  102. fieldCallBack: (content) {
  103. setState(() {
  104. password = content;
  105. });
  106. },
  107. counterStyle: TextStyle(color: BG_SUB_COLOR, fontSize: 0),
  108. ),
  109. ),
  110. widget.userId == 0
  111. ? Container(
  112. margin: EdgeInsets.only(top: 10),
  113. child: ITextField(
  114. keyboardType: ITextInputType.password,
  115. prefixIcon: Container(
  116. padding: EdgeInsets.all(10),
  117. // color: PRIMARY_COLOR,
  118. child: Image.asset(
  119. 'images/list_icon_yanzhengma.png',
  120. width: 20,
  121. ),
  122. ),
  123. hintText: '请确认您的密码',
  124. hintStyle: TextStyle(
  125. fontSize: 16,
  126. color: Color(0xFF727785),
  127. ),
  128. textStyle: TextStyle(color: Colors.white),
  129. fieldCallBack: (content) {
  130. setState(() {
  131. surePass = content;
  132. });
  133. },
  134. counterStyle: TextStyle(color: BG_SUB_COLOR, fontSize: 0),
  135. ),
  136. )
  137. : Container(
  138. padding: EdgeInsetsDirectional.only(top: 10),
  139. child: InkWell(
  140. child: Text('忘记密码?',
  141. style: TextStyle(
  142. color: Theme.of(context).primaryColor,
  143. )),
  144. onTap: () {
  145. MyDialog.showDialog(context, '找回密码请联系客服',
  146. textList: [
  147. {'value': 'QQ:${_qq}', 'isImportant': false}
  148. ],
  149. title: '找回密码');
  150. },
  151. ),
  152. ),
  153. Container(
  154. height: 52,
  155. ),
  156. LinearButton(
  157. btntext: widget.userId == 0 ? '注册' : '登录',
  158. onTapHomeMenu: () async {
  159. print(password);
  160. if (password == '' || password == null) {
  161. Toast.show(context, '请输入密码', 1500, 'info');
  162. } else if (password != surePass && widget.userId == 0) {
  163. Toast.show(context, '两次密码输入不一致', 1500, 'info');
  164. } else {
  165. print(password);
  166. print(widget.phone);
  167. Toast.show(context, '加载中', -1, 'loading');
  168. final Result res =
  169. await HttpManager.post('auth/login', data: {'username': widget.phone, 'password': password, 'requireToken': true});
  170. Toast.hide();
  171. if (res.success) {
  172. final prefs = await SharedPreferences.getInstance();
  173. await prefs.setString('token', res.token);
  174. HttpManager.token = res.token;
  175. StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(UserInfo.fromJson(res.data)));
  176. Toast.show(context, '登录成功', 1500, 'success');
  177. Navigator.pushReplacement(
  178. context,
  179. CupertinoPageRoute(builder: (context) => HomePage()),
  180. );
  181. } else {
  182. Toast.show(context, res.error, 1500, 'info');
  183. }
  184. }
  185. },
  186. ),
  187. Container(
  188. padding: EdgeInsetsDirectional.only(top: 40),
  189. child: Center(
  190. child: InkWell(
  191. child: Text('注册/登录即同意用户隐私协议', style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 12)),
  192. onTap: () {
  193. Navigator.push(
  194. context,
  195. CupertinoPageRoute(builder: (context) => Rule()),
  196. );
  197. },
  198. ),
  199. ),
  200. ),
  201. ],
  202. ),
  203. ),
  204. )
  205. ],
  206. ),
  207. )),
  208. ),
  209. onWillPop: () {
  210. Toast.hide();
  211. Navigator.pop(context);
  212. return Future.value(false);
  213. });
  214. }
  215. Future<void> sendMsg() async {
  216. Toast.show(context, '加载中', -1, 'loading');
  217. final Result res = await HttpManager.get('rong/sendCode', data: {'phone': widget.phone});
  218. Toast.hide();
  219. if (res.success) {
  220. Toast.show(context, '发送成功', 1500, 'success');
  221. _sessionID = res.data;
  222. setState(() {
  223. isSend = true;
  224. });
  225. sendTime = 60;
  226. timer = Timer.periodic(Duration(seconds: 1), (timer) {
  227. if (sendTime == 0) {
  228. setState(() {
  229. isSend = false;
  230. });
  231. } else {
  232. setState(() {
  233. sendTime = sendTime - 1;
  234. });
  235. }
  236. });
  237. } else {
  238. Toast.show(context, res.error, 1500, 'info');
  239. }
  240. }
  241. }