提交 e0c8f572 authored 作者: JarvanMo's avatar JarvanMo

1.better api 2.add some params support in android

上级 ad4db98a
package com.jarvan.fluwx
import android.util.Log
import com.jarvan.fluwx.constant.CallResult
import com.jarvan.fluwx.constant.WeChatPluginMethods
import com.jarvan.fluwx.handler.WeChatPluginHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.PluginRegistry.Registrar
class FluwxPlugin(private var registrar: Registrar) : MethodCallHandler {
......
......@@ -22,4 +22,8 @@ public class WechatPluginKeys {
public static final String DESCRIPTION = "description";
public static final String PACKAGE = "?package=";
public static final String MESSAGE_EXT ="messageExt";
public static final String MEDIA_TAG_NAME = "mediaTagName ";
public static final String MESSAGE_ACTION = "messageAction";
}
......@@ -72,7 +72,13 @@ object WeChatPluginHandler {
msg.description = call.argument(WechatPluginKeys.TEXT)
val req = SendMessageToWX.Req()
req.message = msg
setCommonArguments(call, req)
msg.description
msg.messageAction = call.argument<String>(WechatPluginKeys.MESSAGE_ACTION)
msg.messageExt = call.argument<String>(WechatPluginKeys.MESSAGE_EXT)
msg.mediaTagName = call.argument<String>(WechatPluginKeys.MEDIA_TAG_NAME)
setCommonArguments(call, req,msg)
wxApi?.sendReq(req)
result.success(true)
......@@ -99,7 +105,7 @@ object WeChatPluginHandler {
val req = SendMessageToWX.Req()
setCommonArguments(call, req)
setCommonArguments(call, req,msg)
req.message = msg
wxApi?.sendReq(req)
result.success(true)
......@@ -123,10 +129,14 @@ object WeChatPluginHandler {
val msg = WXMediaMessage()
msg.mediaObject = imgObj
msg.thumbData = WeChatThumbnailUtil.thumbnailForCommon(call.argument(WechatPluginKeys.THUMBNAIL), registrar)
msg.title = call.argument<String>(WechatPluginKeys.TITLE)
msg.description = call.argument<String>(WechatPluginKeys.DESCRIPTION)
//
val req = SendMessageToWX.Req()
setCommonArguments(call, req)
// req.message = msg
setCommonArguments(call, req,msg)
req.message = msg
wxApi?.sendReq(req)
result.success(true)
......@@ -151,7 +161,7 @@ object WeChatPluginHandler {
}
val req = SendMessageToWX.Req()
setCommonArguments(call, req)
setCommonArguments(call, req,msg)
req.message = msg
wxApi?.sendReq(req)
result.success(true)
......@@ -175,8 +185,9 @@ object WeChatPluginHandler {
msg.thumbData = WeChatThumbnailUtil.thumbnailForCommon(thumbnail, registrar)
}
val req = SendMessageToWX.Req()
setCommonArguments(call, req)
setCommonArguments(call, req,msg)
req.message = msg
wxApi?.sendReq(req)
result.success(true)
......@@ -197,7 +208,7 @@ object WeChatPluginHandler {
}
val req = SendMessageToWX.Req()
setCommonArguments(call, req)
setCommonArguments(call, req,msg)
req.message = msg
wxApi?.sendReq(req)
result.success(true)
......@@ -242,7 +253,11 @@ object WeChatPluginHandler {
else -> SendMessageToWX.Req.WXSceneTimeline
}
private fun setCommonArguments(call: MethodCall, req: SendMessageToWX.Req) {
private fun setCommonArguments(call: MethodCall, req: SendMessageToWX.Req,msg:WXMediaMessage) {
msg.messageAction = call.argument<String>(WechatPluginKeys.MESSAGE_ACTION)
msg.messageExt = call.argument<String>(WechatPluginKeys.MESSAGE_EXT)
msg.mediaTagName = call.argument<String>(WechatPluginKeys.MEDIA_TAG_NAME)
req.transaction = call.argument(WechatPluginKeys.TRANSACTION)
req.scene = getScene(call.argument(WechatPluginKeys.SCENE))
}
......
......@@ -18,12 +18,11 @@ class _MyAppState extends State<MyApp> {
void initState() {
super.initState();
// initPlatformState();
Fluwx.init("wxd930ea5d5a258f4f").then((_){
Fluwx.init("wxd930ea5d5a258f4f").then((_) {
print("succes");
},onError: (value){
}, onError: (value) {
print("--->$value");
});
}
// Platform messages are asynchronous, so we initialize in an async method.
......@@ -54,15 +53,15 @@ class _MyAppState extends State<MyApp> {
title: const Text('Plugin example app'),
),
body: new Center(
child:
new FlatButton(onPressed: () {
child: new FlatButton(
onPressed: () {
var fluwx = Fluwx();
fluwx.shareText(WeChatShareTextModel(text: "share text from flutter",transaction: "hehe"));
}, child: new Text("share text to wechat")),
fluwx.share(WeChatShareTextModel(
text: "share text from flutter", transaction: "hehe"));
},
child: new Text("share text to wechat")),
),
),
);
}
}
library fluwe;
library fluwx;
export 'src/wechat_plugin.dart';
export 'src/wechat_share_models.dart';
export 'src/wechat_scene.dart';
......
......@@ -4,56 +4,48 @@ import 'package:flutter/services.dart';
import 'package:fluwx/src/wechat_share_models.dart';
class Fluwx {
static const Map<Type, String> _shareModelMethodMapper = {
WeChatShareTextModel: "shareText",
WeChatShareImageModel: "shareImage",
WeChatShareMusicModel: "shareMusic",
WeChatShareVideoModel: "shareVideo",
WeChatShareWebPageModel: "shareWebPage",
WeChatShareMiniProgramModel: "shareMiniProgram"
};
static const MethodChannel _channel = const MethodChannel('fluwx');
static const MethodChannel _channel = const MethodChannel('fluwx');
StreamController<Map> _responseStreamController =
new StreamController.broadcast();
StreamController<Map> _responseStreamController = new StreamController.broadcast();
Stream<Map> get weChatResponseUpdate=>_responseStreamController.stream;
Stream<Map> get weChatResponseUpdate => _responseStreamController.stream;
static Future init(String appId) async{
return await _channel.invokeMethod("initWeChat",appId);
static Future init(String appId) async {
return await _channel.invokeMethod("initWeChat", appId);
}
void listen(){
void listen() {
_channel.setMethodCallHandler(_handler);
}
void dispose(){
void dispose() {
_responseStreamController.close();
}
Future shareText(WeChatShareTextModel model) async{
await _channel.invokeMethod("shareText",model.toMap());
}
Future shareMiniProgram(WeChatShareMiniProgramModel model)async{
return await _channel.invokeMethod("shareMiniProgram",model.toMap());
}
Future shareMusic(WeChatShareMusicModel model)async{
return await _channel.invokeMethod("shareMusic",model.toMap());
}
Future shareVideo(WeChatShareVideoModel model)async{
return await _channel.invokeMethod("shareVideo",model.toMap());
}
Future shareWebPage(WeChatShareWebPageModel model)async{
return await _channel.invokeMethod("shareWebPage",model.toMap());
Future share(WeChatShareModel model) async {
if (_shareModelMethodMapper.containsKey(model.runtimeType)) {
return await _channel.invokeMethod(
_shareModelMethodMapper[model.runtimeType], model.toMap());
} else {
return Future.error("no method mapper found[${model.runtimeType}]");
}
}
Future<dynamic> _handler(MethodCall methodCall){
if("onWeChatResponse" == methodCall.method){
Future<dynamic> _handler(MethodCall methodCall) {
if ("onWeChatResponse" == methodCall.method) {
_responseStreamController.add(methodCall.arguments);
}
return Future.value(true);
return Future.value(true);
}
}
......@@ -4,31 +4,63 @@ const String _scene = "scene";
const String _transaction = "transaction";
const String _thumbnail = "thumbnail";
const String _title = "title";
const String _description ="description";
const String _description = "description";
const String _messageExt = "messageExt";
const String _mediaTagName = "mediaTagName ";
const String _messageAction = "messageAction";
abstract class WeChatShareModel {
final String messageExt;
final String messageAction;
final String mediaTagName;
final WeChatScene scene;
WeChatShareModel(
{this.messageExt,
this.messageAction,
this.mediaTagName,
this.scene: WeChatScene.SESSION});
Map toMap();
}
class WeChatShareTextModel {
class WeChatShareTextModel extends WeChatShareModel {
final String text;
final String transaction;
final WeChatScene scene;
WeChatShareTextModel({String text, String transaction, WeChatScene scene})
WeChatShareTextModel(
{String text,
String transaction,
WeChatScene scene,
String messageExt,
String messageAction,
String mediaTagName})
: this.text = text ?? "",
this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE;
super(
mediaTagName: mediaTagName,
messageAction: messageAction,
messageExt: messageExt,
scene: scene);
@override
Map toMap() {
return {"text": text, _transaction: transaction, _scene: scene.toString()};
return {
"text": text,
_transaction: transaction,
_scene: scene.toString(),
_messageExt: messageExt,
_messageAction: messageAction,
_mediaTagName: mediaTagName
};
}
}
class WeChatShareMiniProgramModel {
class WeChatShareMiniProgramModel extends WeChatShareModel {
static const int MINI_PROGRAM_TYPE_RELEASE = 0;
static const int MINI_PROGRAM_TYPE_TEST = 1;
static const int MINI_PROGRAM_TYPE_PREVIEW = 2;
final WeChatScene scene = WeChatScene.SESSION;
final String webPageUrl;
final int miniProgramType;
final String userName;
......@@ -50,13 +82,23 @@ class WeChatShareMiniProgramModel {
this.title,
this.description,
this.thumbnail,
String transaction})
String transaction,
WeChatScene scene,
String messageExt,
String messageAction,
String mediaTagName})
: this.transaction = transaction ?? "miniProgram",
this.miniProgramType = miniProgramType ?? MINI_PROGRAM_TYPE_RELEASE,
assert(webPageUrl != null && webPageUrl.isNotEmpty),
assert(userName != null && userName.isNotEmpty),
assert(path != null && path.isNotEmpty);
assert(path != null && path.isNotEmpty),
super(
mediaTagName: mediaTagName,
messageAction: messageAction,
messageExt: messageExt,
scene: scene);
@override
Map toMap() {
return {
'webPageUrl': webPageUrl,
......@@ -72,31 +114,51 @@ class WeChatShareMiniProgramModel {
}
}
class WeChatShareImageModel {
class WeChatShareImageModel extends WeChatShareModel {
final String transaction;
final WeChatScene scene;
final String image;
final String thumbnail;
final String title;
final String description;
WeChatShareImageModel(
{String transaction, WeChatScene scene, this.image, String thumbnail})
{String transaction,
this.image,
this.description,
String thumbnail,
WeChatScene scene,
String messageExt,
String messageAction,
String mediaTagName,
this.title})
: this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "",
assert(image != null);
assert(image != null),
super(
mediaTagName: mediaTagName,
messageAction: messageAction,
messageExt: messageExt,
scene: scene);
@override
Map toMap() {
return {
_transaction: transaction,
_scene: scene.toString(),
"image": image,
_thumbnail: thumbnail
_thumbnail: thumbnail,
_mediaTagName: mediaTagName,
_messageAction: messageAction,
_messageExt: messageExt,
_title: title,
_description: description
};
}
}
class WeChatShareMusicModel {
class WeChatShareMusicModel extends WeChatShareModel {
final String transaction;
final WeChatScene scene;
final String musicUrl;
......@@ -105,35 +167,46 @@ class WeChatShareMusicModel {
final String title;
final String description;
WeChatShareMusicModel(
{String transaction,
WeChatScene scene,
this.musicUrl,
this.musicLowBandUrl ,
this.title:"",
this.description:"",
String thumbnail})
: this.transaction = transaction ?? "text",
WeChatShareMusicModel({
String transaction,
this.musicUrl,
this.musicLowBandUrl,
this.title: "",
this.description: "",
String thumbnail,
WeChatScene scene,
String messageExt,
String messageAction,
String mediaTagName,
}) : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "",
assert(musicUrl != null || musicLowBandUrl != null ),
assert(thumbnail != null);
assert(musicUrl != null || musicLowBandUrl != null),
assert(thumbnail != null),
super(
mediaTagName: mediaTagName,
messageAction: messageAction,
messageExt: messageExt,
scene: scene);
@override
Map toMap() {
return {
_transaction: transaction,
_scene: scene.toString(),
"musicUrl": musicUrl,
"musicLowBandUrl":musicLowBandUrl,
"musicLowBandUrl": musicLowBandUrl,
_thumbnail: thumbnail,
_title:title,
_description:description,
_title: title,
_description: description,
_mediaTagName: mediaTagName,
_messageAction: messageAction,
_messageExt: messageExt,
};
}
}
class WeChatShareVideoModel {
class WeChatShareVideoModel extends WeChatShareModel {
final String transaction;
final WeChatScene scene;
final String videoUrl;
......@@ -142,34 +215,50 @@ class WeChatShareVideoModel {
final String title;
final String description;
WeChatShareVideoModel(
{String transaction,
WeChatScene scene,
this.videoUrl,
this.videoLowBandUrl ,
this.title:"",
this.description:"",
String thumbnail})
: this.transaction = transaction ?? "text",
final String messageExt;
final String messageAction;
final String mediaTagName;
WeChatShareVideoModel({
String transaction,
WeChatScene scene,
this.videoUrl,
this.videoLowBandUrl,
this.title: "",
this.description: "",
String thumbnail,
this.messageExt,
this.messageAction,
this.mediaTagName,
}) : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "",
assert(videoUrl != null || videoLowBandUrl != null ),
assert(thumbnail != null);
assert(videoUrl != null || videoLowBandUrl != null),
assert(thumbnail != null),
super(
mediaTagName: mediaTagName,
messageAction: messageAction,
messageExt: messageExt,
scene: scene);
@override
Map toMap() {
return {
_transaction: transaction,
_scene: scene.toString(),
"videoUrl": videoUrl,
"videoLowBandUrl":videoLowBandUrl,
"videoLowBandUrl": videoLowBandUrl,
_thumbnail: thumbnail,
_title:title,
_description:description
_title: title,
_description: description,
_mediaTagName: mediaTagName,
_messageAction: messageAction,
_messageExt: messageExt,
};
}
}
class WeChatShareWebPageModel {
class WeChatShareWebPageModel extends WeChatShareModel {
final String transaction;
final WeChatScene scene;
final String webPage;
......@@ -177,27 +266,39 @@ class WeChatShareWebPageModel {
final String title;
final String description;
WeChatShareWebPageModel(
{String transaction,
WeChatScene scene,
this.webPage,
this.title:"",
this.description:"",
String thumbnail})
: this.transaction = transaction ?? "text",
WeChatShareWebPageModel({
String transaction,
this.webPage,
this.title: "",
this.description: "",
String thumbnail,
WeChatScene scene,
String messageExt,
String messageAction,
String mediaTagName,
}) : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "",
assert(webPage != null ),
assert(thumbnail != null);
assert(webPage != null),
assert(thumbnail != null),
super(
mediaTagName: mediaTagName,
messageAction: messageAction,
messageExt: messageExt,
scene: scene);
@override
Map toMap() {
return {
_transaction: transaction,
_scene: scene.toString(),
"webPage": webPage,
_thumbnail: thumbnail,
_title:title,
_description:description
_title: title,
_description: description,
_mediaTagName: mediaTagName,
_messageAction: messageAction,
_messageExt: messageExt,
};
}
}
\ No newline at end of file
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论