/**
* 字符操作工具类
* @author cls
* @date 2019-05-17
*/
import md5 from "md5";
import pinyin from 'js-pinyin';
export const StringUtil = {
/**
* showdoc
* @catalog JS工具类/StringUtil
* @title 获取随机字符串
* @description 根据传入字符串长度,获取随机字符串id
* @method static
* @url import { StringUtil } from '@birt/funclib/StringUtil.js'
* @param len 可选 随机字符长度
* @return String 随机字符
*/
randomString(len) {
len = len || 32;
var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678';
var maxPos = $chars.length;
var str = '';
for (var i = 0; i < len; i++) {
str += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return str;
},
/**
* showdoc
* @catalog API/工具/String
* @title 获取指定范围随机数
* @description 获取中文全拼音
* @url randomInt(min, max)
* @method $instance.randomInt(min, max)
* @param min 必选 Int 起止数字
* @param max 必选 Int 结束数字
* @return String
* @number 60
*/
randomInt: function (min, max) {
var Range = max - min;
var Rand = Math.random();
return (min + Math.round(Rand * Range));
},
/**
* showdoc
* @catalog API/工具/String
* @title 生成MD5
* @description 生成MD5
* @url md5(str)
* @method $instance.md5(str)
* @param str 必选 String 需生成的字符
* @return String
* @number 60
*/
md5: function (str) {
return md5(str);
},
/**
* showdoc
* @catalog API/工具/String
* @title 获取GUID
* @description 获取GUID
* @url guid()
* @method $instance.guid()
* @return String
* @number 60
*/
guid: function () {
var nowDateTime = new Date();
var myRandom1 = this.randomInt(1, 1000);
var myRandom2 = this.randomInt(1, 1000);
var myRandom3 = this.randomInt(1, 1000);
var decodeStr = nowDateTime.valueOf() + "-"
+ nowDateTime.getMilliseconds() + "-" + myRandom1 + "-"
+ myRandom2 + "-" + myRandom3;
var encodeStr = this.md5(decodeStr);
var guid = encodeStr.substring(0, 8) + "-" + encodeStr.substring(8, 12)
+ "-" + encodeStr.substring(12, 16) + "-"
+ encodeStr.substring(16, 20) + "-"
+ encodeStr.substring(20, 32);
return guid.toUpperCase();
},
/**
* showdoc
* @catalog API/工具/String
* @title 字符串非空
* @description 判断字符串非空
* @url isEmpty(obj)
* @method StringUtil.isEmpty(obj)
* @param obj 必选 Object 被判断对象
* @return Boolean
* @number 60
*/
isEmpty(obj) {
return (typeof obj === 'undefined' || obj === null || obj === "");
},
/**
* showdoc
* @catalog API/工具/String
* @title 获取首字母拼音
* @description 获取首字母拼音
* @url getCamelChars(label)
* @method StringUtil.getCamelChars(label)
* @param label 必选 String 中文字符
* @return String
* @number 60
*/
getCamelChars(label) {
return pinyin.getCamelChars(label || "")
}
};