-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutils.js
208 lines (192 loc) · 6.03 KB
/
utils.js
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
import Vue from 'vue'
export default function ({ store, $config }) {
function getConfig (key, returnType = 'value') {
if (!key) {
throw new Error('Please input key')
}
const configurationList = this.$store.getters.configurationList || []
const configurationTarget = configurationList.find((item) => {
return item.key === key || item.id === key
})
if (!configurationTarget) {
return `{{ ${key} }}`
}
return returnType === 'object' ? configurationTarget : configurationTarget.value || ''
}
function getOssUrl (url, needProxy = false) {
if (!url) {
return ''
}
const isWithDomain = url.includes('http://') || url.includes('https://')
const is3rdResource = !url.includes($config.serverHost)
const resourceDomain = needProxy ? '/oss' : $config.ossBaseurl
return isWithDomain
? (is3rdResource ? url : `${resourceDomain}/${url.replace(/^http(s)?:\/\/(.*?)\//, '')}`)
: `${resourceDomain}/${url}`
}
function getStringCount (string) {
if (!string) {
return 0
}
try {
const hasMany = string.includes(',')
if (hasMany) {
const tagArray = string.split(',')
return tagArray.length
}
return 1
} catch (e) {
return 1
}
}
function getImageAsset (key, returnType = 'value') {
if (!key) {
return '[ key is null ]'
}
const imageAssetList = this.$store.getters.imageAssetList || []
const imageAssetTarget = imageAssetList.find((item) => {
return item.key === key || item.id === key
})
if (!imageAssetTarget) {
return `[ imageAsset ${key} not found ]`
}
return returnType === 'object' ? imageAssetTarget : this.$getOssUrl(imageAssetTarget.url) || ''
}
function getFileAsset (key, returnType = 'value') {
if (!key) {
return '[ key is null ]'
}
const fileAssetList = this.$store.getters.fileAssetList || []
const fileAssetTarget = fileAssetList.find((item) => {
return item.key === key || item.id === key
})
if (!fileAssetTarget) {
return `[ fileAsset ${key} not found ]`
}
return returnType === 'object' ? fileAssetTarget : this.$getOssUrl(fileAssetTarget.url) || ''
}
function getSeoInfo (type, value) {
const seoConfig = {
description: {
value: `${value} - ${$config.siteDescription}`
},
keywords: {
value: `${value} - ${$config.siteKeywords}`
}
}
return seoConfig[type].value || ''
}
function getModuleConfig (module) {
const mainConfigList = store.getters.moduleList || []
const configList = {
timeline: {
name: '时间轴', dateField: 'date'
},
day: {
name: '日迹', dateField: 'date'
},
blog: {
name: '博文', dateField: 'datetime'
},
movie: {
name: '观影', dateField: 'datetime'
},
project: {
name: '项目', dateField: 'datetime_start'
},
plan: {
name: '计划', dateField: 'datetime_start'
},
idea: {
name: '想法', dateField: 'datetime'
},
mailbox: {
name: '邮局', dateField: 'datetime'
},
word: {
name: '话语', dateField: 'date'
},
monument: {
name: '纪念碑', dateField: 'date'
}
}
if (mainConfigList && mainConfigList.length > 0) {
mainConfigList.forEach((item) => {
if (configList[item.url]) {
configList[item.url] = { ...configList[item.url], ...item }
}
})
}
return module ? configList[module] : configList
}
function checkFormValidate (validateList = {}) {
const validateValueList = Object.values(validateList) || {}
const falseItem = validateValueList.find(item => item === false)
return !!(falseItem || falseItem === undefined)
}
function fillStateByLocalStorage (actionKey, cacheKey, valueType, defaultValue) {
if (process.client) {
const localCache = localStorage.getItem(`ShareManBox~${cacheKey}`)
let finalValue = null
if (!localCache) {
finalValue = defaultValue
}
if (valueType === 'number') {
finalValue = Number.parseInt(localCache) || defaultValue
}
if (valueType === 'object') {
finalValue = JSON.parse(localCache) || defaultValue
}
if (valueType === 'string') {
finalValue = JSON.parse(localCache) || defaultValue
}
this.$store.commit(actionKey, finalValue)
}
}
function constructTree (originData, idFieldName = 'id', parentIdFieldName = 'parent_id', uselessFieldNameArray = []) {
const data = JSON.parse(JSON.stringify(originData))
data.forEach(function (item) {
// delete useless field
['children', ...uselessFieldNameArray].forEach((fieldName) => {
item[fieldName] && (delete item[fieldName])
})
})
const itemMapFromId = {}
data.forEach(function (item) {
itemMapFromId[item[idFieldName]] = item
})
const dataTree = []
data.forEach((item) => {
const parent = itemMapFromId[item[parentIdFieldName]]
parent ? ((parent.children || (parent.children = []))).push(item) : dataTree.push(item)
})
return dataTree
}
function getFilteredTagArray (tagsText) {
const [fuzzyTags, hiddenTags] = [
[],
[]
]
return tagsText
.trim()
.split(',')
.map((tag) => {
if (fuzzyTags.includes(tag)) {
return tag[0].padEnd(tag.length, '*')
}
return tag
})
.filter(tag => (tag !== '') && !hiddenTags.includes(tag))
}
Vue.prototype.$getConfig = getConfig
Vue.prototype.$getFileAsset = getFileAsset
Vue.prototype.$getImageAsset = getImageAsset
Vue.prototype.$getOssUrl = getOssUrl
Vue.prototype.$getStringCount = getStringCount
Vue.prototype.$getSeoInfo = getSeoInfo
Vue.prototype.$getModuleConfig = getModuleConfig
Vue.prototype.$checkFormValidate = checkFormValidate
Vue.prototype.$fillStateByLocalStorage = fillStateByLocalStorage
Vue.prototype.$constructTree = constructTree
Vue.prototype.$getFilteredTagArray = getFilteredTagArray
}