🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## Axios 使用方式 在根项目/src/main.js 进行全局引用 ~~~ import Axios from "axios" Vue.prototype.$axios = Axios import qs from "qs" Vue.prototype.HOST = '/v1'//调取跨域 config/index ~~~ ~~~ //请求封装 import helper from "../build/helper.js"; Vue.prototype.helper = helper ~~~ ~~~ // 添加请求拦截器 Axios.interceptors.request.use(function (config) { if (config.method === 'post') { config.data = qs.stringify(config.data) } // 在发送请求之前做些什么 return config; }, function (error) { // 对请求错误做些什么 return Promise.reject(error) }); // 添加响应拦截器 Axios.interceptors.response.use(function (response) { if (response.status !== 200){ return; } // 对响应数据做点什么 return response; }, function (error) { // 对响应错误做点什么 return Promise.reject(error); }); ~~~~~~ ``` new Vue({ router, }) ``` ### 封装Axios请求 创建 build/helper.js ~~~ import Vue from "vue"; import Axios from "axios" //引用 axios import qs from "qs" //引用 axios Vue.prototype.HOST = '/v1'//调取跨域 config/index let localhosts = '/v1'; //版本 const md5 = require('./md5.min.js'); //引用MD5加密 const getnow = Date.now || function() { return new Date().getTime();//获取当前时间 }; export default { //sing进行排序加密 setSign: function(options = []) { options.sort(); let str = options.join("&"); return md5(str); }, //获取当前时间 11位 time:15, now: function() { let time = getnow(); return Math.ceil(time / 1000) }, filters: { //控制显示字数 content:function(data,rs='10'){ let content = data.replace(/<[^>]+>/g,""); let dataslice=content.slice(0,rs); return dataslice+'...'; }, //时间戳转换日期 parsetime:function(time, cFormat) { if (arguments.length === 0) { return null } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time = parseInt(time) } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { const value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] } return value.toString().padStart(2, '0') }) return time_str } }, //封装Axios请求 req: function(options) { let api = options.api == undefined ? "" : options.api; let header = options.header ==undefined ?{}:options.header; let data = options.data == undefined ? {} : options.data; let method = options.method == undefined ? "POST" : options.method; let timestamp = options.timestamp ==undefined ?"":options.timestamp; let sign = options.sign ==undefined ?"":options.sign; //对参数进行处理 return new Promise((reslove, reject) => { Axios({ method: method, header: header, url:localhosts+api+'?timestamp='+timestamp+'&sign='+sign, data: data }) .then(res =>{ if (res.data.message == undefined) { this.open3('请求异常') return; } //发送成功 if (res.data.message == 'ok') { reslove(res); } else { this.open3('操作异常') return; } }) .catch(error =>{ console.log(error) //this.open3(error) return; }); }); }, } ~~~