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)
				})
					
			},
相关推荐
4277240034 分钟前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo1 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦1 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个1 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
晴空万里藏片云1 小时前
elment Table多级表头固定列后,合计行错位显示问题解决
前端·javascript·vue.js
曦月合一1 小时前
html中iframe标签 隐藏滚动条
前端·html·iframe
奶球不是球1 小时前
el-button按钮的loading状态设置
前端·javascript
kidding7232 小时前
前端VUE3的面试题
前端·typescript·compositionapi·fragment·teleport·suspense
Σίσυφος19004 小时前
halcon 条形码、二维码识别、opencv识别
前端·数据库
乔冠宇4 小时前
微信小程序修改个人信息头像(uniapp开发)
微信小程序·小程序·uni-app