添加 .prettierrc.js 格式化 index.js
This commit is contained in:
parent
9f005f2112
commit
547d0f7183
16
.prettierrc.js
Normal file
16
.prettierrc.js
Normal file
@ -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
|
||||||
|
}
|
89
index.js
89
index.js
@ -4,21 +4,23 @@
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
clog(...message) {
|
clog(...message) {
|
||||||
console.log('【【【【【【【【【【',
|
console.log(
|
||||||
|
'【【【【【【【【【【',
|
||||||
getCurrentPages().length > 0 ? getCurrentPages().pop().route : 'pages/Welcome', // 在首页时,getApp() 或 getCurrentPages() 有可能获取不到。
|
getCurrentPages().length > 0 ? getCurrentPages().pop().route : 'pages/Welcome', // 在首页时,getApp() 或 getCurrentPages() 有可能获取不到。
|
||||||
...message,
|
...message,
|
||||||
'】】】】】】】】】】】')
|
'】】】】】】】】】】】'
|
||||||
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
|
sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
|
||||||
|
|
||||||
async request({ method = 'POST', url, header = {}, data = {} }) {
|
async request({ method = 'POST', url, header = {}, data = {} }) {
|
||||||
|
|
||||||
url = this.makeUrl(url)
|
url = this.makeUrl(url)
|
||||||
if (uni.getStorageSync('_passtoken')) {
|
if (uni.getStorageSync('_passtoken')) {
|
||||||
header._passtoken = uni.getStorageSync('_passtoken')
|
header._passtoken = uni.getStorageSync('_passtoken')
|
||||||
}
|
}
|
||||||
if (method === 'GET') { // 如果不是 POST 方法,要额外把参数JSON化
|
if (method === 'GET') {
|
||||||
|
// 如果不是 POST 方法,要额外把参数JSON化
|
||||||
for (let key in data) {
|
for (let key in data) {
|
||||||
data[key] = JSON.stringify(data[key])
|
data[key] = JSON.stringify(data[key])
|
||||||
}
|
}
|
||||||
@ -30,7 +32,16 @@ module.exports = {
|
|||||||
return [error, response]
|
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
|
let filePath
|
||||||
if (mediaType === 'image') {
|
if (mediaType === 'image') {
|
||||||
let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
|
let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
|
||||||
@ -43,14 +54,14 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (filePath) {
|
if (filePath) {
|
||||||
|
|
||||||
if (uni.getStorageSync('_passtoken')) {
|
if (uni.getStorageSync('_passtoken')) {
|
||||||
header._passtoken = uni.getStorageSync('_passtoken')
|
header._passtoken = uni.getStorageSync('_passtoken')
|
||||||
} else {
|
} else {
|
||||||
return [{ _ERROR: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' }, null]
|
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])
|
formData[key] = JSON.stringify(formData[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,14 +72,12 @@ module.exports = {
|
|||||||
if (response && response.data) {
|
if (response && response.data) {
|
||||||
try {
|
try {
|
||||||
response.data = JSON.parse(response.data)
|
response.data = JSON.parse(response.data)
|
||||||
} catch (exception) { }
|
} catch (exception) {}
|
||||||
}
|
}
|
||||||
return [errorUpload, response]
|
return [errorUpload, response]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [{ _ERROR: 'USER_CANCELED' }, null]
|
return [{ _ERROR: 'USER_CANCELED' }, null]
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async uploadFile({ url, name = 'file', formData = {}, header = {} } = {}) {
|
async uploadFile({ url, name = 'file', formData = {}, header = {} } = {}) {
|
||||||
@ -79,7 +88,8 @@ module.exports = {
|
|||||||
return [{ errMsg: 'offline user cannot upload files' }, null]
|
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])
|
formData[key] = JSON.stringify(formData[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,7 +97,7 @@ module.exports = {
|
|||||||
if (response && response.data) {
|
if (response && response.data) {
|
||||||
try {
|
try {
|
||||||
response.data = JSON.parse(response.data)
|
response.data = JSON.parse(response.data)
|
||||||
} catch (exception) { }
|
} catch (exception) {}
|
||||||
}
|
}
|
||||||
return [error, response]
|
return [error, response]
|
||||||
},
|
},
|
||||||
@ -109,7 +119,7 @@ module.exports = {
|
|||||||
cloudPath = name
|
cloudPath = name
|
||||||
// #endif
|
// #endif
|
||||||
} else {
|
} else {
|
||||||
return { _state: 'FAIL' }
|
return { _state: 'UNKNOWN_MEDIA_TYPE' }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filePath) {
|
if (filePath) {
|
||||||
@ -119,14 +129,16 @@ module.exports = {
|
|||||||
cloudPath: cloudPath,
|
cloudPath: cloudPath,
|
||||||
fileType: mediaType,
|
fileType: mediaType,
|
||||||
onUploadProgress: function (progressEvent) {
|
onUploadProgress: function (progressEvent) {
|
||||||
console.log(progressEvent);
|
console.log(progressEvent)
|
||||||
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
|
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
uni.hideLoading()
|
uni.hideLoading()
|
||||||
|
|
||||||
console.log('文件上传结果:', { fileID, requestId })
|
console.log('文件上传结果:', { fileID, requestId })
|
||||||
return { _state: 'SUCCESS', fileID, requestId }
|
if (fileID) {
|
||||||
|
return { _state: 'SUCCESS', fileID, requestId }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return { _state: 'FAIL' }
|
return { _state: 'FAIL' }
|
||||||
},
|
},
|
||||||
@ -136,7 +148,7 @@ module.exports = {
|
|||||||
plus.runtime.openURL(url)
|
plus.runtime.openURL(url)
|
||||||
// #endif
|
// #endif
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
window.open(url, "_blank")
|
window.open(url, '_blank')
|
||||||
// #endif
|
// #endif
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -153,7 +165,8 @@ module.exports = {
|
|||||||
// systemInfo.platform = 'tablet'
|
// systemInfo.platform = 'tablet'
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (/MicroMessenger/.test(window.navigator.userAgent)) { // 微信内置浏览器
|
if (/MicroMessenger/.test(window.navigator.userAgent)) {
|
||||||
|
// 微信内置浏览器
|
||||||
systemInfo.browser = 'wechat'
|
systemInfo.browser = 'wechat'
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +187,8 @@ module.exports = {
|
|||||||
|
|
||||||
showToast({ type, icon, image, title, duration, ...rest }) {
|
showToast({ type, icon, image, title, duration, ...rest }) {
|
||||||
let pageNow = this.$store ? this : getCurrentPages().pop()
|
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 })
|
pageNow.$refs.toast.open({ type, content: title, duration, ...rest })
|
||||||
} else {
|
} else {
|
||||||
// #ifdef APP-PLUS
|
// #ifdef APP-PLUS
|
||||||
@ -220,35 +234,28 @@ module.exports = {
|
|||||||
date = new Date()
|
date = new Date()
|
||||||
}
|
}
|
||||||
|
|
||||||
format = (format && typeof format === 'string')
|
format = format && typeof format === 'string' ? format : 'yyyy-mm-dd HH:MM:SS'
|
||||||
? format
|
|
||||||
: 'yyyy-mm-dd HH:MM:SS'
|
|
||||||
let o = {
|
let o = {
|
||||||
'm+': date.getMonth() + 1, //月份
|
'm+': date.getMonth() + 1, //月份
|
||||||
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
|
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
|
||||||
'd+': date.getDate(), //日
|
'd+': date.getDate(), //日
|
||||||
'H+': date.getHours(), //小时
|
'H+': date.getHours(), //小时
|
||||||
'M+': date.getMinutes(), //分
|
'M+': date.getMinutes(), //分
|
||||||
'S+': date.getSeconds(), //秒
|
'S+': date.getSeconds(), //秒
|
||||||
's': date.getMilliseconds() //毫秒
|
s: date.getMilliseconds(), //毫秒
|
||||||
}
|
}
|
||||||
if (/(y+)/.test(format))
|
if (/(y+)/.test(format)) format = format.replace(RegExp.$1, `${date.getFullYear()}`.substr(4 - RegExp.$1.length))
|
||||||
format = format.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length))
|
|
||||||
for (var k in o) {
|
for (var k in o) {
|
||||||
if (new RegExp(`(${k})`).test(format))
|
if (new RegExp(`(${k})`).test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length))
|
||||||
format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length)))
|
|
||||||
}
|
}
|
||||||
return format
|
return format
|
||||||
},
|
},
|
||||||
|
|
||||||
hash(data, { hasher = 'sha256', salt, input = 'utf8', output = 'hex' } = {}) {
|
hash(data, { hasher = 'sha256', salt, input = 'utf8', output = 'hex' } = {}) {
|
||||||
if (typeof (data) !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView))
|
if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data)
|
||||||
data = JSON.stringify(data)
|
if (salt && typeof salt === 'string') data = data + salt
|
||||||
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 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)
|
return require('crypto').createHash(hasher).update(data, inputEncoding).digest(outputEncoding)
|
||||||
},
|
},
|
||||||
|
}
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user