2020-02-27 17:21:45 +08:00
|
|
|
import 'dart:async';
|
2020-04-26 14:57:08 +08:00
|
|
|
import 'dart:math';
|
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);
|
|
|
|
|
2020-02-27 17:21:45 +08:00
|
|
|
class Mop {
|
|
|
|
static final Mop _instance = new Mop._internal();
|
|
|
|
MethodChannel _channel;
|
|
|
|
EventChannel _mopEventChannel;
|
|
|
|
int eventId = 0;
|
|
|
|
List<Map<String, dynamic>> _mopEventQueye = <Map<String, dynamic>>[];
|
2020-04-01 14:44:01 +08:00
|
|
|
|
2020-04-26 14:57:08 +08:00
|
|
|
Map<String, ExtensionApiHandler> _extensionApis = {};
|
|
|
|
|
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() {
|
|
|
|
print('mop: _internal');
|
|
|
|
// init
|
|
|
|
_channel = new MethodChannel('mop');
|
2020-04-26 14:57:08 +08:00
|
|
|
_channel.setMethodCallHandler(_handlePlatformMethodCall);
|
2020-02-27 17:21:45 +08:00
|
|
|
_mopEventChannel = new EventChannel('plugins.mop.finogeeks.com/mop_event');
|
|
|
|
_mopEventChannel.receiveBroadcastStream().listen((dynamic value) {
|
|
|
|
print('matrix: receiveBroadcastStream $value');
|
|
|
|
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 {
|
|
|
|
print("_handlePlatformMethodCall: method:${call.method}");
|
|
|
|
if (call.method.startsWith("extensionApi:")) {
|
|
|
|
final name = call.method.substring("extensionApi:".length);
|
|
|
|
final handler = _extensionApis[name];
|
|
|
|
if (handler != null) {
|
|
|
|
return await handler(call.arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 17:21:45 +08:00
|
|
|
///
|
|
|
|
///
|
|
|
|
/// initialize mop miniprogram engine.
|
|
|
|
/// 初始化小程序
|
|
|
|
/// [appkey] is required. it can be getted from mp.finogeeks.com
|
|
|
|
/// [secret] is required. it can be getted from mp.finogeeks.com
|
|
|
|
/// [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,
|
2020-07-27 09:13:55 +08:00
|
|
|
{String apiServer,
|
|
|
|
String apiPrefix,
|
|
|
|
String cryptType,
|
|
|
|
bool disablePermission}) 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,
|
|
|
|
'disablePermission': disablePermission
|
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
|
|
|
|
///
|
|
|
|
///
|
|
|
|
Future<Map> openApplet(final String appId,
|
2020-04-01 14:44:01 +08:00
|
|
|
{final String path, final String query, final int sequence}) 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;
|
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");
|
|
|
|
return Map<String, dynamic>.from(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// close all running applets
|
|
|
|
/// 关闭当前打开的所有小程序
|
|
|
|
///
|
|
|
|
Future closeAllApplets() async {
|
|
|
|
return await _channel.invokeMethod("closeAllApplets");
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// clear applets cache
|
|
|
|
/// 清除缓存的小程序
|
|
|
|
///
|
|
|
|
Future clearApplets() async {
|
|
|
|
return await _channel.invokeMethod("clearApplets");
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:30:54 +08:00
|
|
|
///
|
|
|
|
/// 获取运行时版本号
|
|
|
|
///
|
|
|
|
Future<String> sdkVersion() async {
|
|
|
|
return await _channel
|
|
|
|
.invokeMapMethod("sdkVersion")
|
|
|
|
.then((value) => value["data"]);
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
final res = await handler.getCustomMenus(params["appId"]);
|
|
|
|
res?.map((e) => e.toJson());
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
_extensionApis["onCustomMenuClick"] = (params) {
|
|
|
|
return handler.onCustomMenuClick(params["appId"], params["menuId"]);
|
|
|
|
};
|
|
|
|
_channel.invokeMethod("registerAppletHandler");
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// register extension api
|
|
|
|
/// 注册拓展api
|
|
|
|
///
|
|
|
|
void registerExtensionApi(String name, ExtensionApiHandler handler) {
|
|
|
|
_extensionApis[name] = handler;
|
|
|
|
_channel.invokeMethod("registerExtensionApi", {"name": name});
|
|
|
|
}
|
2020-02-27 17:21:45 +08:00
|
|
|
}
|