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

send auth on android

上级 451f258e
package com.jarvan.fluwx package com.jarvan.fluwx
import com.jarvan.fluwx.constant.WeChatPluginMethods import com.jarvan.fluwx.constant.WeChatPluginMethods
import com.jarvan.fluwx.handler.FluwxLoginHandler
import com.jarvan.fluwx.handler.FluwxShareHandler import com.jarvan.fluwx.handler.FluwxShareHandler
import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
...@@ -32,6 +33,10 @@ class FluwxPlugin(private var registrar: Registrar) : MethodCallHandler { ...@@ -32,6 +33,10 @@ class FluwxPlugin(private var registrar: Registrar) : MethodCallHandler {
return return
} }
if ("sendAuth" == call.method){
FluwxLoginHandler.sendAuth(call,result)
return
}
if( call.method.startsWith("share")){ if( call.method.startsWith("share")){
FluwxShareHandler.handle(call, result) FluwxShareHandler.handle(call, result)
......
package com.jarvan.fluwx.handler
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
object FluwxLoginHandler {
private var channel: MethodChannel? = null
fun setMethodChannel(channel: MethodChannel) {
FluwxLoginHandler.channel = channel
}
fun sendAuth(call: MethodCall, result: MethodChannel.Result) {
val req = SendAuth.Req()
req.scope = call.argument("scope")
req.state = call.argument("state")
result.success(WXAPiHandler.wxApi!!.sendReq(req))
}
fun handleResponse(response:BaseResp){
}
}
\ No newline at end of file
package com.jarvan.fluwx.handler
import com.jarvan.fluwx.constant.WechatPluginKeys
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
object WXAPiHandler {
private var registrar: PluginRegistry.Registrar? = null
var wxApi: IWXAPI? = null
fun setRegistrar(registrar: PluginRegistry.Registrar) {
WXAPiHandler.registrar = registrar
}
fun registerApp(call: MethodCall, result: MethodChannel.Result) {
if(!call.argument<Boolean>(WechatPluginKeys.ANDROID)){
return
}
if (wxApi != null) {
result.success(mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
WechatPluginKeys.RESULT to true
))
return
}
val appId:String? = call.argument(WechatPluginKeys.APP_ID)
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(mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
WechatPluginKeys.RESULT to registered
))
}
}
\ No newline at end of file
...@@ -4,7 +4,7 @@ import 'package:flutter/services.dart'; ...@@ -4,7 +4,7 @@ import 'package:flutter/services.dart';
import 'models/flutter_register_model.dart'; import 'models/flutter_register_model.dart';
import 'models/wechat_share_models.dart'; import 'models/wechat_share_models.dart';
import 'models/wechat_send_auth_model.dart';
class Fluwx { class Fluwx {
static const Map<Type, String> _shareModelMethodMapper = { static const Map<Type, String> _shareModelMethodMapper = {
WeChatShareTextModel: "shareText", WeChatShareTextModel: "shareText",
...@@ -58,6 +58,10 @@ class Fluwx { ...@@ -58,6 +58,10 @@ class Fluwx {
} }
} }
Future sendAuth(WeChatSendAuthModel model) async{
return await _channel.invokeMethod("sendAuth", model.toMap());
}
Future<dynamic> _handler(MethodCall methodCall) { Future<dynamic> _handler(MethodCall methodCall) {
if ("onShareResponse" == methodCall.method) { if ("onShareResponse" == methodCall.method) {
_responseFromShareController.add(methodCall.arguments); _responseFromShareController.add(methodCall.arguments);
......
class WeChatSendAuthModel {
final String scope;
final String state;
WeChatSendAuthModel(this.scope, this.state) :
assert(scope != null && scope
.trim()
.isNotEmpty);
Map toMap() {
return {
"scope":scope,
"state":state
};
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论