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)
				})
					
			},
相关推荐
前端.火鸡1 分钟前
认识vue中的install和使用场景
前端·javascript·vue.js
橘猫云计算机设计3 分钟前
ASP.NET图书馆借阅系统(源码+lw+部署文档+讲解),源码可白嫖!
java·数据库·后端·爬虫·小程序·毕业设计·asp.net
念九_ysl8 分钟前
Java中的列表(List):操作与实现详解
java·开发语言·list
摆烂工程师8 分钟前
Grok3 支持 DeeperSearch 的免费可用次数的查询了
前端·后端·程序员
pubuzhixing9 分钟前
在线白板 - 如何从零实现一个自由画笔
前端·开源
一只小透明啊啊啊啊1 小时前
【leetcode 100】贪心Java版本
java·算法·leetcode
hhw1991121 小时前
vue总结
前端·javascript·vue.js
学习2年半1 小时前
汇丰eee2
前端·spring
代码续发1 小时前
Vue进行前端开发流程
前端·vue.js
zpjing~.~1 小时前
CSS &符号
前端·css