123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:sport/sharesdk/tencent_api_resp.dart';
- import 'package:sport/utils/toast.dart';
- import 'package:tencent_kit/tencent_kit.dart';
- extension MixerTencent on Tencent {
- /// 用户信息
- /// https://wiki.connect.qq.com/get_user_info
- Future<TencentUserInfoResp> getUserInfo({
- required String appId,
- required String openid,
- required String accessToken,
- }) {
- return HttpClient().getUrl(Uri.parse('https://graph.qq.com/user/get_user_info?access_token=$accessToken&oauth_consumer_key=$appId&openid=$openid')).then((HttpClientRequest request) {
- return request.close();
- }).then((HttpClientResponse response) async {
- if (response.statusCode == HttpStatus.ok) {
- final ContentType? contentType = response.headers.contentType;
- final Encoding encoding = Encoding.getByName(contentType?.charset) ?? utf8;
- final String content = await encoding.decodeStream(response);
- return TencentUserInfoResp.fromJson(json.decode(content) as Map<String, dynamic>);
- }
- throw HttpException('HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
- });
- }
- }
- mixin TencentMixin<T extends StatefulWidget> on State<T> {
- static const String _TENCENT_APPID = '1110701531';
- late Tencent _tencent;
- late final StreamSubscription<BaseResp> _respSubs;
- void _listenResp(BaseResp resp) async {
- if (resp.isSuccessful != true) return;
- if (resp is LoginResp) {
- final TencentUserInfoResp userInfo = await Tencent.instance.getUserInfo(
- appId: _TENCENT_APPID,
- openid: resp.openid!,
- accessToken: resp.accessToken!,
- );
- listenTencentLoginMsg(resp, userInfo);
- } else if (resp is ShareMsgResp) {
- listenTencentShareMsg(resp);
- }
- }
- listenTencentLoginMsg(LoginResp loginResp, TencentUserInfoResp resp) {}
- listenTencentShareMsg(ShareMsgResp resp) {}
- @override
- void initState() {
- super.initState();
- _tencent = Tencent.instance;
- _respSubs = _tencent.respStream().listen(_listenResp);
- }
- _initSDK() async {
- await _tencent.setIsPermissionGranted(granted: true);
- await _tencent.registerApp(appId: _TENCENT_APPID);
- }
- @override
- void dispose() {
- _respSubs.cancel();
- super.dispose();
- }
- Future<bool> tencentSupport() async {
- return await _tencent.isQQInstalled() || await _tencent.isTIMInstalled();
- }
- void tencentLogin() async {
- await _initSDK();
- if ((await tencentSupport()) == true) {
- _tencent.login(
- scope: <String>[TencentScope.GET_SIMPLE_USERINFO],
- );
- } else {
- ToastUtil.show("您没有安装腾讯QQ客户端");
- }
- }
- void tencentShare(String filePath) async {
- await _initSDK();
- await _tencent.shareImage(
- scene: TencentScene.SCENE_QQ,
- imageUri: Uri.file(filePath),
- );
- }
- void tencentShareLink(String url) async {
- await _initSDK();
- await _tencent.shareWebpage(scene: TencentScene.SCENE_QQ, title: "分享页面", targetUrl: url);
- }
- }
|