import env from '@/api/config.js'
import moment from 'dayjs'
//url 对应请求路径 parmas对象请求的参数 method对应请求方法 loadimg对应是否显示加载中 boolean
const request = (url, parmas, method, loading = false, nostatus = false) => {
if (!nostatus) {
if (!uni.getStorageSync('token') || moment().valueOf() - moment(uni.getStorageSync('last_time')).valueOf() >= 82800000) {
uni.navigateTo({
url: '/pages/login/login'
})
setTimeout(() => {
uni.showToast({
title: '登陆过期!',
icon: 'none',
duration: 2000,
})
}, 300)
return
}
}
const obj = {
GET: {
"Content-type":'application/x-www-form-urlencoded;charset=utf-8'
},
POST: {
"Content-type":'application/json'
}
}
return new Promise((resolve, reject) => {
if (loading) {
uni.showLoading({ mask: true, title: '加载中' })
}
uni.request({
// url: env.mainUrl + url,
url: '/api' + url,
data: parmas,
header: {
// 'Content-Type': obj[method],
'request_origin': 'app',
'access_token': uni.getStorageSync('token'),
},
timeout: 60000,
// uni自带的request 会根据post或者get 对content type 设置
method: method,
success: (res) => {
if (res.statusCode !== 200) {
uni.showToast({
title: res.errMsg,
icon: 'none',
duration: 2000
})
}
if (res.data.status !== 200) {
uni.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
})
}
resolve(res)
},
fail: (err) => {
uni.showToast({
title: "网络连接超时",
icon: 'none',
duration: 3000,
})
reject(err)
},
complete: () => {
if (loading) {
uni.hideLoading()
}
}
})
});
}
export { request }
Uniapp-H5端封装request请求
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 基于uniapp适配h5,微信小程序,app的网络请求的封装 1.创建request.js文件,配置如下: 引入c...
- 以下内容直接放入main.js中,方法都是挂载在vue原型上的,使用this直接调用 import Vue fro...
- 1.创建一个request.js文件 const baseUrl = ' '//定义一个请求地址 const re...