uni-app封装的request请求

javascript 复制代码
import {
	baseUrl,
	token
} from '../common/config.js'

const request = (url = '', data = {}, type = 'GET', header = {}, costomUrl = '') => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: type,
			url: costomUrl ? `${costomUrl}` : `${baseUrl + url}`,
			data: data,
			header: {
				...header,
				Authorization: token()
			},
			dataType: 'json',
		}).then((response) => {
			let [error, res] = response;

			if (res.statusCode == 400) {
				uni.$u.toast("请求参数有误,检查后再次请求");
			}
			if (res.statusCode == 401 && uni.getStorageSync('saas_access_token')) {
				uni.navigateTo({
					url: '/pages/specialpage/401'
				})
			}
			if (res.statusCode == 403) {
				uni.navigateTo({
					url: '/pages/specialpage/403'
				})
			}
			if (res.statusCode == 404) {
				uni.$u.toast("未找到数据")
			}
			if (res.statusCode == 500) {
				const str = res.data.Error.Message;
				var pattern = new RegExp("[\u4E00-\u9FA5]+");
				if (pattern.test(str)) {
					uni.$u.toast(str);
				} else {
					uni.$u.toast("系统异常,请联系管理员");
					// uni.navigateTo({
					// 	url: '/pages/specialpage/500'
					// })
				}
			}
			resolve(res);
		}).catch(error => {

			let [err, res] = error;
			reject(err, res)
		})
	});
}
export default request

config.js

javascript 复制代码
// 正式
// const baseUrl = 'https://esm.api.si-iip.com' //https://esmapi.dev.si-iip.com
// const uacUrl = 'https://uac.api.si-iip.com' //https://newuacapi.dev.si-iip.com
// const userUrl = 'https://uac.api.si-iip.com'  //https://newuacapi.dev.si-iip.com
// const imgUrl = 'https://linkfile.api.si-iip.com' //https://linkfileapi.dev.si-iip.com
// 测试
const baseUrl = 'https://esmapi.dev.si-iip.com' //https://esmapi.dev.si-iip.com
const uacUrl = 'https://newuacapi.dev.si-iip.com' //https://newuacapi.dev.si-iip.com
const userUrl = 'https://newuacapi.dev.si-iip.com'  //https://newuacapi.dev.si-iip.com
const imgUrl = 'https://linkfileapi.dev.si-iip.com' //https://linkfileapi.dev.si-iip.com
function token(){
	// #ifdef APP-PLUS
		return 'Bearer '+uni.getStorageSync('sa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Il0sImFtciI6WyJ0ZW5hbnQiXX0.ooJg6R5-DipN63dozo3odHALH3C6kslwjWHovoXoF5HRwmh3u2wSzAHyIAkhyDTQczqUrIaZePesZYhe9iIyJBuKkfkiR9ToV9zYfAW81-M0JokriKK4zwXfgtnCtzM_0CzPCNkXBzkUYbAWhl4VuWAKwCoJN_iP19yhK4w37ZfZbhUKykL6VEt12x0Xhp31HcoXadlMuKmILwjQV_OO6X_WPtAgmmSxNLcTAMA4ZpuQhcGmiY17e3uHBfkPKP4IeNyIN6qQyMRzHk1UbG-_VhUabRg3AEk96HLhqX8nQl2TTkTQZGqpo-4eAlxDgh2suGpZh2vzwgyquesR2s-mdw"
	//#endif
}
export {
	uacUrl,
	baseUrl,
	token,
	userUrl,
	imgUrl
}

main.js

javascript 复制代码
// request 封装
import request from 'common/request.js'
Vue.prototype.$request = request

页面使用:

javascript 复制代码
			async getData() {
				this.status = "loading"
				let res;
				res= await this.$request('',{
						PageNumber: this.PageNumber,
						MaxResultCount: this.MaxResultCount,
						SearchText: this.SearchText,
					},'GET',{},`${uacUrl}/api/app/tenant-user/tenant-users-by-tenant-id`)
					.then((res)=>{
						this.TotalCount = +res.data.totalCount
						this.indexList = this.indexList.concat(res.data.items ? res.data.items : res.data)
						if (this.indexList.length == 0 || this.indexList.length == this.TotalCount || this.TotalCount <
							this.PageNumber * this.MaxResultCount) {
							this.status = 'nomore'
						}
					}).catch(err=>{
						this.status = 'nomore'
						console.log(err)
				})
					
			},
相关推荐
gqkmiss25 分钟前
Chrome 浏览器插件获取网页 iframe 中的 window 对象
前端·chrome·iframe·postmessage·chrome 插件
_oP_i1 小时前
Pinpoint 是一个开源的分布式追踪系统
java·分布式·开源
mmsx1 小时前
android sqlite 数据库简单封装示例(java)
android·java·数据库
武子康1 小时前
大数据-258 离线数仓 - Griffin架构 配置安装 Livy 架构设计 解压配置 Hadoop Hive
java·大数据·数据仓库·hive·hadoop·架构
豪宇刘2 小时前
MyBatis的面试题以及详细解答二
java·servlet·tomcat
秋恬意2 小时前
Mybatis能执行一对一、一对多的关联查询吗?都有哪些实现方式,以及它们之间的区别
java·数据库·mybatis
m0_748247552 小时前
Web 应用项目开发全流程解析与实战经验分享
开发语言·前端·php
m0_748255023 小时前
前端常用算法集合
前端·算法
FF在路上3 小时前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
真的很上进3 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html