Hibok
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

351 рядки
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. textScaleFactor: 1.0,
  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. textScaleFactor: 1.0,
  192. style:
  193. TextStyle(color: Color(0xFF3875E9), fontSize: 20.22),
  194. ),
  195. ),
  196. )
  197. ],
  198. ))
  199. : Container();
  200. }
  201. Widget _buildBody() {
  202. List<Widget> show = [
  203. _buildLoginButton(),
  204. _buildRegisterButton(),
  205. _buildOtherLogin(),
  206. ];
  207. List<Widget> hidden = [
  208. buildNetState(),
  209. ];
  210. return Stack(
  211. alignment: Alignment.center,
  212. children: <Widget>[
  213. Positioned(
  214. top: 0, child: isOffline ? _buildErrorBg() : _buildNormalBg()),
  215. Column(
  216. children: isShow ? show : hidden,
  217. )
  218. ],
  219. );
  220. }
  221. //构建底部第三方登陆
  222. Widget _buildOtherLogin() {
  223. return new Expanded(
  224. child: Column(
  225. mainAxisAlignment: MainAxisAlignment.end,
  226. children: <Widget>[
  227. new Container(
  228. alignment: Alignment.bottomCenter,
  229. child: OtherLogin().builder(context),
  230. )
  231. ],
  232. ),
  233. );
  234. }
  235. //构建登陆按钮
  236. Widget _buildLoginButton() {
  237. Text text = fixedText(I18n.of(context).login,
  238. fontSize: Constants.ShaderButtonFontSize, color: Colors.white);
  239. return InkWell(
  240. onTap: () {
  241. Navigator.of(context).push(
  242. new MaterialPageRoute(
  243. builder: (context) {
  244. return LoginPage();
  245. },
  246. ),
  247. );
  248. },
  249. child: Container(
  250. margin: EdgeInsets.only(top: ScreenUtil().setHeight(460)),
  251. alignment: Alignment.center,
  252. height: Constants.ShaderButtonHeight,
  253. width: ScreenUtil.instance.setWidth(282.9),
  254. decoration: BoxDecoration(
  255. color: Constants.ConfrimButtonColor,
  256. //border: Border.all(color: const Color(0x803875E9)),
  257. borderRadius:
  258. BorderRadius.all(Radius.circular(Constants.BigButtonRadius))),
  259. child: text,
  260. ),
  261. );
  262. }
  263. //构建注册按钮
  264. Widget _buildRegisterButton() {
  265. Text text = fixedText(I18n.of(context).number_registration,
  266. fontSize: Constants.ShaderButtonFontSize,
  267. color: Constants.ConfrimButtonColor);
  268. return InkWell(
  269. onTap: () {
  270. Navigator.of(context).push(
  271. new MaterialPageRoute(
  272. builder: (context) {
  273. return RegisterPage(
  274. type: PageType.register.index,
  275. );
  276. },
  277. ),
  278. );
  279. },
  280. child: Container(
  281. margin: EdgeInsets.only(top: 30),
  282. alignment: Alignment.center,
  283. height: Constants.ShaderButtonHeight,
  284. width: ScreenUtil.instance.setWidth(282.9),
  285. decoration: BoxDecoration(
  286. border: Border.all(color: Constants.ConfrimButtonColor),
  287. borderRadius:
  288. BorderRadius.all(Radius.circular(Constants.BigButtonRadius))),
  289. child: text,
  290. ),
  291. );
  292. }
  293. // Widget _buildLogo() {
  294. // return Container(
  295. // alignment: Alignment.center,
  296. // margin: EdgeInsets.only(top: ScreenUtil.instance.setHeight(53)),
  297. // child: Image.asset(
  298. // 'assets/images/login/SY_logo.png',
  299. // width: ScreenUtil.instance.setWidth(120.5),
  300. // ));
  301. // }
  302. Widget _buildNormalBg() {
  303. return Container(
  304. margin: EdgeInsets.only(top: ScreenUtil.instance.setHeight(35)),
  305. child: Image.asset(
  306. 'assets/images/login/SY_bg.png',
  307. width: Screen.width,
  308. //width: ScreenUtil.instance.setHeight(266),
  309. ),
  310. );
  311. }
  312. Widget _buildErrorBg() {
  313. return Container(
  314. margin: EdgeInsets.only(
  315. top: ScreenUtil.instance.setHeight(180),
  316. bottom: ScreenUtil.instance.setHeight(49)),
  317. child: Image.asset(
  318. 'assets/images/net_error.png',
  319. width: ScreenUtil.instance.setWidth(150),
  320. ),
  321. );
  322. }
  323. }