提交 29305aa2 authored 作者: JarvanMo's avatar JarvanMo

1.initWechat reanmed to registerApp,2.add unregisterApp Channel

上级 30186a5e
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
......@@ -23,19 +22,19 @@ class FluwxPlugin(private var registrar: Registrar) : MethodCallHandler {
}
override fun onMethodCall(call: MethodCall, result: Result): Unit {
Log.e("---",call.method)
if(call.method == WeChatPluginMethods.INIT ){
val api = WXAPIFactory.createWXAPI(registrar.context().applicationContext, call.arguments as String?)
api.registerApp(call.arguments as String)
WeChatPluginHandler.setWxApi(api)
if(call.method == WeChatPluginMethods.REGISTER_APP ){
WeChatPluginHandler.registerApp(call,result)
return
}
if(WeChatPluginHandler.apiIsNull()){
result.error(CallResult.RESULT_API_NULL, "please config wxapi first", null)
if(call.method == WeChatPluginMethods.UNREGISTER_APP){
WeChatPluginHandler.unregisterApp()
result.success(true)
return
}
if( call.method.startsWith("share")){
WeChatPluginHandler.handle(call, result)
}else{
......
......@@ -6,7 +6,8 @@ package com.jarvan.fluwx.constant;
* 万里飞雪,将穹苍作烘炉,熔万物为白银。
**/
public class WeChatPluginMethods {
public static final String INIT = "initWeChat";
public static final String REGISTER_APP = "registerApp";
public static final String UNREGISTER_APP = "unregisterApp";
public static final String WE_CHAT_RESPONSE = "onWeChatResponse";
public static final String SHARE_TEXT = "shareText";
......
package com.jarvan.fluwx.handler
import android.graphics.BitmapFactory
import android.util.Log
import com.jarvan.fluwx.R
import com.jarvan.fluwx.constant.CallResult
import com.jarvan.fluwx.constant.WeChatPluginMethods
import com.jarvan.fluwx.constant.WechatPluginKeys
......@@ -12,6 +10,7 @@ import com.jarvan.fluwx.utils.WeChatThumbnailUtil
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.*
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
......@@ -34,14 +33,32 @@ object WeChatPluginHandler {
private var registrar: PluginRegistry.Registrar? = null
fun apiIsNull() = wxApi == null
fun setMethodChannel(channel: MethodChannel) {
WeChatPluginHandler.channel = channel
}
fun setWxApi(wxApi: IWXAPI) {
WeChatPluginHandler.wxApi = wxApi
fun registerApp(call: MethodCall, result: MethodChannel.Result) {
if (wxApi != null) {
result.success(true)
return
}
val appId = call.arguments as String?
if (appId.isNullOrBlank()) {
result.error("invalid app id", "are you sure your app id is correct ?", appId)
return
}
val api = WXAPIFactory.createWXAPI(registrar!!.context().applicationContext, appId)
val registered = api.registerApp(appId)
wxApi = api
result.success(registered)
}
fun unregisterApp() {
wxApi?.unregisterApp()
wxApi = null
}
fun setRegistrar(registrar: PluginRegistry.Registrar) {
......@@ -50,6 +67,11 @@ object WeChatPluginHandler {
fun handle(call: MethodCall, result: MethodChannel.Result) {
if (wxApi == null) {
result.error(CallResult.RESULT_API_NULL, "please config wxapi first", null)
return
}
if (!wxApi!!.isWXAppInstalled) {
result.error(CallResult.RESULT_WE_CHAT_NOT_INSTALLED, CallResult.RESULT_WE_CHAT_NOT_INSTALLED, null)
return
......@@ -104,7 +126,7 @@ object WeChatPluginHandler {
if (thumbnail.isNullOrBlank()) {
msg.thumbData = null
} else {
msg.thumbData = getThumbnailByteArrayMiniProgram(registrar,thumbnail!!)
msg.thumbData = getThumbnailByteArrayMiniProgram(registrar, thumbnail!!)
}
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
......@@ -116,33 +138,34 @@ object WeChatPluginHandler {
}
private suspend fun getThumbnailByteArrayMiniProgram(registrar: PluginRegistry.Registrar?,thumbnail:String):ByteArray{
private suspend fun getThumbnailByteArrayMiniProgram(registrar: PluginRegistry.Registrar?, thumbnail: String): ByteArray {
return async(CommonPool) {
val result = WeChatThumbnailUtil.thumbnailForMiniProgram(thumbnail, registrar)
result?: byteArrayOf()
result ?: byteArrayOf()
}.await()
}
private suspend fun getImageByteArrayCommon(registrar: PluginRegistry.Registrar?,imagePath:String):ByteArray{
return async(CommonPool){
private suspend fun getImageByteArrayCommon(registrar: PluginRegistry.Registrar?, imagePath: String): ByteArray {
return async(CommonPool) {
val result = ShareImageUtil.getImageData(registrar, imagePath)
result ?: byteArrayOf()
}.await()
}
private suspend fun getThumbnailByteArrayCommon(registrar: PluginRegistry.Registrar?,thumbnail:String):ByteArray{
return async(CommonPool){
private suspend fun getThumbnailByteArrayCommon(registrar: PluginRegistry.Registrar?, thumbnail: String): ByteArray {
return async(CommonPool) {
val result = WeChatThumbnailUtil.thumbnailForCommon(thumbnail, registrar)
result ?: byteArrayOf()
}.await()
}
private fun shareImage(call: MethodCall, result: MethodChannel.Result) {
val imagePath = call.argument<String>(WechatPluginKeys.IMAGE)
launch(UI){
val byteArray :ByteArray? = getImageByteArrayCommon(registrar,imagePath)
launch(UI) {
val byteArray: ByteArray? = getImageByteArrayCommon(registrar, imagePath)
val imgObj = if (byteArray != null && byteArray.isNotEmpty()) {
WXImageObject(byteArray)
......@@ -155,27 +178,27 @@ object WeChatPluginHandler {
return@launch
}
var thumbnail:String? = call.argument(WechatPluginKeys.THUMBNAIL)
var thumbnail: String? = call.argument(WechatPluginKeys.THUMBNAIL)
if (thumbnail.isNullOrBlank()){
if (thumbnail.isNullOrBlank()) {
thumbnail = imagePath
}
val thumbnailData = getThumbnailByteArrayCommon(registrar,thumbnail!!)
val thumbnailData = getThumbnailByteArrayCommon(registrar, thumbnail!!)
// val thumbnailData = Util.bmpToByteArray(bitmap,true)
handleShareImage(imgObj,call,thumbnailData,result)
handleShareImage(imgObj, call, thumbnailData, result)
}
}
private fun handleShareImage(imgObj:WXImageObject,call: MethodCall,thumbnailData: ByteArray?, result: MethodChannel.Result){
private fun handleShareImage(imgObj: WXImageObject, call: MethodCall, thumbnailData: ByteArray?, result: MethodChannel.Result) {
val msg = WXMediaMessage()
msg.mediaObject = imgObj
if(thumbnailData == null || thumbnailData.isEmpty()){
if (thumbnailData == null || thumbnailData.isEmpty()) {
msg.thumbData = null
}else {
} else {
msg.thumbData = thumbnailData
}
......@@ -197,7 +220,7 @@ object WeChatPluginHandler {
music.musicDataUrl = call.argument("musicDataUrl")
} else {
music.musicLowBandUrl = musicLowBandUrl
music.musicLowBandDataUrl =call.argument("musicLowBandDataUrl")
music.musicLowBandDataUrl = call.argument("musicLowBandDataUrl")
}
val msg = WXMediaMessage()
msg.mediaObject = music
......@@ -205,9 +228,9 @@ object WeChatPluginHandler {
msg.description = call.argument("description")
val thumbnail: String? = call.argument("thumbnail")
launch(UI){
launch(UI) {
if (thumbnail != null && thumbnail.isNotBlank()) {
msg.thumbData = getThumbnailByteArrayCommon(registrar,thumbnail)
msg.thumbData = getThumbnailByteArrayCommon(registrar, thumbnail)
}
val req = SendMessageToWX.Req()
......@@ -234,9 +257,9 @@ object WeChatPluginHandler {
msg.description = call.argument(WechatPluginKeys.DESCRIPTION)
val thumbnail: String? = call.argument(WechatPluginKeys.THUMBNAIL)
launch(UI){
launch(UI) {
if (thumbnail != null && thumbnail.isNotBlank()) {
msg.thumbData = getThumbnailByteArrayCommon(registrar,thumbnail)
msg.thumbData = getThumbnailByteArrayCommon(registrar, thumbnail)
}
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
......@@ -253,7 +276,7 @@ object WeChatPluginHandler {
webPage.webpageUrl = call.argument("webPage")
val msg = WXMediaMessage()
Log.e("tag","share web")
Log.e("tag", "share web")
msg.mediaObject = webPage
msg.title = call.argument(WechatPluginKeys.TITLE)
msg.description = call.argument(WechatPluginKeys.DESCRIPTION)
......
......@@ -16,7 +16,7 @@ class _MyAppState extends State<MyApp> {
void initState() {
super.initState();
// initPlatformState();
Fluwx.init("wxd930ea5d5a258f4f").then((result) {
Fluwx.registerApp("wxd930ea5d5a258f4f").then((result) {
print("succes-->$result");
}, onError: (value) {
print("--->$value");
......
......@@ -32,11 +32,19 @@ BOOL isWeChatRegistered = NO;
}
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if ([registerApp isEqualToString:call.method]) {
[self initWeChatIfNeeded:call result:result];
return;
}
if ([initWeChat isEqualToString:call.method]) {
if ([unregisterApp isEqualToString:call.method]) {
[self initWeChatIfNeeded:call result:result];
} if ([call.method hasPrefix:@"share"]) {
return;
}
if ([call.method hasPrefix:@"share"]) {
[_fluwxShareHandler handleShare:call result:result];
return;
} else {
result(FlutterMethodNotImplemented);
}
......@@ -55,7 +63,6 @@ BOOL isWeChatRegistered = NO;
}
NSString *appId = call.arguments;
NSLog(appId);
if ([StringUtil isBlank:appId]) {
result([FlutterError errorWithCode:@"invalid app id" message:@"are you sure your app id is correct ? " details:appId]);
return;
......@@ -66,5 +73,9 @@ BOOL isWeChatRegistered = NO;
result(@YES);
}
- (void)unregisterApp:(FlutterMethodCall *)call result:(FlutterResult)result {
[WXApi unregisterApp];
result(@YES);
}
@end
......@@ -4,7 +4,8 @@
#import <Foundation/Foundation.h>
extern NSString *const initWeChat;
extern NSString *const registerApp;
extern NSString *const unregisterApp;
extern NSString *const weChatResponse;
extern NSString *const shareText;
extern NSString *const shareImage;
......
......@@ -4,7 +4,8 @@
#import "FluwxMethods.h"
NSString *const initWeChat = @"initWeChat";
NSString *const registerApp = @"registerApp";
NSString *const unregisterApp = @"unregisterApp";
NSString *const weChatResponse = @"onWeChatResponse";
NSString *const shareText = @"shareText";
NSString *const shareImage = @"shareImage";
......
......@@ -20,10 +20,13 @@ class Fluwx {
Stream<Map> get weChatResponseUpdate => _responseStreamController.stream;
static Future init(String appId) async {
return await _channel.invokeMethod("initWeChat", appId);
static Future registerApp(String appId) async {
return await _channel.invokeMethod("registerApp", appId);
}
static Future unregisterApp() async{
return await _channel.invokeMethod("unregisterApp");
}
void listen() {
_channel.setMethodCallHandler(_handler);
}
......@@ -33,7 +36,6 @@ class Fluwx {
}
Future share(WeChatShareModel model) async {
var s = _shareModelMethodMapper[model.runtimeType];
if (_shareModelMethodMapper.containsKey(model.runtimeType)) {
return await _channel.invokeMethod(
_shareModelMethodMapper[model.runtimeType], model.toMap());
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论