Browse Source

引入axios网络请求

master
453530270@qq.com 3 years ago
parent
commit
ef1e983073
  1. 4
      public/config.js
  2. 1
      public/index.html
  3. 4
      src/api/home.js
  4. 5
      src/config/public.js
  5. 12
      src/main.js
  6. 10
      src/utils/request.js
  7. 10
      src/views/HomeView.vue

4
public/config.js

@ -0,0 +1,4 @@
window.publicConfig = {
// 请求地址
BASE_API:'http://btgym.xingtongworld.com/index.php?s=/api/',
}

1
public/index.html

@ -31,6 +31,7 @@
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<script type="text/javascript" src="config.js"></script>
<!-- built files will be auto injected -->
</body>
</html>

4
src/api/home.js

@ -4,7 +4,7 @@ import request from '@/utils/request'
// 定义接口的传参
const api={
// 日客流趋势
daily_klqs:'index.php/allGroupTimeNum?granularity=daily'
daily_klqs:'pass.passcc/allGroupTimeNum'
}
// 获取群组
@ -12,6 +12,6 @@ export function allGroupTimeNum(param) {
return request({
url: api.daily_klqs,
method: 'get',
param,
params:param,
})
}

5
src/config/public.js

@ -0,0 +1,5 @@
window.publicConfig = {
// 请求地址
BASE_API: 'http://btgym.xingtongworld.com/index.php?s=/api/',
}

12
src/main.js

@ -3,4 +3,14 @@ import App from './App.vue'
import router from './router'
import store from './store'
createApp(App).use(store).use(router).mount('#app')
//axios
import axios from 'axios'
const app = createApp(App)
app.use(router)
app.provide('$axios', axios)
app.mount("#app")
// createApp(App).use(store)
// .use(router)
// .mount('#app')

10
src/utils/request.js

@ -2,10 +2,13 @@ import axios from 'axios'
//https://www.jb51.net/article/246548.htm
//站点配置
const publicConfig = window.publicConfig
// 创建一个 axios 实例
const service = axios.create({
//baseURL: '/api', // 所有的请求地址前缀部分
//baseURL: publicConfig.BASE_API,
baseURL: publicConfig.BASE_API,
//baseURL:ahost,
timeout: 60000, // 请求超时时间毫秒
withCredentials: true, // 异步请求携带cookie
@ -30,13 +33,16 @@ service.interceptors.request.use(
// 添加响应拦截器
service.interceptors.response.use(
function (response) {
//console.log(response)
console.log(response)
// 2xx 范围内的状态码都会触发该函数。
// 对响应数据做点什么
// dataAxios 是 axios 返回数据中的 data
const dataAxios = response.data
// 这个状态码是和后端约定的
const code = dataAxios.reset
if(code!='1'){
return Promise.reject(response)
}
return dataAxios
},
function (error) {

10
src/views/HomeView.vue

@ -151,6 +151,7 @@
<script>
import * as echarts from 'echarts';
import * as btApi from '../api/home'
export default {
name: 'HomeView',
@ -164,9 +165,18 @@ export default {
this.showorders()
this.cgrs_line()
this.cgchart()
//
this.btgetdata()
},
//
methods: {
//
btgetdata(){
//
btApi.allGroupTimeNum({'granularity':'daily'}).then(res=>{
console.log(res)
});
},
//
selitem(type) {
this.klselt = type;

Loading…
Cancel
Save