TemplateEditor.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <a-modal v-model:visible="visible" width="100%" modal-class="my_custom_template" :mask="false">
  3. <div class="container">
  4. <TemplateHeader @toBack="toBack"></TemplateHeader>
  5. <TemplateDetail
  6. :md_data="template_content"
  7. :template_name="template_name"
  8. :template_id="template_id"
  9. :draft_id="draft_id"
  10. :template_save_time="template_save_time"
  11. :is_loading_template_detail="is_loading_template_detail"
  12. :common_config="target_common_config"
  13. @save-template="handleSaveTemplate"
  14. @update-draft="handleUpdateDraft"
  15. @send-report="handleSendReport"
  16. ></TemplateDetail>
  17. </div>
  18. </a-modal>
  19. <TipsModel v-model:visible="showTips" :loading="is_loading_edit" @ok="toSaveSetting" @cancel="toNotSaveSetting" />
  20. <SaveErrorModal
  21. v-model:visible="show_save_error"
  22. :complete_list="save_error_list"
  23. @ok="handleSaveErrorOk"
  24. ></SaveErrorModal>
  25. </template>
  26. <script setup lang="js">
  27. import TemplateHeader from './TemplateHeader.vue'
  28. import TemplateDetail from './TemplateDetail.vue'
  29. import { ref,computed,watch } from 'vue'
  30. import {str_md} from './moke.js'
  31. import {checkTemplateDetail,editTemplateItem} from '../conversation-dialog/api-template.js'
  32. import { useReportEditor } from '../../base/store/use-report-editor';
  33. import {formatDateTime} from '../../utils/utils.js'
  34. import TipsModel from './editor-model/TipsModel.vue'
  35. import SaveErrorModal from './editor-model/SaveErrorModal.vue'
  36. import { Message } from '@arco-design/web-vue';
  37. const reportEditor = useReportEditor();
  38. const props = defineProps({
  39. target_template_id: {
  40. type: String,
  41. default: ''
  42. },
  43. target_outline_md:{
  44. type: String,
  45. default:''
  46. },
  47. target_common_config:{
  48. type: Object,
  49. default:()=>{
  50. return {
  51. start_date:'',
  52. end_date:'',
  53. theme:'',
  54. area:''
  55. }
  56. }
  57. },
  58. })
  59. const template_content = ref(props.target_outline_md||'')
  60. const template_config = ref([])
  61. const common_config = ref({})
  62. const template_name = ref('')
  63. const template_id = ref('') // 模板id,用于保存模板
  64. const draft_id = ref('') // 草稿id,用户编辑草稿时使用
  65. const template_save_time = ref('')
  66. const is_loading_template_detail = ref(false)
  67. const showTips = ref(false)
  68. const is_loading_edit = ref(false)
  69. const save_error_list = ref([])
  70. const show_save_error = ref(false)
  71. const emit = defineEmits(['toBackHome','send-report']);
  72. const visible = computed(()=>{
  73. return true
  74. })
  75. const getTemplateDetail = async () => {
  76. if(props.target_template_id) {
  77. is_loading_template_detail.value = true
  78. try{
  79. const res = await checkTemplateDetail({id: props.target_template_id})
  80. if(res.code/1 === 200) {
  81. console.log(res,'模板详情')
  82. const content = res.data.content
  83. template_content.value = content
  84. if(res.data.template_config){
  85. template_config.value = JSON.parse(res.data.template_config)
  86. }
  87. if(res.data.common_config){
  88. common_config.value = JSON.parse(res.data.common_config)
  89. }
  90. template_name.value = res.data.name
  91. template_save_time.value =formatDateTime(res.data.updated_at) || ''
  92. // common_config.value
  93. if(res.data.type/1 === 1){
  94. template_id.value = res.data.id
  95. }else {
  96. draft_id.value = res.data.id
  97. }
  98. reportEditor.setMetricsList(template_config.value)
  99. for(const key in common_config.value) {
  100. reportEditor.setCommonConfig(key,common_config.value[key])
  101. }
  102. }
  103. }catch(e) {
  104. console.log(e)
  105. }finally {
  106. is_loading_template_detail.value = false
  107. }
  108. }
  109. }
  110. const handleSaveTemplate = (data) => {
  111. // console.log(data,'保存模板')
  112. template_id.value = data.id
  113. template_name.value = data.name
  114. template_save_time.value = formatDateTime(data.created_at) || ''
  115. }
  116. const handleUpdateDraft = (data) => {
  117. // console.log(data,'保存草稿')
  118. draft_id.value = data.id
  119. template_name.value = data.name
  120. template_save_time.value = formatDateTime(data.created_at) || ''
  121. }
  122. const toBack = () => {
  123. const is_need_save_template = reportEditor.QueryIsNeedSaveTemplate
  124. if(is_need_save_template && template_id.value) {
  125. // ElMessage.warning('当前模板有未保存的内容,是否确认放弃?')
  126. showTips.value = true
  127. } else {
  128. emit('toBackHome');
  129. reportEditor.clearAll()
  130. }
  131. // emit('toBackHome');
  132. }
  133. const toSaveSetting = async () => {
  134. const complete_list = reportEditor.checkMetricConfigComplete();
  135. if (complete_list.length > 0) {
  136. // Message.error('指标配置不完善,请完善后再保存');
  137. save_error_list.value = complete_list;
  138. show_save_error.value = true;
  139. return;
  140. }
  141. const res = await editTemplateFunc(template_id.value)
  142. if(res.code/1 === 200) {
  143. Message.success('保存成功')
  144. showTips.value = false
  145. template_save_time.value = formatDateTime(res.data.updated_at) || ''
  146. reportEditor.setIsNeedSaveTemplate(false)
  147. emit('toBackHome');
  148. }
  149. }
  150. const handleSaveErrorOk = () => {
  151. show_save_error.value = false
  152. save_error_list.value = []
  153. showTips.value = true
  154. }
  155. const toNotSaveSetting = () => {
  156. showTips.value = false
  157. reportEditor.setIsNeedSaveTemplate(false)
  158. emit('toBackHome');
  159. }
  160. const editTemplateFunc = async (id) => {
  161. try {
  162. is_loading_edit.value = true
  163. const res = await editTemplateItem({
  164. id,
  165. name: template_name.value,
  166. content: reportEditor.target_report_markdown,
  167. template_config: JSON.stringify(reportEditor.metrics_list),
  168. common_config: JSON.stringify(common_config.value)
  169. });
  170. if (res.code / 1 === 200) {
  171. return res;
  172. } else {
  173. Message.error('保存失败');
  174. }
  175. } catch (error) {
  176. Message.error('保存失败');
  177. } finally {
  178. is_loading_edit.value = false
  179. }
  180. };
  181. const handleSendReport = (data) => {
  182. emit('send-report', data);
  183. reportEditor.clearAll()
  184. };
  185. watch(()=>props.target_template_id, () => {
  186. if(props.target_template_id) {
  187. getTemplateDetail();
  188. }
  189. }, {immediate: true})
  190. watch(()=>props.target_common_config,() => {
  191. // console.log(props.target_common_config)
  192. reportEditor.setCommonConfig('common_start_time', props.target_common_config?.start_date);
  193. reportEditor.setCommonConfig('common_end_time', props.target_common_config?.end_date);
  194. reportEditor.setCommonConfig('common_area', props.target_common_config?.area);
  195. // console.log(props.target_common_config,reportEditor)
  196. },{
  197. immediate:true,
  198. deep:true
  199. })
  200. </script>
  201. <style scoped lang="css">
  202. .container {
  203. height: 100%;
  204. box-sizing: border-box;
  205. padding: 12px 10px;
  206. font-family: '思源黑体';
  207. background: #f5fafa;
  208. }
  209. </style>
  210. <style>
  211. .my_custom_template {
  212. height: 100vh;
  213. }
  214. .my_custom_template .arco-modal-header {
  215. display: none;
  216. }
  217. .my_custom_template .arco-modal-footer {
  218. display: none;
  219. }
  220. .my_custom_template .arco-modal-body {
  221. height: 100%;
  222. box-sizing: border-box;
  223. padding: 0px;
  224. }
  225. </style>