Recently, a project was made with uniapp, but uniapp’s uni.request({}) does not have request interception, so I tried to encapsulate it. The background of encapsulation is that many requests of the project need to carry token. Below is the request.js file

const baseURL='https://story.genielink.cn'
const http=(options) = >{
	const token=uni.getStorageSync('token')
	// If the token does not exist jump to the personal page and let the user log in
	if(! token){ uni.navigateTo({url:'.. /people/people_main'
		})
		return ;
	}else{
		return new Promise((resolve,reject) = >{
			uni.request({
				url:baseURL+options.url,
				// If method is not present in the request URL, the default is get
				method:options.methods||'GET'.// If there is no data in the request URL, it defaults to null
				data: options.data || {},
				header: {Authorization:token},
				success:res= >{
					if(! res){return uni.showToast({icon:'loading'.title:'Failed to get data'})}console.log(res.data)
					resolve(res)
				},
				fail:err= >{
					return uni.showToast({
						icon:'loading'.title:'Request failed'
					})
					reject(err)
				}
			})
		})
	}
} 
Copy the code

Then mount it globally in main.js,

import http from 'pages/utils/request.js'

Vue.prototype.$http=http

This.$HTTP ({url:,method:})