main.js 引用 /MinRequest.js /api.js
import Vue from 'vue'
import App from './App'
import MinRequest from './MinRequest.js'
import minRequest from './api.js'
import store from './store'
Vue.use(MinRequest)
import uView from "uview-ui";
Vue.use(uView);
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
store,
...App,
minRequest
})
app.$mount()
MinRequest.js 文件
const config = Symbol('config')
const isCompleteURL = Symbol('isCompleteURL')
const requestBefore = Symbol('requestBefore')
const requestAfter = Symbol('requestAfter')
var type = 0;
// let data = uni.getStorageSync('token') ? uni.getStorageSync('token'): ''
class MinRequest {
[config] = {
baseURL: '',
header: {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('token'),
},
method: 'GET',
dataType: 'json',
responseType: 'text',
}
interceptors = {
request: (func) => {
if (func) {
MinRequest[requestBefore] = func
} else {
MinRequest[requestBefore] = (request) => request
}
},
response: (func) => {
if (func) {
MinRequest[requestAfter] = func
} else {
MinRequest[requestAfter] = (response) => response
}
}
}
static[requestBefore](config) {
return config
}
static[requestAfter](response) {
console.log(response)
return response
}
static[isCompleteURL](url) {
return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
}
setConfig(func) {
this[config] = func(this[config])
}
request(options = {}) {
options.baseURL = options.baseURL || this[config].baseURL
options.dataType = options.dataType || this[config].dataType
options.url = MinRequest[isCompleteURL](options.url) ? options.url : (options.baseURL + options.url)
options.data = options.data
options.header = { ...options.header,
...this[config].header
}
options.header['Authorization'] = uni.getStorageSync('token');
options.method = options.method || this[config].method
options = { ...options,
...MinRequest[requestBefore](options)
}
return new Promise((resolve, reject) => {
options.success = function(res) {
resolve(MinRequest[requestAfter](res))
}
options.fail = function(err) {
reject(MinRequest[requestAfter](err))
}
uni.request(options)
})
}
get(url, data, options = {}) {
options.url = url
options.data = data
options.method = 'GET'
let cookie = uni.getStorageSync('cookie');
options.header = {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('token'),
cookie: cookie
}
return this.request(options)
}
post(url, data, options = {}) {
options.url = url
options.data = data
options.method = 'POST'
let cookie = uni.getStorageSync('cookie');
options.header = {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('token'),
cookie: cookie
}
return this.request(options)
}
delete(url, data, options = {}) {
options.url = url
options.data = data
options.method = 'DELETE'
let cookie = uni.getStorageSync('cookie');
options.header = {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('token'),
cookie: cookie
}
return this.request(options)
}
put(url, data, options = {}) {
options.url = url
options.data = data
options.method = 'PUT'
let cookie = uni.getStorageSync('cookie');
options.header = {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('token'),
cookie: cookie
}
return this.request(options)
}
}
MinRequest.install = function(Vue) {
Vue.mixin({
beforeCreate: function() {
if (this.$options.minRequest) {
console.log(this.$options.minRequest)
Vue._minRequest = this.$options.minRequest
}
}
})
Object.defineProperty(Vue.prototype, '$minApi', {
get: function() {
return Vue._minRequest.apis
}
})
}
export default MinRequest
api.js 文件
import MinRequest from './MinRequest.js'
const minRequest = new MinRequest()
// 请求拦截器
minRequest.interceptors.request((request) => {
return request
})
// 响应拦截器
minRequest.interceptors.response((response) => {
if (response.statusCode === 401) {
uni.navigateTo({
url: '/pages/login/login'
});
}
if (response.cookies) {
console.log(response.cookies)
if (response.cookies.length != 0) {
uni.setStorageSync('cookie', response.cookies[0]);
}
}
return response.data
})
// 设置默认配置
minRequest.setConfig((config) => {
config.baseURL = ''
return config
})
export default {
// 这里统一管理api请求
apis: {
// 登录
Login(data) {
return minRequest.post('/api/ALgoin/Login', data)
},
}
}