1. 응용 프로그램을 만드는 WeChat 개방형 플랫폼
"모바일 애플리케이션 생성"을 클릭하고 관련 정보를 입력한 후 검토를 위해 제출하십시오.
공유 권한 신청 및 승인 후 로그인
2.ios 관련 구성
1. 관련 도메인:
xcode에서 차례로 클릭: 왼쪽 루트 디렉토리 Runner--"Runner under TARGETS--"Signing & Capabilities--"+Capability--"Add Associated Domains
도메인에 도메인 이름을 입력합니다. 예를 들어 도메인 이름이 www.abc.com인 경우 입력할 형식은 applinks:www.abc.com입니다.
2. apple-app-site-association 파일 구성
파일을 만들고 파일에 씁니다.
{
"applinks": {
"apps": [],
"details": [
{
"appID": "teamID.BundleID",
"paths": [ "/app/*" ]
}
]
}
}
appID는 teamID와 BundleID로 구성되며 둘 다 Apple Developer Center에서 볼 수 있습니다.
인증서, 식별자 및 프로필에서 식별자를 클릭하고 자신의 애플리케이션을 클릭하면 앱 ID 구성 편집에서 애플리케이션의 관련 정보를 볼 수 있으며 teamID는 앱 ID 접두사 아래의 콘텐츠입니다
얻은 teamID와 BundleID를 teamID.BundleID에 따라 appID에 입력합니다.
구성된 apple-app-site-association 파일을 서버에 배포합니다.
3.URL체계
xcode에서 순서대로 클릭: 왼쪽 루트 디렉토리의 Runner--"TARGETS 아래의 Runner--"Info--"확장할 URL 유형 클릭--"In Identifier write weixin , URL Schemes WeChat Open Platform의 애플리케이션 세부 정보에 AppID를 작성합니다.
4.LSApplicationQueriesSchemes 구성 화이트리스트
xcode에서 Info.plist 파일 클릭 -- "Custom IOS Target Properties --" LSApplicationQueriesSchemes의 항목 항목 확장 -- "화이트리스트 추가
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
<string>weixinULAPI</string>
<string>wechat</string>
</array>
3. 플러그인 소개
WeChat 결제 기능이 설계되지 않았기 때문에 fluwx 및 fluwx_no_pay에서 두 번째를 선택합니다.
fluwx_no_pay: ^2.5.2
4. 초기화
universalLink의 값: "https://" 뒤에 xcode에 구성된 도메인 이름을 입력합니다. 이는 WeChat 개방형 플랫폼의 범용 링크와 일치합니다.
void initState() {
initFluwx();
}
void initFluwx() async {
await registerWxApi(
appId: "微信开放平台的AppID",
doOnAndroid: true,
doOnIOS: true,
universalLink: "https://*********"
);
}
5. 공유 코드
공유하기 전에 WeChat이 설치되어 있는지 확인하십시오.
isWeChatInstalled.then((value){
if(value){
// 执行分享操作
}else{
print('无法打开微信,请检查是否安装了微信');
}
});
링크 공유, 사진 공유, 동영상 공유 등 친구 서클, 친구와의 대화, 즐겨 찾기 등 다양한 공유 유형이 있으며 패키징 방법은 다음과 같습니다.
// share.dart
import 'dart:typed_data';
import 'package:fluwx_no_pay/fluwx_no_pay.dart';
import 'dart:io';
class share_wx{
/**
* 分享链接
* url=链接
* thumbFile=缩略图本地路径
* scene=分享场景,1好友会话,2朋友圈,3收藏
*/
static void ShareUrl(String url,
{String thumbFile,
Uint8List thumbBytes,
String title,
String desc,
int scene = 1,
String networkThumb,
String assetThumb}) {
// 字符串不为空
bool strNoEmpty(String value) {
if (value == null) return false;
return value.trim().isNotEmpty;
}
desc = desc ?? "";
title = title ?? "";
if (desc.length > 54) {
desc = desc.substring(0, 54) + "...";
}
if (title.length > 20) {
title = title.substring(0, 20) + "...";
}
WeChatScene wxScene = WeChatScene.SESSION;
if (scene == 2) {
wxScene = WeChatScene.TIMELINE;
} else if (scene == 3) {
wxScene = WeChatScene.FAVORITE;
}
WeChatImage image = null;
if (thumbFile != null) {
image = WeChatImage.file(File(thumbFile));
} else if (thumbBytes != null) {
image = WeChatImage.binary(thumbBytes);
} else if (strNoEmpty(networkThumb)) {
image = WeChatImage.network(Uri.encodeFull(networkThumb));
} else if (strNoEmpty(assetThumb)) {
image = WeChatImage.asset(assetThumb, suffix: ".png");
}
var model = WeChatShareWebPageModel(
url,
thumbnail: image,
title: title,
description: desc,
scene: wxScene,
);
shareToWeChat(model);
}
/**
* 分享图片到微信,
* file=本地路径
* url=网络地址
* asset=内置在app的资源图片
* scene=分享场景,1好友会话,2朋友圈,3收藏
*/
void ShareImage({String title,
String decs,
String file,
String url,
String asset,
int scene = 1}) async {
WeChatScene wxScene = WeChatScene.SESSION;
if (scene == 2) {
wxScene = WeChatScene.TIMELINE;
} else if (scene == 3) {
wxScene = WeChatScene.FAVORITE;
}
WeChatShareImageModel model = null;
if (file != null) {
model = WeChatShareImageModel(WeChatImage.file(File(file)),
title: title, description: decs, scene: wxScene);
} else if (url != null) {
model = WeChatShareImageModel(WeChatImage.network(url),
title: title, description: decs, scene: wxScene);
} else if (asset != null) {
model = WeChatShareImageModel(WeChatImage.asset(asset),
title: title, description: decs, scene: wxScene);
} else {
throw Exception("缺少图片资源信息");
}
shareToWeChat(model);
}
}
6. 로그인 코드
WeChat 로그인을 클릭한 후 먼저 WeChat이 설치되어 있는지 확인한 다음 WeChat 인증을 보냅니다.
GestureDetector(
onTap: (){
//判断是否安装了微信
isWeChatInstalled.then((value){
if(value){
//发送微信授权
sendWeChatAuth(scope: 'snsapi_userinfo', state:"wechat_sdk_demo_test").then((value){
print('拉取微信用户信息:${value}');
}).catchError((e){});
}else{
print('无法打开微信,请检查是否安装了微信');
}
});
},
)
WeChat 인증 반환 결과를 모니터링하고 인증에 성공하면 로그인 로직을 실행합니다.
void initState() {
_weChatResponseEventHandler();
}
void _weChatResponseEventHandler(){
weChatResponseEventHandler.distinct((a, b) => a == b).listen((event) {
print('监听微信:${event.errCode}');
if(event is WeChatAuthResponse){
int errCode = event.errCode;
print('微信登录返回值:ErrCode :${event.errCode} code:${event.code}');
if(errCode == 0){
print('用户同意授权登录');
String code = event.code;
// 调接口将code传给后端,执行登陆操作
......
}else if(errCode == -4){
print('用户拒绝授权登录');
}else if(errCode == -2){
print('用户取消授权登录');
}
}
});
}