MCPcopy Create free account
hub / github.com/circircir-circle/human_exist_device / guid

Method guid

mini_program/common/Tools.js:609–637  ·  view source on GitHub ↗

* 全局唯一标识符(uuid,Globally Unique Identifier),也称作 uuid(Universally Unique IDentifier) * 一般用于多个组件之间,给它一个唯一的标识符,或者v-for循环的时候,如果使用数组的index可能会导致更新列表出现问题 * 最可能的情况是左滑删除item或者对某条信息流"不喜欢"并去掉它的时候,会导致组件内的数据可能出现错乱 * v-for的时候,推荐使用后端返回的id而不是循环的index * @param {Number} len uuid的长度 * @param {Boolean} firstU

(len = 32, firstU = true, radix = null)

Source from the content-addressed store, hash-verified

607 * @param {Nubmer} radix 生成uuid的基数(意味着返回的字符串都是这个基数),2-二进制,8-八进制,10-十进制,16-十六进制
608 */
609 guid(len = 32, firstU = true, radix = null) {
610 let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
611 let uuid = [];
612 radix = radix || chars.length;
613
614 if (len) {
615 // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
616 for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
617 } else {
618 let r;
619 // rfc4122标准要求返回的uuid中,某些位为固定的字符
620 uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
621 uuid[14] = '4';
622
623 for (let i = 0; i < 36; i++) {
624 if (!uuid[i]) {
625 r = 0 | Math.random() * 16;
626 uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
627 }
628 }
629 }
630 // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
631 if (firstU) {
632 uuid.shift();
633 return 'u' + uuid.join('');
634 } else {
635 return uuid.join('');
636 }
637 }
638
639 /**
640 * 查询节点信息

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected