uniapp封装请求工具

import useStore from '@/store'
const baseURL = `http://localhost:3000`



class Server{
	Request(options){
      const { url,method,params,timeout,header } = options
      return new Promise((resolve,reject)=>{
		  uni.request({
		  	url:baseURL + url,
		  	data:params,
		  	method:method,
		  	timeout:timeout ? timeout : 10000,
			header: header,
			success:(res)=>{
				resolve(res.data)
			},
			fail:(err)=>{
				uni.showToast({ title:'请求错误',icon:'error' })
				reject(err)
			}
		  })
	  })
	}
}

const request  = (options)=>{
	const store = useStore()
	const header = {
		'Content-Type':'application/json',
		'authorization': store.token
	}
	return new Server().Request({
		url:options.url,
		params:options.params,
		method:options.method ? options.method :'GET',
		header:options.header ? options.header :header
	})
}

export default request

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