Hibok
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

219 líneas
6.8 KiB

  1. import 'dart:io';
  2. import 'package:apple_sign_in/apple_sign_in.dart';
  3. import 'package:chat/data/UserData.dart';
  4. import 'package:chat/generated/i18n.dart';
  5. import 'package:chat/utils/screen.dart';
  6. import 'package:dio/dio.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_facebook_login/flutter_facebook_login.dart';
  9. import 'package:fluwx_no_pay/fluwx_no_pay.dart' as fluwx;
  10. import 'package:oktoast/oktoast.dart';
  11. import 'package:wifi_info_plugin/wifi_info_plugin.dart';
  12. import 'HttpUtil.dart';
  13. import 'LoadingDialog.dart';
  14. import 'TokenMgr.dart';
  15. import 'apple_sign_in_btn.dart' as apple;
  16. class OtherLogin {
  17. static final FacebookLogin facebookSignIn = new FacebookLogin();
  18. static bool isAvailableForAppleSignIn = false;
  19. ///是否支持苹果登录-ios苹果登录只支持ios13+手机
  20. Widget _buildSelection(context) {
  21. List<Widget> loginList = [];
  22. if(Platform.isAndroid ){
  23. // fluwx.isWeChatInstalled() 这里ios要判断是否已经安装
  24. loginList.add(InkWell(
  25. borderRadius: BorderRadius.all(Radius.circular(30.0)),
  26. child: new Image.asset(
  27. 'assets/images/login/dly_icon2.png',
  28. height: 24,
  29. ),
  30. onTap: () {
  31. print('微信登陆');
  32. fluwx.sendAuth(
  33. scope: "snsapi_userinfo", state: "wechat_sdk_demo_test");
  34. },
  35. ));
  36. }
  37. loginList.add(InkWell(
  38. borderRadius: BorderRadius.all(Radius.circular(30.0)),
  39. child: new Image.asset(
  40. 'assets/images/login/dly_icon3.png',
  41. height: 24,
  42. ),
  43. onTap: () async {
  44. print('facebook');
  45. facebookSignIn.loginBehavior = FacebookLoginBehavior.webViewOnly;
  46. final FacebookLoginResult result =
  47. await facebookSignIn.logIn(['email']);
  48. switch (result.status) {
  49. case FacebookLoginStatus.loggedIn:
  50. final FacebookAccessToken accessToken = result.accessToken;
  51. thirdLogin(context, accessToken.userId, 1);
  52. break;
  53. case FacebookLoginStatus.cancelledByUser:
  54. break;
  55. case FacebookLoginStatus.error:
  56. break;
  57. }
  58. },
  59. ));
  60. if (isAvailableForAppleSignIn) {
  61. loginList.add(apple.AppleSignInBtn(
  62. style: apple.ButtonStyle.black,
  63. type: apple.ButtonType.signIn,
  64. onPressed: () async {
  65. print('apple sign in');
  66. showDialog(
  67. context: context,
  68. barrierDismissible: false,
  69. builder: (BuildContext context) {
  70. return LoadingDialog(
  71. text: "",
  72. );
  73. });
  74. final AuthorizationResult result = await AppleSignIn.performRequests([
  75. AppleIdRequest(requestedScopes: [Scope.fullName])
  76. ]);
  77. Navigator.of(context).pop();
  78. switch (result.status) {
  79. case AuthorizationStatus.authorized:
  80. print('result.credential.user:${result.credential.user}');
  81. // Store user ID
  82. thirdLogin(context, result.credential.user, 2);
  83. break;
  84. case AuthorizationStatus.error:
  85. print("Sign in failed: ${result.error.localizedDescription}");
  86. break;
  87. case AuthorizationStatus.cancelled:
  88. print('User cancelled');
  89. break;
  90. }
  91. },
  92. ));
  93. // loginList.add(InkWell(
  94. // borderRadius: BorderRadius.all(Radius.circular(30.0)),
  95. // child: new Image.asset(
  96. // 'assets/images/login/dly_icon4.png',
  97. // height: 24,
  98. // ),
  99. // onTap: () async {
  100. // print('apple sign in');
  101. // showDialog(
  102. // context: context,
  103. // barrierDismissible: false,
  104. // builder: (BuildContext context) {
  105. // return LoadingDialog(
  106. // text: "",
  107. // );
  108. // });
  109. // final AuthorizationResult result = await AppleSignIn.performRequests([
  110. // AppleIdRequest(requestedScopes: [Scope.fullName])
  111. // ]);
  112. // Navigator.of(context).pop();
  113. // switch (result.status) {
  114. // case AuthorizationStatus.authorized:
  115. // print('result.credential.user:${result.credential.user}');
  116. // // Store user ID
  117. // thirdLogin(context, result.credential.user, 2);
  118. // break;
  119. // case AuthorizationStatus.error:
  120. // print("Sign in failed: ${result.error.localizedDescription}");
  121. // break;
  122. // case AuthorizationStatus.cancelled:
  123. // print('User cancelled');
  124. // break;
  125. // }
  126. // },
  127. // ));
  128. }
  129. return new Container(
  130. width: MediaQuery.of(context).size.width * 0.7,
  131. child: Row(
  132. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  133. children: loginList,
  134. ),
  135. );
  136. }
  137. thirdLogin(BuildContext context, String onlyId, int type) async {
  138. var data = {
  139. "onlyid": onlyId,
  140. "type": type,
  141. "language": UserData().language,
  142. };
  143. data['sign'] = TokenMgr().getSign(data);
  144. data['openid'] = onlyId;
  145. data['lng'] = UserData().longitude;
  146. data['lat'] = UserData().latitude;
  147. try {
  148. WifiInfoWrapper wifiObject = await WifiInfoPlugin.wifiDetails;
  149. if (wifiObject != null) {
  150. data['routerName'] = wifiObject.ssid;
  151. data['mac'] = wifiObject.bssId;
  152. }
  153. } catch (e) {
  154. print(e);
  155. }
  156. Response res = await HttpUtil()
  157. .post('user/auth/login', data: data, isShowLoading: true);
  158. var resData = res.data;
  159. print('user/auth/login $resData');
  160. if (resData['code'] != 0) {
  161. showToast(resData['msg']);
  162. return;
  163. }
  164. if (resData['data'] != null) {
  165. HttpUtil().changePage(context, resData);
  166. }
  167. }
  168. Widget builder(context) {
  169. return Column(
  170. children: <Widget>[
  171. _buildBottomLabel(context),
  172. SizedBox(height: 10),
  173. _buildSelection(context),
  174. SizedBox(height: 20),
  175. ],
  176. );
  177. }
  178. Widget _buildBottomLabel(context) {
  179. return new Row(
  180. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  181. children: <Widget>[
  182. new SizedBox(
  183. width: MediaQuery.of(context).size.width * 0.3,
  184. child: Divider(
  185. height: 1.0,
  186. color: Colors.grey,
  187. ),
  188. ),
  189. fixedText(
  190. I18n.of(context).other_login,
  191. color: Colors.grey[500],
  192. fontSize: 11.34,
  193. ),
  194. new SizedBox(
  195. width: MediaQuery.of(context).size.width * 0.3,
  196. child: Divider(
  197. height: 1.0,
  198. color: Colors.grey,
  199. ),
  200. ),
  201. ],
  202. );
  203. }
  204. }