shaoyongjun 0bd5a87973 to:sync
2024-11-05 22:43:34 +08:00

160 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
define(function (require, exports, module) {
return {
/**
*
*/
charSources: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_'.split(''),
/**
* 十进制 转 63进制
* @param val
* @constructor
*/
Parse10To63(val) {
if (val <= 10) {
return (val - 1) + "";
}
let sources = this.charSources;
let result = [];
let num = 0;
let mx = 63, baseMix = 63;
let tmp = 0;
while (val > 0) {
let mod = val % mx;
if (num === 0) {
tmp = mod - 1;
result.push(sources[tmp])
val = val - mod;
} else {
tmp = mod / (mx / baseMix);
result.push(sources[tmp - 1])
val = val - mod;
}
// 递增
num++;
mx = mx * baseMix;
//log
// console.log("mod: ", mod, " mx: ", mx, " val: ", val, " tmp: ", tmp)
}
return result.reverse().join('')
},
/**
* 生成UUID
* @param usn
* @param docType
* @returns {string}
* @constructor
*/
Uuid(usn, docType = 0) {
let sources = this.charSources;
let radix = sources.length;
let uuid = [];
let myCrypto = window.crypto;
//1. docIdPrefix长度3
let docIdPrefix = "";
if (myCrypto) {
//生成一个 8位的数组长度是 3
let num = new Uint8Array(3);
//随机生成
myCrypto.getRandomValues(num);
// console.log("Your lucky numbers:");
for (let i = 0; i < 3; i++) {
uuid[i] = sources[num[i] >> 2]
}
// docId + uid + docType + ts
docIdPrefix = uuid.join('');
} else {
// Compact form
for (let i = 0; i < 3; i++) uuid[i] = sources[0 | Math.random() * radix];
docIdPrefix = uuid.join('');
}
//2. 基于 2024-11-03 16:37:38 1730623058 最大长度可自增
let curTs4Sec = Date.parse(new Date()) / 1000 - 1730623058;
let tsStr = this.Parse10To63(curTs4Sec);
// console.log("curTs4Sec : ", curTs4Sec, " tsStr: ", tsStr)
// docId(长度固定3) + uid(长度固定6) + docType(长度固定1) + ts(短期内1-6)
return docIdPrefix + docType + usn + tsStr;
},
/**
* 阻止默认事件
* @constructor
*/
ProhibitDefaultEvent(event) {
event.preventDefault()
event.returnValue = false
},
/**
* 获取 触发事件的元素
* @param event
* @constructor
*/
GetEventTarget(event) {
return event.target
},
ParseEvent(e) {
return e || window.event //标准化事件处理
},
GetKeyCode(event) {
return event.keyCode || event.which || event.charCode
},
/**
* 当前选区。 兼容不同浏览器
* @returns {Selection|*}
*/
GetSelection() {
return window.getSelection() || document.selection
},
/**
* 是否是数字
* @param value
* @returns {boolean}
*/
IsNum(value) {
return !isNaN(parseFloat(value)) && isFinite(value)
},
/**
* 在节点node后面插入新节点newNode
* @method InsertAfter
* @param { Node } node 目标节点
* @param { Node } newNode 新插入的节点, 该节点将置于目标节点之后
* @return { Node } 新插入的节点
*/
InsertAfter(node, newNode) {
return node.nextSibling
? node.parentNode.insertBefore(newNode, node.nextSibling)
: node.parentNode.appendChild(newNode)
},
TriggerFocus(selector) {
const targetElement = document.querySelector(selector);
if (document.createEvent) {
const event = document.createEvent('HTMLEvents');
event.initEvent('focus', true, false);
targetElement.dispatchEvent(event);
} else if (document.createEventObject) {
//兼容IE
targetElement.fireEvent('focus');
}
}
}
});