新建项目
This commit is contained in:
13
utils/auth.token.js
Normal file
13
utils/auth.token.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const TokenKey = 'Global-Auth-Token'
|
||||
|
||||
const token = {
|
||||
|
||||
getToken: function() {
|
||||
return uni.getStorageSync(TokenKey)
|
||||
},
|
||||
setToken: function(token) {
|
||||
return uni.setStorageSync(TokenKey, token)
|
||||
}
|
||||
}
|
||||
|
||||
export default token
|
||||
13
utils/request/index.js
Normal file
13
utils/request/index.js
Normal file
@@ -0,0 +1,13 @@
|
||||
// 引入配置
|
||||
import config from '@/common/config'
|
||||
// 初始化请求配置
|
||||
uni.$u.http.setConfig((defaultConfig) => {
|
||||
/* defaultConfig 为默认全局配置 */
|
||||
defaultConfig.baseURL = config.baseUrl /* 根域名 */
|
||||
return defaultConfig
|
||||
})
|
||||
|
||||
module.exports = (vm) => {
|
||||
require('./requestInterceptors')(vm)
|
||||
require('./responseInterceptors')(vm)
|
||||
}
|
||||
35
utils/request/requestInterceptors.js
Normal file
35
utils/request/requestInterceptors.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import conf from "@/common/config.js"
|
||||
import token from '@/utils/auth.token.js'
|
||||
/**
|
||||
* 请求拦截
|
||||
* @param {Object} http
|
||||
*/
|
||||
module.exports = (vm) => {
|
||||
uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
|
||||
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
|
||||
config.data = config.data || {}
|
||||
// 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
|
||||
// console.log(vm.$store.state);
|
||||
|
||||
// 设置请求头中token的名字,以及token的值
|
||||
let _token = token.getToken()
|
||||
let _tokenName = 'Authorization'
|
||||
if (conf.tokenName) {
|
||||
_tokenName = conf.tokenName
|
||||
}
|
||||
config.header[_tokenName] = _token
|
||||
|
||||
// 设置针对响应结果的处理方式
|
||||
let _cus = {
|
||||
showLoading: true, // 是否显示加载等待框
|
||||
loadingTitle: '加载中', // 加载等待框的提示文字
|
||||
showFailMessage: true, // 返回失败信息是否显示
|
||||
catchError: true, // 是否集中管理Catch,
|
||||
}
|
||||
Object.assign(_cus, config.custom)
|
||||
config.custom = _cus
|
||||
|
||||
return config
|
||||
}, (config) => // 可使用async await 做异步操作
|
||||
Promise.reject(config))
|
||||
}
|
||||
130
utils/request/responseInterceptors.js
Normal file
130
utils/request/responseInterceptors.js
Normal file
@@ -0,0 +1,130 @@
|
||||
import conf from "@/common/config.js"
|
||||
/**
|
||||
* 响应拦截
|
||||
* @param {Object} http
|
||||
*/
|
||||
module.exports = (vm) => {
|
||||
uni.$u.http.interceptors.response.use((response) => {
|
||||
/* 对响应成功做点什么 可使用async await 做异步操作*/
|
||||
|
||||
// 自定义参数
|
||||
let _cus = {
|
||||
showLoading: true, // 是否显示加载等待框
|
||||
loadingTitle: '加载中', // 加载等待框的提示文字
|
||||
showFailMessage: true, // 返回失败信息是否显示
|
||||
catchError: true, // 是否集中管理Catch,
|
||||
}
|
||||
if (response.config && response.config.custom)
|
||||
Object.assign(_cus, response.config.custom)
|
||||
|
||||
if (200 == response.statusCode) {
|
||||
let data = response.data
|
||||
if (_cus.catchError) {
|
||||
if (data.success) {
|
||||
return data.data
|
||||
} else {
|
||||
let loginTimeoutCode = "5000"
|
||||
let loginTimeoutPage = "/pages/login/index"
|
||||
if (conf.loginTimeoutCode) {
|
||||
loginTimeoutCode = "" + conf.loginTimeoutCode
|
||||
}
|
||||
if (conf.loginTimeoutPage) {
|
||||
loginTimeoutPage = conf.loginTimeoutPage
|
||||
}
|
||||
if (loginTimeoutCode === data.code) {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '登录过期或失效,请重新登录!',
|
||||
showCancel: false,
|
||||
success: function(res) {
|
||||
uni.reLaunch({
|
||||
url: loginTimeoutPage
|
||||
})
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (_cus.showFailMessage) {
|
||||
uni.showToast({
|
||||
title: data.msg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
})
|
||||
}
|
||||
}
|
||||
return Promise.reject(data)
|
||||
}
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
} else {
|
||||
console.log('response.statusCode--', response)
|
||||
let em = {
|
||||
"code": "" + response.statusCode,
|
||||
"data": "",
|
||||
"msg": "网络请求失败~",
|
||||
"success": false,
|
||||
"timestamp": new Date().getTime()
|
||||
}
|
||||
if (res.data) {
|
||||
em.msg = res.data
|
||||
}
|
||||
if (_cus.showFailMessage) {
|
||||
uni.showToast({
|
||||
title: em.msg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
})
|
||||
}
|
||||
return Promise.reject(em)
|
||||
}
|
||||
|
||||
// const data = response.data
|
||||
// if (data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
|
||||
// // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
|
||||
// if (custom.toast !== false) {
|
||||
// uni.$u.toast(data.message)
|
||||
// }
|
||||
// // 如果需要catch返回,则进行reject
|
||||
// if (custom?.catch) {
|
||||
// return Promise.reject(data)
|
||||
// } else {
|
||||
// // 否则返回一个pending中的promise
|
||||
// return new Promise(() => {})
|
||||
// }
|
||||
// }
|
||||
// return data.data || {}
|
||||
}, (error) => {
|
||||
console.log('request-fail', error)
|
||||
let _opts = {
|
||||
showLoading: true, // 是否显示加载等待框
|
||||
loadingTitle: '加载中', // 加载等待框的提示文字
|
||||
showFailMessage: true, // 返回失败信息是否显示
|
||||
catchError: true, // 是否集中管理Catch,
|
||||
}
|
||||
if (error.config && error.config.custom)
|
||||
Object.assign(_opts, response.config.custom)
|
||||
let em = {
|
||||
"code": "600",
|
||||
"data": "",
|
||||
"msg": "网络请求失败~",
|
||||
"success": false,
|
||||
"timestamp": new Date().getTime()
|
||||
}
|
||||
if (404 == error.statusCode) {
|
||||
em.code = "404"
|
||||
em.msg = "404,请求地址错误"
|
||||
}
|
||||
if (500 == error.statusCode) {
|
||||
em.code = "500"
|
||||
em.msg = "500,内部服务错误"
|
||||
}
|
||||
if (_opts.showFailMessage) {
|
||||
uni.showToast({
|
||||
title: em.msg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
})
|
||||
}
|
||||
return Promise.reject(em)
|
||||
})
|
||||
}
|
||||
196
utils/requester.js
Normal file
196
utils/requester.js
Normal file
@@ -0,0 +1,196 @@
|
||||
/**
|
||||
* config 配置项说明
|
||||
* baseUrl = "", // 接口的根地址
|
||||
* tokenName = "Authorization", // 请求头中token的名字,与服务器端对应
|
||||
* loginTimeoutCode : "5000", // 登录超时或失效的情况下,服务器端返回的错误码
|
||||
* loginTimeoutPage = "/pages/login/index", // 登录超时或失效的情况下,跳转到的登录页面
|
||||
*/
|
||||
import config from '@/common/config.js'
|
||||
import token from '@/utils/auth.token.js'
|
||||
|
||||
/**
|
||||
* request options配置项说明
|
||||
* url: '', // url String 是 开发者服务器接口地址
|
||||
* data: {}, // data Object/String/ArrayBuffer 否 请求的参数 App 3.3.7 以下不支持 ArrayBuffer 类型
|
||||
* header: {}, // header Object 否 设置请求的 header,header 中不能设置 Referer。 App、H5端会自动带上cookie,且H5端不可手动修改
|
||||
* method: 'GET', // method String 否 GET 有效值详见下方说明
|
||||
* timeout: 6000, // timeout Number 否 60000 超时时间,单位 ms H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)、微信小程序(2.10.0)、支付宝小程序
|
||||
* showLoading: true, // 是否显示加载等待框
|
||||
* loadingTitle: '加载中', // 加载等待框的提示文字
|
||||
* showFailMessage: true, // 返回失败信息是否显示
|
||||
* catchError: true, // 是否集中管理返回的success为false的情况,如果为false,在请求处判断code值做业务处理
|
||||
*/
|
||||
const request = (options, noLoading) => {
|
||||
let _opts = {
|
||||
url: '', // url String 是 开发者服务器接口地址
|
||||
data: {}, // data Object/String/ArrayBuffer 否 请求的参数 App 3.3.7 以下不支持 ArrayBuffer 类型
|
||||
header: {}, // header Object 否 设置请求的 header,header 中不能设置 Referer。 App、H5端会自动带上cookie,且H5端不可手动修改 {"content-type": "application/json"}
|
||||
method: 'GET', // method String 否 GET 有效值详见下方说明
|
||||
timeout: 60000, // timeout Number 否 60000 超时时间,单位 ms H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)、微信小程序(2.10.0)、支付宝小程序
|
||||
// dataType String 否 json 如果设为 json,会尝试对返回的数据做一次 JSON.parse
|
||||
// responseType String 否 text 设置响应的数据类型。合法值:text、arraybuffer 支付宝小程序不支持
|
||||
// sslVerify Boolean 否 true 验证 ssl 证书 仅App安卓端支持(HBuilderX 2.3.3+),不支持离线打包
|
||||
// withCredentials Boolean 否 false 跨域请求时是否携带凭证(cookies) 仅H5支持(HBuilderX 2.6.15+)
|
||||
// firstIpv4 Boolean 否 false DNS解析时优先使用ipv4 仅 App-Android 支持 (HBuilderX 2.8.0+)
|
||||
// success Function 否 收到开发者服务器成功返回的回调函数
|
||||
// fail Function 否 接口调用失败的回调函数
|
||||
// complete Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
|
||||
showLoading: !noLoading, // 是否显示加载等待框
|
||||
loadingTitle: '加载中', // 加载等待框的提示文字
|
||||
showFailMessage: true, // 返回失败信息是否显示
|
||||
catchError: true, // 是否集中管理Catch,
|
||||
}
|
||||
Object.assign(_opts, options)
|
||||
let _token = token.getToken()
|
||||
let _baseUrl = config.baseUrl
|
||||
let _tokenName = config.tokenName
|
||||
if (_tokenName) {
|
||||
_opts.header[_tokenName] = _token
|
||||
} else {
|
||||
_opts.header['Authorization'] = _token
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (_opts.showLoading) {
|
||||
uni.showLoading({
|
||||
title: _opts.loadingTitle || '加载中',
|
||||
mask: true
|
||||
})
|
||||
}
|
||||
return uni.request({
|
||||
url: (_baseUrl || '') + (_opts.url || ''),
|
||||
method: _opts.method,
|
||||
data: _opts.data || null,
|
||||
header: _opts.header,
|
||||
timeout: _opts.timeout || 60000,
|
||||
success: res => {
|
||||
if (200 === res.statusCode) {
|
||||
let data = res.data
|
||||
if (_opts.catchError) {
|
||||
if (data.success) {
|
||||
resolve(data.data)
|
||||
} else {
|
||||
let loginTimeoutCode = "5000"
|
||||
let loginTimeoutPage = "/pages/login/index"
|
||||
if (config.loginTimeoutCode) {
|
||||
loginTimeoutCode = "" + config.loginTimeoutCode
|
||||
}
|
||||
if (config.loginTimeoutPage) {
|
||||
loginTimeoutPage = config.loginTimeoutPage
|
||||
}
|
||||
if (loginTimeoutCode === data.code) {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '登录过期或失效,请重新登录!',
|
||||
showCancel: false,
|
||||
success: function(res) {
|
||||
uni.reLaunch({
|
||||
url: loginTimeoutPage
|
||||
})
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (_opts.showFailMessage) {
|
||||
uni.showToast({
|
||||
title: data.msg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
})
|
||||
}
|
||||
}
|
||||
reject(data)
|
||||
}
|
||||
} else {
|
||||
resolve(data)
|
||||
}
|
||||
} else {
|
||||
let em = {
|
||||
"code": "404",
|
||||
"data": "",
|
||||
"msg": "网络请求失败~",
|
||||
"success": false,
|
||||
"timestamp": new Date().getTime()
|
||||
}
|
||||
if (res.data) {
|
||||
if (res.data.error) {
|
||||
if (res.data.path) {
|
||||
em.msg = res.data.path + res.data.error
|
||||
} else {
|
||||
em.msg = res.data.error
|
||||
}
|
||||
}
|
||||
if (res.data.status) {
|
||||
em.code = "" + res.data.status
|
||||
}
|
||||
}
|
||||
if (_opts.showFailMessage) {
|
||||
uni.showToast({
|
||||
title: em.msg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
})
|
||||
}
|
||||
reject(em)
|
||||
}
|
||||
},
|
||||
fail: error => {
|
||||
console.log('request-fail: ', error)
|
||||
let em = {
|
||||
"code": "600",
|
||||
"data": "",
|
||||
"msg": "网络请求失败~",
|
||||
"success": false,
|
||||
"timestamp": new Date().getTime()
|
||||
}
|
||||
if (error.errMsg) {
|
||||
// em.msg = error.errMsg
|
||||
}
|
||||
if (_opts.showFailMessage) {
|
||||
uni.showToast({
|
||||
title: em.msg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
})
|
||||
}
|
||||
reject(em)
|
||||
},
|
||||
complete() {
|
||||
if (_opts.showLoading) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
const req = function(url, method = "GET", data = {}, header = {}, options = {}, noLoading) {
|
||||
let _opts = {
|
||||
url: url,
|
||||
method: method,
|
||||
data: data,
|
||||
header: header
|
||||
}
|
||||
Object.assign(options, _opts)
|
||||
return request(options, noLoading)
|
||||
}
|
||||
|
||||
const get = function(url, data = {}, header = {}, options = {}, noLoading) {
|
||||
return req(url, "GET", data, header, options, noLoading)
|
||||
}
|
||||
const post = function(url, data = {}, header = {}, options = {}, noLoading) {
|
||||
return req(url, "POST", data, header, options, noLoading)
|
||||
}
|
||||
const formpost = function(url, data = {}, header = {}, options = {}, noLoading) {
|
||||
let _head = {
|
||||
"content-type": "application/x-www-form-urlencoded"
|
||||
}
|
||||
Object.assign(header, _head)
|
||||
return req(url, "POST", data, header, options, noLoading)
|
||||
}
|
||||
request.get = get
|
||||
request.post = post
|
||||
request.formpost = formpost
|
||||
request.req = req
|
||||
|
||||
export default request
|
||||
Reference in New Issue
Block a user