api
import {
request
} from '@/utlis/request'
// banner
export function getBanner() {
let res = request('/banner', 'GET')
return res
}
axios
import axios from 'axios'
import {
Toast
} from 'mint-ui'
export const instance = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 20 * 1000
})
// request
instance.interceptors.request.use(config => {
return config
}, error => {
Promise.reject(error)
})
// response
instance.interceptors.response.use(response => {
const res = response.data
return Promise.resolve(res)
}, error => {
Toast(error.message)
return Promise.reject(error)
})
export const request = async (url = '', type = 'GET', data = {}, isForm = false) => {
let result
type = type.toUpperCase()
let requestOptions = {
method: type,
url: url
}
if (isForm) {
let form = new FormData()
Object.keys(data).forEach(key => {
let value = data[key]
if (Array.isArray(value)) {
value.forEach(item => {
form.append(key, item)
})
} else {
form.append(key, data[key])
}
})
data = form
}
requestOptions['headers'] = {
'Content-type': isForm ? 'multipart/form-data' : 'application/json'
}
if (type === 'GET') {
requestOptions['params'] = data
} else {
requestOptions['data'] = data
}
await instance(requestOptions).then(res => {
result = res
})
return result
}
page
<template>
<div class="index">
<!-- banner -->
<music-banner :json='bannerJson'></music-banner>
</div>
</template>
<script>
import MusicBanner from '@/components/MusicBanner'
import {getBanner} from '@/api/index'
export default {
name: 'index',
components: { MusicBanner },
data() {
return{
bannerJson: []
}
},
mounted() {
this.getBannerData()
},
methods: {
getBannerData(){
getBanner().then(res => {
the.bannerJson = res.data
})
}
}
}
</script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…