-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.js
More file actions
544 lines (476 loc) · 13.7 KB
/
utils.js
File metadata and controls
544 lines (476 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import { readonly, reactive, ref } from 'vue'
import { Loading } from '@opentiny/vue'
import { iconLoadingShadow } from '@opentiny/vue-icon'
import { isInternalEnv } from '@/utils/env'
import defaultImg from '../common/json/default'
import {
useModal,
isTenantAdmin,
isPlatformAdmin,
isAdmin,
isAppAdmin,
isAppDeveloper,
isMaster,
isGuest,
isNoTenant,
setHeadMenuList
} from '.'
export const LOCAL_STORAGE = readonly({
material: 'tiny_lowcode_material',
tenant: 'tiny_lowcode_tenant'
})
export const SESSION_STORAGE = readonly({
appSetting: 'tiny_lowcode_app_setting',
platformSetting: 'tiny_lowcode_platform_setting',
component: 'tiny_lowcode_component',
componentLib: 'tiny_lowcode_component_lib',
block: 'tiny_lowcode_block',
material: 'tiny_lowcode_material',
createPlatformType: 'tiny_lowcode_create_platform_type',
createMaterialType: 'tiny_lowcode_create_material_type',
lastMaterialData: 'tiny_lowcode_last_material_data',
lastComponentData: 'tiny_lowcode_last_component_data',
lastComponentLibData: 'tiny_lowcode_last_component_lib_data',
lastBlockData: 'tiny_lowcode_last_block_data',
categories: 'tiny_lowcode_business_categories'
})
export const ROLE = readonly({
admin: 'Tinybuilder_Admin',
tenantAdmin: 'Tinybuilder_Tenant_Admin',
platformAdmin: 'Tinybuilder_Platform_Admin',
appAdmin: 'Tinybuilder_App_Admin',
appDeveloper: 'Tinybuilder_App_Developer',
guest: 'Guest'
})
export const PERMISSION_TYPE = readonly({
app: 'apps',
platform: 'platforms',
tenant: 'tenant'
})
export const ACTION_ID = readonly({
home: 'home',
platform: 'platform',
app: 'app',
appCenter: 'appCenter',
platformCenter: 'platformCenter',
material: 'material',
ecologyCenter: 'ecologyCenter',
protocol: 'protocol',
helpCenter: 'helpCenter',
monitorCenter: 'monitorCenter',
view: 'view',
develop: 'develop',
delete: 'delete',
setting: 'setting',
edit: 'edit',
check: 'check',
add: 'add',
create: 'create',
createFromTemplate: 'createFromTemplate',
collect: 'collect',
checkHistory: 'checkHistory'
})
export const PERMISSION_ID = {
personalCenter: 'personalCenter',
permission: 'permissionList'
}
export const sizeChange = (state, size) => {
state.pageSize = size
const data = state.searchData.slice((state.currentPage - 1) * state.pageSize, state.currentPage * state.pageSize)
state.activeName ? (state.data[state.activeName] = data) : (state.data = data)
}
export const currentChange = (state, current) => {
state.currentPage = current
const data = state.searchData.slice((state.currentPage - 1) * state.pageSize, state.currentPage * state.pageSize)
state.activeName ? (state.data[state.activeName] = data) : (state.data = data)
}
export const setBuildingMessage = ({ buildMessage, id }) => {
const msg = buildMessage[id] || {}
msg.color = 'grey'
msg.content = '构建中...'
buildMessage[id] = msg
}
export const setBuildErrorMessage = ({ buildMessage, id, error }) => {
const msg = buildMessage[id] || {}
msg.color = 'red'
msg.content = '构建失败'
buildMessage[id] = msg
}
export const setBuildPercent = ({ buildPercent, id, percent }) => {
buildPercent[id] = percent
}
const proUrl = 'https://opentiny.design'
export const EXPERIENCE_URL = !isInternalEnv()
? `${import.meta.env.VITE_APP_STATIC_PATH}/tiny-engine-editor/index.html`
: `${import.meta.env.VITE_APP_ORIGIN}/platform-center/entry/portal-platform?id=918&tenant=1` // 构建体验流程的URL
export const EXPERIENCE_URL_FLOW = `${import.meta.env.VITE_APP_ORIGIN}/platform-center/entry${
import.meta.env.VITE_APP_ORIGIN === proUrl
? '/RF-Designer?type=app&id=1284&tenant=1'
: '/RF-Designer?type=app&id=1418&tenant=1'
}`
export const EXPERIENCE_URL_CONSOLE = `${import.meta.env.VITE_APP_ORIGIN}/platform-center/entry${
import.meta.env.VITE_APP_ORIGIN === proUrl
? '/console-designer?type=app&id=1305&tenant=1'
: '/console-designer?type=app&id=1520&tenant=1'
}`
export const EXPERIENCE_URL_BDVIEW = `${import.meta.env.VITE_APP_ORIGIN}/platform-center/entry${
import.meta.env.VITE_APP_ORIGIN === proUrl
? '/%E5%A4%A7%E5%B1%8F%E4%B8%93%E7%94%A8%E8%AE%BE%E8%AE%A1%E5%99%A8?type=app&id=1274&tenant=1&pageid=2883'
: '/%E5%A4%A7%E5%B1%8F%E4%B8%93%E7%94%A8%E8%AE%BE%E8%AE%A1%E5%99%A8?type=app&id=1514&tenant=1&pageid=3406'
}`
export const TINY_ENGINE_DEFAULT_URL = `${import.meta.env.BASE_URL}tiny-engine-editor/index.html?type=app&id=1&tenant=1`
export const TINY_ENGINE_DESIGNER_URL = `https://res-static.opentiny.design/tiny-engine-designer/latest/index.html`
export const openLowCodeEditor = () => {
window.open(EXPERIENCE_URL, '_blank')
}
let loadingInstance = null
const spinnerIcon = iconLoadingShadow()
export const openLoading = (target = document.getElementById('sideNavLayout'), text = '加载中...') => {
if (loadingInstance && target?.compareDocumentPosition(loadingInstance.state.target) === 0) {
loadingInstance?.close()
}
loadingInstance = Loading.service({
text,
target,
spinner: spinnerIcon,
customClass: 'common-loading',
background: 'rgba(0, 0, 0, 0.3)'
})
return loadingInstance
}
export const framework = [
{
value: 'Vue',
label: 'Vue',
disabled: false
},
{
value: 'Angular',
label: 'Angular',
disabled: false
},
{
value: 'React',
label: 'React',
disabled: true
},
{
value: 'HTML',
label: 'HTML',
disabled: true
}
]
export const sortOptions = [
{
value: 'updateReverse',
label: '按修改时间倒序'
},
{
value: 'createReverse',
label: '按创建时间倒序'
}
]
export const appTemplateTypeList = [
{
label: '云服务开发',
value: 'serviceDevelop'
},
{
label: '人事管理',
value: 'personnelManagement'
},
{
label: '行政办公',
value: 'AdministrativeOffice'
},
{
label: '项目管理',
value: 'projectManagement'
}
]
export const formValidate = (type) => {
const rules = {
nameZh: {
reg: /^[-\w\u4e00-\u9fa5]+$/,
message: '名称不能包含特殊字符'
},
nameEn: {
reg: /(?=.*[A-Z].*[A-Z])(^([A-Z][a-z0-9._-]{2,})*?([A-Z][a-z0-9._-])*?)$/,
message: '两个单词以上, 且是大写开头驼峰格式'
},
nameId: {
reg: /^[a-zA-Z_]([a-zA-Z0-9._-]+)?$/,
message: '以字母、下划线开头,允许数字、字母、点、下划线与中划线'
},
componentId: {
reg: /(\b([A-Z][a-z]+){2,}\b)+$/,
message: '两个单词以上, 且是大写开头驼峰格式,例:TinyButton'
},
version: {
reg: /^(\d+\.){2}[\w-\.]+$/,
message: '版本号应遵循 SemVer 规范(例:1.0.0)'
},
npm: {
reg: /^(([a-z\d][a-z\d_-]*)|(@[a-z\d_-]+\/[a-z\d_-]+))$/,
message: 'npm包名不规范'
},
https: {
reg: /^(https):\/\/([\w.]+\/?)\S*/,
message: '请输入https开头的地址'
},
attr: {
reg: /^[a-zA-Z-_]+$/,
message: '只能包含字母、中划线'
},
event: {
reg: /^[a-zA-Z-:]+$/,
message: '只能包含字母、中划线、冒号'
},
material: {
reg: /^([a-z][a-z0-9]*)(-?[a-z0-9]+)*$/,
message: '以小写字母开头,包含小写字母、数字、中划线(例:a-c)'
},
url: {
reg: /^https?:\/\/[\w-]\/?$/,
message: '输入不符合要求,请重新输入'
},
repositoryUrl: {
reg: /^https?:\/\/(.*?)\.git$/,
message: '请输入http(s)类型git地址'
}
}
if (rules[type]) {
return (rule, value, callback) => {
const { reg, message } = rules[type]
if (value && !reg.test(value)) {
callback(new Error(message))
} else {
callback()
}
}
}
return undefined
}
export const validatePass = (rule, value, callback) => {
if (!/^[-\w\u4e00-\u9fa5]+$/.test(value)) {
callback(new Error('名称不能包含特殊字符'))
} else {
callback()
}
}
export const getDefaultImg = (data, type) => {
if (!type) {
return data
}
const len = defaultImg[type].length
const newData = data.map((item, idx) => {
if (!item.image_url) {
if (idx < len) {
item.image_url = defaultImg[type][idx]
} else {
item.image_url = defaultImg[type][idx % len]
}
}
return item
})
return newData
}
export const getSortParams = (value) => {
const sortParams = {
name: 'name:ASC',
nameReverse: 'name:DESC',
update: 'updated_at:ASC',
updateReverse: 'updated_at:DESC',
create: 'created_at:ASC',
createReverse: 'created_at:DESC'
}
const _sort = sortParams[value]
return _sort ? { _sort } : {}
}
const getReqParams = (state, config, getExtParams) => {
const { pageSize, currentPage } = state
const params = {
_limit: pageSize,
_start: (currentPage - 1) * pageSize,
...getSortParams(state.sort),
...getExtParams?.()
}
if (state.filter) {
Object.assign(params, { name_contains: state.filter })
}
return params
}
const fetchState = {
data: [],
currentPage: 1,
pageSize: 12,
pageSizes: [12, 24, 36],
filter: '',
total: 100,
sort: 'updateReverse',
stack: 'Vue',
empty: false
}
const doFetchCount = ({ params, reqest, state }) => {
delete params._limit
delete params._start
delete params._sort
reqest.count?.(params).then((count) => {
state.total = count
})
}
const showLoading = (loading) => {
loading.value = openLoading(document.getElementsByClassName('page-layout')[1])
}
const closeLoading = (loading) => {
loading.value?.close()
loading.value = null
}
const updateMethod = (state, reqestMethods, config) => {
const { request, getCount } = config
if (typeof request === 'function') {
reqestMethods.list = request
state.currentPage = config.oldPage ? state.currentPage : 1
state.filter = ''
state.sort = 'updateReverse'
}
if (typeof getCount === 'function') {
reqestMethods.count = getCount
}
}
export const useFetchData = (config) => {
const { request, routeName, errorMsg, getExtParams, getCount, getMoreAction } = config
const loading = ref(null)
const reqestMethods = {
list: request,
count: getCount
}
const state = reactive({ ...fetchState })
const resetPage = () => {
state.currentPage = 1
state.filter = ''
state.sort = 'updateReverse'
loading.value = null
}
const debounce = (func, delay) => {
let timer = null
return function (...args) {
clearTimeout(timer)
timer = setTimeout(() => {
func.apply(this, args)
}, delay)
}
}
const doFetch = debounce((config = {}) => {
updateMethod(state, reqestMethods, config)
const requestParams = getReqParams(state, config, getExtParams)
showLoading(loading)
reqestMethods
.list(requestParams)
.then((data) => {
closeLoading(loading)
state.data = getDefaultImg(data, routeName)
state.data = getMoreAction && typeof getMoreAction === 'function' ? getMoreAction(state.data) : state.data
state.empty = !data.length
})
.catch((error) => {
closeLoading(loading)
useModal().message({ message: `${config.errorMsg || errorMsg}: ${error.message || error}`, status: 'error' })
})
doFetchCount({ params: requestParams, state, reqest: reqestMethods })
}, 100)
const currentChange = (val) => {
state.currentPage = val
doFetch()
}
const pageSizeChange = (val) => {
state.currentPage = 1
state.pageSize = val
doFetch()
}
return {
fetchState: state,
currentChange,
pageSizeChange,
getParams: getReqParams,
isLoading: loading,
doFetch,
resetPage
}
}
export const refreshHeadMenuList = () => {
const defaultHeadMenuList = [
{
id: ACTION_ID.app,
title: '我的应用',
link: '/my-application'
},
{
id: ACTION_ID.appCenter,
title: '应用中心',
link: '/application-center'
},
{
id: ACTION_ID.platform,
title: '我的设计器',
link: '/my-platform'
},
{
id: ACTION_ID.ecologyCenter,
title: '生态中心',
link: '/ecosystem'
},
{
id: ACTION_ID.protocol,
title: '协议规范',
link: '/protocol'
},
{
id: ACTION_ID.helpCenter,
title: '帮助中心',
link: '/help-center'
}
]
const roleBlackList = {
// 未选组织、游客,看不到我的设计器、我的应用、我的物料、监控中心
noTenant: [ACTION_ID.platform, ACTION_ID.app, ACTION_ID.monitorCenter],
// 超级管理员,看不到我的设计器、我的应用
admin: [ACTION_ID.platform, ACTION_ID.app],
// 组织管理员、设计器管理员,看不到监控中心
tenantAdmin: [ACTION_ID.monitorCenter],
// 应用管理员、应用开发人员,看不到我的设计器、监控中心
appAdmin: [ACTION_ID.platform, ACTION_ID.monitorCenter],
// 开发专用,什么都能看到
master: []
}
const map = new Map([
[isNoTenant, roleBlackList.noTenant],
[isAdmin, roleBlackList.admin],
[isTenantAdmin, roleBlackList.tenantAdmin],
[isPlatformAdmin, roleBlackList.tenantAdmin],
[isAppAdmin, roleBlackList.appAdmin],
[isAppDeveloper, roleBlackList.appAdmin],
[isMaster, roleBlackList.master],
[isGuest, roleBlackList.noTenant]
])
// 各角色的黑名单求交集
let blackList = defaultHeadMenuList
for (let [key, value] of map) {
if (key()) {
blackList = blackList.filter((item) => value.includes(item.id))
}
}
const headMenuList = defaultHeadMenuList.filter((item) => !blackList.includes(item))
setHeadMenuList(headMenuList)
}
export const TIMELINE_TYPES = {
COMPONENTS: 'user_components',
COMPONENT_LIB: 'componentLibrary',
BLOCKS: 'blocks',
MATERIAL: 'material_history',
THEME: 'theme',
PLUGINS: 'plugins',
TOOLBAR: 'toolbar',
DSL: 'dsl',
APP_EXTEND: 'app_extend'
}