mop-flutter-sdk/lib/mop.dart

340 lines
11 KiB
Dart
Raw Normal View History

2020-02-27 17:21:45 +08:00
import 'dart:async';
import 'dart:ffi';
2020-02-27 17:21:45 +08:00
import 'package:flutter/foundation.dart';
2020-02-27 17:21:45 +08:00
import 'package:flutter/services.dart';
2020-04-26 14:57:08 +08:00
import 'package:mop/api.dart';
2020-02-27 17:21:45 +08:00
typedef MopEventCallback = void Function(dynamic event);
typedef MopEventErrorCallback = void Function(dynamic event);
2020-04-26 14:57:08 +08:00
typedef ExtensionApiHandler = Future Function(dynamic params);
class FinStoreConfig {
String sdkKey; //创建应用时生成的SDK Key
String sdkSecret; //创建应用时生成的SDK secret
String apiServer; //服务器地址,客户部署的后台地址
String apmServer; //apm统计服务器的地址,如果不填则默认与apiServer一致
int cryptType; //网络接口加密类型默认为MD5 0:MD5 1:国密MD5
String fingerprint; //SDK指纹 证联环境(https://open.fdep.cn/) 时必填,其他环境的不用填
String
encryptServerData; //是否需要接口加密验证初始化多服务器时使用默认为不开启当设置为YES时开启接口返回加密数据并处理
}
class FinAppletUIConfig {
Map<String, dynamic> navigationTitleTextAttributes; //导航栏的标题样式目前支持了font
}
2020-02-27 17:21:45 +08:00
class Mop {
static final Mop _instance = new Mop._internal();
2021-08-18 22:10:15 +08:00
late MethodChannel _channel;
late EventChannel _mopEventChannel;
late int eventId = 0;
2021-10-10 23:50:23 +08:00
final List<Map<String, dynamic>> _mopEventQueye = <Map<String, dynamic>>[];
2020-04-01 14:44:01 +08:00
2021-10-10 23:50:23 +08:00
final Map<String, ExtensionApiHandler> _extensionApis = {};
2020-04-26 14:57:08 +08:00
Map<String, ExtensionApiHandler> _webExtensionApis = {};
2020-02-27 17:21:45 +08:00
factory Mop() {
return _instance;
}
2020-04-01 14:44:01 +08:00
2020-02-27 17:21:45 +08:00
Mop._internal() {
2021-10-10 23:50:23 +08:00
debugPrint('mop: _internal');
2020-02-27 17:21:45 +08:00
// init
2021-10-10 23:50:23 +08:00
_channel = const MethodChannel('mop');
2020-04-26 14:57:08 +08:00
_channel.setMethodCallHandler(_handlePlatformMethodCall);
2021-10-10 23:50:23 +08:00
_mopEventChannel = const EventChannel('plugins.mop.finogeeks.com/mop_event');
2020-02-27 17:21:45 +08:00
_mopEventChannel.receiveBroadcastStream().listen((dynamic value) {
2021-10-10 23:50:23 +08:00
debugPrint('matrix: receiveBroadcastStream $value');
2020-02-27 17:21:45 +08:00
for (Map m in _mopEventQueye) {
if (m['event'] == value['event']) {
m['MopEventCallback'](value['body']);
}
}
}, onError: (dynamic value) {
// failure(value);
});
}
2020-04-01 14:44:01 +08:00
2020-02-27 17:21:45 +08:00
static Mop get instance => _instance;
Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
2020-04-26 14:57:08 +08:00
Future<dynamic> _handlePlatformMethodCall(MethodCall call) async {
2021-10-10 23:50:23 +08:00
debugPrint("_handlePlatformMethodCall: method:${call.method}");
2020-04-26 14:57:08 +08:00
if (call.method.startsWith("extensionApi:")) {
final name = call.method.substring("extensionApi:".length);
final handler = _extensionApis[name];
if (handler != null) {
return await handler(call.arguments);
}
} else if (call.method.startsWith("webExtentionApi:")) {
final name = call.method.substring("webExtentionApi:".length);
final handler = _webExtensionApis[name];
if (handler != null) {
return await handler(call.arguments);
}
}
2020-04-26 14:57:08 +08:00
}
2020-02-27 17:21:45 +08:00
///
///
/// initialize mop miniprogram engine.
/// 初始化小程序
2021-01-12 15:24:37 +08:00
/// [appkey] is required. it can be getted from api.finclip.com
/// [secret] is required. it can be getted from api.finclip.com
2020-02-27 17:21:45 +08:00
/// [apiServer] is optional. the mop server address. default is https://mp.finogeek.com
/// [apiPrefix] is optional. the mop server prefix. default is /api/v1/mop
2020-05-19 16:42:10 +08:00
/// [cryptType] is optional. cryptType, should be MD5/SM
2020-07-27 09:13:55 +08:00
/// [disablePermission] is optional.
2020-02-27 17:21:45 +08:00
///
Future<Map> initialize(String appkey, String secret,
2021-08-18 22:12:50 +08:00
{ String? apiServer,
String? apiPrefix,
String? cryptType,
bool? disablePermission,
String? userId,
2021-08-09 16:28:47 +08:00
bool encryptServerData = false,
2021-08-25 22:03:04 +08:00
bool debug = false,
bool bindAppletWithMainProcess = false}) async {
2020-02-27 17:21:45 +08:00
final Map ret = await _channel.invokeMethod('initialize', {
'appkey': appkey,
'secret': secret,
'apiServer': apiServer,
2020-05-19 16:42:10 +08:00
'apiPrefix': apiPrefix,
2020-07-27 09:13:55 +08:00
'cryptType': cryptType,
2021-06-02 11:33:07 +08:00
'disablePermission': disablePermission,
'userId': userId,
2021-08-09 16:28:47 +08:00
"encryptServerData": encryptServerData,
2021-08-25 22:03:04 +08:00
"debug": debug,
"bindAppletWithMainProcess": bindAppletWithMainProcess
2020-02-27 17:21:45 +08:00
});
return ret;
}
/// open the miniprogram [appId] from the mop server.
/// 打开小程序
/// [appId] is required.
/// [path] is miniprogram open path. example /pages/index/index
/// [query] is miniprogram query parameters. example key1=value1&key2=value2
2020-12-04 09:09:53 +08:00
/// [sequence] is miniprogram sequence. example 0,1.2.3,4,5...
/// [apiServer] is optional. the mop server address. default is https://mp.finogeek.com
/// [apiPrefix] is optional. the mop server prefix. default is /api/v1/mop
/// [fingerprint] is optional. the mop sdk fingerprint. is nullable
/// [cryptType] is optional. cryptType, should be MD5/SM
Future<Map> openApplet(
final String appId, {
2021-08-18 22:12:50 +08:00
final String? path,
final String? query,
final int? sequence,
final String? apiServer,
final String? apiPrefix,
final String? fingerprint,
final String? cryptType,
final String? scene,
}) async {
2020-04-26 14:57:08 +08:00
Map<String, Object> params = {'appId': appId};
Map param = {};
if (path != null) param["path"] = path;
if (query != null) param["query"] = query;
if (param.length > 0) params["params"] = param;
2020-04-01 14:44:01 +08:00
if (sequence != null) params["sequence"] = sequence;
if (apiServer != null) params["apiServer"] = apiServer;
if (apiPrefix != null) params["apiPrefix"] = apiPrefix;
if (fingerprint != null) params["fingerprint"] = fingerprint;
if (cryptType != null) params["cryptType"] = cryptType;
2021-07-27 10:39:26 +08:00
if (scene != null) param["scene"] = scene;
2020-02-27 17:21:45 +08:00
final Map ret = await _channel.invokeMethod('openApplet', params);
return ret;
}
2020-04-26 14:57:08 +08:00
///
/// get current using applet
/// 获取当前正在使用的小程序信息
/// {appId,name,icon,description,version,thumbnail}
///
///
Future<Map<String, dynamic>> currentApplet() async {
final ret = await _channel.invokeMapMethod("currentApplet");
2021-08-18 22:10:15 +08:00
return Map<String, dynamic>.from(ret!);
2020-04-26 14:57:08 +08:00
}
///
/// close all running applets
/// 关闭当前打开的所有小程序
///
Future closeAllApplets() async {
return await _channel.invokeMethod("closeAllApplets");
}
///
/// clear applets cache
/// 清除缓存的小程序
///
Future clearApplets() async {
return await _channel.invokeMethod("clearApplets");
}
/// 清除指定的小程序本体缓存
Future removeUsedApplet(String appId) async {
Map<String, Object> params = {'appId': appId};
return await _channel.invokeMethod("removeUsedApplet", params);
}
2020-07-22 15:30:54 +08:00
///
/// 获取运行时版本号
///
Future<String> sdkVersion() async {
return await _channel
.invokeMapMethod("sdkVersion")
2021-08-18 22:10:15 +08:00
.then((value) => value?["data"]);
2020-07-22 15:30:54 +08:00
}
2021-06-07 18:53:00 +08:00
///
/// (扫码后)解密-鉴权-打开小程序
///
Future scanOpenApplet(String info) async {
Map<String, Object> params = {'info': info};
return await _channel.invokeMapMethod("scanOpenApplet", params);
2021-06-07 18:53:00 +08:00
}
///
/// 根据微信QrCode信息解析小程序信息
///
Future<Map<String, dynamic>> parseAppletInfoFromWXQrCode(
String qrCode, String apiServer) async {
final ret = await _channel.invokeMapMethod("parseAppletInfoFromWXQrCode",
{"qrCode": qrCode, "apiServer": apiServer});
2021-08-18 22:10:15 +08:00
return Map<String, dynamic>.from(ret!);
}
2021-06-07 18:53:00 +08:00
2020-04-26 14:57:08 +08:00
///
/// register handler to provide custom info or behaviour
/// 注册小程序事件处理
///
void registerAppletHandler(AppletHandler handler) {
_extensionApis["forwardApplet"] = (params) async {
2020-11-27 17:15:32 +08:00
handler.forwardApplet(Map<String, dynamic>.from(params));
2020-04-26 14:57:08 +08:00
};
_extensionApis["getUserInfo"] = (params) {
return handler.getUserInfo();
};
_extensionApis["getCustomMenus"] = (params) async {
2021-08-18 22:19:26 +08:00
final res = await handler.getCustomMenus(params["appId"]);
2021-03-02 14:04:51 +08:00
List<Map<String, dynamic>> list = [];
2021-08-18 22:19:26 +08:00
res.forEach((element) {
2021-03-02 14:04:51 +08:00
Map<String, dynamic> map = Map();
map["menuId"] = element.menuId;
map["image"] = element.image;
map["title"] = element.title;
map["type"] = element.type;
list.add(map);
});
2021-10-10 23:50:23 +08:00
debugPrint("registerAppletHandler getCustomMenus list $list");
2021-03-02 14:04:51 +08:00
return list;
2020-04-26 14:57:08 +08:00
};
_extensionApis["onCustomMenuClick"] = (params) async {
2021-10-10 23:50:23 +08:00
return handler.onCustomMenuClick(params["appId"], params["path"], params["menuId"], params["appInfo"]);
2020-04-26 14:57:08 +08:00
};
_extensionApis["appletDidOpen"] = (params) async {
return handler.appletDidOpen(params["appId"]);
};
2020-04-26 14:57:08 +08:00
_channel.invokeMethod("registerAppletHandler");
}
///
/// register extension api
/// 注册拓展api
///
void registerExtensionApi(String name, ExtensionApiHandler handler) {
_extensionApis[name] = handler;
_channel.invokeMethod("registerExtensionApi", {"name": name});
}
2021-04-21 21:19:48 +08:00
/// 获取国密加密
Future<String> getSMSign(String plainText) async {
var result =
await _channel.invokeMapMethod("smsign", {'plainText': plainText});
2021-08-18 22:10:15 +08:00
var data = result?['data']['data'];
2021-10-10 23:50:23 +08:00
debugPrint(data);
2021-04-21 21:19:48 +08:00
return data;
}
/// WKWebView的弹性设置
void webViewBounces(bool bounces) async {
await _channel.invokeMapMethod("webViewBounces", {'bounces': bounces});
return;
}
//20211220新增Api
//配置多个服务器
Future<void> setFinStoreConfigs(List<FinStoreConfig> configs) async {}
//定制ui样式
Future<void> setUiConfig() async {}
//自定义ua setUiConfig包含设置ua
Future<void> setCustomWebViewUserAgent(String ua) async {}
//支持指定服务器,启动参数,支持扫码打开小程序参数 之前已实现 启动参数 扫码打开小程序
Future<void> startApplet() async {}
//定时批量更新小程序的数量
Future<void> setAppletIntervalUpdateLimit(int count) async {}
//关闭小程序
Future<void> closeApplet(String appletId, bool animated) async {
await _channel.invokeMethod(
"closeApplet", {"appletId": appletId, "animated": animated});
return;
}
//结束小程序 关闭小程序
Future<void> finishRunningApplet(String appletId, bool animated) async {
await _channel.invokeMethod(
"finishRunningApplet", {"appletId": appletId, "animated": animated});
return;
}
//删除小程序 removeUsedApplet? 删掉缓存小程序包
Future<void> removeApplet(String appletId) async {
await _channel.invokeMethod("removeApplet", {"appletId": appletId});
return;
}
//设置小程序切换动画 安卓
Future<void> setActivityTransitionAnim() async {}
//发送事件给小程序
Future<void> sendCustomEvent(Map<String, dynamic> eventData) async {
await _channel.invokeMethod("sendCustomEvent", {"eventData": eventData});
return;
}
//原生调用js
Future<void> callJS(String eventName, String nativeViewId,
Map<String, dynamic> eventData) async {
await _channel.invokeMethod("callJS", {
"eventName": eventName,
"nativeViewId": nativeViewId,
"eventData": eventData
});
return;
}
//注册h5的拓展接口
void addWebExtentionApi(String name, ExtensionApiHandler handler) {
_webExtensionApis[name] = handler;
_channel.invokeMethod("addWebExtentionApi", {"name": name});
}
2020-02-27 17:21:45 +08:00
}