原生sd.js----------------------------------------------------------------
const API_ROOT_URL = "http://www.api.com"; const $d= { __getData(type, url, data, doing) { /**调用舒工AJAX-lite 1.0*/ //别名转义 ---------------------------------------------------------------- doing.s && (doing.success = doing.s); //精简别名 doing.f && (doing.fail = doing.f); //精简别名 doing.e && (doing.error = doing.e); //精简别名 $g.$utils.ajax({ post: {type, url, data}, get: { success(d) { if (d.code == 0) { doing.success && doing.success(d.data || d); } else { doing.fail && doing.fail(d); } }, error(d) { doing.error && doing.error(d); console.log("【报错】" + JSON.stringify(d, null, 4)); } } }); }, get(doing) { this.__getData("get", `${API_ROOT_URL}/apiPath/getName`, null, doing); }, post(data, doing) { this.__getData("post", `${API_ROOT_URL}/apiPath/postName`, data, doing); } };
<!--引入sd文件--> <script src="js/sd.js"></script>
//调用sd $d.get( { s: d => { /* loading.close();//停止加载*/ console.log("拿到数据了", d); }, f: d => { console.log("失败了", d); } /*,e:d=> { console.log("报错了", d); }*/ } ); $d.post( {param1: "参数值1"}, { s: d => { /* loading.close();//停止加载*/ console.log("拿到数据了", d); }, f: d => { console.log("失败了", d); } /*,e:d=> { console.log("报错了", d); }*/ } );
vue axios的sd.js----------------------------------------------------------------
import axios from 'axios'; // const API_ROOT_URL = "http://xxxxxxxxxxxxx"; //公司测试服务器 const API_ROOT_URL = "https://xxxxxxxxxxxxx"; //学校正式服务器 axios.defaults.timeout = 10 * 60 * 1000; //设置默认超时时间10分钟 export default { _login_jwt: API_ROOT_URL + "/xxxxxxxxxxxxx", //首次登陆 _gate_record_new: API_ROOT_URL + "/xxxxxxxxxxxxx", //入校登记记录 _system_currentTime: API_ROOT_URL + "/xxxxxxxxxxxxx", //获取当前服务器系统时间 _weixin_mp_config: API_ROOT_URL + "/xxxxxxxxxxxxx", //获取微信配置 _turnoverSchool_getCode: API_ROOT_URL + "/xxxxxxxxxxxxx", //判断用户是不是被绿了 __getData(type, url, data, doing, otherConfig = {}) { var headers = data ? data.headers : null; data && data.headers && delete data.headers JSON.stringify(data) === '{}' && (data = null) var axiosData = { method: type || "post", url, data } headers || (headers = {}); headers['Content-type'] || (headers['Content-type'] = 'application/json'); headers && (axiosData.headers = headers); axios(axiosData).then(d => { d = d.data; if (d.code == 0) { doing.success && doing.success(otherConfig.isGetAllData ? d : (d.data || d)); } else { doing.fail && doing.fail(d); } }).catch(d => { doing.error && doing.error(d); console.log("【报错】" + JSON.stringify(d, null, 4)); }) }, // 用户登录接口 login_jwt(data, doing) { var url = this._login_jwt; this.__getData("post", url, data, doing); }, // 存储扫码用户记录 gate_record_new(data, doing) { var url = this._gate_record_new; this.__getData("post", url, data, doing); }, // 获取服务器系统时间 system_currentTime(data, doing) { var url = this._system_currentTime; this.__getData("get", url, data, doing); }, // 获取微信配置(获取用户坐标之前需要) weixin_mp_config(data, doing) { var url = this._weixin_mp_config + '?url=' + data.url; this.__getData("get", url, null, doing); }, // 判断绿码 turnoverSchool_getCode(data, doing) { var url = this._turnoverSchool_getCode; this.__getData("get", url, data, doing); }, }
main.js引入
import sdfrom './sd'; Vue.prototype.$d= sd;
. vue调用
methods: { firstLogin() { this.showLoading = true; var data = { username: this.ucode, password: this.psw }; this.$d.login_jwt(data, { success: d => { this.showLoading = false; this.success = true; this.$common.cookie.set("token", d.token); //存储余老师酷爱的token,他特别讨厌每次都重新登陆! this.playSuccessSound(); // console.log("拿到数据了", d); }, fail: d => { console.log("失败了", d.msg); }/*,error:d=> { console.log("报错了", d); }*/ }); }, autoLogin() { this.showLoading = true; var data = {}; data.headers = { "X-Authorization": this.$common.cookie.get("token") }; this.$d.login_jwt(data, { success: d => { this.showLoading = false; this.success = true; this.playSuccessSound(); console.log("拿到数据了", d); }, fail: d => { console.log("失败了", d.msg); }/*,error:d=> { console.log("报错了", d); }*/ }); }