wechat_login_service.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:wechat_kit/wechat_kit.dart';
  5. import '../di/get_it.dart';
  6. import '../utils/atmob_log.dart';
  7. @lazySingleton
  8. class WechatLoginService {
  9. final String tag = "WechatLoginService";
  10. final String _appId = "wx21272929e8fd33e9"; //AppID
  11. final String? _universalLink = null; // universalLink
  12. late final StreamSubscription<WechatResp> _respSub;
  13. void Function(String code)? _onSuccess;
  14. void Function(int code, String msg)? _onError;
  15. VoidCallback? _onCancel;
  16. String? lastCode;
  17. WechatLoginService() {
  18. AtmobLog.d(tag, '$tag....init');
  19. _init();
  20. }
  21. void _init() {
  22. // 注册 App 并监听回调
  23. WechatKitPlatform.instance.registerApp(
  24. appId: _appId,
  25. universalLink: _universalLink,
  26. );
  27. _respSub = WechatKitPlatform.instance.respStream().listen(_handleWechatResp);
  28. }
  29. void login({
  30. required void Function(String code) onSuccess,
  31. void Function(int code, String msg)? onError,
  32. VoidCallback? onCancel,
  33. }) async {
  34. try {
  35. final isInstalled = await WechatKitPlatform.instance.isInstalled();
  36. if (!isInstalled) {
  37. onError?.call(
  38. WechatResp.kErrorCodeUnsupport,
  39. _getErrorMsg(WechatResp.kErrorCodeUnsupport),
  40. );
  41. return;
  42. }
  43. // 保存回调函数
  44. _onSuccess = onSuccess;
  45. _onError = onError;
  46. _onCancel = onCancel;
  47. // 发送授权请求
  48. WechatKitPlatform.instance.auth(
  49. scope: <String>[WechatScope.kSNSApiUserInfo],
  50. state: 'wechat_login',
  51. );
  52. } catch (e) {
  53. onError?.call(-999, '微信初始化失败: ${e.toString()}');
  54. }
  55. }
  56. void _handleWechatResp(WechatResp resp) {
  57. if (resp is WechatAuthResp) {
  58. debugPrint("【WechatLoginService】收到微信响应: ${resp.errorCode} ${resp.errorMsg}");
  59. switch (resp.errorCode) {
  60. case WechatResp.kErrorCodeSuccess:
  61. if (resp.code != null) {
  62. lastCode = resp.code;
  63. _onSuccess?.call(resp.code!);
  64. } else {
  65. _onError?.call(-998, '未获取到授权 code');
  66. }
  67. break;
  68. case WechatResp.kErrorCodeUserCancel:
  69. _onCancel?.call();
  70. break;
  71. default:
  72. _onError?.call(resp.errorCode, _getErrorMsg(resp.errorCode));
  73. }
  74. // 一次登录完成后清空回调
  75. _clearCallbacks();
  76. }
  77. }
  78. void _clearCallbacks() {
  79. _onSuccess = null;
  80. _onError = null;
  81. _onCancel = null;
  82. }
  83. String _getErrorMsg(int code) {
  84. switch (code) {
  85. case WechatResp.kErrorCodeCommon:
  86. return '普通错误,可能是网络问题';
  87. case WechatResp.kErrorCodeUserCancel:
  88. return '用户取消了登录';
  89. case WechatResp.kErrorCodeSentFail:
  90. return '发送请求失败';
  91. case WechatResp.kErrorCodeAuthDeny:
  92. return '授权失败';
  93. case WechatResp.kErrorCodeUnsupport:
  94. return '未检测到微信客户端';
  95. default:
  96. return '未知错误: $code';
  97. }
  98. }
  99. void dispose() {
  100. _respSub?.cancel();
  101. }
  102. static WechatLoginService getInstance() => getIt.get<WechatLoginService>();
  103. }