keyboard_method_handler.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import 'dart:convert';
  2. import 'package:flutter/services.dart';
  3. import 'package:get/get.dart';
  4. import 'package:injectable/injectable.dart';
  5. import 'package:keyboard/data/repository/chat_repository.dart';
  6. import 'package:keyboard/data/repository/keyboard_repository.dart';
  7. import 'package:keyboard/utils/atmob_log.dart';
  8. import '../data/api/response/chat_prologue_response.dart';
  9. import '../data/api/response/chat_super_speak_response.dart';
  10. import '../data/api/response/keyboard_list_response.dart';
  11. import '../data/consts/constants.dart';
  12. import '../utils/http_handler.dart';
  13. import '../utils/mmkv_util.dart';
  14. import '../utils/toast_util.dart';
  15. @injectable
  16. class KeyboardMethodHandler {
  17. final tag = "KeyboardMethodHandler";
  18. // 用处存储选中的键盘id
  19. static const String keyboardSelect = 'keyboard_select';
  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(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. KVUtil.putString(keyboardSelect, jsonEncode(selectedKeyboard.toJson()));
  89. }
  90. return "{}";
  91. }
  92. Future<String> _handleGetCurrentKeyboardInfo(MethodCall call) async {
  93. final String? keyboardJsonStr = KVUtil.getString(keyboardSelect, null);
  94. if (keyboardJsonStr != null) {
  95. try {
  96. final jsonMap = jsonDecode(keyboardJsonStr);
  97. return jsonEncode(jsonMap);
  98. } catch (e) {
  99. AtmobLog.e(tag, "Failed to decode keyboard JSON: $e");
  100. }
  101. }
  102. return "{}";
  103. }
  104. Future<String> _handleGetCharacterList(MethodCall call) async {
  105. final String keyboardId = call.arguments['keyboardId'];
  106. final characterList = await KeyboardRepository.getInstance()
  107. .getKeyboardCharacterList(keyboardId: keyboardId);
  108. return jsonEncode(characterList.toJson());
  109. }
  110. // 获取开场白列表
  111. Future<String> _handleGetPrologueList(MethodCall call) async {
  112. final prologueList =
  113. await KeyboardRepository.getInstance().getPrologueList();
  114. return jsonEncode(prologueList.toJson());
  115. }
  116. // 超会回
  117. Future<String> _handleChatSuperReply(MethodCall call) async {
  118. final String content = call.arguments['content'];
  119. final String keyboardId = call.arguments['keyboardId'];
  120. final String characterId = call.arguments['characterId'];
  121. try {
  122. final chatSuperReplyResponse = await ChatRepository.getInstance()
  123. .chatSuperReply(
  124. content: content,
  125. keyboardId: keyboardId,
  126. characterId: characterId,
  127. );
  128. return jsonEncode(chatSuperReplyResponse.toJson());
  129. } catch (error) {
  130. return handleError(error);
  131. }
  132. }
  133. // 超会说
  134. Future<String> _handleChatSuperSpeak(MethodCall call) async {
  135. final String content = call.arguments['content'];
  136. final String keyboardId = call.arguments['keyboardId'];
  137. final String characterId = call.arguments['characterId'];
  138. final ChatSuperSpeakResponse chatSuperSpeakResponse;
  139. try {
  140. chatSuperSpeakResponse = await ChatRepository.getInstance()
  141. .chatSuperSpeak(
  142. content: content,
  143. keyboardId: keyboardId,
  144. characterId: characterId,
  145. );
  146. return jsonEncode(chatSuperSpeakResponse.toJson());
  147. } catch (error) {
  148. return handleError(error);
  149. }
  150. }
  151. // 开场白
  152. Future<String> _handleChatPrologue(MethodCall call) async {
  153. final String name = call.arguments['name'];
  154. final ChatPrologueResponse chatPrologueResponse;
  155. try {
  156. chatPrologueResponse = await ChatRepository.getInstance().chatPrologue(
  157. name: name,
  158. );
  159. return jsonEncode(chatPrologueResponse.toJson());
  160. }catch (error) {
  161. return handleError(error);
  162. }
  163. }
  164. Future<String> _handleJumpAppPage(MethodCall call) async {
  165. try {
  166. final String path = call.arguments['path'];
  167. final dynamic args = call.arguments['args'];
  168. final dynamic offAll = call.arguments['offAll'] ?? false;
  169. Map<String, dynamic> parsedArgs = {};
  170. if (args is String && args.isNotEmpty) {
  171. try {
  172. parsedArgs = Map<String, dynamic>.from(json.decode(args));
  173. } catch (e) {
  174. return '{}';
  175. }
  176. } else if (args is Map) {
  177. parsedArgs = Map<String, dynamic>.from(args);
  178. }
  179. // 是否跳转到新页面后,并清除上层的所有页面
  180. if (offAll) {
  181. Get.offAllNamed(
  182. path,
  183. parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
  184. );
  185. } else {
  186. // 普通路由跳转
  187. Get.toNamed(
  188. path,
  189. parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
  190. );
  191. }
  192. return '{}';
  193. } catch (e) {
  194. print("跳转失败: $e");
  195. return '{}';
  196. }
  197. }
  198. String handleError(dynamic error) {
  199. if (error is ServerErrorException) {
  200. AtmobLog.e(tag, "接口请求失败: ${error.message}");
  201. return jsonEncode({'code': error.code, 'message': error.message});
  202. } else {
  203. AtmobLog.e(tag, "未知异常: $error");
  204. return jsonEncode({'code': -1, 'message': '$error'});
  205. }
  206. }
  207. }