当前位置:网站首页>封装 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()
边栏推荐
猜你喜欢

单片机——DHT11 温湿度传感器

同一行div或者其他行间块状标签,垂直高度不一解决办法

Property or method “XXX“ is not defined on the instance but referenced during render.

Non-resolvableparent POM for xxxx: Could not find artifact xxx and ‘parent.relativePath‘ points at

Centos下载安装redis- 使用yum

神经网络论文Enhancing deep neural networks via multiple kernel learning

用js写一个简单的前世今生

单片机--IIC总线篇
![[highcharts application - double pie chart]](/img/e9/e580d0e7dfcd00be137140abfe8569.png)
[highcharts application - double pie chart]

Everything原理探究以及C#实现
随机推荐
二叉树前序遍历、中序遍历、后序遍历的迭代版
AtCoder Beginner Contest 263(A~F)
IO操作
【highcharts应用-双饼图】
charles简单使用
目标检测论文 Precise detection of Chinese characters in historical documents with DRL
去噪论文 Attention-Guided CNN for Image Denoising
各类测试场景的检查点
去噪论文 Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising
GeoServer入门学习:06-发布多层级TIF地图大图数据
[MEF]第04篇 MEF的多部件导入(ImportMany)和目录服务
修改浏览器滚动条样式
GeoServer入门学习:05-多层级MBTiles规范数据发布
pm2安装配置与基本命令你知道吗?
go实现快速排序
记录非Gui模式Jmeter使用
a标签的三种禁止跳转方式
【梦想的声音】
使用LBP特征进行图像分类
day12 Elasticserach