wechat_auth_cubit.dart
2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import 'package:appframe/config/locator.dart';
import 'package:appframe/data/repositories/wechat_auth_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluwx/fluwx.dart';
class WechatAuthState extends Equatable {
final String? sessionCode;
final String? userCode;
final String? classCode;
final int? userType;
final String? stuId;
final String? result;
const WechatAuthState(this.sessionCode, this.userCode, this.classCode, this.userType, this.stuId, this.result);
WechatAuthState copyWith({
String? sessionCode,
String? userCode,
String? classCode,
int? userType,
String? stuId,
String? result,
}) {
return WechatAuthState(
sessionCode ?? this.sessionCode,
userCode ?? this.userCode,
classCode ?? this.classCode,
userType ?? this.userType,
stuId ?? this.stuId,
result ?? this.result,
);
}
@override
List<Object?> get props => [sessionCode, userCode, classCode, userType, stuId, result];
}
class WechatAuthCubit extends Cubit<WechatAuthState> {
late final Fluwx _fluwx;
late final WechatAuthRepository _wechatAuthRepository;
WechatAuthCubit(super.initialState) {
_fluwx = Fluwx();
_register();
_subscribe();
_wechatAuthRepository = getIt<WechatAuthRepository>();
}
Future<void> _register() async {
await _fluwx.registerApi(appId: "wx8c32ea248f0c7765", universalLink: "https://univerallink.banxe.cn/link/");
}
void _subscribe() {
_fluwx.addSubscriber((response) async {
if (response is WeChatAuthResponse) {
var result = 'state :${response.state} \n code:${response.code}';
// emit(state.copyWith(result: result));
dynamic data = await _wechatAuthRepository.codeToSk(response.code!);
print("===============================================");
print(data.toString());
var dd = data['data'];
var role = dd['roles'][0];
print(dd['sessionCode']);
print(dd['userCode']);
print(role['classCode']);
print(role['userType']);
print(role['stuId']);
emit(
state.copyWith(
sessionCode: dd['sessionCode'],
userCode: dd['userCode'],
classCode: role['classCode'],
userType: role['userType'],
stuId: role['stuId'],
),
);
}
});
}
void auth() async {
var result = await _fluwx.authBy(
which: NormalAuth(scope: 'snsapi_userinfo', state: 'wechat_sdk_test'),
);
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
print(result);
}
}