controller.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:o2oa_all_platform/common/index.dart';
  4. import '../../../common/create_form/index.dart';
  5. import '../cms_document/index.dart';
  6. import 'index.dart';
  7. class CmsAppController extends GetxController {
  8. CmsAppController();
  9. final state = CmsAppState();
  10. CmsCategoryData? _category;
  11. List<CmsCategoryData> publishCategoryList = [];
  12. /// 在 widget 内存中分配后立即调用。
  13. @override
  14. void onInit() {
  15. super.onInit();
  16. }
  17. /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  18. @override
  19. void onReady() {
  20. final map = Get.arguments;
  21. if (map != null && map["appData"] != null && map["appData"] is CmsAppData) {
  22. CmsAppData app = map["appData"] as CmsAppData;
  23. state.app.value = app;
  24. loadCanPublish();
  25. final categoryList = app.wrapOutCategoryList ?? [];
  26. state.categoryList.clear();
  27. state.categoryList.addAll(categoryList);
  28. if (map["categoryId"] != null) {
  29. String categoryId = map["categoryId"] as String;
  30. for (var i = 0; i < categoryList.length; i++) {
  31. CmsCategoryData category = categoryList[i];
  32. if (category.id == categoryId) {
  33. state.initCategoryIndex = i;
  34. break;
  35. }
  36. }
  37. }
  38. } else {
  39. Loading.toast('cms_app_error'.tr);
  40. Get.back();
  41. }
  42. super.onReady();
  43. }
  44. void loadCanPublish() async {
  45. if (state.app.value != null && state.app.value?.id != null) {
  46. final app = await CmsAssembleControlService.to
  47. .getAppCanPublishCategories(state.app.value!.id!);
  48. if (app?.wrapOutCategoryList?.isNotEmpty == true) {
  49. publishCategoryList.clear();
  50. publishCategoryList.addAll(app!.wrapOutCategoryList!);
  51. state.canPublish = true;
  52. }
  53. }
  54. }
  55. void clickPublish() {
  56. _category = null;
  57. _showCmsCategoryChoose();
  58. }
  59. /// 选择分类
  60. void _showCmsCategoryChoose() {
  61. final c = Get.context;
  62. if (c != null) {
  63. O2UI.showBottomSheetWithCancel(
  64. c,
  65. publishCategoryList
  66. .map((e) => ListTile(
  67. onTap: () {
  68. Navigator.pop(c);
  69. _category = e;
  70. _startCreateDocument();
  71. },
  72. title: Align(
  73. alignment: Alignment.center,
  74. child: Text(e.categoryName ?? '',
  75. style: Theme.of(c).textTheme.bodyMedium),
  76. ),
  77. ))
  78. .toList());
  79. }
  80. }
  81. void _startCreateDocument() async {
  82. if (state.app.value == null || _category == null || _category?.id == null) {
  83. Loading.toast('cms_create_document_no_args'.tr);
  84. return;
  85. }
  86. var config = state.app.value?.config;
  87. bool ignoreTitle = false; // 是否忽略标题
  88. bool latest = true; // 是否查询草稿
  89. if (config != null && config.isNotEmpty) {
  90. final map = O2Utils.parseStringToJson(config);
  91. final ig = map['ignoreTitle'];
  92. if (ig != null && ig is bool) {
  93. ignoreTitle = ig;
  94. }
  95. final la = map['latest'];
  96. if (la != null && la is bool) {
  97. latest = la;
  98. }
  99. }
  100. if (latest) {
  101. var drafts =
  102. await CmsAssembleControlService.to.listDocumentDraft(_category!.id!);
  103. if (drafts != null && drafts.isNotEmpty) {
  104. OLogger.i('有草稿。。。。直接打开草稿!');
  105. CmsDocumentPage.open(drafts[0].id!,
  106. title: drafts[0].title ?? '', options: {"readonly": false});
  107. return;
  108. }
  109. }
  110. CreateFormPage.startCmsDoc(ignoreTitle, category: _category!);
  111. }
  112. }