request.js
export default function(config) {
const {baseURL} = config;
return function (Vue) {
Vue.prototype.http= async function (params) {
const {url ,method, data,headers} = params
uni.showLoading({
title:'加载中...',
mask: 'true'
})
const header = {
"content-type": "application/x-www-form-urlencoded",
"ticket": uni.getStorageSync("ticket"),
"cookie": "JSESSIONID=" + uni.getStorageSync("sessionid")
};
const res = await uni.request({
url: baseURL+url,
method,
data,
header: headers || header
})
uni.hideLoading();
return res[1]
}
}
}
main.js
import Vue from 'vue'
import App from './App'
import request from '@/utils/request'
Vue.config.productionTip = false
App.mpType = 'app'
const plugin = request({
baseURL: 'https://***.***com/TSY'
});
Vue.use(plugin)
const app = new Vue({
...App
})
app.$mount()
使用请求
this.http({
url:'/api/pubshare/testPaper/getListJsonData',
method:'post',
data:{},
}).then(res => {
console.log('试卷',res)
if(res.type == 'success'){
this.shijuan = res.datas
}
}).catch(err =>{
uni.showToast({
title:'请求失败'
})
})