微信登錄
1、安裝依賴
dependencies:
fluwx: ^3.12.2
2、配置
android
無需配置
iOS
1.配置Universal Links
image.png
2.在 Xcode 中洞斯,選擇工程設(shè)置項(xiàng),選中“TARGETS”坑赡,在“info”標(biāo)簽欄的“URL type“添加“URL scheme”為appID
image.png
3.在 Xcode 中烙如,選擇你的工程設(shè)置項(xiàng),選中“TARGETS”一欄毅否,在 “info”標(biāo)簽欄的“LSApplicationQueriesSchemes“添加weixin亚铁、weixinULAPI、weixinURLParamsAPI
image.png
詳情參考:微信官方iOS接入指南文檔
3螟加、dart層代碼使用
注冊(cè)wxApi
registerWxApi(appId: "替換對(duì)應(yīng)的appID",universalLink: "https://替換對(duì)應(yīng)的universalLink");
拉起微信登錄
void wxLogin() {
fluwx.sendWeChatAuth(scope: "snsapi_userinfo", state: "wechat_sdk_demo_test").then((data) {
if (!data) {
print("沒有安裝微信徘溢,請(qǐng)安裝微信后使用該功能");
}
}).catchError((e) {
print('weChatLogin error $e');
});
}
監(jiān)聽微信登錄
late StreamSubscription<fluwx.BaseWeChatResponse> wxStream;
wxStream = fluwx.weChatResponseEventHandler.distinct((a, b) => a == b).listen((res) {
if (res is fluwx.WeChatAuthResponse) {
int errCode = res.errCode!;
// print('微信登錄返回值:ErrCode :$errCode code:${res.code} ');
if (errCode == 0) {
String code = res.code!;
wxLoginAuth(code);
} else if (errCode == -4) {
BotToast.showText(text: "用戶拒絕授權(quán)");
} else if (errCode == -2) {
BotToast.showText(text: "用戶取消授權(quán)");
}
}
});
微信登錄驗(yàn)證
void wxLoginAuth(String code) async {
ThirdApi().wechatLoginAuth(code).then((response) {
Map map = json.decode(response.data);
ThirdApi().getWechatUserInfo(map["access_token"], map["openid"]).then((response) {
Map userInfo = json.decode(response.data);
// 這里再調(diào)自己業(yè)務(wù)層的登錄接口處理
// ···
});
});
}
登錄驗(yàn)證接口
// 微信登錄驗(yàn)證
Future<Response> wechatLoginAuth(String code) async {
return await Dio().get(
"https://api.weixin.qq.com/sns/oauth2/access_token",
queryParameters: {
"appid": "填寫appid",
"secret": "填寫secret",
"code": code,
"grant_type": "authorization_code"
},
);
}
獲取微信用戶信息
// 微信用戶信息
Future<Response> getWechatUserInfo(String accessToken, String openid) async {
return await Dio().get(
"https://api.weixin.qq.com/sns/userinfo",
queryParameters: {
"access_token": accessToken,
"openid": openid,
},
);
}
更多接口詳情查閱官方文檔移動(dòng)應(yīng)用微信登錄開發(fā)指南
QQ登錄
1、安裝依賴
dependencies:
tencent_kit: ^4.0.0
2捆探、配置
android
配置appID
android {
defaultConfig{
addManifestPlaceholders([
TENCENT_APP_ID: "your tencent appId"
])
// 或者
// manifestPlaceholders += [
// TENCENT_APP_ID: "your tencent appId"
// ]
}
}
iOS
1.配置Universal Links
Universal Links
Signing&Capabilities -> Associated Domain -> Domains -> your Universal Links
2.在 Xcode 中然爆,選擇工程設(shè)置項(xiàng),選中“TARGETS”徐许,在“info”標(biāo)簽欄的“URL type“添加“URL scheme”為appID
tencent: identifier=tencent schemes=tencent${appId}
3.在 Xcode 中施蜜,選擇你的工程設(shè)置項(xiàng),選中“TARGETS”一欄雌隅,在 “info”標(biāo)簽欄的“LSApplicationQueriesSchemes“添加
<key>LSApplicationQueriesSchemes</key>
<array>
<string>tim</string>
<string>mqq</string>
<string>mqqapi</string>
<string>mqqbrowser</string>
<string>mttbrowser</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV4</string>
<string>mqzone</string>
<string>mqzoneopensdk</string>
<string>mqzoneopensdkapi</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapiV2</string>
<string>mqqapiwallet</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkavatar</string>
<string>mqqopensdkminiapp</string>
<string>mqqopensdkdataline</string>
<string>mqqgamebindinggroup</string>
<string>mqqopensdkgrouptribeshare</string>
<string>tencentapi.qq.reqContent</string>
<string>tencentapi.qzone.reqContent</string>
<string>mqqthirdappgroup</string>
<string>mqqopensdklaunchminiapp</string>
<string>mqqopensdkproxylogin</string>
<string>mqqopensdknopasteboard</string>
<string>mqqopensdkcheckauth</string>
</array>
3翻默、dart層代碼使用
初始化
await Tencent.instance.setIsPermissionGranted(granted: true); // 授權(quán)
await Tencent.instance.registerApp(appId: "appId");
拉起qq登錄
void qqLogin() async {
bool isInstalledQQ = await Tencent.instance.isQQInstalled();
bool isInstalledTIM = await Tencent.instance.isTIMInstalled();
if (isInstalledQQ || isInstalledTIM) {
Tencent.instance.login(scope: <String>[TencentScope.GET_SIMPLE_USERINFO]);
} else {
BotToast.showText(text: "沒有安裝QQ缸沃,請(qǐng)安裝QQ后使用該功能");
}
}
監(jiān)聽
late final StreamSubscription<BaseResp> qqStream;
LoginResp? _qqLoginResp;
qqStream = Tencent.instance.respStream().listen(_listenQQLogin);
void _listenQQLogin(BaseResp resp) {
if (resp is LoginResp) {
_qqLoginResp = resp;
if (resp.openid != null && resp.accessToken != null) {
getQQUserInfo();
}
}
}
獲取qq用戶信息
void getQQUserInfo() async {
showLoadingDialog("正在登錄");
if ((_qqLoginResp?.isSuccessful ?? false) && !(_qqLoginResp!.isExpired ?? true)) {
await ThirdApi()
.getQQUserInfo(_qqLoginResp!.accessToken!, _qqLoginResp!.openid!)
.then((response) {
Map _userInfo = json.decode(response.data);
if (_userInfo["ret"] == 0) {
// 這里再調(diào)自己業(yè)務(wù)層的登錄接口處理
// ···
}
}