当前位置:网站首页>封装 uniapp request 请求
封装 uniapp request 请求
2022-08-08 21:10:00 【前端开发工程师在杭州】
封装 uniapp request 请求
'use strict';
class Request {
config = {
debug: false,
baseurl: '',
encoding: 'utf-8',
content_type: 'json',
loading: 'nav-bar',
loading_duration: 0,
data: {
},
header: {
}
}
req_config = {
url: '',
data: {
},
header: {
},
method: 'GET',
timeout: 30000,
dataType: 'json',
responseType: 'text'
}
static isurl(url) {
return /(http|https):\/\/([\w.]+\/?)\S*/.test(url);
}
static geturl(config) {
return Request.isurl(config.url) ? (config.url || '') : (config.baseurl + config.url);
}
static get_content_type(config) {
switch (config.content_type){
case 'json':
return 'application/json';
case 'form':
return 'application/x-www-form-urlencoded';
case 'file':
return 'multipart/form-data';
case 'text':
return 'text/plain';
case 'html':
return 'text/html';
default:
throw new Error('content type error: ' + type);
}
}
/** * 拦截器 * */
interceptor = {
request: undefined,
response: undefined,
fail: undefined
}
/** * request 请求 * */
// 定义了一个默认参数
request(options = {
}) {
let that = this;
let config = Object.assign({
}, this.config, options);
config.url = Request.geturl(config);
if (!config.header['Content-Type']) {
config.header['Content-Type'] = Request.get_content_type(config);
}
if (typeof that.interceptor.request === 'function') {
config = that.interceptor.request(config);
}
let task = undefined
let promise = new Promise((resolve, reject) => {
let extras = {
}
that._prepare(that, config, extras)
if (config.content_type === 'file') {
task = uni.uploadFile({
...config,
success: res => {
that._success(that, config, res, resolve, reject)
},
fail: res => {
that._fail(that, config, res, resolve, reject)
},
complete: (res) => {
that._complete(that, config, res, extras)
}
})
if (typeof config.progress === 'function') {
task.onProgressUpdate(_res => {
config.progress(_res, task)
})
}
} else {
this._set_req_config(config);
task = uni.request({
...this.req_config,
success: res => {
that._success(that, config, res, resolve, reject)
},
fail: res => {
that._fail(that, config, res, resolve, reject)
},
complete: (res) => {
that._complete(that, config, res, extras)
}
})
}
})
if (config.success || config.fail || config.complete) {
return task;
}
return promise;
}
get(options = {
}) {
options.method = 'GET'
return this.request(options)
}
post(options = {
}) {
options.method = 'POST'
return this.request(options)
}
put(options = {
}) {
options.method = 'PUT'
return this.request(options)
}
delete(options = {
}) {
options.method = 'DELETE'
return this.request(options)
}
upload(options = {
}) {
options.method = 'POST'
options.content_type = 'file'
return this.request(options)
}
_success = function(that, config, res, resolve, reject) {
if (config.debug) {
console.log('response success res: ', res)
}
if (res.statusCode >= 200 && res.statusCode <= 302) {
let result = res.data
if (typeof result !== 'object') {
try{
result = JSON.parse(res.data);
}catch(e){
}
}
let is_success = !0;
if (typeof that.interceptor.response === 'function' && !config.skip_interceptor_response) {
is_success = that.interceptor.response(result, config)
}
if (is_success) {
if (config.debug) {
console.log('response success: ', result)
}
config.success ? config.success(result) : resolve(result)
return;
}
return;
}
that._fail(that, config, res, resolve, reject)
}
_fail = function(that, config, res, resolve, reject) {
if (config.debug) {
console.error('response failure: ', res)
}
if (res.errMsg === 'request:fail abort') {
return
}
if (typeof that.interceptor.fail === 'function') {
res = that.interceptor.fail(res, config)
}
config.fail ? config.fail(res) : reject(res)
}
_prepare = function(that, config, obj = {
}) {
obj.startTime = Date.now()
if (config.loading) {
if (config.loading === 'nav-bar') {
uni.showNavigationBarLoading();
} else {
uni.showLoading({
title: config.loading
})
}
}
if (config.content_type === 'file') {
if (config.formData === undefined || config.formData === null) {
config.formData = config.data
delete config.data
}
config.method = 'POST'
}
if (config.debug) {
console.log('request: ', config)
}
}
_complete = function(that, config, res, obj = {
}) {
obj.endTime = Date.now()
let diff = obj.endTime - obj.startTime;
if (config.debug) {
console.log('request completed in ' + (obj.endTime - obj.startTime) + ' ms')
}
if (config.loading) {
let duration = config.loading_duration || 0
if (diff < duration) {
diff = duration - diff
} else {
diff = 0
}
let __timer = setTimeout(function() {
if (config.loading === 'nav-bar') {
uni.hideNavigationBarLoading()
} else {
uni.hideLoading()
}
clearTimeout(__timer);
}, diff)
}
}
_set_req_config = function(config){
for(let key in this.req_config){
if (config.hasOwnProperty(key)){
this.req_config[key] = config[key]
}
}
}
}
export default new Request()
边栏推荐
猜你喜欢
神经网络论文Enhancing deep neural networks via multiple kernel learning
【项目经验】--环保项目
ssh 登录connectction reset by peer
2 Scrapy
阿里云祝顺民:算力网络架构的新探索
GeoServer Getting Started Learning: 06-Publishing Multi-level TIF Map Data
SIGIR 2022 | MCCLK: 一个用于知识感知推荐的多层次的交叉视图对比框架
C#实现Everything——UI与查询 附源码
GeoServer introductory learning: 05-Multi-level MBTiles specification data release
360杜跃进ISC演讲:保障信创软件的可信性和安全性是信创安全体系的基础
随机推荐
目标检测论文 Bridng the Gap Between Anchor-based and Anchor-free Detection via ATSS
sudo控制用户权限实战操作
keras调用load_model时报错ValueError: Unknown layer:*解决办法
【时间戳转普通时间格式的方法】
GeoServer Getting Started Learning: 06-Publishing Multi-level TIF Map Data
单片机——DHT11 温湿度传感器
修改浏览器滚动条样式
SIGIR 2022 | MCCLK: 一个用于知识感知推荐的多层次的交叉视图对比框架
蚂蚁感冒,蓝桥杯,简易AC代码讲解
Jenkins下载安装
jmeter逻辑控制器使用
小程序-按钮透明无边框
【Export PDF-Project Application】
分别用BeautifulSoup和scrapy爬取某一城市天气预报
pytorch实现数据集读取/下载
Centos下载安装redis- 使用yum
Idea修改全部变量名
H5页面调用手机打电话功能
使用LBP特征进行图像分类
C 预处理器