12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325 |
- import 'dart:convert';
- import 'dart:io';
- import 'package:app_settings/app_settings.dart';
- import 'package:device_info_plus/device_info_plus.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter_inappwebview/flutter_inappwebview.dart';
- import 'package:get/get.dart' as my_get;
- import 'package:get/get.dart';
- import 'package:o2oa_all_platform/common/extension/index.dart';
- import 'package:share_plus/share_plus.dart';
- import 'package:url_launcher/url_launcher.dart';
- import '../../pages/apps/process/process_picker/index.dart';
- import '../../pages/common/create_form/index.dart';
- import '../../pages/common/inner_webview/index.dart';
- import '../../pages/common/portal/index.dart';
- import '../../pages/common/scan/index.dart';
- import '../../pages/home/contact/contact_picker/index.dart';
- import '../../pages/home/contact/org_person_list/index.dart';
- import '../../pages/home/contact/person/index.dart';
- import '../../pages/home/im/im_chat/index.dart';
- import '../../pages/home/im/instant_chat/index.dart';
- import '../../pages/home/im/speech_assistant_chat/index.dart';
- import '../api/index.dart';
- import '../models/index.dart';
- import '../routers/index.dart';
- import '../services/index.dart';
- import '../values/o2.dart';
- import '../widgets/index.dart';
- import 'geolocator_helper.dart';
- import 'javascript_navigation_interface.dart';
- import 'loading.dart';
- import 'log_util.dart';
- import 'o2_api_manager.dart';
- import 'o2_contact_permission_manager.dart';
- import 'o2_file_path_util.dart';
- import 'o2_utils.dart';
- typedef O2ScanCallback = void Function(String result);
- ///
- /// webview js api 注入
- ///
- class O2JavascriptHandler {
- // webview 的 controller 必须注入
- InAppWebViewController? webviewController;
- // 操作窗口相关的接口 必须注入
- JsNavigationInterface? jsNavigationInterface;
- // 输入的 controller
- TextEditingController? _inputController;
- O2JavascriptHandlerData o2mNotification() {
- return O2JavascriptHandlerData(
- key: "o2mNotification",
- callback: (arguments) {
- if (arguments.isNotEmpty) {
- _notification((arguments[0] as String?) ?? "");
- }
- return true;
- });
- }
- O2JavascriptHandlerData o2mUtil() {
- return O2JavascriptHandlerData(
- key: "o2mUtil",
- callback: (arguments) {
- if (arguments.isNotEmpty) {
- _util((arguments[0] as String?) ?? '');
- }
- return true;
- });
- }
- O2JavascriptHandlerData o2mBiz() {
- return O2JavascriptHandlerData(
- key: "o2mBiz",
- callback: (arguments) {
- if (arguments.isNotEmpty) {
- _biz((arguments[0] as String?) ?? '');
- }
- return true;
- });
- }
- /// o2mBiz 模块处理
- _biz(String msg) {
- OLogger.d("执行 Biz , 命令:$msg");
- if (msg.isEmpty == true) {
- return;
- }
- final jsMessage = JsMessage.fromJson(O2Utils.parseStringToJson(msg));
- switch (jsMessage.type) {
- case 'contact.departmentPicker':
- _departmentPicker(jsMessage);
- break;
- case 'contact.identityPicker':
- _identityPicker(jsMessage);
- break;
- case 'contact.groupPicker':
- _groupPicker(jsMessage);
- break;
- case 'contact.personPicker':
- _personPicker(jsMessage);
- break;
- case 'contact.complexPicker':
- _complexPicker(jsMessage);
- break;
- case 'contact.searchPage':
- _searchPage(jsMessage);
- break;
- case 'contact.personInfoPage':
- _personInfoPage(jsMessage);
- case 'contact.orgInfoPage':
- _orgInfoPage(jsMessage);
- break;
- case 'settings.openMyProfile':
- _settingOpenMyProfile(jsMessage);
- break;
- case 'settings.safeAndAccount':
- _settingOpenSafeAndAccount(jsMessage);
- break;
- case 'settings.skin':
- _settingOpenAppSkin(jsMessage);
- break;
- case 'settings.notice':
- _settingOpenAppNotice(jsMessage);
- break;
- case 'settings.common':
- _settingOpenAppCommon(jsMessage);
- break;
- case 'settings.about':
- _settingOpenAppAbout(jsMessage);
- break;
- case 'settings.logout':
- _settingLogout(jsMessage);
- break;
- case 'im.openConversation':
- _openImConversation(jsMessage);
- break;
- case 'im.openChatWithPerson':
- _openImChatWithSomeone(jsMessage);
- break;
- case 'im.openInstantChat':
- _openImInstantChat(jsMessage);
- break;
- case 'im.startSingleChat':
- _startSingleChat(jsMessage);
- break;
- case 'im.startGroupChat':
- _startGroupChat(jsMessage);
- break;
- default:
- OLogger.e('错误的类型 ${jsMessage.type}');
- break;
- }
- }
- /// o2mUtil 模块处理
- _util(String msg) {
- OLogger.d("执行 Util , 命令:$msg");
- if (msg.isEmpty == true) {
- return;
- }
- final jsMessage = JsMessage.fromJson(O2Utils.parseStringToJson(msg));
- switch (jsMessage.type) {
- case 'date.datePicker':
- _datePicker(jsMessage);
- break;
- case 'date.timePicker':
- _timePicker(jsMessage);
- break;
- case 'calendar.chooseOneDay':
- _datePicker(jsMessage);
- break;
- case 'calendar.chooseDateTime':
- _dateTimePicker(jsMessage);
- break;
- case 'date.dateTimePicker':
- _dateTimePicker(jsMessage);
- break;
- case 'calendar.chooseInterval':
- _chooseInterval(jsMessage);
- break;
- case 'device.rotate':
- _rotate(jsMessage);
- break;
- case 'device.getPhoneInfo':
- _getPhoneInfo(jsMessage);
- break;
- case 'device.scan':
- _scan(jsMessage);
- break;
- case 'device.location':
- _location(jsMessage);
- break;
- case 'device.localAuth':
- _localAuth(jsMessage);
- break;
- case 'navigation.setTitle':
- _navigationSetTitle(jsMessage);
- break;
- case 'navigation.close':
- _navigationClose(jsMessage);
- break;
- case 'navigation.goBack':
- _navigationGoBack(jsMessage);
- break;
- case 'navigation.openInnerApp':
- _openInnerApp(jsMessage);
- break;
- case 'navigation.openOtherApp':
- _openOtherApp(jsMessage);
- break;
- case 'navigation.openWindow':
- _openWindow(jsMessage);
- break;
- case 'navigation.openInBrowser':
- _openCurrentPageInBrowser(jsMessage);
- break;
- case 'navigation.clearCache':
- _clearCache(jsMessage);
- break;
- case 'navigation.share':
- _share(jsMessage);
- break;
- case 'settings.openMyProfile':
- _settingOpenMyProfile(jsMessage);
- break;
- case 'settings.safeAndAccount':
- _settingOpenSafeAndAccount(jsMessage);
- break;
- case 'settings.skin':
- _settingOpenAppSkin(jsMessage);
- break;
- case 'settings.notice':
- _settingOpenAppNotice(jsMessage);
- break;
- case 'settings.common':
- _settingOpenAppCommon(jsMessage);
- break;
- case 'settings.about':
- _settingOpenAppAbout(jsMessage);
- break;
- case 'settings.logout':
- _settingLogout(jsMessage);
- break;
- default:
- OLogger.e('错误的类型 ${jsMessage.type}');
- break;
- }
- }
- /// o2mNotification 模块处理
- _notification(String msg) {
- OLogger.d("执行 Notification , 命令:$msg");
- if (msg.isEmpty == true) {
- return;
- }
- final jsMessage = JsMessage.fromJson(O2Utils.parseStringToJson(msg));
- switch (jsMessage.type) {
- case 'alert':
- _alert(jsMessage);
- break;
- case 'confirm':
- _confirm(jsMessage);
- break;
- case 'prompt':
- _prompt(jsMessage);
- break;
- case 'vibrate':
- _vibrate(jsMessage);
- break;
- case 'toast':
- _toast(jsMessage);
- break;
- case 'actionSheet':
- _actionSheet(jsMessage);
- break;
- case 'showLoading':
- _showLoading(jsMessage);
- break;
- case 'hideLoading':
- _hideLoading(jsMessage);
- break;
- default:
- OLogger.e('错误的类型 ${jsMessage.type}');
- break;
- }
- }
- /// alert 提示框
- _alert(JsMessage jsMessage) {
- final context = my_get.Get.context;
- JsNotificationAlertMessage alertMessage =
- JsNotificationAlertMessage.fromJson(jsMessage.data ?? {});
- String callback = jsMessage.callback ?? '';
- if (alertMessage.message == null || alertMessage.message?.isEmpty == true) {
- Loading.showError('没有传入内容!');
- return;
- }
- O2UI.showAlert(context, alertMessage.message!,
- title: alertMessage.title, okText: alertMessage.buttonName);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback()");
- }
- }
- /// confirm 确认框
- _confirm(JsMessage jsMessage) {
- final context = my_get.Get.context;
- JsNotificationConfirmMessage confirmMessage =
- JsNotificationConfirmMessage.fromJson(jsMessage.data ?? {});
- String callback = jsMessage.callback ?? '';
- if (confirmMessage.message == null ||
- confirmMessage.message?.isEmpty == true) {
- Loading.showError('args_error'.tr);
- return;
- }
- var buttons = confirmMessage.buttonLabels ?? [];
- if (buttons.length != 2) {
- buttons = ['confirm'.tr, 'cancel'.tr];
- }
- O2UI.showConfirm(context, confirmMessage.message!,
- title: confirmMessage.title,
- okText: buttons[0],
- cancelText: buttons[1], okPressed: () {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback(0)");
- }
- }, cancelPressed: () {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback(1)");
- }
- });
- }
- /// sheet 底部按钮选择器
- _actionSheet(JsMessage jsMessage) {
- final context = my_get.Get.context;
- if (context == null) {
- OLogger.e('没有 context, 无法打开 actionSheet');
- return;
- }
- JsNotificationActionSheetMessage sheetMsg =
- JsNotificationActionSheetMessage.fromJson(jsMessage.data ?? {});
- String callback = jsMessage.callback ?? '';
- if (sheetMsg.otherButtons == null ||
- sheetMsg.otherButtons?.isEmpty == true) {
- Loading.showError('args_error'.tr);
- return;
- }
- final buttons = sheetMsg.otherButtons ?? [];
- final title = sheetMsg.title ?? '';
- bool tooManyButtons = sheetMsg.tooManyButtons ?? false;
- List<Widget> widgets = buttons.mapWithIndex((e, i) {
- return ListTile(
- onTap: () {
- Navigator.pop(context);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback($i)");
- }
- },
- title: Align(
- alignment: Alignment.center,
- child: Text(e, style: Theme.of(context).textTheme.bodyMedium),
- ),
- );
- }).toList();
- if (widgets.length > 6 ) {
- tooManyButtons = true;
- }
- if (tooManyButtons) {
- O2UI.showBottomSheetLongList(context, widgets,
- title: title, cancelText: sheetMsg.cancelButton, cancelPressed: () {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback(-1)");
- }
- });
- } else {
- O2UI.showBottomSheetWithCancel(context, widgets,
- title: title, cancelText: sheetMsg.cancelButton, cancelPressed: () {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback(-1)");
- }
- });
- }
- }
- /// prompt 输入框
- _prompt(JsMessage jsMessage) async {
- JsNotificationConfirmMessage confirmMessage =
- JsNotificationConfirmMessage.fromJson(jsMessage.data ?? {});
- String callback = jsMessage.callback ?? '';
- var buttons = confirmMessage.buttonLabels ?? [];
- if (buttons.length != 2) {
- buttons = ['confirm'.tr, 'cancel'.tr];
- }
- String content = confirmMessage.message ?? '';
- String title = confirmMessage.title ?? 'alert'.tr;
- _inputController = TextEditingController(text: content);
- var result = await O2UI.showCustomDialog(
- my_get.Get.context,
- title,
- TextField(
- controller: _inputController,
- maxLines: 1,
- keyboardType: TextInputType.text,
- textInputAction: TextInputAction.done,
- ),
- positiveBtnText: buttons[0],
- cancelBtnText: buttons[1]);
- if (result == O2DialogAction.positive) {
- var jsonBack =
- '{buttonIndex: 0, value: "${_inputController?.text ?? ''}"}';
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback('$jsonBack')");
- }
- } else if (result == O2DialogAction.cancel) {
- var jsonBack =
- '{buttonIndex: 1, value: "${_inputController?.text ?? ''}"}';
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback('$jsonBack')");
- }
- }
- }
- /// 震动
- _vibrate(JsMessage jsMessage) async {
- String callback = jsMessage.callback ?? '';
- HapticFeedback.mediumImpact();
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback()");
- }
- }
- /// toast 消息
- _toast(JsMessage jsMessage) async {
- String callback = jsMessage.callback ?? '';
- JsNotificationToastMessage toastMessage =
- JsNotificationToastMessage.fromJson(jsMessage.data ?? {});
- if (toastMessage.message != null &&
- toastMessage.message?.isNotEmpty == true) {
- Loading.toast(toastMessage.message!);
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback()");
- }
- }
- /// loading 层显示
- _showLoading(JsMessage jsMessage) async {
- String callback = jsMessage.callback ?? '';
- JsNotificationLoadingMessage loadingMessage =
- JsNotificationLoadingMessage.fromJson(jsMessage.data ?? {});
- Loading.show(text: loadingMessage.text);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback()");
- }
- }
- /// 关闭 loading 层
- _hideLoading(JsMessage jsMessage) async {
- String callback = jsMessage.callback ?? '';
- Loading.dismiss();
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: "$callback()");
- }
- }
- /// 日期选择器
- _datePicker(JsMessage jsMessage) async {
- final context = my_get.Get.context;
- if (context == null) {
- OLogger.e('没有 context, 无法打开 _datePicker');
- return;
- }
- String callback = jsMessage.callback ?? '';
- JsUtilDatePickerMessage message =
- JsUtilDatePickerMessage.fromJson(jsMessage.data ?? {});
- DateTime? initDate;
- if (message.value != null && message.value?.isNotEmpty == true) {
- initDate = DateTime.tryParse(message.value!);
- }
- initDate ??= DateTime.now();
- final result = await showDatePicker(
- context: context,
- initialDate: initDate,
- firstDate: initDate.addYears(-100),
- lastDate: initDate.addYears(100));
- if (result != null) {
- final date = result.ymd();
- OLogger.d('选择日期 $date');
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'{"value": "$date"}\')');
- }
- }
- }
- /// 时间选择器
- _timePicker(JsMessage jsMessage) async {
- final context = my_get.Get.context;
- if (context == null) {
- OLogger.e('没有 context, 无法打开 _timePicker');
- return;
- }
- String callback = jsMessage.callback ?? '';
- JsUtilTimePickerMessage message =
- JsUtilTimePickerMessage.fromJson(jsMessage.data ?? {});
- DateTime? initDate;
- if (message.value != null && message.value?.isNotEmpty == true) {
- initDate = DateTime.tryParse(message.value!);
- }
- initDate ??= DateTime.now();
- TimeOfDay initTime =
- TimeOfDay(hour: initDate.hour, minute: initDate.minute);
- final startTime =
- await showTimePicker(context: context, initialTime: initTime);
- if (startTime != null) {
- final startHour =
- startTime.hour > 9 ? '${startTime.hour}' : '0${startTime.hour}';
- final startMinute =
- startTime.minute > 9 ? '${startTime.minute}' : '0${startTime.minute}';
- final time = '$startHour:$startMinute';
- OLogger.d('选择时间 $time');
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'{"value": "$time"}\')');
- }
- }
- }
- /// 日期时间选择器
- _dateTimePicker(JsMessage jsMessage) async {
- final context = my_get.Get.context;
- if (context == null) {
- OLogger.e('没有 context, 无法打开 _timePicker');
- return;
- }
- String callback = jsMessage.callback ?? '';
- JsUtilTimePickerMessage message =
- JsUtilTimePickerMessage.fromJson(jsMessage.data ?? {});
- DateTime? initDate;
- if (message.value != null && message.value?.isNotEmpty == true) {
- initDate = DateTime.tryParse(message.value!);
- }
- initDate ??= DateTime.now();
- TimeOfDay initTime =
- TimeOfDay(hour: initDate.hour, minute: initDate.minute);
- final result = await showDatePicker(
- context: context,
- initialDate: initDate,
- firstDate: initDate.addYears(-100),
- lastDate: initDate.addYears(100));
- if (result != null) {
- var date = result.ymd();
- OLogger.d('选择日期 $date');
- // ignore: use_build_context_synchronously
- if (!context.mounted) {
- return;
- }
- final startTime =
- await showTimePicker(context: context, initialTime: initTime);
- if (startTime != null) {
- final startHour =
- startTime.hour > 9 ? '${startTime.hour}' : '0${startTime.hour}';
- final startMinute = startTime.minute > 9
- ? '${startTime.minute}'
- : '0${startTime.minute}';
- date = '$date $startHour:$startMinute';
- OLogger.d('选择时间 $date');
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'{"value": "$date"}\')');
- }
- }
- }
- /// 日期间隔选择器
- _chooseInterval(JsMessage jsMessage) async {
- final context = my_get.Get.context;
- if (context == null) {
- OLogger.e('没有 context, 无法打开 _timePicker');
- return;
- }
- String callback = jsMessage.callback ?? '';
- JsUtilDateIntervalPickerMessage message =
- JsUtilDateIntervalPickerMessage.fromJson(jsMessage.data ?? {});
- DateTime? startDate;
- DateTime? endDate;
- if (message.startDate != null && message.startDate?.isNotEmpty == true) {
- startDate = DateTime.tryParse(message.startDate!);
- }
- startDate ??= DateTime.now();
- if (message.endDate != null && message.endDate?.isNotEmpty == true) {
- endDate = DateTime.tryParse(message.endDate!);
- }
- endDate ??= DateTime.now();
- final result = await showDatePicker(
- context: context,
- initialDate: startDate,
- firstDate: startDate.addYears(-100),
- lastDate: startDate.addYears(100));
- if (result != null) {
- final start = result.ymd();
- OLogger.d('选择开始日期 $start');
- // ignore: use_build_context_synchronously
- if (!context.mounted) {
- return;
- }
- final resultEnd = await showDatePicker(
- context: context,
- initialDate: endDate,
- firstDate: endDate.addYears(-100),
- lastDate: endDate.addYears(100));
- if (resultEnd != null) {
- final end = resultEnd.ymd();
- OLogger.d('选择结束日期 $end');
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source:
- '$callback(\'{ "startDate": "$start", "endDate": "$end" }\')');
- }
- }
- }
- }
- /// 屏幕旋转
- _rotate(JsMessage jsMessage) async {
- final context = my_get.Get.context;
- if (context == null) {
- OLogger.e('没有 context, 无法打开 _rotate');
- return;
- }
- final callback = jsMessage.callback ?? '';
- if (MediaQuery.of(context).orientation == Orientation.landscape) {
- await SystemChrome.setPreferredOrientations(
- [DeviceOrientation.landscapeRight]);
- } else {
- await SystemChrome.setPreferredOrientations(
- [DeviceOrientation.portraitUp]);
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 获取手机信息
- _getPhoneInfo(JsMessage jsMessage) async {
- final context = my_get.Get.context;
- if (context == null) {
- OLogger.e('没有 context, 无法打开 _getPhoneInfo');
- return;
- }
- final callback = jsMessage.callback ?? '';
- var brand = '';
- var model = '';
- var version = '';
- final width = MediaQuery.of(context).size.width;
- final height = MediaQuery.of(context).size.height;
- OLogger.d('屏幕 width $width height $height');
- final deviceInfoPlugin = DeviceInfoPlugin();
- if (Platform.isAndroid) {
- final android = await deviceInfoPlugin.androidInfo;
- brand = android.brand;
- model = android.model;
- version = '${android.version.baseOS ?? ''} ${android.version.sdkInt}';
- } else {
- final ios = await deviceInfoPlugin.iosInfo;
- brand = ios.utsname.machine;
- model = ios.model;
- version = '${ios.systemName} ${ios.systemVersion}';
- }
- if (callback.isNotEmpty) {
- final res = JsDevicePhoneInfoResponse(
- screenWidth: "$width",
- screenHeight: "$height",
- brand: brand,
- model: model,
- version: version,
- netInfo: '',
- operatorType: '');
- webviewController?.evaluateJavascript(
- source: '$callback(\'${json.encode(res.toJson())}\')');
- }
- }
- /// 扫二维码
- _scan(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- ScanPage.o2ScanBiz(callback: (result) {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'${result.o2SimpleString()}\')');
- }
- });
- }
- /// 定位
- _location(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- final helper = GeolocatorHelper(
- isSingleLocation: true,
- callback: (position) {
- OLogger.d('定位返回结果 ${position.latitude} ${position.longitude}');
- final res = JsDeviceLocationResponse(
- latitude: '${position.latitude}',
- longitude: '${position.longitude}',
- address: '${position.address}');
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'${json.encode(res.toJson())}\')');
- }
- });
- helper.startLocation();
- // final helper = BaiduLocationHelper(
- // callback: (result) {
- // OLogger.d('定位返回结果 ${result.latitude} ${result.longitude}');
- // final res = JsDeviceLocationResponse(
- // latitude: '${result.latitude}',
- // longitude: '${result.longitude}',
- // address: '${result.address}');
- // if (callback.isNotEmpty) {
- // webviewController?.evaluateJavascript(
- // source: '$callback(\'${json.encode(res.toJson())}\')');
- // }
- // },
- // isSingleLocation: true);
- // helper.initLocationAndStartRequest();
- }
- /// 手机生物识别认证
- _localAuth(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- if (callback.isNotEmpty) {
- final result = await O2Utils.bioAuth();
- final res = {"value": result};
- webviewController?.evaluateJavascript(
- source: '$callback(\'${json.encode(res)}\')');
- } else {
- OLogger.e('_localAuth, 没有回调函数!');
- }
- }
- /// 设置标题
- _navigationSetTitle(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- JsUtilNavigationMessage message =
- JsUtilNavigationMessage.fromJson(jsMessage.data ?? {});
- if (message.title?.isNotEmpty == true) {
- jsNavigationInterface?.setNavigationTitle(message.title!);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- } else {
- OLogger.e('传入的标题内容为空!');
- }
- }
- /// 关闭当前窗口
- _navigationClose(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- jsNavigationInterface?.closeWindow();
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 返回上一页
- _navigationGoBack(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- jsNavigationInterface?.goBack();
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 分享图片, 目前只支持 base64
- Future<void> _share(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- final base64String = jsMessage.data?['base64'];
- // final subject = jsMessage.data?['subject'];
- // final text = jsMessage.data?['text'];
- if (base64String != null && base64String is String) {
- final filePath = await _base64ToImage(base64String);
- if (filePath != null && filePath.isNotEmpty) {
- _shareImage(filePath);
- }
- } else {
- OLogger.e('错误的参数传入!');
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// baes64 转图片后 分享
- Future<String?> _base64ToImage(String base64) async {
- try {
- final bytes = base64Decode(base64);
- //图片临时存储
- final timeString =
- 'share_base64_image_${DateTime.now().millisecondsSinceEpoch}';
- var dir = await O2FilePathUtil.getTempFolderWithUrl(timeString);
- if (dir == null || dir.isEmpty) {
- OLogger.e('本地文件目录生成失败! url: $timeString');
- Loading.showError('common_image_generate_fail'.tr);
- return null;
- }
- String filePath = '$dir/$timeString.png';
- File file = File(filePath);
- file.writeAsBytesSync(bytes);
- OLogger.i('保存图片完成,path:$filePath');
- return filePath;
- } catch (e) {
- OLogger.e('图片生成失败!', e);
- Loading.showError('common_image_generate_fail'.tr);
- return null;
- }
- }
- /// 分享图片
- Future<void> _shareImage(String filePath) async {
- final context = Get.context;
- if (context == null) {
- OLogger.e('context 为空!');
- return;
- }
- final box = context.findRenderObject() as RenderBox?;
- final result = await Share.shareXFiles([XFile(filePath)],
- sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
- if (result.status != ShareResultStatus.success) {
- OLogger.i('分享失败,${result.status}');
- return;
- }
- Loading.toast('common_share_success'.tr);
- }
- /// 打开内部原生应用
- _openInnerApp(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- JsUtilOpenInnerAppMessage message =
- JsUtilOpenInnerAppMessage.fromJson(jsMessage.data ?? {});
- final appKey = message.appKey;
- final appDisplayName = message.appDisplayName;
- final portalFlag = message.portalFlag;
- final portalTitle = message.portalTitle;
- final portalPage = message.portalPage;
- final parameters = message.parameters ?? {};
- if (appKey?.isNotEmpty == true) {
- // 门户处理
- if (appKey == 'portal') {
- if (portalFlag?.isEmpty == true) {
- Loading.showError('args_error'.tr);
- OLogger.e('门户没有传入参数 portalFlag');
- return;
- }
- final portalParameters = parameters['portalParameters'];
- PortalPage.open(portalFlag!,
- title: portalTitle ?? '',
- pageId: portalPage,
- portalParameters: portalParameters);
- } else if (appKey == 'startProcess') {
- //启动流程
- await _startProcess();
- } else if (appKey == 'speechAssistant') {
- // 语音助手
- SpeechAssistantChatPage.open();
- } else {
- String flutterPath = '';
- if (appKey == O2NativeAppEnum.task.key) {
- flutterPath = O2OARoutes.appTask;
- } else if (appKey == O2NativeAppEnum.taskcompleted.key) {
- flutterPath = O2OARoutes.appTaskcompleted;
- } else if (appKey == O2NativeAppEnum.read.key) {
- flutterPath = O2OARoutes.appRead;
- } else if (appKey == O2NativeAppEnum.readcompleted.key) {
- flutterPath = O2OARoutes.appReadcompleted;
- } else if (appKey == O2NativeAppEnum.meeting.key) {
- flutterPath = O2OARoutes.appMeeting;
- } else if (appKey == O2NativeAppEnum.bbs.key) {
- flutterPath = O2OARoutes.appBBS;
- } else if (appKey == O2NativeAppEnum.yunpan.key ||
- appKey == 'clouddisk') {
- flutterPath = FileAssembleService.to.isV3()
- ? O2OARoutes.appCloudDiskV3
- : O2OARoutes.appYunpan;
- } else if (appKey == O2NativeAppEnum.attendance.key) {
- flutterPath = O2OARoutes.appAttendance;
- } else if (appKey == O2NativeAppEnum.attendanceOld.key) {
- flutterPath = O2OARoutes.appAttendanceOld;
- } else if (appKey == O2NativeAppEnum.cms.key) {
- flutterPath = O2OARoutes.appCms;
- } else if (appKey == O2NativeAppEnum.calendar.key) {
- flutterPath = O2OARoutes.appCalendar;
- } else if (appKey == O2NativeAppEnum.mindMap.key) {
- flutterPath = O2OARoutes.appMindMap;
- }
- if (flutterPath.isNotEmpty) {
- if (O2OARoutes.appAttendance == flutterPath ||
- O2OARoutes.appAttendanceOld == flutterPath) {
- FastCheckInService.instance.stop(); // 进入考勤 先关闭极速打卡
- }
- if (appDisplayName?.trim().isNotEmpty == true) {
- if (flutterPath.contains('?')) {
- flutterPath += '&displayName=$appDisplayName';
- } else {
- flutterPath += '?displayName=$appDisplayName';
- }
- }
- my_get.Get.toNamed(flutterPath);
- } else {
- Loading.showError('args_error'.tr);
- OLogger.e('错误的 appkey $appKey');
- }
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- } else {
- OLogger.e('没传入 appKey ');
- Loading.showError('args_error'.tr);
- }
- }
- // 选择启动流程
- Future<void> _startProcess() async {
- var result = await ProcessPickerPage.startPicker(ProcessPickerMode.process);
- if (result != null && result is ProcessData) {
- CreateFormPage.startProcess(true, process: result);
- }
- }
- /// 打开外部应用
- _openOtherApp(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- JsUtilOtherAppMessage message =
- JsUtilOtherAppMessage.fromJson(jsMessage.data ?? {});
- if (message.schema?.isNotEmpty == true) {
- final uri = Uri.parse(message.schema!);
- bool can = await canLaunchUrl(uri);
- if (!can) {
- Loading.showError(
- 'common_error_open_other_app'.trArgs([message.schema!]));
- return;
- }
- await launchUrl(uri, mode: LaunchMode.externalApplication);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- } else {
- OLogger.e('没传入 schema ');
- }
- }
- /// 打开新窗口
- _openWindow(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- JsUtilOpenWindowMessage message =
- JsUtilOpenWindowMessage.fromJson(jsMessage.data ?? {});
- if (message.url?.isNotEmpty == true) {
- InnerWebviewPage.open(message.url!);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- } else {
- OLogger.e('没有传入 url');
- }
- }
- /// 当前页面在手机默认浏览器打开
- _openCurrentPageInBrowser(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- final uri = await webviewController?.getUrl();
- if (uri != null) {
- if (await canLaunchUrl(uri)) {
- final result = await launchUrl(
- uri,
- mode: LaunchMode.externalApplication,
- webViewConfiguration: WebViewConfiguration(headers: <String, String>{
- O2ApiManager.instance.tokenName:
- O2ApiManager.instance.o2User?.token ?? ''
- }),
- );
- if (!result) {
- OLogger.e('打开 url ${uri.toString()} 失败!');
- }
- } else {
- OLogger.e('uri 不正确,${uri.toString()}');
- }
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 清除缓存并刷新页面
- _clearCache(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- //TODO 清除缓存
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 组织选择器
- _departmentPicker(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- JsBizPickerMessage message =
- JsBizPickerMessage.fromJson(jsMessage.data ?? {});
- final result = await ContactPickerPage.startPicker(
- [ContactPickMode.departmentPicker],
- topUnitList: message.topList,
- unitType: message.orgType,
- maxNumber: message.maxNumber,
- multiple: message.multiple,
- initDeptList: message.pickedDepartments);
- if (result is ContactPickerResult) {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'${json.encode(result.toJson())}\')');
- }
- }
- }
- /// 身份选择器
- _identityPicker(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- JsBizPickerMessage message =
- JsBizPickerMessage.fromJson(jsMessage.data ?? {});
- final result = await ContactPickerPage.startPicker(
- [ContactPickMode.identityPicker],
- topUnitList: message.topList,
- maxNumber: message.maxNumber,
- multiple: message.multiple,
- dutyList: message.duty,
- initIdList: message.pickedIdentities);
- if (result is ContactPickerResult) {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'${json.encode(result.toJson())}\')');
- }
- }
- }
- /// 人员选择器
- _personPicker(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- JsBizPickerMessage message =
- JsBizPickerMessage.fromJson(jsMessage.data ?? {});
- final result = await ContactPickerPage.startPicker(
- [ContactPickMode.personPicker],
- maxNumber: message.maxNumber,
- multiple: message.multiple,
- initUserList: message.pickedUsers);
- if (result is ContactPickerResult) {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'${json.encode(result.toJson())}\')');
- }
- }
- }
- /// 组合选择器
- _complexPicker(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- JsBizPickerMessage message =
- JsBizPickerMessage.fromJson(jsMessage.data ?? {});
- final pickModes = message.pickMode ?? [];
- if (pickModes.isEmpty) {
- Loading.showError('args_error'.tr);
- OLogger.e('pickMode 不能为空');
- return;
- }
- final result = await ContactPickerPage.startPicker(_pickModeList(pickModes),
- topUnitList: message.topList,
- maxNumber: message.maxNumber,
- multiple: message.multiple,
- unitType: message.orgType,
- dutyList: message.duty,
- initDeptList: message.pickedDepartments,
- initIdList: message.pickedIdentities,
- initGroupList: message.pickedGroups,
- initUserList: message.pickedUsers);
- if (result is ContactPickerResult) {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'${json.encode(result.toJson())}\')');
- }
- }
- }
- ///
- List<ContactPickMode> _pickModeList(List<String> pickModes) {
- return pickModes
- .map((e) {
- switch (e) {
- case 'departmentPicker':
- return ContactPickMode.departmentPicker;
- case 'identityPicker':
- return ContactPickMode.identityPicker;
- case 'groupPicker':
- return ContactPickMode.groupPicker;
- case 'personPicker':
- return ContactPickMode.personPicker;
- default:
- return null;
- }
- })
- .where((element) => element != null)
- .map((e) => e!)
- .toList();
- }
- /// 群组选择器
- _groupPicker(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- JsBizPickerMessage message =
- JsBizPickerMessage.fromJson(jsMessage.data ?? {});
- final result = await ContactPickerPage.startPicker(
- [ContactPickMode.groupPicker],
- maxNumber: message.maxNumber,
- multiple: message.multiple,
- initGroupList: message.pickedGroups);
- if (result is ContactPickerResult) {
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(
- source: '$callback(\'${json.encode(result.toJson())}\')');
- }
- }
- }
- /// 打开我的页面
- _settingOpenMyProfile(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- Get.toNamed(O2OARoutes.homeSettingsMyProfile);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- _settingOpenSafeAndAccount(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- Get.toNamed(O2OARoutes.homeSettingsAccountSafe);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- _settingOpenAppSkin(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- Get.toNamed(O2OARoutes.homeSettingsSkin);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- _settingOpenAppNotice(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- if (GetPlatform.isMobile) {
- AppSettings.openNotificationSettings();
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- _settingOpenAppCommon(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- Get.toNamed(O2OARoutes.homeSettingsCommon);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- _settingOpenAppAbout(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- Get.toNamed(O2OARoutes.homeSettingsAbout);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- Future<void> _settingLogout(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- O2Utils.logout();
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 人员组织搜索页面
- Future<void> _searchPage(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- if (O2ContactPermissionManager.instance.isCurrentPersonCannotQueryAll() ||
- O2ContactPermissionManager.instance.isCurrentPersonCannotQueryOuter()) {
- OLogger.i('当前用户没有权限搜索通讯录');
- Loading.toast('contact_no_permission'.tr);
- } else {
- Get.toNamed(O2OARoutes.homeContactSearch);
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 人员详情页面
- _personInfoPage(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- final personDn = jsMessage.data?['personDn'];
- if (personDn != null && personDn is String) {
- PersonPage.open(personDn);
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 根据组织dn打开 下级组织和人员页面
- Future<void> _orgInfoPage(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- final orgDn = jsMessage.data?['orgDn'];
- final orgName = jsMessage.data?['orgName'];
- if (orgDn != null && orgDn is String) {
- OrgPersonListPage.open(
- top: O2Unit(
- distinguishedName: orgDn,
- name: (orgName != null && orgName is String) ? orgName : ''));
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 打开IM会话
- Future<void> _openImConversation(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- final conversationId = jsMessage.data?['conversationId'];
- if (conversationId == null || conversationId is String) {
- ImChatPage.open((conversationId as String));
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 跟某人聊天
- Future<void> _openImChatWithSomeone(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- final person = jsMessage.data?['person'];
- if (person == null || person is String) {
- await _createConversation(
- O2.imConversationTypeSingle, [(person as String)]);
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 打开系统消息会话
- Future<void> _openImInstantChat(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- InstantChatPage.open();
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 选择人员创建单聊
- Future<void> _startSingleChat(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- var result =
- await ContactPickerPage.startPicker([ContactPickMode.personPicker]);
- if (result is ContactPickerResult) {
- if (result.users != null && result.users!.isNotEmpty) {
- if (result.users![0].distinguishedName! ==
- O2ApiManager.instance.o2User?.distinguishedName) {
- Loading.toast('im_msg_create_conversation_not_with_self'.tr);
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- return;
- }
- await _createConversation(
- O2.imConversationTypeSingle, [result.users![0].distinguishedName!]);
- }
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- /// 选择人员创建群聊
- Future<void> _startGroupChat(JsMessage jsMessage) async {
- final callback = jsMessage.callback ?? '';
- var result = await ContactPickerPage.startPicker(
- [ContactPickMode.personPicker],
- multiple: true);
- if (result is ContactPickerResult) {
- List<String> personList = [];
- result.users?.forEach((element) {
- personList.add(element.distinguishedName!);
- });
- await _createConversation(O2.imConversationTypeGroup, personList);
- }
- if (callback.isNotEmpty) {
- webviewController?.evaluateJavascript(source: '$callback()');
- }
- }
- Future<void> _createConversation(String type, List<String> personList) async {
- if (type.isEmpty || personList.isEmpty) {
- return;
- }
- final conv = await MessageCommunicationService.to
- .createConversation(type, personList);
- if (conv != null && conv.id != null) {
- ImChatPage.open(conv.id!);
- }
- }
- }
|