v0.0.2
This commit is contained in:
63
src/tool/Common.js
Normal file
63
src/tool/Common.js
Normal file
@ -0,0 +1,63 @@
|
||||
import {
|
||||
SystemConfig
|
||||
} from '../config/main.js'
|
||||
|
||||
// 截取字符串,多余的部分用...代替
|
||||
export let setString = (str, len) => {
|
||||
let StrLen = 0
|
||||
let s = ''
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
if (str.charCodeAt(i) > 128) {
|
||||
StrLen += 2
|
||||
} else {
|
||||
StrLen++
|
||||
}
|
||||
s += str.charAt(i)
|
||||
if (StrLen >= len) {
|
||||
return s + '...'
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// 格式化设置
|
||||
export let OptionFormat = (GetOptions) => {
|
||||
let options = '{'
|
||||
for (let n = 0; n < GetOptions.length; n++) {
|
||||
options = options + '\'' + GetOptions[n].option_name + '\':\'' + GetOptions[n].option_value + '\''
|
||||
if (n < GetOptions.length - 1) {
|
||||
options = options + ','
|
||||
}
|
||||
}
|
||||
return JSON.parse(options + '}')
|
||||
}
|
||||
|
||||
// 替换SQL字符串中的前缀
|
||||
export let SqlFormat = (str) => {
|
||||
if (SystemConfig.mysql_prefix !== 'bm_') {
|
||||
str = str.replace(/bm_/g, SystemConfig.mysql_prefix)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// 数组去重
|
||||
export let HovercUnique = (arr) => {
|
||||
let n = {}
|
||||
let r = []
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
if (!n[arr[i]]) {
|
||||
n[arr[i]] = true
|
||||
r.push(arr[i])
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// 获取json长度
|
||||
export let getJsonLength = (jsonData) => {
|
||||
var arr = []
|
||||
for (var item in jsonData) {
|
||||
arr.push(jsonData[item])
|
||||
}
|
||||
return arr.length
|
||||
}
|
34
src/tool/PluginLoader.js
Normal file
34
src/tool/PluginLoader.js
Normal file
@ -0,0 +1,34 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import compose from 'koa-compose'
|
||||
|
||||
function getDirs (srcpath) {
|
||||
return fs.readdirSync(srcpath).filter(file => {
|
||||
return fs.statSync(path.join(srcpath, file)).isDirectory()
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = (srcpath, filename = 'index.js') => {
|
||||
let plugins = {}
|
||||
|
||||
let dirs = getDirs(srcpath)
|
||||
let list = []
|
||||
|
||||
for (let name of dirs) {
|
||||
let fn = require(path.join(srcpath, name, filename))
|
||||
|
||||
if (typeof fn !== 'function' && typeof fn.default === 'function') {
|
||||
fn = fn.default
|
||||
} else {
|
||||
throw (new Error('plugin must be a function!'))
|
||||
}
|
||||
|
||||
plugins[name] = fn
|
||||
|
||||
list.push(function (ctx, next) {
|
||||
return fn(ctx, next) || next()
|
||||
})
|
||||
}
|
||||
|
||||
return compose(list)
|
||||
}
|
Reference in New Issue
Block a user