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

349 regels
10 KiB

  1. import 'package:chat/data/UserData.dart';
  2. import 'package:chat/generated/i18n.dart';
  3. import 'package:chat/utils/screen.dart';
  4. import 'package:connectivity/connectivity.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter_screenutil/flutter_screenutil.dart';
  8. import 'package:oktoast/oktoast.dart';
  9. import 'package:shared_preferences/shared_preferences.dart';
  10. import 'package:wifi_info_plugin/wifi_info_plugin.dart';
  11. import '../utils/OtherLogin.dart';
  12. import 'LoginPage.dart';
  13. import 'Registerpage.dart';
  14. import 'package:fluwx_no_pay/fluwx_no_pay.dart' as fluwx;
  15. import 'package:chat/data/constants.dart';
  16. import 'package:chat/utils/HttpUtil.dart';
  17. import 'package:dio/dio.dart';
  18. import 'dart:convert';
  19. import 'package:chat/utils/TokenMgr.dart';
  20. const RATE_NUM = 0.82;
  21. class IndexPage extends StatefulWidget {
  22. IndexPage({Key key}) : super(key: key);
  23. _IndexPageState createState() => _IndexPageState();
  24. }
  25. class _IndexPageState extends State<IndexPage> {
  26. bool isShow = false;
  27. var subscription = Connectivity();
  28. bool isOffline = false;
  29. var subStript;
  30. GlobalKey<ScaffoldState> registKey = new GlobalKey();
  31. @override
  32. void initState() {
  33. super.initState();
  34. fluwx.responseFromAuth.listen((data) {
  35. if (data.errCode == 0) {
  36. getLoginData(data.code);
  37. } else {}
  38. });
  39. // subscription.onConnectivityChanged.listen((ConnectivityResult result) {
  40. // if (result == ConnectivityResult.none) {
  41. // if (mounted) {
  42. // setState(() {
  43. // isOffline = true;
  44. // });
  45. // }
  46. // } else {
  47. // if (mounted) {
  48. // setState(() {
  49. // if (isOffline == true) {
  50. // isOffline = false;
  51. // autoLogin();
  52. // }
  53. // });
  54. // }
  55. // }
  56. // });
  57. autoLogin();
  58. }
  59. void autoLogin({showLoading: false}) async {
  60. SharedPreferences prefs = await SharedPreferences.getInstance();
  61. String autoLoginKey = prefs.getString(Constants.AutoLoginKey);
  62. if (autoLoginKey != null) {
  63. if (UserData().basicInfo.userId == null) {
  64. Future.delayed(Duration(seconds: 10), () {
  65. if (mounted) {
  66. setState(() {
  67. isOffline = true;
  68. });
  69. }
  70. });
  71. var data = {
  72. "key": autoLoginKey,
  73. "language": UserData().language,
  74. };
  75. data['sign'] = TokenMgr().getSign(data);
  76. print('~~~~~~~~~~~indexPage autologin ~~~~~~~~~~');
  77. Response res = await HttpUtil()
  78. .post('user/auto/login', data: data, isShowLoading: showLoading);
  79. var resData = res.data;
  80. if (resData['code'] == 0) {
  81. print('### 跳转 成功--');
  82. HttpUtil().changePage(context, resData);
  83. } else {
  84. isShow = true;
  85. setState(() {});
  86. showToast(resData['msg']);
  87. }
  88. } else {
  89. HttpUtil().changePage(context, {
  90. 'data': {
  91. 'userid': UserData().basicInfo.userId,
  92. 'sex': UserData().basicInfo.sex,
  93. 'mobile': UserData().mobile.toString(),
  94. 'bindId': UserData().agentId
  95. }
  96. });
  97. }
  98. } else {
  99. print('### 跳转 失败 autoLoginKey null--');
  100. isShow = true;
  101. setState(() {});
  102. }
  103. }
  104. getLoginData(String code) async {
  105. Response f = await HttpUtil().getDataWX(
  106. 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=${Constants.AppId}&secret=${Constants.Secret}&code=$code&grant_type=authorization_code');
  107. var tokenData = json.decode(f.data);
  108. print(tokenData);
  109. if (tokenData['unionid'] == null || tokenData['unionid'] == '') {
  110. return;
  111. }
  112. var data = {
  113. "onlyid": tokenData['unionid'],
  114. "type": 0,
  115. "language": UserData().language,
  116. };
  117. data['sign'] = TokenMgr().getSign(data);
  118. data['openid'] = tokenData['openid'];
  119. data['lng'] = UserData().longitude;
  120. data['lat'] = UserData().latitude;
  121. try {
  122. WifiInfoWrapper wifiObject = await WifiInfoPlugin.wifiDetails;
  123. if (wifiObject != null) {
  124. data['routerName'] = wifiObject.ssid.replaceAll('"', '');
  125. data['mac'] = wifiObject.bssId;
  126. }
  127. } catch (e) {
  128. print(e);
  129. }
  130. Response res = await HttpUtil()
  131. .post('user/auth/login', data: data, isShowLoading: true);
  132. var resData = res.data;
  133. print(resData);
  134. if (resData['code'] != 0) {
  135. showToast(resData['msg']);
  136. return;
  137. }
  138. if (resData['data'] != null) {
  139. HttpUtil().changePage(Constants.getCurrentContext(), resData);
  140. }
  141. }
  142. @override
  143. void dispose() {
  144. subStript?.cancel();
  145. super.dispose();
  146. }
  147. @override
  148. Widget build(BuildContext context) {
  149. ScreenUtil.instance = ScreenUtil(width: 360, height: 744)..init(context);
  150. return new Scaffold(
  151. key: registKey,
  152. backgroundColor: Colors.white,
  153. body: SafeArea(
  154. child: Center(
  155. child: Container(
  156. height: MediaQuery.of(context).size.height,
  157. width: MediaQuery.of(context).size.width,
  158. child: _buildBody(),
  159. ),
  160. ),
  161. ));
  162. }
  163. Widget buildNetState() {
  164. return isOffline
  165. ? Container(
  166. margin: EdgeInsets.only(top: ScreenUtil().setHeight(450)),
  167. width: Screen.width,
  168. child: Column(
  169. children: <Widget>[
  170. Container(
  171. child: Text(
  172. I18n.of(context).net_error,
  173. style: TextStyle(color: Color(0xFF8F8E8E), fontSize: 21.5),
  174. ),
  175. ),
  176. InkWell(
  177. onTap: () {
  178. print('~~~~~~~~~~~indexPage click autologin ~~~~~~~~~~');
  179. autoLogin(showLoading: true);
  180. },
  181. child: Container(
  182. margin: EdgeInsets.only(top: 28),
  183. padding:
  184. EdgeInsets.symmetric(horizontal: 26, vertical: 9.5),
  185. decoration: BoxDecoration(
  186. border: Border.all(color: const Color(0xFF3875E9)),
  187. borderRadius: BorderRadius.circular(8)),
  188. child: Text(
  189. I18n.of(context).re_connect,
  190. style:
  191. TextStyle(color: Color(0xFF3875E9), fontSize: 20.22),
  192. ),
  193. ),
  194. )
  195. ],
  196. ))
  197. : Container();
  198. }
  199. Widget _buildBody() {
  200. List<Widget> show = [
  201. _buildLoginButton(),
  202. _buildRegisterButton(),
  203. _buildOtherLogin(),
  204. ];
  205. List<Widget> hidden = [
  206. buildNetState(),
  207. ];
  208. return Stack(
  209. alignment: Alignment.center,
  210. children: <Widget>[
  211. Positioned(
  212. top: 0, child: isOffline ? _buildErrorBg() : _buildNormalBg()),
  213. Column(
  214. children: isShow ? show : hidden,
  215. )
  216. ],
  217. );
  218. }
  219. //构建底部第三方登陆
  220. Widget _buildOtherLogin() {
  221. return new Expanded(
  222. child: Column(
  223. mainAxisAlignment: MainAxisAlignment.end,
  224. children: <Widget>[
  225. new Container(
  226. alignment: Alignment.bottomCenter,
  227. child: OtherLogin().builder(context),
  228. )
  229. ],
  230. ),
  231. );
  232. }
  233. //构建登陆按钮
  234. Widget _buildLoginButton() {
  235. Text text = fixedText(I18n.of(context).login,
  236. fontSize: Constants.ShaderButtonFontSize, color: Colors.white);
  237. return InkWell(
  238. onTap: () {
  239. Navigator.of(context).push(
  240. new MaterialPageRoute(
  241. builder: (context) {
  242. return LoginPage();
  243. },
  244. ),
  245. );
  246. },
  247. child: Container(
  248. margin: EdgeInsets.only(top: ScreenUtil().setHeight(460)),
  249. alignment: Alignment.center,
  250. height: Constants.ShaderButtonHeight,
  251. width: ScreenUtil.instance.setWidth(282.9),
  252. decoration: BoxDecoration(
  253. color: Constants.ConfrimButtonColor,
  254. //border: Border.all(color: const Color(0x803875E9)),
  255. borderRadius:
  256. BorderRadius.all(Radius.circular(Constants.BigButtonRadius))),
  257. child: text,
  258. ),
  259. );
  260. }
  261. //构建注册按钮
  262. Widget _buildRegisterButton() {
  263. Text text = fixedText(I18n.of(context).number_registration,
  264. fontSize: Constants.ShaderButtonFontSize,
  265. color: Constants.ConfrimButtonColor);
  266. return InkWell(
  267. onTap: () {
  268. Navigator.of(context).push(
  269. new MaterialPageRoute(
  270. builder: (context) {
  271. return RegisterPage(
  272. type: PageType.register.index,
  273. );
  274. },
  275. ),
  276. );
  277. },
  278. child: Container(
  279. margin: EdgeInsets.only(top: 30),
  280. alignment: Alignment.center,
  281. height: Constants.ShaderButtonHeight,
  282. width: ScreenUtil.instance.setWidth(282.9),
  283. decoration: BoxDecoration(
  284. border: Border.all(color: Constants.ConfrimButtonColor),
  285. borderRadius:
  286. BorderRadius.all(Radius.circular(Constants.BigButtonRadius))),
  287. child: text,
  288. ),
  289. );
  290. }
  291. // Widget _buildLogo() {
  292. // return Container(
  293. // alignment: Alignment.center,
  294. // margin: EdgeInsets.only(top: ScreenUtil.instance.setHeight(53)),
  295. // child: Image.asset(
  296. // 'assets/images/login/SY_logo.png',
  297. // width: ScreenUtil.instance.setWidth(120.5),
  298. // ));
  299. // }
  300. Widget _buildNormalBg() {
  301. return Container(
  302. margin: EdgeInsets.only(top: ScreenUtil.instance.setHeight(35)),
  303. child: Image.asset(
  304. 'assets/images/login/SY_bg.png',
  305. width: Screen.width,
  306. //width: ScreenUtil.instance.setHeight(266),
  307. ),
  308. );
  309. }
  310. Widget _buildErrorBg() {
  311. return Container(
  312. margin: EdgeInsets.only(
  313. top: ScreenUtil.instance.setHeight(180),
  314. bottom: ScreenUtil.instance.setHeight(49)),
  315. child: Image.asset(
  316. 'assets/images/net_error.png',
  317. width: ScreenUtil.instance.setWidth(150),
  318. ),
  319. );
  320. }
  321. }