YSBindInfo.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutterappfuyou/code/YSMeans.dart';
  5. import 'package:fluwx/fluwx.dart' as fluwx;
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. import '../YSPage.dart';
  8. import 'base/YSBase.dart';
  9. import 'base/YSNetWorking.dart';
  10. class YSBindInfo extends StatefulWidget {
  11. @override
  12. _YSBindInfoState createState() => _YSBindInfoState();
  13. }
  14. class _YSBindInfoState extends State<YSBindInfo> {
  15. Map _info = {};
  16. bool _isWechat = false;
  17. bool _isCheck = false;
  18. @override
  19. void initState() {
  20. fluwx.weChatResponseEventHandler.distinct((a, b) => a == b).listen((res) {
  21. if (res is fluwx.WeChatAuthResponse) {
  22. if(res.isSuccessful&&_isCheck==false){
  23. _isCheck = true;
  24. print("code================== ${res.code}");
  25. _bindWeChat(res.code);
  26. }
  27. }
  28. });
  29. Future.delayed(Duration(seconds: 0)).then((value) {
  30. _getUserData();
  31. });
  32. super.initState();
  33. }
  34. _bindWeChat(code) async{
  35. Map dict = await ysRequestHttp(context, requestType.post, 'sign/appInWechat', {'code':code});
  36. if(dict!=null){
  37. Map data = dict['data'];
  38. if(data['bind_code']!=null){
  39. Map dictSub = await ysRequestHttp(context, requestType.post, 'sign/bindOauthByCode', {'bind_code':data['bind_code']});
  40. if(dictSub!=null){
  41. _getBindData();
  42. }
  43. }
  44. }
  45. }
  46. @override
  47. void dispose() {
  48. super.dispose();
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. return YSBase(
  53. ystitle: '手机绑定',
  54. yschild: Container(
  55. decoration: BoxDecoration(
  56. color: Colors.white,
  57. borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20))
  58. ),
  59. child: Column(
  60. crossAxisAlignment: CrossAxisAlignment.start,
  61. children: [
  62. Container(
  63. margin: EdgeInsets.only(top: 20,bottom: 10,left: 20,right: 20),
  64. child: Text('当前账号',style: TextStyle(fontSize: 14,color: Color(0xFF808080)),),
  65. ),
  66. Container(
  67. margin: EdgeInsets.only(left: 20,right: 20,bottom: 15),
  68. child: Row(
  69. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  70. children: [
  71. Text('已绑定手机',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
  72. Text(_info['phone']??'',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
  73. ],
  74. ),
  75. ),
  76. Container(
  77. width: MediaQuery.of(context).size.width,
  78. height: 1.5,
  79. child: Image.asset('lib/images/line.png'),
  80. ),
  81. Container(
  82. margin: EdgeInsets.only(top: 10,bottom: 10,left: 20,right: 20),
  83. child: Text('第三方帐号绑定',style: TextStyle(fontSize: 14,color: Color(0xFF808080)),),
  84. ),
  85. Container(
  86. margin: EdgeInsets.only(left: 20,right: 20,bottom: 5),
  87. child: Row(
  88. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  89. children: [
  90. Text('微信',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
  91. CupertinoButton(
  92. padding: EdgeInsets.all(0),
  93. onPressed: () async{
  94. if(!_isWechat){
  95. _isCheck = false;
  96. _weChatLogin();
  97. }
  98. },
  99. child: Container(
  100. height: 30,
  101. padding: EdgeInsets.only(left: 10,right: 10),
  102. alignment: Alignment.center,
  103. decoration: BoxDecoration(
  104. borderRadius: BorderRadius.all(Radius.circular(15)),
  105. border: Border.all(color: Color(0xFFEB5781),width: 1)
  106. ),
  107. child: Text(_isWechat?'已绑定':'未绑定',style: TextStyle(fontSize: 14,color: Color(0xFFEB5781)),),
  108. ),
  109. )
  110. ],
  111. ),
  112. ),
  113. Container(
  114. width: MediaQuery.of(context).size.width,
  115. height: 1.5,
  116. child: Image.asset('lib/images/line.png'),
  117. ),
  118. ],
  119. ),
  120. ),
  121. );
  122. }
  123. _getUserData() async{
  124. Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'user/info', {});
  125. if(dict!=null){
  126. _info = dict;
  127. _getBindData();
  128. }
  129. }
  130. _getBindData() async{
  131. Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'user/info/oauthBinds', {});
  132. if(dict!=null){
  133. _isWechat = dict['data']['wechat']??false;
  134. setState(() {});
  135. }
  136. }
  137. _weChatLogin() async{
  138. fluwx.sendWeChatAuth(
  139. scope: 'snsapi_userinfo',
  140. state: 'wechat_sdk_demo_test'
  141. ).then((data) {
  142. print('data===========$data');
  143. });
  144. }
  145. }
  146. class YSBindWeChat extends StatefulWidget {
  147. final String userName;
  148. final String passWord;
  149. const YSBindWeChat({Key key, this.userName, this.passWord}) : super(key: key);
  150. @override
  151. _YSBindWeChatState createState() => _YSBindWeChatState();
  152. }
  153. class _YSBindWeChatState extends State<YSBindWeChat> {
  154. @override
  155. void initState() {
  156. super.initState();
  157. fluwx.weChatResponseEventHandler.distinct((a, b) => a == b).listen((res) {
  158. if (res is fluwx.WeChatAuthResponse) {
  159. if(res.isSuccessful){
  160. print("code================== ${res.code}");
  161. _weChatLogIn(res.code);
  162. }
  163. }
  164. });
  165. }
  166. _weChatLogIn(code) async{
  167. Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'sign/appInWechat', {'code':code,'type':2});
  168. if(dict!=null){
  169. Map data = dict['data'];
  170. if(true){//data['is_new_user']==
  171. _loginToPage(data['is_new_user']==true?data['token']:'');
  172. }else{
  173. Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
  174. _prefer.then((value){
  175. value.setString('token', dict['data'].toString());
  176. });
  177. ysFlutterToast(context, '该微信已绑定账号');
  178. }
  179. }
  180. }
  181. _loginToPage(String token) async{
  182. Map dict = await ysRequestHttp(context,requestType.post, 'sign/in', {
  183. 'username': widget.userName,
  184. 'password': widget.passWord,
  185. if(token.isNotEmpty)'token':token
  186. });
  187. if(dict!=null){
  188. SharedPreferences _perfer = await SharedPreferences.getInstance();
  189. _perfer.setString('token', dict['data'].toString());
  190. Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
  191. builder: (context){
  192. return YSMeans(isFirst: true,);
  193. }
  194. ), (route) => false);
  195. }
  196. }
  197. @override
  198. Widget build(BuildContext context) {
  199. return Scaffold(
  200. backgroundColor: Colors.white,
  201. body: SingleChildScrollView(
  202. child: Column(
  203. crossAxisAlignment: CrossAxisAlignment.start,
  204. children: [
  205. Container(
  206. margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top+50,left: 50),
  207. child: GestureDetector(
  208. child: Icon(Icons.arrow_back_ios,size: 25,color: Colors.pinkAccent,),
  209. onTap: (){
  210. Navigator.pop(context);
  211. },
  212. )
  213. ),
  214. Container(
  215. padding: EdgeInsets.only(left: 60,top: 40,right: 60),
  216. child: Column(
  217. crossAxisAlignment: CrossAxisAlignment.start,
  218. children: [
  219. Text('注册成功',style: TextStyle(fontSize: 20,color: Color(0xFF282828)),),
  220. Container(
  221. margin: EdgeInsets.only(top: 20,bottom: 40),
  222. child: Text('感谢您注册成为西安母子手册的会员\n马上就好',style: TextStyle(fontSize: 10,color: Color(0xFF707070)),),
  223. ),
  224. Container(
  225. margin: EdgeInsets.only(top: 150,bottom: 25),
  226. child: Text('开启微信一键登录,免去输入密码的烦恼,方便快捷!',style: TextStyle(fontSize: 10,color: Color(0xFF707070)),),
  227. ),
  228. GestureDetector(
  229. child: Container(
  230. margin: EdgeInsets.only(bottom: 10),
  231. width: MediaQuery.of(context).size.width-100,
  232. height: 40,
  233. decoration: BoxDecoration(
  234. color: Color(0xFFEA6C8F),
  235. borderRadius: BorderRadius.all(Radius.circular(20))
  236. ),
  237. alignment: Alignment.center,
  238. child: Text('绑定',style: TextStyle(fontSize: 15,color: Colors.white,decoration: TextDecoration.none),),
  239. ),
  240. onTap: (){
  241. if(Platform.isIOS){
  242. Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
  243. builder: (context){
  244. return YSPage();
  245. }
  246. ), (route) => false);
  247. }else{
  248. _weChatLogin();
  249. }
  250. },
  251. ),
  252. CupertinoButton(
  253. padding: EdgeInsets.all(0),
  254. child: Container(
  255. width: MediaQuery.of(context).size.width-100,
  256. child: Text('跳过',style: TextStyle(fontSize: 11,color: Colors.grey,decoration: TextDecoration.none),),
  257. alignment: Alignment.center,
  258. ),
  259. onPressed: (){
  260. FocusScope.of(context).unfocus();
  261. Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
  262. builder: (context){
  263. return YSMeans(isFirst: true,);
  264. }
  265. ), (route) => false);
  266. },
  267. ),
  268. ],
  269. ),
  270. ),
  271. ],
  272. ),
  273. ),
  274. );
  275. }
  276. _weChatLogin() async{
  277. fluwx.sendWeChatAuth(
  278. scope: 'snsapi_userinfo',
  279. state: 'wechat_sdk_demo_test'
  280. ).then((data) {
  281. print('data===========$data');
  282. });
  283. }
  284. }