创建server.js请求路径
const ENV = "dev"
let origin = "" ;
switch ( ENV ) {
case "dev" :
origin = ""
break
case "test" :
origin = ""
break
case "prd" :
origin = ""
break
}
export default { origin }
创建http.js请求方法
import axios from "axios"
import server from "@/utils/server"
function get ( url, params = { } ) {
return request ( 'get' , url, params)
}
function post ( url, params = { } ) {
return request ( 'post' , url, params)
}
function request ( methods, url, params) {
let config = {
method: methods,
url: server. origin + url,
withCredentials: true ,
}
switch ( methods) {
case 'get' :
config. params = params
break
case 'post' :
config. data = params
break
}
config. headers = { }
return new Promise ( ( resolve, reject) => {
axios ( config) . then ( res => {
if ( ! res || ! res. data) {
reject ( { code: 500 , msg: "服务器异常" } )
return
}
const data = res. data
const code = Number ( data. code)
switch ( code) {
case 10001 :
resolve ( data. data)
break
case - 99999 :
break
default :
reject ( { code, msg: data. msg } )
break
}
} )
} )
}
export default {
get ,
post
}
请求接口
import http from "@/utils/http"
const AAA = "/aaa"
const BBB = "/bbb"
export function aaaGet ( params) {
return http. get ( AAA , params) . then ( res => {
return res
} )
}
export function bbbPost ( params) {
return http. post ( BBB , params) . then ( res => {
return res
} )
}