This commit is contained in:
fan
2019-10-24 19:48:49 +08:00
parent f4bda4688c
commit 387ba9640c
55 changed files with 4784 additions and 2756 deletions
-3
View File
@@ -1,3 +0,0 @@
export default function(a, b) {
return a.filter(x => b.includes(x));
}
+39
View File
@@ -0,0 +1,39 @@
export default class MemoryCache {
constructor() {
this.table = {};
}
get(key) {
return key ? this.table[key] : this.table;
}
set(key, val) {
this.table[key] = val;
}
// setOnly(key, val) {
// if (!this.has(key)) this.set(key, val);
// }
remove(key) {
if (key) {
delete this.table[key];
} else {
this.table = {};
}
}
has(key) {
return !!this.table[key];
}
}
// export default {
// data: {},
// get(name) {
// console.log(this.data);
// }
// };
// class MemoryCache {
// constructor() {
// super();
// }
// get($name) {
// console.log(1);
// }
// }
// export default MemoryCache;
+16
View File
@@ -0,0 +1,16 @@
export const EMIT_AVATAR_CLICK = "avatar-click";
export const DEFAULT_MENU_LASTMESSAGES = "lastMessages";
export const DEFAULT_MENU_CONTACTS = "contacts";
export const DEFAULT_MENUS = [DEFAULT_MENU_LASTMESSAGES, DEFAULT_MENU_CONTACTS];
/**
* 聊天消息类型
*/
export const MESSAGE_TYPE = ["voice", "file", "video", "image", "text"];
/**
* 聊天消息状态
*/
export const MESSAGE_STATUS = ["going", "succeed", "failed"];
export const CONTACT_TYPE = ["many", "single"];
+130
View File
@@ -0,0 +1,130 @@
import { MESSAGE_TYPE, MESSAGE_STATUS, CONTACT_TYPE } from "utils/constant";
import { error } from "utils";
import { isPlainObject } from "utils/validate";
const constraintContactBasic = data =>
constraintObject(data, {
id: true,
displayName: true,
avatar: true,
type: {
required: true,
has: CONTACT_TYPE
}
});
const constraintMessageBasic = data =>
constraintObject(data, {
content: true,
sendTime: true,
type: {
required: true,
has: MESSAGE_TYPE
}
});
// constraintContact({
// id: "123",
// displayName: "123asd",
// avatar: "123",
// type: "single",
// message: {
// unread: 0,
// sendTime: 12312312,
// content: "12312312",
// type: "image"
// }
// });
constraintContact({
id: "123",
displayName: "123asd",
avatar: "123",
type: "single",
unread: 0,
lastSendTime: "",
subText: "12312312"
// message: {
// unread: 0,
// sendTime: 12312312,
// content: "12312312",
// type: "image"
// }
});
// constraintRecentContact({
// fromContactId: 0,
// unread: 0,
// sendTime: 12312312,
// content: "12312312"
// });
constraintMessage({
id: "123",
status: "succeed",
type: "image",
sendTime: 12312312312,
content: "asdas",
fromContactId: "123",
fromUser: { id: "123", displayName: "123", avatar: "123", type: "single" }
});
export function constraintObject(data, options) {
if (!data || !isPlainObject(data)) {
error("argument must be an object");
}
Object.keys(options).forEach(k => {
const option = options[k];
const val = data[k];
if ((option === true || option.required === true) && val === undefined) {
error(`"${k}" cannot be "${val}" `);
} else if (option.has && !option.has.includes(val)) {
error(
`"${k}" cannot be "${val}",can only be the following data "${
option.has
}"`
);
}
});
return true;
}
// export function constraintRecentContact(data) {
// constraintContact(data);
// constraintMessageBasic(data.message);
// constraintObject(data, {
// unread: true
// });
// }
export function constraintContact(data) {
constraintContactBasic(data);
// constraintObject(data, {
// unread: true,
// lastSendTime: true,
// lastContent: true
// });
}
export function constraintMessage(data) {
constraintObject(data, {
status: {
required: true,
has: MESSAGE_STATUS
},
fromContactId: true
});
constraintMessageBasic(data);
constraintContactBasic(data.fromUser);
let options = {};
switch (data.type) {
case "file":
options = {
fileSize: true,
fileName: true
};
break;
case "text":
options = {
text: true
};
break;
}
constraintObject(data, options);
}
+128
View File
@@ -0,0 +1,128 @@
import { isPlainObject, isFunction } from "utils/validate";
/**
* 使用某个组件上的作用域插槽
* @param {VueComponent} inject
* @param {String} slotName
* @param {Node} defaultElement
* @param {Object} props
*/
export function useScopedSlot(slot, def, props) {
return slot ? slot(props) : def;
}
export function padZero(val) {
return val < 10 ? `0${val}` : val;
}
export function hoursTimeFormat(t) {
const date = new Date(t);
const nowDate = new Date();
const Y = t => {
return t.getFullYear();
};
const MD = t => {
return `${t.getMonth() + 1}-${t.getDate()}`;
};
const dateY = Y(date);
const nowDateY = Y(nowDate);
let format;
if (dateY !== nowDateY) {
format = "y年m月d日 h:i";
} else if (`${dateY}-${MD(date)}` === `${nowDateY}-${MD(nowDate)}`) {
format = "h:i";
} else {
format = "m月d日 h:i";
}
return timeFormat(t, format);
}
export function timeFormat(t, format) {
if (!format) format = "y-m-d h:i:s";
if (t) t = new Date(t);
else t = new Date();
const formatArr = [
t.getFullYear().toString(),
padZero((t.getMonth() + 1).toString()),
padZero(t.getDate().toString()),
padZero(t.getHours().toString()),
padZero(t.getMinutes().toString()),
padZero(t.getSeconds().toString())
];
const reg = "ymdhis";
for (let i = 0; i < formatArr.length; i++) {
format = format.replace(reg.charAt(i), formatArr[i]);
}
return format;
}
export function fastDone(event, callback) {
if (isFunction(event)) {
event(() => {
callback();
});
} else {
callback();
}
}
/**
* 获取数组相交的值组成新数组
* @param {Array} a
* @param {Array} b
*/
export function arrayIntersect(a, b) {
return a.filter(x => b.includes(x));
}
export function error(text) {
throw new Error(text);
}
export function cloneDeep(obj) {
const newobj = { ...obj };
for (const key in newobj) {
const val = newobj[key];
if (isPlainObject(val)) {
newobj[key] = cloneDeep(val);
}
}
return newobj;
}
export function mergeDeep(o1, o2) {
for (const key in o2) {
if (isPlainObject(o1[key])) {
o1[key] = mergeDeep(o1[key], o2[key]);
} else {
o1[key] = o2[key];
}
}
return o1;
}
export function toEmojiName(str) {
return str.replace(/<img emoji-name=\"([^\"]*?)\" [^>]*>/gi, "[!$1]");
}
export function formatByte(value) {
if (null == value || value == "") {
return "0 Bytes";
}
var unitArr = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
var index = 0;
var srcsize = parseFloat(value);
index = Math.floor(Math.log(srcsize) / Math.log(1024));
var size = srcsize / Math.pow(1024, index);
size = parseFloat(size.toFixed(2));
return size + unitArr[index];
}
export function generateUUID() {
var d = new Date().getTime();
if (window.performance && typeof window.performance.now === "function") {
d += performance.now(); //use high-precision timer if available
}
var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(
c
) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
});
return uuid;
}
+37
View File
@@ -0,0 +1,37 @@
export function isPlainObject(obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
}
export function isString(str) {
return typeof str == "string";
}
export function isToday(time) {
return new Date().getTime() - time < 86400000;
}
export function isEmpty(obj) {
if (!obj) return true;
if (Array.isArray(obj) && obj.length == 0) return true;
if (isPlainObject(obj) && Object.values(obj).length == 0) return true;
return false;
}
export function isUrl(str) {
const reg =
"^((https|http|ftp|rtsp|mms)?://)" +
"?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" + //ftp的user@
"(([0-9]{1,3}.){3}[0-9]{1,3}" + // IP形式的URL- 199.194.52.184
"|" + // 允许IP和DOMAIN(域名)
"([0-9a-z_!~*'()-]+.)*" + // 域名- www.
"([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." + // 二级域名
"[a-z]{2,6})" + // first level domain- .com or .museum
"(:[0-9]{1,4})?" + // 端口- :80
"((/?)|" + // 如果没有文件名,则不需要斜杠
"(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
return new RegExp(reg).test(str) ? true : false;
}
export function isFunction(val) {
return val && typeof val === "function";
}
export function isEng(val) {
return /^[A-Za-z]+$/.test(val);
}