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)
				})
					
			},
相关推荐
yinuo1 小时前
前端跨页面通讯终极指南⑥:SharedWorker 用法全解析
前端
CoderYanger5 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
C++业余爱好者5 小时前
Java 提供了8种基本数据类型及封装类型介绍
java·开发语言·python
想用offer打牌5 小时前
RocketMQ如何防止消息丢失?
java·后端·架构·开源·rocketmq
皮卡龙5 小时前
Java常用的JSON
java·开发语言·spring boot·json
PineappleCoder5 小时前
还在重复下载资源?HTTP 缓存让二次访问 “零请求”,用户体验翻倍
前端·性能优化
拉不动的猪6 小时前
webpack编译中为什么不建议load替换ast中节点删除consolg.log
前端·javascript·webpack
李姆斯6 小时前
Agent时代下,ToB前端的UI和交互会往哪走?
前端·agent·交互设计
利刃大大6 小时前
【JavaSE】十三、枚举类Enum && Lambda表达式 && 列表排序常见写法
java·开发语言·枚举·lambda·排序
float_六七6 小时前
Java反射:万能遥控器拆解编程
java·开发语言