goview_vue/src/views/chart/hooks/useSync.hook.ts

235 lines
8.2 KiB
TypeScript
Raw Normal View History

import { onUnmounted } from 'vue';
2022-05-27 20:09:48 +08:00
import html2canvas from 'html2canvas'
import { getUUID, httpErrorHandle, fetchRouteParamsLocation, base64toFile } from '@/utils'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
2022-05-27 11:49:25 +08:00
import { EditCanvasTypeEnum, ChartEditStoreEnum, ProjectInfoEnum, ChartEditStorage } from '@/store/modules/chartEditStore/chartEditStore.d'
import { useChartHistoryStore } from '@/store/modules/chartHistoryStore/chartHistoryStore'
import { useSystemStore } from '@/store/modules/systemStore/systemStore'
import { fetchChartComponent, fetchConfigComponent, createComponent } from '@/packages/index'
2022-05-24 17:42:49 +08:00
import { saveInterval } from '@/settings/designSetting'
2022-06-22 18:50:15 +08:00
import throttle from 'lodash/throttle'
// 接口状态
import { ResultEnum } from '@/enums/httpEnum'
// 接口
import { saveProjectApi, fetchProjectApi, uploadFile, updateProjectApi } from '@/api/path'
// 画布枚举
import { SyncEnum } from '@/enums/editPageEnum'
2022-08-09 21:06:09 +08:00
import { CreateComponentType, CreateComponentGroupType, ConfigType } from '@/packages/index.d'
import { PublicGroupConfigClass } from '@/packages/public/publicConfig'
// 请求处理
export const useSync = () => {
const chartEditStore = useChartEditStore()
const chartHistoryStore = useChartHistoryStore()
const systemStore = useSystemStore()
/**
* *
* @param projectData
* @param isReplace
* @returns
*/
const updateComponent = async (projectData: ChartEditStorage, isReplace = false, changeId = false) => {
if (isReplace) {
// 清除列表
chartEditStore.componentList = []
// 清除历史记录
chartHistoryStore.clearBackStack()
chartHistoryStore.clearForwardStack()
}
// 列表组件注册
2022-08-09 21:06:09 +08:00
projectData.componentList.forEach(async (e: CreateComponentType | CreateComponentGroupType) => {
const intComponent = (target: CreateComponentType) => {
if (!window['$vue'].component(target.chartConfig.chartKey)) {
window['$vue'].component(target.chartConfig.chartKey, fetchChartComponent(target.chartConfig))
window['$vue'].component(target.chartConfig.conKey, fetchConfigComponent(target.chartConfig))
}
}
if (e.isGroup) {
;(e as CreateComponentGroupType).groupList.forEach(groupItem => {
intComponent(groupItem)
})
} else {
intComponent(e as CreateComponentType)
}
})
// 数据赋值
for (const key in projectData) {
// 组件
if (key === ChartEditStoreEnum.COMPONENT_LIST) {
for (const comItem of projectData[key]) {
// 重新创建是为了处理类种方法消失的问题
const create = async (
_componentInstance: CreateComponentType,
callBack?: (componentInstance: CreateComponentType) => void
) => {
// 补充 class 上的方法
let newComponent: CreateComponentType = await createComponent(_componentInstance.chartConfig)
if (callBack) {
if (changeId) {
callBack(Object.assign(newComponent, { ..._componentInstance, id: getUUID() }))
} else {
callBack(Object.assign(newComponent, _componentInstance))
}
} else {
if (changeId) {
chartEditStore.addComponentList(
Object.assign(newComponent, { ..._componentInstance, id: getUUID() }),
false,
true
)
} else {
chartEditStore.addComponentList(Object.assign(newComponent, _componentInstance), false, true)
}
}
}
if (comItem.isGroup) {
// 创建分组
let groupClass = new PublicGroupConfigClass()
if (changeId) {
groupClass = Object.assign(groupClass, { ...comItem, id: getUUID() })
} else {
groupClass = Object.assign(groupClass, comItem)
}
// 注册子应用
const targetList: CreateComponentType[] = []
;(comItem as CreateComponentGroupType).groupList.forEach(groupItem => {
create(groupItem, e => {
targetList.push(e)
})
})
groupClass.groupList = targetList
// 分组插入到列表
chartEditStore.addComponentList(groupClass, false, true)
} else {
create(comItem as CreateComponentType)
}
}
} else {
// 非组件(顺便排除脏数据)
if (key !== 'editCanvasConfig' && key !== 'requestGlobalConfig') return
2022-08-15 11:43:32 +08:00
Object.assign(chartEditStore[key], projectData[key])
}
}
}
2022-05-26 01:01:59 +08:00
/**
* *
* @param projectData
* @returns
*/
2022-05-27 11:49:25 +08:00
const updateStoreInfo = (projectData: {
id: string,
projectName: string,
indexImage: string,
remarks: string,
2022-06-03 14:48:58 +08:00
state: number
2022-05-27 11:49:25 +08:00
}) => {
2022-06-03 14:48:58 +08:00
const { projectName, remarks, indexImage, state } = projectData
2022-05-27 11:49:25 +08:00
// 名称
chartEditStore.setProjectInfo(ProjectInfoEnum.PROJECT_NAME, projectName)
// 描述
chartEditStore.setProjectInfo(ProjectInfoEnum.REMARKS, remarks)
// 缩略图
chartEditStore.setProjectInfo(ProjectInfoEnum.THUMBNAIL, indexImage)
2022-06-03 14:48:58 +08:00
// 发布
chartEditStore.setProjectInfo(ProjectInfoEnum.RELEASE, state === 1)
2022-05-26 01:01:59 +08:00
}
2022-05-27 20:09:48 +08:00
// * 数据获取
const dataSyncFetch = async () => {
chartEditStore.setEditCanvas(EditCanvasTypeEnum.SAVE_STATUS, SyncEnum.START)
try {
2022-06-01 22:41:11 +08:00
const res = await fetchProjectApi({ projectId: fetchRouteParamsLocation() }) as unknown as MyResponseType
if (res.code === ResultEnum.SUCCESS) {
if (res.data) {
2022-05-27 11:49:25 +08:00
updateStoreInfo(res.data)
2022-05-26 01:01:59 +08:00
// 更新全局数据
2022-05-27 11:49:25 +08:00
await updateComponent(JSON.parse(res.data.content))
return
}
setTimeout(() => {
chartEditStore.setEditCanvas(EditCanvasTypeEnum.SAVE_STATUS, SyncEnum.SUCCESS)
}, 1000)
return
}
chartEditStore.setEditCanvas(EditCanvasTypeEnum.SAVE_STATUS, SyncEnum.FAILURE)
} catch (error) {
chartEditStore.setEditCanvas(EditCanvasTypeEnum.SAVE_STATUS, SyncEnum.FAILURE)
httpErrorHandle()
}
}
2022-05-27 20:09:48 +08:00
// * 数据保存
2022-06-22 18:50:15 +08:00
const dataSyncUpdate = throttle(async () => {
2022-05-27 11:49:25 +08:00
if(!fetchRouteParamsLocation()) return
if(!systemStore.getFetchInfo.OSSUrl) {
window['$message'].error('数据保存失败,请刷新页面重试!')
return
}
chartEditStore.setEditCanvas(EditCanvasTypeEnum.SAVE_STATUS, SyncEnum.START)
2022-05-27 20:09:48 +08:00
// 获取缩略图片
const range = document.querySelector('.go-edit-range') as HTMLElement
// 生成图片
2022-06-09 08:48:28 +08:00
const canvasImage: HTMLCanvasElement = await html2canvas(range, {
backgroundColor: null
})
2022-05-28 11:50:17 +08:00
// 上传预览图
2022-05-27 20:09:48 +08:00
let uploadParams = new FormData()
uploadParams.append('object', base64toFile(canvasImage.toDataURL(), `${fetchRouteParamsLocation()}_index_preview.png`))
2022-06-01 22:41:11 +08:00
const uploadRes = await uploadFile(systemStore.getFetchInfo.OSSUrl, uploadParams) as unknown as MyResponseType
2022-05-28 11:50:17 +08:00
// 保存预览图
if(uploadRes.code === ResultEnum.SUCCESS) {
2022-05-28 11:50:17 +08:00
await updateProjectApi({
id: fetchRouteParamsLocation(),
indexImage: uploadRes.data.objectContent.httpRequest.uri
2022-05-28 11:50:17 +08:00
})
}
// 保存数据
2022-05-24 15:05:51 +08:00
let params = new FormData()
2022-05-26 01:01:59 +08:00
params.append('projectId', fetchRouteParamsLocation())
2022-05-24 15:05:51 +08:00
params.append('content', JSON.stringify(chartEditStore.getStorageInfo || {}))
2022-06-01 22:41:11 +08:00
const res= await saveProjectApi(params) as unknown as MyResponseType
2022-05-24 15:05:51 +08:00
if (res.code === ResultEnum.SUCCESS) {
// 成功状态
setTimeout(() => {
chartEditStore.setEditCanvas(EditCanvasTypeEnum.SAVE_STATUS, SyncEnum.SUCCESS)
}, 1000)
return
}
// 失败状态
chartEditStore.setEditCanvas(EditCanvasTypeEnum.SAVE_STATUS, SyncEnum.FAILURE)
2022-06-22 18:50:15 +08:00
}, 3000)
2022-05-27 20:09:48 +08:00
// * 定时处理
2022-05-24 17:42:49 +08:00
const intervalDataSyncUpdate = () => {
// 定时获取数据
const syncTiming = setInterval(() => {
dataSyncUpdate()
}, saveInterval * 1000)
2022-05-24 17:42:49 +08:00
// 销毁
onUnmounted(() => {
clearInterval(syncTiming)
})
}
return {
updateComponent,
2022-05-26 01:01:59 +08:00
updateStoreInfo,
dataSyncFetch,
2022-05-24 17:42:49 +08:00
dataSyncUpdate,
intervalDataSyncUpdate
}
}