flutter 简单集成第三方腾讯QQ分享/登录

flutter 集成腾讯QQ分享登录

原创:@As.Kai
博客地址:https://blog.csdn.net/qq_42362997
如果以下内容对您有帮助,点赞点赞点赞~

项目内容:

  • 当前环境监测(当前设备中有没有qq或者TIM)
  • 登录
  • 获取用户信息
  • 获取UnionID
  • 分享说说
  • 文本分享
  • 网页分享
  • 图片分享

首先需要在腾讯开放平台申请一个开发者账号并且添加应用授权
腾讯开放平台:https://open.tencent.com/
位置信息要填写具体到X栋X号 不然很容易审核失败
别问我为什么会知道

审核通过后,点击创建应用-选择移动应用-选择Android平台

上传应用时会让你填写一个应用包名和一个应用签名
应用包名在哪找我就不细说了 因为对接过其他的第三方都知道

这里主要说一下应用签名在哪看:
官方提供了一个获取应用签名的app
我这边已经提取出来打了包 AppManage.apk

自行下载下就行(Android环境下运行)

打开AppManage.apk后找到你需要集成QQ的项目
选择左边一键复制即可复制项目的应用签名
应用签名包名填写完成后提交审核

在这里插入图片描述
审核通过后 会给到你一个appId和appKey
拿到我们的appId和appKey回到我们的项目中来

找到app/build.gradle中的,进项相对应配置

android{
    .......
    defaultConfig{
            ......
            ndk{
                //选择要添加的对应 cpu 类型的 .so 库。
                    abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a'
            }
            manifestPlaceholders = [
                TENCENT_APP_ID: "你的AppId"//腾讯QQ AppId
            ]
        }
}

接着回到page页面
引入所需要的包:

import 'package:tencent_kit/tencent_kit.dart';
import 'package:okhttp_kit/okhttp_kit.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:path/path.dart' as path;

并且声明变量以及监听方法

//QQ分享
static const String _TENCENT_APPID = '你的APPID';
Tencent _tencent = Tencent()..registerApp(appId: _TENCENT_APPID);
TabController _controller;


//分享
StreamSubscription<TencentShareResp> _share;


//登录
StreamSubscription<TencentLoginResp> _login;
TencentLoginResp _loginResp;


//分享监听回调
void _listenShare(TencentShareResp resp) {
  String content = 'share:${resp.ret} - ${resp.msg}';
  _showTips('分享', content);
}


//登录监听回调
void _listenLogin(TencentLoginResp resp) {
  _loginResp = resp;
  String content = 'login: ${resp.openid} - ${resp.accessToken}';
  _showTips('登录', content);
}


//展示提示
void _showTips(String title, String content) {
  showDialog<void>(
      context: context,
      builder: (BuildContext btx) {
        return AlertDialog(
          title: new Text(title),
          content: new Text(content),
        );
      });
}
@override
void initState(){
    //初始化监听
    _share = _tencent.shareResp().listen(_listenShare);
    _login = _tencent.loginResp().listen(_listenLogin);
    super.initState();
}

内容build中代码

build(BuildContext context){
...............
new Container(
  width: MediaQuery.of(context).size.width,
  height: MediaQuery.of(context).size.height,
  child: new Column(
    children: [
      new ListTile(
        title: const Text(
          '环境检查',
          style: TextStyle(color: Colors.white),
        ),
        onTap: () async {
          String content =
              'QQ install: ${await _tencent.isQQInstalled()}\nTIM install: ${await _tencent.isTIMInstalled()}';
          _showTips('环境检查', content);
        },
      ),
      new ListTile(
        title: const Text('登录',
            style: TextStyle(color: Colors.white)),
        onTap: () {
          //获取简单的登录信息
          _tencent.login(scope: <String>[
            TencentScope.GET_SIMPLE_USERINFO
          ]);
        },
      ),
      new ListTile(
        title: const Text('获取用户信息',
            style: TextStyle(color: Colors.white)),
        onTap: () async {
          if (_loginResp != null &&
              _loginResp.isSuccessful() &&
              !_loginResp.isExpired()) {
            TencentUserInfoResp userInfo =
                await _tencent.getUserInfo(
                    appId: _TENCENT_APPID,
                    openid: _loginResp.openid,
                    accessToken: _loginResp.accessToken);
            if (userInfo.isSuccessful()) {
              _showTips('用户信息',
                  '${userInfo.nickname} - ${userInfo.gender} - ${userInfo.genderType}');
            } else {
              print('用户信息msg===${userInfo.msg}');
            }
          } else {
            print('请先登录');
            _showTips('请先登录', '未找到登录信息,请重新登录后重试!');
          }
        },
      ),
      new ListTile(
          title: const Text('获取UnionId',
              style: TextStyle(color: Colors.white)),
          onTap: () async {
            //_login不为空并且成功并且没过期
            if (_loginResp != null &&
                _loginResp.isSuccessful() &&
                !_loginResp.isExpired()) {
              TencentUnionidResp unionid =
                  await _tencent.getUnionId(
                      accessToken: _loginResp.accessToken);
              if (unionid.isSuccessful()) {
                _showTips('UnionId',
                    '${unionid.clientId} - ${unionid.openid} - ${unionid.unionid}');
              } else {
                _showTips('UnionId',
                    '${unionid.error} - ${unionid.errorDescription}');
              }
            } else {
              print('请先登录');
              _showTips('请先登录', '未找到登录信息,请重新登录后重试!');
            }
          }),
      new ListTile(
        title: const Text('分享说说',
            style: TextStyle(color: Colors.white)),
        onTap: () {
          _tencent.shareMood(
              scene: TencentScene.SCENE_QZONE,
              summary: '说说分享测试');
        },
      ),
      new ListTile(
        title: const Text('文本分享',
            style: TextStyle(color: Colors.white)),
        onTap: () {
          _tencent.shareText(
            scene: TencentScene.SCENE_QQ,
            summary: '分享测试',
          );
        },
      ),
      new ListTile(
        title: const Text('网页分享',
            style: TextStyle(color: Colors.white)),
        onTap: () {
          _tencent.shareWebpage(
              scene: TencentScene.SCENE_QQ,
              title: 'title',
              targetUrl: 'https://www.baidu.com/');
        },
      ),
      new ListTile(
        title: const Text('图片分享',
            style: TextStyle(color: Colors.white)),
        onTap: () async {
          OkHttpClient client = OkHttpClientBuilder().build();
          var resp = await client
              .newCall(RequestBuilder()
                  .get()
                  .url(HttpUrl.parse(
                      'https://document-export.canva.cn/RLH-M/DAESUzRLH-M/11/thumbnail/GHHxA-Zy0XwAMUv5EpJSow-0001-216768191.png'))
                  .build())
              .enqueue();
          if (resp.isSuccessful()) {
            Directory saveDir = Platform.isIOS
                ? await path_provider
                    .getApplicationDocumentsDirectory()
                : await path_provider
                    .getExternalStorageDirectory();
            File saveFile =
                File(path.join(saveDir.path, 'timg.png'));
            if (!saveFile.existsSync()) {
              saveFile.createSync(recursive: true);
              saveFile.writeAsBytesSync(
                await resp.body().bytes(),
                flush: true,
              );
            }
            await _tencent.shareImage(
              scene: TencentScene.SCENE_QQ,
              imageUri: Uri.file(saveFile.path),
            );
          }
        },
      ),
    ],
  ),
),
}

效果展示:

在这里插入图片描述

别忘了点赞~
关注我,一起成长!
@As.Kai

猜你喜欢

转载自blog.csdn.net/qq_42362997/article/details/113523175