CI / build-and-test (push) Canceled after 0s
以 ckcloudManage a44810c 为基础,只保留资产页面(台账、单据、基础数据、同步任务)、 登录/错误/个人资料等公共页面,以及资产单据“查看流程”用到的流程详情页; 统一登录应用编码默认 CK_ASSET,标题“资产管理”,本地端口 9090。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
103 lines
3.1 KiB
TypeScript
103 lines
3.1 KiB
TypeScript
import type { App, Plugin } from 'vue'
|
|
import type { AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
|
import { createMemoryHistory, createRouter, type Router } from 'vue-router'
|
|
import VueDOMPurifyHTML from 'vue-dompurify-html'
|
|
import { setupAuth, setupMountedFocus } from '@/directives'
|
|
import { setupElementPlus } from '@/plugins/elementPlus'
|
|
import { setupGlobCom } from '@/components'
|
|
import { service } from '@/config/axios/service'
|
|
import { useUserStore } from '@/store/modules/user'
|
|
import { getAuthSession, setToken, withAuthSessionLock } from '@/utils/auth'
|
|
|
|
export interface RecordedRequest {
|
|
method: string
|
|
url: string
|
|
params?: unknown
|
|
data?: unknown
|
|
}
|
|
|
|
export type Responder = (request: RecordedRequest) => unknown
|
|
|
|
/** 分页接口返回空页,其余返回空数组;可按 URL 定制 */
|
|
export const defaultResponder: Responder = (request) =>
|
|
/page|list-page/i.test(request.url) ? { list: [], total: 0 } : []
|
|
|
|
/**
|
|
* 以 axios adapter 替换网络层:业务代码走真实的 request 封装与拦截器,
|
|
* 只把响应换成测试数据,并记录发出的请求。
|
|
*/
|
|
export const mockBackend = (responder: Responder = defaultResponder) => {
|
|
const requests: RecordedRequest[] = []
|
|
service.defaults.adapter = async (config: InternalAxiosRequestConfig) => {
|
|
const request: RecordedRequest = {
|
|
method: (config.method || 'get').toUpperCase(),
|
|
url: config.url || '',
|
|
params: config.params,
|
|
data: typeof config.data === 'string' ? safeJson(config.data) : config.data
|
|
}
|
|
requests.push(request)
|
|
return {
|
|
status: 200,
|
|
statusText: 'OK',
|
|
headers: {},
|
|
config,
|
|
request: { responseType: config.responseType },
|
|
data: { code: 200, data: responder(request), msg: '' }
|
|
} as AxiosResponse
|
|
}
|
|
return requests
|
|
}
|
|
|
|
const safeJson = (value: string) => {
|
|
try {
|
|
return JSON.parse(value)
|
|
} catch {
|
|
return value
|
|
}
|
|
}
|
|
|
|
/** 已登录、拥有全部权限的管理员会话 */
|
|
export const loginAsAdmin = async () => {
|
|
if (!getAuthSession().accessToken) {
|
|
await withAuthSessionLock(() =>
|
|
setToken(
|
|
{
|
|
id: 1,
|
|
accessToken: 'test-access',
|
|
refreshToken: 'test-refresh',
|
|
userId: 1,
|
|
userType: 2,
|
|
clientId: 'ckcloud',
|
|
expiresTime: 0
|
|
},
|
|
'test-session'
|
|
)
|
|
)
|
|
}
|
|
const userStore = useUserStore()
|
|
userStore.permissions = new Set(['*:*:*'])
|
|
userStore.roles = ['super_admin']
|
|
userStore.user = { id: 1, avatar: '', nickname: '管理员', deptId: 1 }
|
|
userStore.isSetUser = true
|
|
}
|
|
|
|
export const createTestRouter = (): Router =>
|
|
createRouter({
|
|
history: createMemoryHistory(),
|
|
routes: [{ path: '/:pathMatch(.*)*', name: 'any', component: { render: () => null } }]
|
|
})
|
|
|
|
/** 与 main.ts 一致的全局注册(不含 i18n / 打印等与渲染无关的插件) */
|
|
export const appPlugins = (router: Router = createTestRouter()): Plugin[] => [
|
|
router,
|
|
VueDOMPurifyHTML,
|
|
{
|
|
install(app: App) {
|
|
setupElementPlus(app)
|
|
setupGlobCom(app)
|
|
setupAuth(app)
|
|
setupMountedFocus(app)
|
|
}
|
|
}
|
|
]
|