From 547d0f7183cbf6fd1689e7c5fb4b5f78edc9ea72 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Tue, 21 Jul 2020 12:16:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20.prettierrc.js=20=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=20index.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierrc.js | 16 +++++++++ index.js | 89 +++++++++++++++++++++++++++----------------------- 2 files changed, 64 insertions(+), 41 deletions(-) create mode 100644 .prettierrc.js diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..e001ecd --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,16 @@ +/* +对 VSCode Prettier 有效;建议一直要有本配置文件,否则不同版本的 Prettier 的默认配置会不同,例如 TrailingComma +对 VSCode Prettier Standard 无效,似乎是集成了不能修改的配置。 +*/ +module.exports = { + printWidth: 160, // default 80 + tabWidth: 2, // default 2 + useTabs: false, + semi: false, // default true + singleQuote: true, // default false + trailingComma: 'es5', // none (default in v 1.*), es5 (default in v2.0.0), all + bracketSpacing: true, // default true + jsxBracketSameLine: false, // default false + arrowParens: 'always', // avoid (default in v1.9.0), always (default since v2.0.0) + quoteProps: 'as-needed', // as-needed (default), consistent, preserve +} diff --git a/index.js b/index.js index e446cbe..d6ca542 100644 --- a/index.js +++ b/index.js @@ -4,21 +4,23 @@ module.exports = { clog(...message) { - console.log('【【【【【【【【【【', + console.log( + '【【【【【【【【【【', getCurrentPages().length > 0 ? getCurrentPages().pop().route : 'pages/Welcome', // 在首页时,getApp() 或 getCurrentPages() 有可能获取不到。 ...message, - '】】】】】】】】】】】') + '】】】】】】】】】】】' + ) }, sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)), async request({ method = 'POST', url, header = {}, data = {} }) { - url = this.makeUrl(url) if (uni.getStorageSync('_passtoken')) { header._passtoken = uni.getStorageSync('_passtoken') } - if (method === 'GET') { // 如果不是 POST 方法,要额外把参数JSON化 + if (method === 'GET') { + // 如果不是 POST 方法,要额外把参数JSON化 for (let key in data) { data[key] = JSON.stringify(data[key]) } @@ -30,7 +32,16 @@ module.exports = { return [error, response] }, - async pickupFile({ mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'], url, header = {}, formData = {}, name = 'file' } = {}) { + async pickupFile({ + mediaType = 'image', + count = 1, + sizeType = ['original', 'compressed'], + sourceType = ['album', 'camera'], + url, + header = {}, + formData = {}, + name = 'file', + } = {}) { let filePath if (mediaType === 'image') { let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType }) @@ -43,14 +54,14 @@ module.exports = { } if (filePath) { - if (uni.getStorageSync('_passtoken')) { header._passtoken = uni.getStorageSync('_passtoken') } else { return [{ _ERROR: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' }, null] } - for (let key in formData) { // multer 不会自动处理 JSON 数据,必须前后端配合处理 + for (let key in formData) { + // multer 不会自动处理 JSON 数据,必须前后端配合处理 formData[key] = JSON.stringify(formData[key]) } @@ -61,14 +72,12 @@ module.exports = { if (response && response.data) { try { response.data = JSON.parse(response.data) - } catch (exception) { } + } catch (exception) {} } return [errorUpload, response] - } return [{ _ERROR: 'USER_CANCELED' }, null] - }, async uploadFile({ url, name = 'file', formData = {}, header = {} } = {}) { @@ -79,7 +88,8 @@ module.exports = { return [{ errMsg: 'offline user cannot upload files' }, null] } - for (let key in formData) { // multer 不会自动处理 JSON 数据,必须前后端配合处理 + for (let key in formData) { + // multer 不会自动处理 JSON 数据,必须前后端配合处理 formData[key] = JSON.stringify(formData[key]) } @@ -87,7 +97,7 @@ module.exports = { if (response && response.data) { try { response.data = JSON.parse(response.data) - } catch (exception) { } + } catch (exception) {} } return [error, response] }, @@ -109,7 +119,7 @@ module.exports = { cloudPath = name // #endif } else { - return { _state: 'FAIL' } + return { _state: 'UNKNOWN_MEDIA_TYPE' } } if (filePath) { @@ -119,14 +129,16 @@ module.exports = { cloudPath: cloudPath, fileType: mediaType, onUploadProgress: function (progressEvent) { - console.log(progressEvent); - var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total); - } + console.log(progressEvent) + var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total) + }, }) uni.hideLoading() console.log('文件上传结果:', { fileID, requestId }) - return { _state: 'SUCCESS', fileID, requestId } + if (fileID) { + return { _state: 'SUCCESS', fileID, requestId } + } } return { _state: 'FAIL' } }, @@ -136,7 +148,7 @@ module.exports = { plus.runtime.openURL(url) // #endif // #ifdef H5 - window.open(url, "_blank") + window.open(url, '_blank') // #endif }, @@ -153,7 +165,8 @@ module.exports = { // systemInfo.platform = 'tablet' // } - if (/MicroMessenger/.test(window.navigator.userAgent)) { // 微信内置浏览器 + if (/MicroMessenger/.test(window.navigator.userAgent)) { + // 微信内置浏览器 systemInfo.browser = 'wechat' } @@ -174,7 +187,8 @@ module.exports = { showToast({ type, icon, image, title, duration, ...rest }) { let pageNow = this.$store ? this : getCurrentPages().pop() - if (pageNow.$refs && pageNow.$refs.toast) { // 在 ios app 里,虽然能获得 pageNow,但是不存在 pageNow.$refs,不知为何。android app 没有测试 + if (pageNow.$refs && pageNow.$refs.toast) { + // 在 ios app 里,虽然能获得 pageNow,但是不存在 pageNow.$refs,不知为何。android app 没有测试 pageNow.$refs.toast.open({ type, content: title, duration, ...rest }) } else { // #ifdef APP-PLUS @@ -220,35 +234,28 @@ module.exports = { date = new Date() } - format = (format && typeof format === 'string') - ? format - : 'yyyy-mm-dd HH:MM:SS' + format = format && typeof format === 'string' ? format : 'yyyy-mm-dd HH:MM:SS' let o = { - 'm+': date.getMonth() + 1, //月份 - 'q+': Math.floor((date.getMonth() + 3) / 3), //季度 - 'd+': date.getDate(), //日 - 'H+': date.getHours(), //小时 - 'M+': date.getMinutes(), //分 - 'S+': date.getSeconds(), //秒 - 's': date.getMilliseconds() //毫秒 + 'm+': date.getMonth() + 1, //月份 + 'q+': Math.floor((date.getMonth() + 3) / 3), //季度 + 'd+': date.getDate(), //日 + 'H+': date.getHours(), //小时 + 'M+': date.getMinutes(), //分 + 'S+': date.getSeconds(), //秒 + s: date.getMilliseconds(), //毫秒 } - if (/(y+)/.test(format)) - format = format.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length)) + if (/(y+)/.test(format)) format = format.replace(RegExp.$1, `${date.getFullYear()}`.substr(4 - RegExp.$1.length)) for (var k in o) { - if (new RegExp(`(${k})`).test(format)) - format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length))) + if (new RegExp(`(${k})`).test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)) } return format }, hash(data, { hasher = 'sha256', salt, input = 'utf8', output = 'hex' } = {}) { - if (typeof (data) !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) - data = JSON.stringify(data) - if (salt && typeof (salt) === 'string') - data = data + salt + if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data) + if (salt && typeof salt === 'string') data = data + salt let inputEncoding = input // my.INPUT_LIST.indexOf(option.input)>=0?option.input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView. - let outputEncoding = (output === 'buf') ? undefined : output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT) // option.output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64' + let outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT) // option.output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64' return require('crypto').createHash(hasher).update(data, inputEncoding).digest(outputEncoding) }, - -} \ No newline at end of file +}