import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutterappfuyou/code/YSMeans.dart'; import 'package:fluwx/fluwx.dart' as fluwx; import 'package:shared_preferences/shared_preferences.dart'; import '../YSPage.dart'; import 'base/YSBase.dart'; import 'base/YSNetWorking.dart'; class YSBindInfo extends StatefulWidget { @override _YSBindInfoState createState() => _YSBindInfoState(); } class _YSBindInfoState extends State { Map _info = {}; bool _isWechat = false; bool _isCheck = false; @override void initState() { fluwx.weChatResponseEventHandler.distinct((a, b) => a == b).listen((res) { if (res is fluwx.WeChatAuthResponse) { if(res.isSuccessful&&_isCheck==false){ _isCheck = true; print("code================== ${res.code}"); _bindWeChat(res.code); } } }); Future.delayed(Duration(seconds: 0)).then((value) { _getUserData(); }); super.initState(); } _bindWeChat(code) async{ Map dict = await ysRequestHttp(context, requestType.post, 'sign/appInWechat', {'code':code}); if(dict!=null){ Map data = dict['data']; if(data['bind_code']!=null){ Map dictSub = await ysRequestHttp(context, requestType.post, 'sign/bindOauthByCode', {'bind_code':data['bind_code']}); if(dictSub!=null){ _getBindData(); } } } } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { return YSBase( ystitle: '手机绑定', yschild: Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20)) ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( margin: EdgeInsets.only(top: 20,bottom: 10,left: 20,right: 20), child: Text('当前账号',style: TextStyle(fontSize: 14,color: Color(0xFF808080)),), ), Container( margin: EdgeInsets.only(left: 20,right: 20,bottom: 15), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('已绑定手机',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),), Text(_info['phone']??'',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),), ], ), ), Container( width: MediaQuery.of(context).size.width, height: 1.5, child: Image.asset('lib/images/line.png'), ), Container( margin: EdgeInsets.only(top: 10,bottom: 10,left: 20,right: 20), child: Text('第三方帐号绑定',style: TextStyle(fontSize: 14,color: Color(0xFF808080)),), ), Container( margin: EdgeInsets.only(left: 20,right: 20,bottom: 5), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('微信',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),), CupertinoButton( padding: EdgeInsets.all(0), onPressed: () async{ if(!_isWechat){ _isCheck = false; _weChatLogin(); } }, child: Container( height: 30, padding: EdgeInsets.only(left: 10,right: 10), alignment: Alignment.center, decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(15)), border: Border.all(color: Color(0xFFEB5781),width: 1) ), child: Text(_isWechat?'已绑定':'未绑定',style: TextStyle(fontSize: 14,color: Color(0xFFEB5781)),), ), ) ], ), ), Container( width: MediaQuery.of(context).size.width, height: 1.5, child: Image.asset('lib/images/line.png'), ), ], ), ), ); } _getUserData() async{ Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'user/info', {}); if(dict!=null){ _info = dict; _getBindData(); } } _getBindData() async{ Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'user/info/oauthBinds', {}); if(dict!=null){ _isWechat = dict['data']['wechat']??false; setState(() {}); } } _weChatLogin() async{ fluwx.sendWeChatAuth( scope: 'snsapi_userinfo', state: 'wechat_sdk_demo_test' ).then((data) { print('data===========$data'); }); } } class YSBindWeChat extends StatefulWidget { final String userName; final String passWord; const YSBindWeChat({Key key, this.userName, this.passWord}) : super(key: key); @override _YSBindWeChatState createState() => _YSBindWeChatState(); } class _YSBindWeChatState extends State { @override void initState() { super.initState(); fluwx.weChatResponseEventHandler.distinct((a, b) => a == b).listen((res) { if (res is fluwx.WeChatAuthResponse) { if(res.isSuccessful){ print("code================== ${res.code}"); _weChatLogIn(res.code); } } }); } _weChatLogIn(code) async{ Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'sign/appInWechat', {'code':code,'type':2}); if(dict!=null){ Map data = dict['data']; if(true){//data['is_new_user']== _loginToPage(data['is_new_user']==true?data['token']:''); }else{ Future _prefer = SharedPreferences.getInstance(); _prefer.then((value){ value.setString('token', dict['data'].toString()); }); ysFlutterToast(context, '该微信已绑定账号'); } } } _loginToPage(String token) async{ Map dict = await ysRequestHttp(context,requestType.post, 'sign/in', { 'username': widget.userName, 'password': widget.passWord, if(token.isNotEmpty)'token':token }); if(dict!=null){ SharedPreferences _perfer = await SharedPreferences.getInstance(); _perfer.setString('token', dict['data'].toString()); Navigator.pushAndRemoveUntil(context, MaterialPageRoute( builder: (context){ return YSMeans(isFirst: true,); } ), (route) => false); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top+50,left: 50), child: GestureDetector( child: Icon(Icons.arrow_back_ios,size: 25,color: Colors.pinkAccent,), onTap: (){ Navigator.pop(context); }, ) ), Container( padding: EdgeInsets.only(left: 60,top: 40,right: 60), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('注册成功',style: TextStyle(fontSize: 20,color: Color(0xFF282828)),), Container( margin: EdgeInsets.only(top: 20,bottom: 40), child: Text('感谢您注册成为西安母子手册的会员\n马上就好',style: TextStyle(fontSize: 10,color: Color(0xFF707070)),), ), Container( margin: EdgeInsets.only(top: 150,bottom: 25), child: Text('开启微信一键登录,免去输入密码的烦恼,方便快捷!',style: TextStyle(fontSize: 10,color: Color(0xFF707070)),), ), GestureDetector( child: Container( margin: EdgeInsets.only(bottom: 10), width: MediaQuery.of(context).size.width-100, height: 40, decoration: BoxDecoration( color: Color(0xFFEA6C8F), borderRadius: BorderRadius.all(Radius.circular(20)) ), alignment: Alignment.center, child: Text('绑定',style: TextStyle(fontSize: 15,color: Colors.white,decoration: TextDecoration.none),), ), onTap: (){ if(Platform.isIOS){ Navigator.pushAndRemoveUntil(context, MaterialPageRoute( builder: (context){ return YSPage(); } ), (route) => false); }else{ _weChatLogin(); } }, ), CupertinoButton( padding: EdgeInsets.all(0), child: Container( width: MediaQuery.of(context).size.width-100, child: Text('跳过',style: TextStyle(fontSize: 11,color: Colors.grey,decoration: TextDecoration.none),), alignment: Alignment.center, ), onPressed: (){ FocusScope.of(context).unfocus(); Navigator.pushAndRemoveUntil(context, MaterialPageRoute( builder: (context){ return YSMeans(isFirst: true,); } ), (route) => false); }, ), ], ), ), ], ), ), ); } _weChatLogin() async{ fluwx.sendWeChatAuth( scope: 'snsapi_userinfo', state: 'wechat_sdk_demo_test' ).then((data) { print('data===========$data'); }); } }