|
| 1 | +// origin resource state code too mess up. |
| 2 | + |
| 3 | +import { reactive, ref } from 'vue' |
| 4 | +import { fetchResourceList, requestAddReSource, requestUpdateReSource, requestDeleteReSource } from '../http' |
| 5 | +import { defineService, getMetaApi, META_SERVICE, useResource } from '@opentiny/tiny-engine-meta-register' |
| 6 | +import { RESOURCE_CATEGORY } from './constants' |
| 7 | + |
| 8 | +export interface INpmUtilItem { |
| 9 | + category: 'utils' |
| 10 | + content: { |
| 11 | + destructuring: boolean |
| 12 | + exportName: string |
| 13 | + main: string |
| 14 | + package: string |
| 15 | + version: string |
| 16 | + } |
| 17 | + id: number |
| 18 | + name: string |
| 19 | + type: 'npm' |
| 20 | + app: number |
| 21 | +} |
| 22 | + |
| 23 | +export interface IFunctionUtilItem { |
| 24 | + category: 'utils' |
| 25 | + content: { |
| 26 | + type: 'JSFunction' |
| 27 | + value: string |
| 28 | + } |
| 29 | + id: number |
| 30 | + name: string |
| 31 | + type: 'function' |
| 32 | + app: number |
| 33 | +} |
| 34 | + |
| 35 | +export type IUtilItem = INpmUtilItem | IFunctionUtilItem |
| 36 | + |
| 37 | +const state = reactive<{ |
| 38 | + utils: IUtilItem[] |
| 39 | +}>({ |
| 40 | + utils: [] |
| 41 | +}) |
| 42 | + |
| 43 | +// 临时用来通知 BridgeManage 刷新列表 |
| 44 | +const updateCount = ref(0) |
| 45 | +const lastOperation = ref<{ id: number | null; type: 'add' | 'update' | 'delete' }>({ id: null, type: 'add' }) |
| 46 | + |
| 47 | +const syncUtilsItemToAppSchemaState = (data: IUtilItem) => { |
| 48 | + const index = useResource().appSchemaState[RESOURCE_CATEGORY.Util].findIndex((item) => item.name === data.name) |
| 49 | + const { content, name, type } = data |
| 50 | + |
| 51 | + if (index === -1) { |
| 52 | + useResource().appSchemaState[RESOURCE_CATEGORY.Util].push({ content, name, type }) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + useResource().appSchemaState[RESOURCE_CATEGORY.Util][index] = { content, name, type } |
| 57 | +} |
| 58 | + |
| 59 | +const refreshUtils = async () => { |
| 60 | + const result = (await fetchResourceList( |
| 61 | + getMetaApi(META_SERVICE.GlobalService).getBaseInfo().id, |
| 62 | + RESOURCE_CATEGORY.Util |
| 63 | + )) as IUtilItem[] |
| 64 | + |
| 65 | + state.utils = result.map(({ category, content, id, name, type, app }) => { |
| 66 | + return { |
| 67 | + category, |
| 68 | + content, |
| 69 | + id, |
| 70 | + name, |
| 71 | + type, |
| 72 | + app |
| 73 | + } as IUtilItem |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +const getUtilById = (id: number) => { |
| 78 | + return state.utils.find((item) => item.id === id) |
| 79 | +} |
| 80 | + |
| 81 | +const getUtilByName = (name: string) => { |
| 82 | + return state.utils.find((item) => item.name === name) |
| 83 | +} |
| 84 | + |
| 85 | +const addUtils = async (data: Omit<IUtilItem, 'id'>) => { |
| 86 | + const result = await requestAddReSource(data) |
| 87 | + |
| 88 | + if (!result) { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + lastOperation.value = { id: null, type: 'add' } |
| 93 | + updateCount.value++ |
| 94 | + |
| 95 | + syncUtilsItemToAppSchemaState(result) |
| 96 | + await refreshUtils() |
| 97 | + |
| 98 | + return result |
| 99 | +} |
| 100 | + |
| 101 | +const updateUtils = async (data: IUtilItem) => { |
| 102 | + const result = await requestUpdateReSource(data) |
| 103 | + |
| 104 | + if (!result) { |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + lastOperation.value = { id: result.id, type: 'update' } |
| 109 | + updateCount.value++ |
| 110 | + |
| 111 | + syncUtilsItemToAppSchemaState(result) |
| 112 | + await refreshUtils() |
| 113 | + |
| 114 | + return result |
| 115 | +} |
| 116 | + |
| 117 | +const deleteUtils = async (id: number) => { |
| 118 | + const params = `app=${getMetaApi(META_SERVICE.GlobalService).getBaseInfo().id}&id=${id}` |
| 119 | + const result = await requestDeleteReSource(params) |
| 120 | + |
| 121 | + if (!result) { |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + lastOperation.value = { id, type: 'delete' } |
| 126 | + updateCount.value++ |
| 127 | + |
| 128 | + const utilItem = getUtilById(id) |
| 129 | + const index = useResource().appSchemaState[RESOURCE_CATEGORY.Util].findIndex((item) => item.name === utilItem?.name) |
| 130 | + |
| 131 | + if (index !== -1) { |
| 132 | + useResource().appSchemaState[RESOURCE_CATEGORY.Util].splice(index, 1) |
| 133 | + } |
| 134 | + |
| 135 | + await refreshUtils() |
| 136 | + |
| 137 | + return result |
| 138 | +} |
| 139 | + |
| 140 | +const getUtils = () => state.utils |
| 141 | +const setUtils = (utils: IUtilItem[]) => { |
| 142 | + state.utils = utils |
| 143 | +} |
| 144 | + |
| 145 | +export default defineService({ |
| 146 | + id: META_SERVICE.UseUtils, |
| 147 | + type: 'MetaService', |
| 148 | + initialState: { |
| 149 | + utils: [] |
| 150 | + }, |
| 151 | + options: {}, |
| 152 | + init: () => { |
| 153 | + refreshUtils() |
| 154 | + }, |
| 155 | + apis: () => ({ |
| 156 | + getUtils: () => getUtils(), |
| 157 | + setUtils: (utils: IUtilItem[]) => setUtils(utils), |
| 158 | + addUtils: (data: Omit<IUtilItem, 'id'>) => addUtils(data), |
| 159 | + updateUtils: (data: IUtilItem) => updateUtils(data), |
| 160 | + deleteUtils: (id: number) => deleteUtils(id), |
| 161 | + getUtilById: (id: number) => getUtilById(id), |
| 162 | + refreshUtils: () => refreshUtils(), |
| 163 | + getUtilByName: (name: string) => getUtilByName(name), |
| 164 | + getUpdateCount: () => updateCount.value, |
| 165 | + getLastOperation: () => lastOperation.value |
| 166 | + }) |
| 167 | +}) |
0 commit comments