config.js
class Config{
constructor() {}
}
/*
* 1 表示正式环境
* 2 表示测试环境
* 3 表示本地环境
*/
Config.official = 1;
if (Config.official == 1) {
Config.domain = 'http://xxxxx'; //正式
}
if (Config.official == 2) {
Config.domain = "http://xxxxxxx"; //测试
}
if(Config.official == 3) {
Config.domain = "http://xxxxxxx"; //本地测试
}
export {Config};
import {
Config
} from './config.js';
class Base {
constructor() {
"use strict"
this.domain = Config.domain;
}
/*
* 1. 封装 uniapp 的请求接口
* 带Token请求--promise封装
* @Author wangqiang
* @DateTime 2019-07-02
* return 返回promise
* 使用方式:
* let opts = {
* url: '/api/device/add',
* method: 'post'
* },
* let data = {
* name:'zs',
* age:10
* }
* this.httpTokenRequest(opts,data);
*/
httpTokenRequest(opts, data = {}) {
let token = "";
uni.getStorage({
key: 'user_token',
success: function(result) {
token = result.data
}
});
if (!opts.method) {
opts.method = 'GET';
}
let httpDefaultOpts = {
url: this.domain + opts.url,
data: data,
method: opts.method.toUpperCase(),
header: opts.method.toUpperCase() == 'POST' ? {
from: 'web',
'Token': token,
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
} : {
from: 'web',
'Token': token,
"Accept": "application/json",
"Content-Type": "application/json; charset=UTF-8"
},
dataType: 'json',
}
let promise = new Promise((resolve, reject) => {
uni.request(httpDefaultOpts)
.then(
res => {
if ((res[1])) {
// 判断以2(2XX)开头的状态码为正确
let _code = res[1].statusCode.toString();
let _startChar = _code.charAt(0);
if (_startChar == '2') {
resolve(res[1].data)
} else {
this._processError(res[1]); //错误处理函数
}
}
}
)
.catch(error => reject(error))
})
return promise;
}
/**
* 2. 封装上传文件接口
* @Author wangqiang
* @DateTime 2019-07-02
* @param {[type]} params [description]
* @return 返回promise对象
*/
uploadFileAsync(opts) {
let token = "";
uni.getStorage({
key: 'user_token',
success: function(res) {
token = res.data
}
});
let httpDefaultOpts = {
url: this.domain + opts.url,
header: {
from: 'web',
token: token,
},
filePath: opts.filePath,
name: 'files'
}
let promise = new Promise((resolve, reject) => {
uni.uploadFile(httpDefaultOpts).then(
res => {
resolve(res[1].data)
}
).catch(
response => {
reject(response)
}
)
})
return promise;
}
/**
* 3. 封装错误异常处理接口
* @Author wangqiang
* @DateTime 2019-07-02
* @param {[type]} params [description]
*/
_processError(params) {
if(params.statusCode==404){
uni.showToast({title: '页面不存在',icon: 'none'})
return uni.navigateBack({delta:1})
}
let token_code= params.data.error_code ? params.data.error_code : ''
var title = params.data.msg.toString();
let _token = uni.getStorageSync('user_token');
//判断token是否过期、Token是否为空
if(token_code === 5007 || _token == "" || _token == null || _token === undefined){
uni.showToast({
title: '离线时间过长,请重新登录!',
icon: 'none'
});
return uni.navigateTo({url: '/pages/login/login'})
}
if (title != 'JWT验证未通过') {
if (title.indexOf("Token")<0){
uni.showToast({
title: title,
icon: 'none'
});
} else {
uni.showToast({
title: "离线时间过长,请重新登录",
icon: 'none'
});
return uni.navigateTo({url: '/pages/login/login'})
}
}else{
uni.showToast({
title: title,
icon: 'none'
});
}
}
/**
* 4.判断是否登录(根据token判断)
* @Author wangqiang
* @DateTime 2019-07-02
* @return 返回布尔值
*/
judgeIsLogined() {
let user_token = uni.getStorageSync('user_token');
if (user_token == "" || user_token == null || user_token === undefined) {
uni.navigateTo({
url: '../login/login'
})
return false;
} else {
return true;
}
}
}
export {
Base
};