yapi/client/common.js

239 lines
5.0 KiB
JavaScript
Raw Normal View History

2018-07-09 16:19:54 +08:00
const moment = require('moment');
const constants = require('./constants/variable');
const Mock = require('mockjs');
const json5 = require('json5');
const MockExtra = require('common/mock-extra.js');
2017-09-17 13:36:51 +08:00
const Roles = {
2018-07-09 16:19:54 +08:00
0: 'admin',
2017-09-17 13:36:51 +08:00
10: 'owner',
20: 'dev',
30: 'guest',
40: 'member'
2018-07-09 16:19:54 +08:00
};
2017-09-17 13:36:51 +08:00
const roleAction = {
2018-07-09 16:19:54 +08:00
manageUserlist: 'admin',
changeMemberRole: 'owner',
editInterface: 'dev',
viewPrivateInterface: 'guest',
viewGroup: 'guest'
};
2017-09-17 13:36:51 +08:00
2018-07-09 16:19:54 +08:00
function isJson(json) {
if (!json) {
return false;
}
try {
json = JSON.parse(json);
2017-10-18 17:46:37 +08:00
return json;
2018-07-09 16:19:54 +08:00
} catch (e) {
2017-10-18 17:46:37 +08:00
return false;
}
}
2017-11-27 13:56:10 +08:00
exports.isJson = isJson;
2018-07-09 16:19:54 +08:00
function isJson5(json) {
if (!json) {
return false;
}
try {
json = json5.parse(json);
return json;
2018-07-09 16:19:54 +08:00
} catch (e) {
return false;
}
2017-12-12 20:52:00 +08:00
}
2018-08-16 17:25:22 +08:00
exports.safeArray = function(arr) {
return Array.isArray(arr) ? arr : [];
};
2018-07-09 16:19:54 +08:00
exports.json5_parse = function(json) {
try {
2017-12-12 20:52:00 +08:00
return json5.parse(json);
2018-07-09 16:19:54 +08:00
} catch (err) {
2017-12-12 20:52:00 +08:00
return json;
}
2018-07-09 16:19:54 +08:00
};
2017-12-18 17:49:23 +08:00
2018-07-09 16:19:54 +08:00
exports.json_parse = function(json) {
try {
2017-12-18 17:49:23 +08:00
return JSON.parse(json);
2018-07-09 16:19:54 +08:00
} catch (err) {
2017-12-18 17:49:23 +08:00
return json;
}
2018-07-09 16:19:54 +08:00
};
2018-07-09 16:19:54 +08:00
function deepCopyJson(json) {
return JSON.parse(JSON.stringify(json));
}
exports.deepCopyJson = deepCopyJson;
exports.isJson5 = isJson5;
2018-07-09 16:19:54 +08:00
exports.checkAuth = (action, role) => {
2017-09-17 13:36:51 +08:00
return Roles[roleAction[action]] <= Roles[role];
2018-07-09 16:19:54 +08:00
};
2017-07-18 17:15:29 +08:00
2018-07-09 16:19:54 +08:00
exports.formatTime = timestamp => {
return moment.unix(timestamp).format('YYYY-MM-DD HH:mm:ss');
};
2017-08-20 18:56:31 +08:00
2017-08-23 15:25:35 +08:00
// 防抖函数,减少高频触发的函数执行的频率
// 请在 constructor 里使用:
// import { debounce } from '$/common';
// this.func = debounce(this.func, 400);
2017-08-22 20:44:16 +08:00
exports.debounce = (func, wait) => {
let timeout;
2018-07-09 16:19:54 +08:00
return function() {
2017-08-22 20:44:16 +08:00
clearTimeout(timeout);
timeout = setTimeout(func, wait);
};
};
2017-08-23 15:25:35 +08:00
// 从 Javascript 对象中选取随机属性
2018-07-09 16:19:54 +08:00
exports.pickRandomProperty = obj => {
2017-08-23 15:25:35 +08:00
let result;
let count = 0;
2018-07-09 16:19:54 +08:00
for (let prop in obj) {
2018-07-09 17:51:15 +08:00
if (Math.random() < 1 / ++count) {
2018-07-09 16:19:54 +08:00
result = prop;
}
}
2017-08-23 15:25:35 +08:00
return result;
2018-07-09 16:19:54 +08:00
};
2017-08-24 17:12:55 +08:00
exports.getImgPath = (path, type) => {
let rate = window.devicePixelRatio >= 2 ? 2 : 1;
return `${path}@${rate}x.${type}`;
2018-07-09 16:19:54 +08:00
};
2017-08-25 11:18:29 +08:00
function trim(str) {
if (!str) {
return str;
}
str = str + '';
return str.replace(/(^\s*)|(\s*$)/g, '');
}
exports.trim = trim;
2018-07-09 16:19:54 +08:00
exports.handlePath = path => {
2017-08-25 11:18:29 +08:00
path = trim(path);
2018-07-09 16:19:54 +08:00
if (!path) {
return path;
}
if (path === '/') {
return '';
}
2017-08-25 11:18:29 +08:00
path = path[0] !== '/' ? '/' + path : path;
path = path[path.length - 1] === '/' ? path.substr(0, path.length - 1) : path;
return path;
2018-07-09 16:19:54 +08:00
};
2018-07-09 16:19:54 +08:00
exports.handleApiPath = path => {
if (!path) {
return '';
}
path = trim(path);
path = path[0] !== '/' ? '/' + path : path;
return path;
2018-07-09 16:19:54 +08:00
};
2017-09-08 11:18:30 +08:00
// 名称限制 constants.NAME_LIMIT 字符
2018-07-09 16:19:54 +08:00
exports.nameLengthLimit = type => {
2017-09-07 21:52:26 +08:00
// 返回字符串长度汉字计数为2
2018-07-09 16:19:54 +08:00
const strLength = str => {
2017-09-07 21:52:26 +08:00
let length = 0;
for (let i = 0; i < str.length; i++) {
2018-07-09 16:19:54 +08:00
str.charCodeAt(i) > 255 ? (length += 2) : length++;
2017-09-07 21:52:26 +08:00
}
return length;
2018-07-09 16:19:54 +08:00
};
2017-09-07 21:52:26 +08:00
// 返回 form中的 rules 校验规则
2018-07-09 16:19:54 +08:00
return [
{
required: true,
validator(rule, value, callback) {
const len = value ? strLength(value) : 0;
if (len > constants.NAME_LIMIT) {
callback(
'请输入' + type + '名称,长度不超过' + constants.NAME_LIMIT + '字符(中文算作2字符)!'
);
} else if (len === 0) {
callback(
'请输入' + type + '名称,长度不超过' + constants.NAME_LIMIT + '字符(中文算作2字符)!'
);
} else {
return callback();
}
2017-09-07 21:52:26 +08:00
}
}
2018-07-09 16:19:54 +08:00
];
};
2017-10-12 18:36:59 +08:00
2018-09-03 14:19:14 +08:00
// 去除所有html标签只保留文字
exports.htmlFilter = html => {
let reg = /<\/?.+?\/?>/g;
return html.replace(reg, '') || '新项目';
};
2017-10-12 18:36:59 +08:00
// 实现 Object.entries() 方法
2018-07-09 16:19:54 +08:00
exports.entries = obj => {
2017-10-12 18:36:59 +08:00
let res = [];
2018-07-09 16:19:54 +08:00
for (let key in obj) {
2017-10-12 18:36:59 +08:00
res.push([key, obj[key]]);
}
return res;
2018-07-09 16:19:54 +08:00
};
2018-07-09 16:19:54 +08:00
exports.getMockText = mockTpl => {
try {
return JSON.stringify(Mock.mock(MockExtra(json5.parse(mockTpl), {})), null, ' ');
} catch (err) {
return '';
}
2018-07-09 16:19:54 +08:00
};
2017-10-20 16:00:30 +08:00
/**
* 合并后新的对象属性与 Obj 一致nextObj 有对应属性则取 nextObj 属性值否则取 Obj 属性值
* @param {Object} Obj 旧对象
* @param {Object} nextObj 新对象
* @return {Object} 合并后的对象
*/
exports.safeAssign = (Obj, nextObj) => {
let keys = Object.keys(nextObj);
return Object.keys(Obj).reduce((result, value) => {
2018-07-09 16:19:54 +08:00
if (keys.indexOf(value) >= 0) {
result[value] = nextObj[value];
} else {
result[value] = Obj[value];
}
return result;
2017-10-20 16:00:30 +08:00
}, {});
};
// 交换数组的位置
exports.arrayChangeIndex = (arr, start, end) => {
let newArr = [].concat(arr);
// newArr[start] = arr[end];
// newArr[end] = arr[start];
let startItem = newArr[start];
newArr.splice(start, 1);
// end自动加1
2018-07-09 16:19:54 +08:00
newArr.splice(end, 0, startItem);
let changes = [];
newArr.forEach((item, index) => {
changes.push({
id: item._id,
index: index
2018-07-09 16:19:54 +08:00
});
});
return changes;
2018-07-09 16:19:54 +08:00
};