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

4
src/api/home.js

@ -4,7 +4,7 @@ import request from '@/utils/request'
// 定义接口的传参 // 定义接口的传参
const api={ const api={
// 日客流趋势 // 日客流趋势
daily_klqs:'index.php/allGroupTimeNum?granularity=daily' daily_klqs:'pass.passcc/allGroupTimeNum'
} }
// 获取群组 // 获取群组
@ -12,6 +12,6 @@ export function allGroupTimeNum(param) {
return request({ return request({
url: api.daily_klqs, url: api.daily_klqs,
method: 'get', 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 router from './router'
import store from './store' 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 //https://www.jb51.net/article/246548.htm
//站点配置
const publicConfig = window.publicConfig
// 创建一个 axios 实例 // 创建一个 axios 实例
const service = axios.create({ const service = axios.create({
//baseURL: '/api', // 所有的请求地址前缀部分 //baseURL: '/api', // 所有的请求地址前缀部分
//baseURL: publicConfig.BASE_API, baseURL: publicConfig.BASE_API,
//baseURL:ahost, //baseURL:ahost,
timeout: 60000, // 请求超时时间毫秒 timeout: 60000, // 请求超时时间毫秒
withCredentials: true, // 异步请求携带cookie withCredentials: true, // 异步请求携带cookie
@ -30,13 +33,16 @@ service.interceptors.request.use(
// 添加响应拦截器 // 添加响应拦截器
service.interceptors.response.use( service.interceptors.response.use(
function (response) { function (response) {
//console.log(response) console.log(response)
// 2xx 范围内的状态码都会触发该函数。 // 2xx 范围内的状态码都会触发该函数。
// 对响应数据做点什么 // 对响应数据做点什么
// dataAxios 是 axios 返回数据中的 data // dataAxios 是 axios 返回数据中的 data
const dataAxios = response.data const dataAxios = response.data
// 这个状态码是和后端约定的 // 这个状态码是和后端约定的
const code = dataAxios.reset const code = dataAxios.reset
if(code!='1'){
return Promise.reject(response)
}
return dataAxios return dataAxios
}, },
function (error) { function (error) {

10
src/views/HomeView.vue

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

Loading…
Cancel
Save