1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| import axios from 'axios'
Date.prototype.Format = function (fmt) { let o = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S": this.getMilliseconds() } if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)) for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))) return fmt }
axios.interceptors.response.use(response => { return response.data }, error => { return error.data }) axios.defaults.withCredentials = false
function checkStatus(response) { if (response && (response.status === 200 || response.status === 304 || response.status === 400)) { return response } return { status: -404, msg: '网络异常' } }
function checkCode(res) { if (res.status === -404) { console.log(res.msg) } if (res.data && (!res.data.success)) { } return res }
function timestampToTime(timestamp) { let date = new Date(timestamp), Y = date.getFullYear() + '-', M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-', D = date.getDate() + ' ', h = date.getHours() + ':', m = date.getMinutes() + ':', s = date.getSeconds() return Y + M + D + h + m + s }
export default { post(url, data) { return axios({ method: 'post', url, data, timeout: 15000, headers: { 'X-Requested-With': 'XMLHttpRequest', 'content-type': 'application/json;charset=UTF-8' } }).then( (response) => { return response } ).then( (res) => { return res } ) },
upload(url, data) { let form = new FormData() Object.keys(data).forEach(value => { form.append(value, data[value]) }) return axios({ method: 'post', url, data, transformRequest: [function (data) { let ret = '' for (let it in data) { ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' } return ret }], timeout: 15000, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then( (response) => { return response } ).then( (res) => { return res } ) },
get(url, params) { return axios({ method: 'get', url, params, timeout: 15000 }).then( (response) => { return response } ).then( (res) => { return res } ) }, put(url, data) { return axios({ method: 'put', baseURL: process.env.BASE_API, url, data, timeout: 15000 }).then( (response) => { return response } ).then( (res) => { return res } ) }, time(tamp) { return timestampToTime(tamp) } }
|