Hibok
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

355 rindas
11 KiB

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