wo-user-part-uniapp/index.js

96 lines
3.0 KiB
JavaScript

module.exports = {
clog(message){
console.log('【【【【【【【【【【', getApp().$options.router.currentRoute.path, message, '】】】】】】】】】】】')
},
async request(obj){
obj.method = 'POST'
obj.url = this.makeUrl(obj.url)
if (uni.getStorageSync('_passtoken')) {
obj.header = obj.header || {}
obj.header._passtoken = uni.getStorageSync('_passtoken')
}
if (obj.data && (typeof(obj.method) === 'undefined' || obj.method==='GET')) { // 如果不是 POST 方法,要额外把参数JSON化
for (let key in obj.data) {
obj.data[key] = JSON.stringify(obj.data[key])
}
}
console.log('👇 👇 👇 👇 👇 👇 👇 👇 < Request >', obj, '👆 👆 👆 👆 👆 👆 👆 👆 < /Request >')
let [error, response] = await uni.request(obj)
console.log('⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ < Response >', response, '⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ < /Response >')
return [error, response]
},
async uploadFile(obj){
obj.url = this.makeUrl(obj.url)
if (uni.getStorageSync('_passtoken')) {
obj.header = obj.header || {}
obj.header._passtoken = uni.getStorageSync('_passtoken')
}else{
return [{ errMsg:'offline user cannot upload files' }, null]
}
if (obj.formData) { // multer 不会自动处理 JSON 数据,必须前后端配合处理
for (let key in obj.formData) {
obj.formData[key] = JSON.stringify(obj.formData[key])
}
}
if (!obj.name) obj.name = 'file'
let [error, response] = await uni.uploadFile(obj)
if (response && response.data) {
try {
response.data = JSON.parse(response.data)
}catch (exception) {}
}
return [error, response]
},
openUrl(url){
// #ifdef APP-PLUS
plus.runtime.openURL(url)
// #endif
// #ifdef H5
window.open(url, "_blank")
// #endif
},
getPlatform(){
if (window && window.navigator) {
var agent = navigator.userAgent.toLowerCase()
if (agent.match(/MicroMessenger/i) == "micromessenger") {
return 'H5.wechat';
} else {
return 'H5'
}
}
switch(uni.getSystemInfoSync().platform){
case 'android': return 'app.android'
case 'ios': return 'app.ios'
case 'devtools': return 'devtools'
default: return 'unknown'
}
},
showToast({type, icon, image, title, duration}){
let pageStack = getCurrentPages()
let pageNow = pageStack[pageStack.length-1]
if (pageNow.$refs && pageNow.$refs.toast) {
pageNow.$refs.toast.open({type, content:title, duration})
}else {
// #ifndef APP-PLUS
if (!image){
image = `../static/Common.${type?type:'info'}.png`
}
uni.showToast({icon, image, title, duration})
// #endif
// #ifdef APP-PLUS
if (uni.getSystemInfoSync().platform==='android') {
uni.showToast({icon:'none', title, duration})
}
// #endif
}
},
}