u
This commit is contained in:
parent
6c3819c544
commit
73d810197d
143
index.js
143
index.js
@ -1,3 +1,7 @@
|
|||||||
|
// #ifdef H5
|
||||||
|
// import device from 'current-device' // https://github.com/matthewhudson/current-device
|
||||||
|
// #endif
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
clog(...message){
|
clog(...message){
|
||||||
console.log('【【【【【【【【【【',
|
console.log('【【【【【【【【【【',
|
||||||
@ -8,42 +12,94 @@ module.exports = {
|
|||||||
|
|
||||||
sleep: (ms)=>new Promise((resolve, reject)=>setTimeout(resolve, ms)),
|
sleep: (ms)=>new Promise((resolve, reject)=>setTimeout(resolve, ms)),
|
||||||
|
|
||||||
async request(obj){
|
async request({method='POST', url, header={}, data={}}){
|
||||||
obj.method = 'POST'
|
|
||||||
|
|
||||||
obj.url = this.makeUrl(obj.url)
|
|
||||||
|
|
||||||
|
url = this.makeUrl(url)
|
||||||
if (uni.getStorageSync('_passtoken')) {
|
if (uni.getStorageSync('_passtoken')) {
|
||||||
obj.header = obj.header || {}
|
header._passtoken = uni.getStorageSync('_passtoken')
|
||||||
obj.header._passtoken = uni.getStorageSync('_passtoken')
|
|
||||||
}
|
}
|
||||||
if (obj.data && (typeof(obj.method) === 'undefined' || obj.method==='GET')) { // 如果不是 POST 方法,要额外把参数JSON化
|
if (method==='GET') { // 如果不是 POST 方法,要额外把参数JSON化
|
||||||
for (let key in obj.data) {
|
for (let key in data) {
|
||||||
obj.data[key] = JSON.stringify(obj.data[key])
|
data[key] = JSON.stringify(data[key])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('👇 👇 👇 👇 < Request > 👇 👇 👇 👇 ', obj, '👆 👆 👆 👆 < /Request > 👆 👆 👆 👆')
|
console.log('👇 👇 👇 👇 < Request > 👇 👇 👇 👇 ', {method, url, header, data}, '👆 👆 👆 👆 < /Request > 👆 👆 👆 👆')
|
||||||
let [error, response] = await uni.request(obj)
|
let [error, response] = await uni.request({method, url, header, data})
|
||||||
console.log('⬇️ ⬇️ ⬇️ ⬇️ < Response > ⬇️ ⬇️ ⬇️ ⬇️ ', response, '⬆️ ⬆️ ⬆️ ⬆️ < /Response > ⬆️ ⬆️ ⬆️ ⬆️')
|
console.log('⬇️ ⬇️ ⬇️ ⬇️ < Response > ⬇️ ⬇️ ⬇️ ⬇️ ', response, '⬆️ ⬆️ ⬆️ ⬆️ < /Response > ⬆️ ⬆️ ⬆️ ⬆️')
|
||||||
return [error, response]
|
return [error, response]
|
||||||
},
|
},
|
||||||
|
|
||||||
async uploadFile(obj){
|
async pickupFile({type='image', count=1, mediaType, sizeType, sourceType, compress=false, url, header={}, formData={}, name='file'}){ // choose and upload file
|
||||||
obj.url = this.makeUrl(obj.url)
|
let picked
|
||||||
|
if (type==='image'){
|
||||||
|
picked = await uni.chooseImage({count, sizeType})
|
||||||
|
}else if (type==='video'){
|
||||||
|
picked = await uni.chooseVideo({count, compressed:compress, sourceType})
|
||||||
|
}else {
|
||||||
|
picked = await uni.chooseMedia({count, mediaType, sizeType, sourceType})
|
||||||
|
}
|
||||||
|
let [errorChoose, {tempFilePaths, tempFiles}={}] = picked
|
||||||
|
|
||||||
|
if (!errorChoose){
|
||||||
|
|
||||||
|
if (compress && tempFiles[0].size>1048576){
|
||||||
|
console.log('========= compressing -------')
|
||||||
|
await uni.compressImage({ // compressImage not implemented yet in H5
|
||||||
|
src:tempFiles[0].path,
|
||||||
|
quality: 50,
|
||||||
|
success: res => {
|
||||||
|
console.log('======= compressed-------')
|
||||||
|
console.log(res)
|
||||||
|
tempFilePaths[0] = res.tempFilePath
|
||||||
|
},
|
||||||
|
fail: err => {
|
||||||
|
console.log('======= compress failed ====')
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (uni.getStorageSync('_passtoken')) {
|
if (uni.getStorageSync('_passtoken')) {
|
||||||
obj.header = obj.header || {}
|
header._passtoken = uni.getStorageSync('_passtoken')
|
||||||
obj.header._passtoken = uni.getStorageSync('_passtoken')
|
}else{
|
||||||
|
return [{ _ERROR: 'USER_OFFLINE', errMsg:'offline user cannot upload files' }, null]
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let key in formData) { // multer 不会自动处理 JSON 数据,必须前后端配合处理
|
||||||
|
formData[key] = JSON.stringify(formData[key])
|
||||||
|
}
|
||||||
|
|
||||||
|
uni.showLoading()
|
||||||
|
let [errorUpload, response] = await uni.uploadFile({ filePath: tempFilePaths[0], url: this.makeUrl(url), header, formData, name })
|
||||||
|
uni.hideLoading()
|
||||||
|
|
||||||
|
if (response && response.data) {
|
||||||
|
try {
|
||||||
|
response.data = JSON.parse(response.data)
|
||||||
|
}catch (exception) {}
|
||||||
|
}
|
||||||
|
return [errorUpload, response]
|
||||||
|
|
||||||
|
}else{
|
||||||
|
return [{ _ERROR:'USER_CANCELED'}, null]
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
async uploadFile({url, name='file', formData={}, header={}}){
|
||||||
|
url = this.makeUrl(url)
|
||||||
|
if (uni.getStorageSync('_passtoken')) {
|
||||||
|
header._passtoken = uni.getStorageSync('_passtoken')
|
||||||
}else{
|
}else{
|
||||||
return [{ errMsg:'offline user cannot upload files' }, null]
|
return [{ errMsg:'offline user cannot upload files' }, null]
|
||||||
}
|
}
|
||||||
if (obj.formData) { // multer 不会自动处理 JSON 数据,必须前后端配合处理
|
|
||||||
for (let key in obj.formData) {
|
for (let key in formData) { // multer 不会自动处理 JSON 数据,必须前后端配合处理
|
||||||
obj.formData[key] = JSON.stringify(obj.formData[key])
|
formData[key] = JSON.stringify(formData[key])
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (!obj.name) obj.name = 'file'
|
let [error, response] = await uni.uploadFile({url,name,formData,header})
|
||||||
let [error, response] = await uni.uploadFile(obj)
|
|
||||||
if (response && response.data) {
|
if (response && response.data) {
|
||||||
try {
|
try {
|
||||||
response.data = JSON.parse(response.data)
|
response.data = JSON.parse(response.data)
|
||||||
@ -61,21 +117,36 @@ module.exports = {
|
|||||||
// #endif
|
// #endif
|
||||||
},
|
},
|
||||||
|
|
||||||
getPlatform(){
|
getSystemInfo(){
|
||||||
if (window && window.navigator) {
|
let systemInfo = uni.getSystemInfoSync()
|
||||||
var agent = navigator.userAgent.toLowerCase()
|
// #ifdef H5
|
||||||
if (agent.match(/MicroMessenger/i) == "micromessenger") {
|
systemInfo.runtime = 'h5'
|
||||||
return 'H5.wechat';
|
|
||||||
} else {
|
// if (device.mobile()){
|
||||||
return 'H5'
|
// systemInfo.platform = 'mobile'
|
||||||
}
|
// }else if (device.desktop()){
|
||||||
}
|
// systemInfo.platform = 'desktop'
|
||||||
switch(uni.getSystemInfoSync().platform){
|
// }else if (device.tablet()){
|
||||||
case 'android': return 'app.android'
|
// systemInfo.platform = 'tablet'
|
||||||
case 'ios': return 'app.ios'
|
// }
|
||||||
case 'devtools': return 'devtools'
|
|
||||||
default: return 'unknown'
|
if (/MicroMessenger/.test(window.navigator.userAgent)) { // 微信内置浏览器
|
||||||
|
systemInfo.browser = 'wechat'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
// #ifdef APP-PLUS || APP-PLUS-NVUE
|
||||||
|
systemInfo.runtime = 'app'
|
||||||
|
// 细分成 systemInfo.platform === ios or android
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
// #ifdef MP
|
||||||
|
systemInfo.runtime = 'mp'
|
||||||
|
// 细分成 WEIXIN, ...
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
return systemInfo
|
||||||
},
|
},
|
||||||
|
|
||||||
showToast({type, icon, image, title, duration, ...rest}){
|
showToast({type, icon, image, title, duration, ...rest}){
|
||||||
@ -102,7 +173,7 @@ module.exports = {
|
|||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
document.title = windowTitle || page.$store.getters['i18n/getAppName'] || page.appName // 必须放在 setNavigationBarTitle 之后,否则会被其覆盖掉。
|
document.title = windowTitle || page.$store.getters['i18n/getAppName'] || page.appName // 必须放在 setNavigationBarTitle 之后,否则会被其覆盖掉。
|
||||||
// #endif
|
// #endif
|
||||||
page.$store.commit('i18n/setTabbar') // 必须要在有 tab 的页面里重置才有效果
|
if (page.$store._mutations['i18n/setTabbar']) page.$store.commit('i18n/setTabbar') // 必须要在有 tab 的页面里重置才有效果
|
||||||
},
|
},
|
||||||
|
|
||||||
localeText(){
|
localeText(){
|
||||||
|
@ -48,8 +48,7 @@ based on
|
|||||||
|
|
||||||
if(!toast.clickable){
|
if(!toast.clickable){
|
||||||
this.disappear(toast.uuid,toast.duration,toast.complete)
|
this.disappear(toast.uuid,toast.duration,toast.complete)
|
||||||
}//可点击消失
|
}else{ //可点击消失
|
||||||
else{
|
|
||||||
this.$emit('uuidCallback',toast.uuid)
|
this.$emit('uuidCallback',toast.uuid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user