1、新建文件 /common/request.js
let baseURL = '';
function myRequest(options) {
let token = options.header ? options.header["token"] : uni.getStorageSync('token');
return new Promise((res, rej) => {
uni.request({
url: options.url,
method: options.method,
data: options.data,
dataType: 'json',
header: {
"Content-Type": 'application/x-www-form-urlencoded',
"token": token
},
success(result) {
res(result.data)
},
fail(e) {
rej()
}
})
})
}
export default myRequest;
2、在main.js
中引入
import myRequest from '@/common/request.js';
Vue.prototype.$myRequest = myRequest;
3、使用
async functionName(e) {
let result = await this.$myRequest({
url: '',
method: 'GET',
data: {}
});
}