Flutter simply integrates third-party Tencent QQ sharing/login

Flutter integrates Tencent QQ sharing login

Original: @As.Kai
Blog address: https://blog.csdn.net/qq_42362997
If the following content is helpful to you, please like it~

Project Description:

  • Current environmental monitoring (Is there any QQ or TIM in the current device)
  • log in
  • Get user information
  • Get UnionID
  • Share and talk
  • Text sharing
  • Web sharing
  • Picture sharing

First, you need to apply for a developer account on the Tencent Open Platform and add application authorization.
Tencent Open Platform: https://open.tencent.com/ The
location information must be filled in to the X number in Building X, otherwise it is easy to fail the audit.
Don’t ask me why I know

After the review is passed, click Create application-select mobile application-select Android platform

When uploading the application, you will
be asked to fill in an application package name and an application signature

Here I mainly talk about where to look for the application signature: the
official provides an app to obtain the application signature.
I have extracted it and packaged AppManage.apk.

Just download it yourself (run under Android environment)

Open AppManage.apk and find the project you need to integrate with QQ.
Select the one-click copy on the left to copy the application signature of the project.
After filling in the application signature package name, submit it for review

Insert picture description here
After the review is passed, you will be given an appId and appKey, and
our appId and appKey will be returned to our project.

Find the corresponding configuration in 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
            ]
        }
}

Then return to the page page to
introduce the required packages:

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;

And declare variables and listen methods

//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();
}

Content build code

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),
            );
          }
        },
      ),
    ],
  ),
),
}

Show results:

Insert picture description here

Don’t forget to like it~
Follow me and grow together!
@As.Kai

Guess you like

Origin blog.csdn.net/qq_42362997/article/details/113523175