import axios from 'axios'
import qs from 'qs'
let cancel ,promiseArr = {}
import * as _ from '../util/tool'
// axios 配置
axios.defaults.timeout = 5000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.headers.token='eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MjE2MDE2NjIsInN1YiI6IntcImVudF9pZFwiOjIzMTgsXCJ1c2VyX2lkXCI6MTY5MSxcInVzZXJfbmFtZVwiOlwiY2hlbnNodXdlblwiLFwibW9iaWxlXCI6bnVsbCxcIm9wZW5faWRcIjpudWxsfSJ9.dukJf6v-_4Iii02LNEPw3IsXtPaigw5rvznpYZRkOME'
axios.defaults.baseURL = 'http://localhost:4000/';
//POST传参序列化
axios.interceptors.request.use((config) => {
if(config.method === 'post'){
config.data = qs.stringify(config.data);
}
if (promiseArr[config.url]) {
promiseArrconfig.url
promiseArr[config.url] = cancel
} else {
promiseArr[config.url] = cancel
}
return config;
},(error) =>{
_.toast("错误的传参", 'fail');
return Promise.reject(error);
});
//返回状态判断
axios.interceptors.response.use((res) =>{
/这里会有坑的看返回的数据格式 才能正确走相应状态/
if(!res.data.data.success){
// _.toast(res.data.msg);
return Promise.reject(res);
}else{
return Promise.resolve(res.data)
}
}, (error) => {
_.toast("网络异常", 'fail');
return Promise.reject(error);
});
export function fetchGet(url) {
return new Promise((resolve, reject) => {
axios.get(url)
.then(response => {
resolve(response.data);
}, err => {
reject(err);
})
.catch((error) => {
reject(error)
})
})
}
export function fetch(url, params) {
return new Promise((resolve, reject) => {
axios.post(url, params)
.then(response => {
console.log(response)
resolve(response.data.data);
}, err => {
reject(err);
})
.catch((error) => {
reject(error)
})
})
}
export default {
/**
* 用户登录
*/
My(params) {
return fetchGet('http://dev.ent.hongyunbang.net/hyb-ent/ent_info/wx/qr_detail',params)
},
Login(params) {
return fetch('/users/api/userLogin', params)
},
/**
* 用户注册
*/
Regist(params) {
return fetch('/users/api/userRegist', params)
},
/**
* 发送注册验证码
*/
RegistVerifiCode(tellphone) {
return fetch('/users/api/registVerifiCode', {tellphone: tellphone})
},
/**
* 获取约跑步列表
*/
SportsList() {
return fetch('/api/sportList')
},
/**
* 获取约出行列表
*/
TravelsList() {
return fetch('/api/travelList')
},
/**
* 获取约跑步详情
*/
SportsDetail(id) {
return fetch('/api/sportDetail', {sportId: id})
},
/**
* 获取约出行详情
*/
TravelsDetail(id) {
return fetch('/api/travelDetail', {travelId: id})
},
/**
* 获取出行活动点击次数
*/
travelClicks(id) {
return fetch('/api/travelClickNum', {travelId: id})
},
/**
* 获取用户信息
*/
UserInfo(id) {
return fetch('/users/api/userInfo', {userId: id})
},
/**
* 获取用户发布约行个数
*/
getPubTotravelNum(id) {
return fetch('/users/api/getPubTotravelNum', {userId: id})
},
/**
* 获取用户自己发布的约行
*/
getMyTravel(id) {
return fetch('/users/api/myTravel', {userId: id})
},
/**
* 发布约行活动
*/
PostTravel(params) {
return fetch()
},
/**
* 获取全国JSON数据
*/
getAddressJson() {
return fetch('/api/address')
}
}