uni-app封装的request请求

uni-app封装的request请求_第1张图片

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

// 正式
// 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('saas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il0sImFtciI6WyJ0ZW5hbnQiXX0.ooJg6R5-DipN63dozo3odHALH3C6kslwjWHovoXoF5HRwmh3u2wSzAHyIAkhyDTQczqUrIaZePesZYhe9iIyJBuKkfkiR9ToV9zYfAW81-M0JokriKK4zwXfgtnCtzM_0CzPCNkXBzkUYbAWhl4VuWAKwCoJN_iP19yhK4w37ZfZbhUKykL6VEt12x0Xhp31HcoXadlMuKmILwjQV_OO6X_WPtAgmmSxNLcTAMA4ZpuQhcGmiY17e3uHBfkPKP4IeNyIN6qQyMRzHk1UbG-_VhUabRg3AEk96HLhqX8nQl2TTkTQZGqpo-4eAlxDgh2suGpZh2vzwgyquesR2s-mdw"
	//#endif
}
export {
	uacUrl,
	baseUrl,
	token,
	userUrl,
	imgUrl
}

 

 main.js

// request 封装
import request from 'common/request.js'
Vue.prototype.$request = request

页面使用:

			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)
				})
					
			},

你可能感兴趣的:(uni-app,java,前端)