123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import 'dart:async';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:sport/utils/toast.dart';
- import 'package:wechat_kit/wechat_kit.dart';
- const String WECHAT_APPID = 'wx9455bd2742259ef7';
- const String WECHAT_UNIVERSAL_LINK = 'https://xie.hiyd.com/'; // iOS 请配置
- const String WECHAT_APPSECRET = 'c58946da98393252077073c29c828d08';
- const String WECHAT_MINIAPPID = 'your wechat miniAppId';
- mixin WechatMixin<T extends StatefulWidget> on State<T> {
- late Wechat _wechat;
- late final StreamSubscription<BaseResp> _respSubs;
- void _listenResp(BaseResp resp) {
- if(resp.isSuccessful != true) return;
- if (resp is AuthResp) {
- listenWechatLoginMsg(resp);
- } else if (resp is ShareMsgResp) {
- listenWechatShareMsg(resp);
- } else if (resp is PayResp) {
- } else if (resp is LaunchMiniProgramResp) {
- }
- }
- listenWechatShareMsg(ShareMsgResp resp) {}
- listenWechatLoginMsg(AuthResp resp) {}
- @override
- void initState() {
- super.initState();
- _wechat = Wechat.instance;
- _respSubs = Wechat.instance.respStream().listen(_listenResp);
- }
- @override
- void dispose() {
- _respSubs.cancel();
- super.dispose();
- }
- Future<bool> wechatSupport() async {
- return await _wechat.isInstalled() || await _wechat.isSupportApi();
- }
- // wechat 登录
- Future<bool> wechatLogin() async {
- await _wechat.registerApp(
- appId: WECHAT_APPID,
- universalLink: WECHAT_UNIVERSAL_LINK,
- );
- if (await _wechat.isInstalled() && await _wechat.isSupportApi()) {
- _wechat.auth(
- scope: <String>[WechatScope.SNSAPI_USERINFO],
- state: 'auth',
- );
- return true;
- }else {
- ToastUtil.show("您没有安装微信客户端");
- }
- return false;
- }
- void wechatSharePage() async{
- await _wechat.registerApp(
- appId: WECHAT_APPID,
- universalLink: WECHAT_UNIVERSAL_LINK,
- );
- _wechat.shareWebpage(
- scene: WechatScene.TIMELINE,
- webpageUrl: 'https://www.baidu.com',
- );
- }
- // void wechatShareImage() async {
- // Dio dio = new Dio();
- // Directory documentsDir = await getApplicationDocumentsDirectory();
- // String documentsPath = documentsDir.path;
- // String path = "$documentsPath/demo/game.jpg";
- // await dio.download("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=229276664,935794386&fm=26&gp=0.jpg", "$path",
- // options: Options(headers: {HttpHeaders.acceptEncodingHeader: "*"}), // disable gzip
- // onReceiveProgress: (received, total) {
- // if (total != -1) {}
- // });
- // print(path);
- // await _wechat.shareImage(scene: WechatScene.SESSION, imageData: File(path).readAsBytesSync());
- // }
- Future<void> wechatShareImage(String path, String type) async {
- await _wechat.registerApp(
- appId: WECHAT_APPID,
- universalLink: WECHAT_UNIVERSAL_LINK,
- );
- if (type == "chat") {
- // 这是 聊天
- return _wechat.shareImage(scene: WechatScene.SESSION, imageUri: File(path).uri);
- } else if (type == "friend") {
- // 这是朋友圈
- return _wechat.shareImage(scene: WechatScene.TIMELINE, imageUri: File(path).uri);
- }
- }
- Future<void> wechatShareLink(String url, String type, String username) async {
- await _wechat.registerApp(
- appId: WECHAT_APPID,
- universalLink: WECHAT_UNIVERSAL_LINK,
- );
- if (type == "chat") {
- await _wechat.shareWebpage(scene: WechatScene.SESSION, webpageUrl: url, title: "$username分享了他的运动记录,快来围观吧~", description: "$username分享了他的运动记录,快来围观吧~");
- } else if (type == "friend") {
- await _wechat.shareWebpage(scene: WechatScene.TIMELINE, webpageUrl: url, title: "$username分享了他的运动记录,快来围观吧~", description: "$username分享了他的运动记录,快来围观吧~");
- }
- return;
- }
- Future<void> wechatShareVideo(String path, String type) async {
- await _wechat.registerApp(
- appId: WECHAT_APPID,
- universalLink: WECHAT_UNIVERSAL_LINK,
- );
- if (type == "chat") {
- // 这是 聊天
- return _wechat.shareVideo(scene: WechatScene.SESSION, videoUrl: File(path).uri.toString());
- } else if (type == "friend") {
- // 这是朋友圈
- return _wechat.shareVideo(scene: WechatScene.TIMELINE, videoUrl: File(path).uri.toString());
- }
- }
- }
|