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

support OpenCustomerServiceChat on android

上级 1b7ff6e9
...@@ -16,6 +16,10 @@ import io.flutter.plugin.common.MethodChannel ...@@ -16,6 +16,10 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry import io.flutter.plugin.common.PluginRegistry
import com.tencent.mm.opensdk.modelbiz.WXOpenCustomerServiceChat
/** FluwxPlugin */ /** FluwxPlugin */
class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,PluginRegistry.NewIntentListener { class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,PluginRegistry.NewIntentListener {
...@@ -79,6 +83,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,PluginRegist ...@@ -79,6 +83,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,PluginRegist
call.method.startsWith("share") -> shareHandler?.share(call, result) call.method.startsWith("share") -> shareHandler?.share(call, result)
call.method == "isWeChatInstalled" -> WXAPiHandler.checkWeChatInstallation(result) call.method == "isWeChatInstalled" -> WXAPiHandler.checkWeChatInstallation(result)
call.method == "getExtMsg" -> getExtMsg(result) call.method == "getExtMsg" -> getExtMsg(result)
call.method == "openWeChatCustomerServiceChat" -> openWeChatCustomerServiceChat(call, result)
else -> result.notImplemented() else -> result.notImplemented()
} }
} }
...@@ -145,6 +150,16 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,PluginRegist ...@@ -145,6 +150,16 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,PluginRegist
result.success(WXAPiHandler.wxApi?.sendReq(request)) result.success(WXAPiHandler.wxApi?.sendReq(request))
} }
private fun openWeChatCustomerServiceChat(call: MethodCall, result: Result) {
val url = call.argument<String>("url") ?: ""
val corpId = call.argument<String>("corpId") ?: ""
val request = WXOpenCustomerServiceChat.Req()
request.corpId = corpId // 企业ID
request.url = url
result.success(WXAPiHandler.wxApi?.sendReq(request))
}
private fun signAutoDeduct(call: MethodCall, result: Result) { private fun signAutoDeduct(call: MethodCall, result: Result) {
val appId: String = call.argument<String>("appid") ?: "" val appId: String = call.argument<String>("appid") ?: ""
val mchId = call.argument<String>("mch_id") ?: "" val mchId = call.argument<String>("mch_id") ?: ""
......
...@@ -88,7 +88,7 @@ object FluwxRequestHandler { ...@@ -88,7 +88,7 @@ object FluwxRequestHandler {
fun onReq(baseReq: BaseReq, activity: Activity) { fun onReq(baseReq: BaseReq, activity: Activity) {
try { try {
val packageManager = activity.packageManager val packageManager = activity.packageManager
var appInfo = packageManager.getApplicationInfo(activity.packageName, PackageManager.GET_META_DATA) val appInfo = packageManager.getApplicationInfo(activity.packageName, PackageManager.GET_META_DATA)
val defaultHandle = appInfo.metaData.getBoolean("handleWeChatRequestByFluwx", true) val defaultHandle = appInfo.metaData.getBoolean("handleWeChatRequestByFluwx", true)
if (defaultHandle) { if (defaultHandle) {
defaultOnReqDelegate(baseReq, activity) defaultOnReqDelegate(baseReq, activity)
......
...@@ -20,6 +20,7 @@ import com.tencent.mm.opensdk.modelbase.BaseResp ...@@ -20,6 +20,7 @@ import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelbiz.SubscribeMessage import com.tencent.mm.opensdk.modelbiz.SubscribeMessage
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessWebview import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessWebview
import com.tencent.mm.opensdk.modelbiz.WXOpenCustomerServiceChat
import com.tencent.mm.opensdk.modelmsg.SendAuth import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelpay.PayResp import com.tencent.mm.opensdk.modelpay.PayResp
...@@ -40,6 +41,7 @@ object FluwxResponseHandler { ...@@ -40,6 +41,7 @@ object FluwxResponseHandler {
is WXLaunchMiniProgram.Resp -> handleLaunchMiniProgramResponse(response) is WXLaunchMiniProgram.Resp -> handleLaunchMiniProgramResponse(response)
is SubscribeMessage.Resp -> handleSubscribeMessage(response) is SubscribeMessage.Resp -> handleSubscribeMessage(response)
is WXOpenBusinessWebview.Resp -> handlerWXOpenBusinessWebviewResponse(response) is WXOpenBusinessWebview.Resp -> handlerWXOpenBusinessWebviewResponse(response)
is WXOpenCustomerServiceChat.Resp -> handlerWXOpenCustomerServiceChatResponse(response)
} }
} }
...@@ -119,4 +121,14 @@ object FluwxResponseHandler { ...@@ -119,4 +121,14 @@ object FluwxResponseHandler {
FluwxPlugin.callingChannel?.invokeMethod("onWXOpenBusinessWebviewResponse", result) FluwxPlugin.callingChannel?.invokeMethod("onWXOpenBusinessWebviewResponse", result)
} }
private fun handlerWXOpenCustomerServiceChatResponse(response: WXOpenCustomerServiceChat.Resp) {
val result = mapOf(
errCode to response.errCode,
errStr to response.errStr,
openId to response.openId,
type to response.type)
FluwxPlugin.callingChannel?.invokeMethod("onWXOpenCustomerServiceChatResponse", result)
}
} }
\ No newline at end of file
...@@ -4,10 +4,11 @@ import android.content.Context ...@@ -4,10 +4,11 @@ import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat import android.graphics.Bitmap.CompressFormat
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import id.zelory.compressor.Compressor
import id.zelory.compressor.constraint.size
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import okio.* import okio.*
import top.zibin.luban.Luban
import java.io.* import java.io.*
import java.io.IOException import java.io.IOException
import java.util.* import java.util.*
...@@ -22,29 +23,31 @@ class ImagesIOIml(override val image: WeChatFile) : ImagesIO { ...@@ -22,29 +23,31 @@ class ImagesIOIml(override val image: WeChatFile) : ImagesIO {
override suspend fun readByteArray(): ByteArray = image.readByteArray() override suspend fun readByteArray(): ByteArray = image.readByteArray()
override suspend fun compressedByteArray(context: Context, maxSize: Int): ByteArray = withContext(Dispatchers.IO) { override suspend fun compressedByteArray(context: Context, maxSize: Int): ByteArray =
val originalByteArray = readByteArray() withContext(Dispatchers.IO) {
if (originalByteArray.isEmpty()) val originalByteArray = readByteArray()
return@withContext originalByteArray if (originalByteArray.isEmpty())
return@withContext originalByteArray
val originFile = inputStreamToFile(ByteArrayInputStream(originalByteArray))
val compressedFile = Luban val originFile = inputStreamToFile(ByteArrayInputStream(originalByteArray))
.with(context) if (image.suffix.contains("gif")) {
.ignoreBy(maxSize) return@withContext originalByteArray
.setTargetDir(context.cacheDir.absolutePath) }
.get(originFile.absolutePath) val compressedFile = Compressor.compress(context, originFile) {
size(maxFileSize = maxSize * 1024L)
if (compressedFile.length() < maxSize) { }
val source = compressedFile.source()
val bufferedSource = source.buffer() if (compressedFile.length() < maxSize) {
val bytes = bufferedSource.readByteArray() val source = compressedFile.source()
source.close() val bufferedSource = source.buffer()
bufferedSource.close() val bytes = bufferedSource.readByteArray()
bytes source.close()
} else { bufferedSource.close()
createScaledBitmapWithRatio(compressedFile, maxSize) bytes
} else {
createScaledBitmapWithRatio(compressedFile, maxSize)
}
} }
}
private fun inputStreamToFile(inputStream: InputStream): File { private fun inputStreamToFile(inputStream: InputStream): File {
val file = File.createTempFile(UUID.randomUUID().toString(), image.suffix) val file = File.createTempFile(UUID.randomUUID().toString(), image.suffix)
...@@ -67,7 +70,11 @@ class ImagesIOIml(override val image: WeChatFile) : ImagesIO { ...@@ -67,7 +70,11 @@ class ImagesIOIml(override val image: WeChatFile) : ImagesIO {
return bmpToByteArray(result, image.suffix) ?: byteArrayOf() return bmpToByteArray(result, image.suffix) ?: byteArrayOf()
} }
private fun createScaledBitmapWithRatio(bitmap: Bitmap, maxLength: Int, recycle: Boolean): Bitmap? { private fun createScaledBitmapWithRatio(
bitmap: Bitmap,
maxLength: Int,
recycle: Boolean
): Bitmap? {
var result = bitmap var result = bitmap
while (true) { while (true) {
val ratio = maxLength.toDouble() / result.byteCount val ratio = maxLength.toDouble() / result.byteCount
...@@ -88,7 +95,10 @@ class ImagesIOIml(override val image: WeChatFile) : ImagesIO { ...@@ -88,7 +95,10 @@ class ImagesIOIml(override val image: WeChatFile) : ImagesIO {
return result return result
} }
private fun bmpToByteArray(bitmap: Bitmap, suffix: String): ByteArray? { // int bytes = bitmap.getByteCount(); private fun bmpToByteArray(
bitmap: Bitmap,
suffix: String
): ByteArray? { // int bytes = bitmap.getByteCount();
val byteArrayOutputStream = ByteArrayOutputStream() val byteArrayOutputStream = ByteArrayOutputStream()
var format = CompressFormat.PNG var format = CompressFormat.PNG
......
...@@ -37,12 +37,12 @@ open class FluwxWXEntryActivity : Activity(), IWXAPIEventHandler { ...@@ -37,12 +37,12 @@ open class FluwxWXEntryActivity : Activity(), IWXAPIEventHandler {
try { try {
if (!WXAPiHandler.wxApiRegistered) { if (!WXAPiHandler.wxApiRegistered) {
var appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA) val appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
val wechatAppId = appInfo.metaData.getString("weChatAppId") val wechatAppId = appInfo.metaData.getString("weChatAppId")
if (wechatAppId != null ){ if (wechatAppId != null ){
WXAPiHandler.setupWxApi(wechatAppId,this) WXAPiHandler.setupWxApi(wechatAppId,this)
WXAPiHandler.setCoolBool(true) WXAPiHandler.setCoolBool(true)
Log.d("fluwx","weChatAppId:" + wechatAppId) Log.d("fluwx","weChatAppId: $wechatAppId")
}else { }else {
Log.e("fluwx","can't load meta-data weChatAppId") Log.e("fluwx","can't load meta-data weChatAppId")
} }
......
...@@ -73,7 +73,7 @@ flutter { ...@@ -73,7 +73,7 @@ flutter {
dependencies { dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
} }
...@@ -16,7 +16,7 @@ class MainActivity: FlutterActivity() { ...@@ -16,7 +16,7 @@ class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
//If you didn't configure WxAPI, add the following code //If you didn't configure WxAPI, add the following code
WXAPiHandler.setupWxApi("wxd930ea5d5a258f4f",this) // WXAPiHandler.setupWxApi("wxd930ea5d5a258f4f",this)
//Get Ext-Info from Intent. //Get Ext-Info from Intent.
FluwxRequestHandler.handleRequestInfoFromIntent(intent) FluwxRequestHandler.handleRequestInfoFromIntent(intent)
} }
......
buildscript { buildscript {
ext.kotlin_version = '1.4.10' ext.kotlin_version = '1.5.21'
repositories { repositories {
google() google()
jcenter()
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:4.1.0' classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
} }
} }
...@@ -15,7 +14,6 @@ buildscript { ...@@ -15,7 +14,6 @@ buildscript {
allprojects { allprojects {
repositories { repositories {
google() google()
jcenter()
mavenCentral() mavenCentral()
} }
} }
......
...@@ -42,6 +42,8 @@ Map<String, _WeChatResponseInvoker> _nameAndResponseMapper = { ...@@ -42,6 +42,8 @@ Map<String, _WeChatResponseInvoker> _nameAndResponseMapper = {
WeChatQRCodeScannedResponse.fromMap(argument), WeChatQRCodeScannedResponse.fromMap(argument),
"onWXShowMessageFromWX": (Map argument) => "onWXShowMessageFromWX": (Map argument) =>
WeChatShowMessageFromWXRequest.fromMap(argument), WeChatShowMessageFromWXRequest.fromMap(argument),
"onWXOpenCustomerServiceChatResponse": (Map argument) =>
WeChatOpenCustomerServiceChatResponse.fromMap(argument),
}; };
class BaseWeChatResponse { class BaseWeChatResponse {
...@@ -125,6 +127,11 @@ class WeChatPaymentResponse extends BaseWeChatResponse { ...@@ -125,6 +127,11 @@ class WeChatPaymentResponse extends BaseWeChatResponse {
super._(map[_errCode], map[_errStr]); super._(map[_errCode], map[_errStr]);
} }
class WeChatOpenCustomerServiceChatResponse extends BaseWeChatResponse {
WeChatOpenCustomerServiceChatResponse.fromMap(Map map)
:super._(map[_errCode], map[_errStr]);
}
class WeChatSubscribeMsgResponse extends BaseWeChatResponse { class WeChatSubscribeMsgResponse extends BaseWeChatResponse {
final String? openid; final String? openid;
final String? templateId; final String? templateId;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论