1 line
14 KiB
Plaintext
1 line
14 KiB
Plaintext
|
{"version":3,"file":"utils.js","sources":["../../../../../../../src/uni_modules/lime-painter/components/l-painter/utils.js"],"sourcesContent":["export const networkReg = /^(http|\\/\\/)/;\r\nexport const isBase64 = (path) => /^data:image\\/(\\w+);base64/.test(path);\r\nexport function sleep(delay) {\r\n\treturn new Promise(resolve => setTimeout(resolve, delay))\r\n}\r\nlet {platform, SDKVersion} = uni.getSystemInfoSync() \r\nexport const isPC = /windows|mac/.test(platform)\r\n// 缓存图片\r\nlet cache = {}\r\nexport function isNumber(value) {\r\n\treturn /^-?\\d+(\\.\\d+)?$/.test(value);\r\n}\r\nexport function toPx(value, baseSize, isDecimal = false) {\r\n\t// 如果是数字\r\n\tif (typeof value === 'number') {\r\n\t\treturn value\r\n\t}\r\n\t// 如果是字符串数字\r\n\tif (isNumber(value)) {\r\n\t\treturn value * 1\r\n\t}\r\n\t// 如果有单位\r\n\tif (typeof value === 'string') {\r\n\t\tconst reg = /^-?([0-9]+)?([.]{1}[0-9]+){0,1}(em|rpx|px|%)$/g\r\n\t\tconst results = reg.exec(value);\r\n\t\tif (!value || !results) {\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\tconst unit = results[3];\r\n\t\tvalue = parseFloat(value);\r\n\t\tlet res = 0;\r\n\t\tif (unit === 'rpx') {\r\n\t\t\tres = uni.upx2px(value);\r\n\t\t} else if (unit === 'px') {\r\n\t\t\tres = value * 1;\r\n\t\t} else if (unit === '%') {\r\n\t\t\tres = value * toPx(baseSize) / 100;\r\n\t\t} else if (unit === 'em') {\r\n\t\t\tres = value * toPx(baseSize || 14);\r\n\t\t}\r\n\t\treturn isDecimal ? res.toFixed(2) * 1 : Math.round(res);\r\n\t}\r\n\treturn 0\r\n}\r\n\r\n// 计算版本\r\nexport function compareVersion(v1, v2) {\r\n\tv1 = v1.split('.')\r\n\tv2 = v2.split('.')\r\n\tconst len = Math.max(v1.length, v2.length)\r\n\twhile (v1.length < len) {\r\n\t\tv1.push('0')\r\n\t}\r\n\twhile (v2.length < len) {\r\n\t\tv2.push('0')\r\n\t}\r\n\tfor (let i = 0; i < len; i++) {\r\n\t\tconst num1 = parseInt(v1[i], 10)\r\n\t\tconst num2 = parseInt(v2[i], 10)\r\n\r\n\t\tif (num1 > num2) {\r\n\t\t\treturn 1\r\n\t\t} else if (num1 < num2) {\r\n\t\t\treturn -1\r\n\t\t}\r\n\t}\r\n\treturn 0\r\n}\r\n\r\nfunction gte(version) {\r\n // #ifdef MP-ALIPAY\r\n SDKVersion = my.SDKVersion\r\n // #endif\r\n return compareVersion(SDKVersion, version) >= 0;\r\n}\r\nexport function canIUseCanvas2d() {\r\n\t// #ifdef MP-WEIXIN\r\n\treturn gte('2.9.2');\r\n\t// #endif\r\n\t// #ifdef MP-ALIPAY\r\n\treturn gte('2.7.15');\r\n\t// #endif\r\n\t// #ifdef MP-TOUTIAO\r\n\treturn gte('1.78.0');\r\n\t// #endif\r\n\treturn false\r\n}\r\n\r\n// #ifdef MP\r\nexport const prefix = () => {\r\n\t// #ifdef MP-TOUTIAO\r\n\treturn tt\r\n\t// #endif\r\n\t// #ifdef MP-WEIXIN\r\n\treturn wx\r\n\t// #endif\r\n\t// #ifdef MP-BAIDU\r\n\treturn swan\r\n\t// #endif\r\n\t// #ifdef MP-ALIPAY\r\n\treturn my\r\n\t// #endif\r\n\t// #ifdef MP-QQ\r\n\treturn qq\r\n\t// #endif\r\n\t// #ifdef MP-360\r\n\treturn qh\r\n\t// #endif\r\n}\r\n// #endif\r\n\r\n\r\n\r\n/**\r\n * base64转路径\r\n * @param {Object} base64\r\n */\r\nexport function base64ToPath(base64) {\r\n\tconst [, format] = /^data:image\\/(\\w+);base64,/.exec(base64) || [];\r\n\r\n\treturn new Promise((resolve, reject) => {\r\n\t\t// #ifdef MP\r\n\t\tconst fs = uni.getFileSystemManager()\r\n\t\t//自定义文件名\r\n\t\tif (!format) {\r\n\t\t\treject(new Error('ERROR_BASE64SRC_PARSE'))\r\n\t\t}\r\n\t\tconst time = new Date().getTime();\r\n\t\tlet pre = prefix()\r\n\t\t// #ifdef MP-TOUTIAO\r\n\t\tconst filePath = `${pre.getEnvInfoSync().common.USER_DATA_PATH}/${time}.${format}`\r\n\t\t// #endif\r\n\t\t// #ifndef MP-TOUTIAO\r\n\t\tconst filePath = `${pre.env.USER_DATA_PATH}/${time}.${format}`\r\n\t\t// #endif\r\n\t\tfs.writeFile({\r\n\t\t\tfilePath,\r\n\t\t\tdata: base64.split(',')[1],\r\n\t\t\tencoding: 'base64',\r\n\t\t\tsuccess() {\r\n\t\t\t\tresolve(filePath)\r\n\t\t\t},\r\n\t\t\tfail(err) {\r\n\t\t\t\tconsole.error(err)\r\n\t\t\t\treject(err)\r\n\t\t\t}\r\n\t\t})\r\n\t\t// #endif\r\n\r\n\t\t// #ifdef H5\r\n\t\t// mime类型\r\n\t\tlet mimeString = base64.split(',')[0].split(':')[1].split(';')[0];\r\n\t\t//base64 解码\r\n\t\tlet byteString = atob(base64
|