TemplateEditor.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 used_source_list = ref([])
  62. const common_config = ref({})
  63. const template_name = ref('')
  64. const template_id = ref('') // 模板id,用于保存模板
  65. const draft_id = ref('') // 草稿id,用户编辑草稿时使用
  66. const template_save_time = ref('')
  67. const is_loading_template_detail = ref(false)
  68. const showTips = ref(false)
  69. const is_loading_edit = ref(false)
  70. const save_error_list = ref([])
  71. const show_save_error = ref(false)
  72. const emit = defineEmits(['toBackHome','send-report']);
  73. const visible = computed(()=>{
  74. return true
  75. })
  76. const getTemplateDetail = async () => {
  77. if(props.target_template_id) {
  78. is_loading_template_detail.value = true
  79. try{
  80. const res = await checkTemplateDetail({id: props.target_template_id})
  81. if(res.code/1 === 200) {
  82. const content = res.data.content
  83. template_content.value = content
  84. if(res.data.source_list){
  85. used_source_list.value = JSON.parse(res.data.source_list)
  86. reportEditor.updateUsedSourceList(used_source_list.value)
  87. }
  88. if(res.data.template_config){
  89. template_config.value = JSON.parse(res.data.template_config)
  90. template_config.value.forEach(item => {
  91. const it = used_source_list.value.find(source => source.id === item.option.metric_id)
  92. if (it) {
  93. item.option.source = it
  94. }
  95. })
  96. console.log(template_config.value, 9090)
  97. }
  98. if(res.data.common_config){
  99. common_config.value = JSON.parse(res.data.common_config)
  100. }
  101. template_name.value = res.data.name
  102. template_save_time.value =formatDateTime(res.data.updated_at) || ''
  103. // common_config.value
  104. if(res.data.type/1 === 1){
  105. template_id.value = res.data.id
  106. }else {
  107. draft_id.value = res.data.id
  108. }
  109. reportEditor.setMetricsList(template_config.value)
  110. for(const key in common_config.value) {
  111. reportEditor.setCommonConfig(key,common_config.value[key])
  112. }
  113. }
  114. }catch(e) {
  115. console.log(e)
  116. }finally {
  117. is_loading_template_detail.value = false
  118. }
  119. }
  120. }
  121. const handleSaveTemplate = (data) => {
  122. // console.log(data,'保存模板')
  123. template_id.value = data.id
  124. template_name.value = data.name
  125. template_save_time.value = formatDateTime(data.created_at) || ''
  126. }
  127. const handleUpdateDraft = (data) => {
  128. // console.log(data,'保存草稿')
  129. draft_id.value = data.id
  130. template_name.value = data.name
  131. template_save_time.value = formatDateTime(data.created_at) || ''
  132. }
  133. const toBack = () => {
  134. const is_need_save_template = reportEditor.QueryIsNeedSaveTemplate
  135. if(is_need_save_template && template_id.value) {
  136. // ElMessage.warning('当前模板有未保存的内容,是否确认放弃?')
  137. showTips.value = true
  138. } else {
  139. emit('toBackHome');
  140. reportEditor.clearAll()
  141. }
  142. // emit('toBackHome');
  143. }
  144. const toSaveSetting = async () => {
  145. const complete_list = reportEditor.checkMetricConfigComplete();
  146. if (complete_list.length > 0) {
  147. // Message.error('指标配置不完善,请完善后再保存');
  148. save_error_list.value = complete_list;
  149. show_save_error.value = true;
  150. return;
  151. }
  152. const res = await editTemplateFunc(template_id.value)
  153. if(res.code/1 === 200) {
  154. Message.success('保存成功')
  155. showTips.value = false
  156. template_save_time.value = formatDateTime(res.data.updated_at) || ''
  157. reportEditor.setIsNeedSaveTemplate(false)
  158. emit('toBackHome');
  159. }
  160. }
  161. const handleSaveErrorOk = () => {
  162. show_save_error.value = false
  163. save_error_list.value = []
  164. showTips.value = true
  165. }
  166. const toNotSaveSetting = () => {
  167. showTips.value = false
  168. reportEditor.setIsNeedSaveTemplate(false)
  169. emit('toBackHome');
  170. }
  171. const editTemplateFunc = async (id) => {
  172. try {
  173. is_loading_edit.value = true
  174. const source_list = reportEditor.metrics_list.map((item) => item.option?.source);
  175. const uniqueMap = new Map();
  176. source_list.forEach((item) => {
  177. // 只有当 Map 中不存在该 id 时才存入,从而保留第一项
  178. if (!uniqueMap.has(item.id)) {
  179. uniqueMap.set(item.id, item);
  180. }
  181. });
  182. const used_source_list = Array.from(uniqueMap.values());
  183. const template_config = reportEditor.metrics_list.map((item) => {
  184. const { source, ...otherOptionProps } = item.option;
  185. return {
  186. ...item,
  187. option: otherOptionProps
  188. };
  189. });
  190. console.log(template_config, 'template_config');
  191. console.log(used_source_list, 'used_source_list');
  192. const res = await editTemplateItem({
  193. id,
  194. name: template_name.value,
  195. content: reportEditor.target_report_markdown,
  196. // template_config: JSON.stringify(reportEditor.metrics_list),
  197. template_config: JSON.stringify(template_config),
  198. source_list: JSON.stringify(used_source_list),
  199. common_config: JSON.stringify(common_config.value)
  200. });
  201. if (res.code / 1 === 200) {
  202. return res;
  203. } else {
  204. Message.error('保存失败');
  205. }
  206. } catch (error) {
  207. Message.error('保存失败');
  208. } finally {
  209. is_loading_edit.value = false
  210. }
  211. };
  212. const handleSendReport = (data) => {
  213. emit('send-report', data);
  214. reportEditor.clearAll()
  215. };
  216. watch(()=>props.target_template_id, () => {
  217. if(props.target_template_id) {
  218. getTemplateDetail();
  219. }
  220. }, {immediate: true})
  221. watch(()=>props.target_common_config,() => {
  222. // console.log(props.target_common_config)
  223. reportEditor.setCommonConfig('common_start_time', props.target_common_config?.start_date);
  224. reportEditor.setCommonConfig('common_end_time', props.target_common_config?.end_date);
  225. reportEditor.setCommonConfig('common_area', props.target_common_config?.area);
  226. // console.log(props.target_common_config,reportEditor)
  227. },{
  228. immediate:true,
  229. deep:true
  230. })
  231. </script>
  232. <style scoped lang="css">
  233. .container {
  234. height: 100%;
  235. box-sizing: border-box;
  236. padding: 12px 10px;
  237. font-family: '思源黑体';
  238. background: #f5fafa;
  239. }
  240. </style>
  241. <style>
  242. .my_custom_template {
  243. height: 100vh;
  244. }
  245. .my_custom_template .arco-modal-header {
  246. display: none;
  247. }
  248. .my_custom_template .arco-modal-footer {
  249. display: none;
  250. }
  251. .my_custom_template .arco-modal-body {
  252. height: 100%;
  253. box-sizing: border-box;
  254. padding: 0px;
  255. }
  256. </style>