keyboard_method_handler.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import 'dart:convert';
  2. import 'package:dio/dio.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:get/get.dart';
  5. import 'package:injectable/injectable.dart';
  6. import 'package:keyboard/data/repository/chat_repository.dart';
  7. import 'package:keyboard/data/repository/keyboard_repository.dart';
  8. import 'package:keyboard/module/keyboard_manage/keyboard_manage_controller.dart';
  9. import 'package:keyboard/utils/atmob_log.dart';
  10. import '../data/api/response/chat_prologue_response.dart';
  11. import '../data/api/response/chat_super_speak_response.dart';
  12. import '../data/api/response/keyboard_list_response.dart';
  13. import '../data/consts/constants.dart';
  14. import '../utils/http_handler.dart';
  15. import '../utils/mmkv_util.dart';
  16. import '../utils/toast_util.dart';
  17. @injectable
  18. class KeyboardMethodHandler {
  19. final tag = "KeyboardMethodHandler";
  20. bool get isLogin => KVUtil.getBool(Constants.keyIsLogin, false);
  21. bool get isMember => KVUtil.getBool(Constants.keyIsMember, false);
  22. late KeyboardListResponse keyboardListResponse;
  23. KeyboardMethodHandler();
  24. Future<dynamic> handleMethodCall(MethodCall call) async {
  25. switch (call.method) {
  26. case 'getKeyboardList':
  27. return _handleGetKeyboardList(call);
  28. case 'selectedKeyboard':
  29. return _handleSelectedKeyboard(call);
  30. case 'getCharacterList':
  31. return _handleGetCharacterList(call);
  32. case 'getCurrentKeyboardInfo':
  33. return _handleGetCurrentKeyboardInfo(call);
  34. case 'getPrologueList':
  35. return _handleGetPrologueList(call);
  36. case 'isLogin':
  37. return isLogin;
  38. case 'isMember':
  39. return isMember;
  40. case 'chatSuperReply':
  41. return _handleChatSuperReply(call);
  42. case 'chatSuperSpeak':
  43. return _handleChatSuperSpeak(call);
  44. case 'chatPrologue':
  45. return _handleChatPrologue(call);
  46. case 'jumpAppPage':
  47. return _handleJumpAppPage(call);
  48. default:
  49. throw MissingPluginException('Not implemented: ${call.method}');
  50. }
  51. }
  52. Future<String> _handleGetKeyboardList(MethodCall call) async {
  53. String? type = call.arguments?['type'] as String?;
  54. final keyboardList = await KeyboardRepository.getInstance().getKeyboardList(
  55. type: type,
  56. );
  57. final selectedKeyboardJson = KVUtil.getString(Constants.keyboardSelect, null);
  58. if (selectedKeyboardJson != null) {
  59. try {
  60. final Map<String, dynamic> keyboardMap = jsonDecode(
  61. selectedKeyboardJson,
  62. );
  63. final String? keyboardId = keyboardMap['id'] as String?;
  64. if (keyboardId != null) {
  65. for (var element in keyboardList.keyboardInfos) {
  66. if (element.id == keyboardId) {
  67. element.isSelect = true;
  68. break;
  69. }
  70. }
  71. }
  72. } catch (e) {
  73. AtmobLog.e(tag, "解析本地选中键盘失败: $e");
  74. }
  75. }
  76. keyboardListResponse = keyboardList;
  77. return jsonEncode(keyboardList.toJson());
  78. }
  79. Future<String> _handleSelectedKeyboard(MethodCall call) async {
  80. final String keyboardId = call.arguments['keyboardId'];
  81. if (keyboardListResponse.keyboardInfos.isEmpty) {
  82. keyboardListResponse =
  83. await KeyboardRepository.getInstance().getKeyboardList();
  84. }
  85. final selectedKeyboard = keyboardListResponse.keyboardInfos
  86. .firstWhereOrNull((element) => element.id == keyboardId);
  87. if (selectedKeyboard != null) {
  88. if(selectedKeyboard.type == KeyboardType.custom.name) {
  89. try {
  90. await KeyboardRepository.getInstance().keyboardChoose(keyboardId:keyboardId);
  91. KeyboardRepository.getInstance().refreshData();
  92. } catch (error) {
  93. return handleError(error);
  94. }
  95. }
  96. KVUtil.putString(Constants.keyboardSelect, jsonEncode(selectedKeyboard.toJson()));
  97. }
  98. return "{}";
  99. }
  100. Future<String> _handleGetCurrentKeyboardInfo(MethodCall call) async {
  101. AtmobLog.d(tag, "_handleGetCurrentKeyboardInfo");
  102. final String? keyboardJsonStr = KVUtil.getString(Constants.keyboardSelect, null);
  103. if (keyboardJsonStr != null) {
  104. try {
  105. final jsonMap = jsonDecode(keyboardJsonStr);
  106. return jsonEncode(jsonMap);
  107. } catch (e) {
  108. AtmobLog.e(tag, "Failed to decode keyboard JSON: $e");
  109. }
  110. }
  111. return "{}";
  112. }
  113. Future<String> _handleGetCharacterList(MethodCall call) async {
  114. final String keyboardId = call.arguments['keyboardId'];
  115. final characterList = await KeyboardRepository.getInstance()
  116. .getKeyboardCharacterList(keyboardId: keyboardId);
  117. return jsonEncode(characterList.toJson());
  118. }
  119. // 获取开场白列表
  120. Future<String> _handleGetPrologueList(MethodCall call) async {
  121. final prologueList =
  122. await KeyboardRepository.getInstance().getPrologueList();
  123. return jsonEncode(prologueList.toJson());
  124. }
  125. // 超会回
  126. Future<String> _handleChatSuperReply(MethodCall call) async {
  127. final String content = call.arguments['content'];
  128. final String keyboardId = call.arguments['keyboardId'];
  129. final String characterId = call.arguments['characterId'];
  130. try {
  131. final chatSuperReplyResponse = await ChatRepository.getInstance()
  132. .chatSuperReply(
  133. content: content,
  134. keyboardId: keyboardId,
  135. characterId: characterId,
  136. );
  137. return jsonEncode(chatSuperReplyResponse.toJson());
  138. } catch (error) {
  139. return handleError(error);
  140. }
  141. }
  142. // 超会说
  143. Future<String> _handleChatSuperSpeak(MethodCall call) async {
  144. final String content = call.arguments['content'];
  145. final String keyboardId = call.arguments['keyboardId'];
  146. final String characterId = call.arguments['characterId'];
  147. final ChatSuperSpeakResponse chatSuperSpeakResponse;
  148. try {
  149. chatSuperSpeakResponse = await ChatRepository.getInstance()
  150. .chatSuperSpeak(
  151. content: content,
  152. keyboardId: keyboardId,
  153. characterId: characterId,
  154. );
  155. return jsonEncode(chatSuperSpeakResponse.toJson());
  156. } catch (error) {
  157. return handleError(error);
  158. }
  159. }
  160. // 开场白
  161. Future<String> _handleChatPrologue(MethodCall call) async {
  162. final String name = call.arguments['name'];
  163. final ChatPrologueResponse chatPrologueResponse;
  164. try {
  165. chatPrologueResponse = await ChatRepository.getInstance().chatPrologue(
  166. name: name,
  167. );
  168. return jsonEncode(chatPrologueResponse.toJson());
  169. }catch (error) {
  170. return handleError(error);
  171. }
  172. }
  173. Future<String> _handleJumpAppPage(MethodCall call) async {
  174. try {
  175. final String path = call.arguments['path'];
  176. final dynamic args = call.arguments['args'];
  177. final dynamic offAll = call.arguments['offAll'] ?? false;
  178. Map<String, dynamic> parsedArgs = {};
  179. if (args is String && args.isNotEmpty) {
  180. try {
  181. parsedArgs = Map<String, dynamic>.from(json.decode(args));
  182. } catch (e) {
  183. return '{}';
  184. }
  185. } else if (args is Map) {
  186. parsedArgs = Map<String, dynamic>.from(args);
  187. }
  188. // 是否跳转到新页面后,并清除上层的所有页面
  189. if (offAll) {
  190. Get.offAllNamed(
  191. path,
  192. parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
  193. );
  194. } else {
  195. // 普通路由跳转
  196. Get.toNamed(
  197. path,
  198. parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
  199. );
  200. }
  201. return '{}';
  202. } catch (e) {
  203. print("跳转失败: $e");
  204. return '{}';
  205. }
  206. }
  207. String handleError(dynamic error) {
  208. if (error is ServerErrorException) {
  209. AtmobLog.e(tag, "接口请求失败: ${error.message}");
  210. // 抛异常给原生,同时也返回json
  211. throw PlatformException(
  212. code: error.code.toString(),
  213. message: error.message,
  214. details: {'code': error.code, 'message': error.message},
  215. );
  216. } else {
  217. AtmobLog.e(tag, "未知异常: $error");
  218. throw PlatformException(
  219. code: "-1",
  220. message: "网络异常",
  221. details: {'code': -1, 'message': error.toString()},
  222. );
  223. }
  224. }
  225. }