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.
 
 
 
 
 
 

350 lines
10 KiB

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