当前位置: 代码迷 >> 综合 >> sd.js帮助您简化繁重的获取数据、存储数据(CRUD)骚操作(吐槽~在安卓9.0以下或者IOS10.X以下手机端H5页面不支持,在这两种情况下的系统只能使用ajax或者原生js请求后台数据)
  详细解决方案

sd.js帮助您简化繁重的获取数据、存储数据(CRUD)骚操作(吐槽~在安卓9.0以下或者IOS10.X以下手机端H5页面不支持,在这两种情况下的系统只能使用ajax或者原生js请求后台数据)

热度:33   发布时间:2023-11-27 19:27:03.0

 

 

原生sd.js---------------------------------------------------------------- 

const API_ROOT_URL = "http://www.api.com";const $d= {_postList_url: API_ROOT_URL + "/apiPath/postName?param1={param1}",_getList_url: API_ROOT_URL + "/apiPath/getName",__getData(type, url, data, doing) {/**调用舒工AJAX-lite 1.0*/$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));}}});},postList(data, doing) {var url = this._postList_url.replace(/{param1}/g, data.param1);this.__getData("post", url, data, doing);},getList(doing) {var url = this._getList_url;this.__getData("get", url, null, doing);}
};
<!--引入sd文件-->
<script src="js/sd.js"></script>
//调用sd
$d.getList1({param1: "参数值1"},{success: d => {/* loading.close();//停止加载*/console.log("拿到数据了", d);},fail: d => {console.log("失败了", d);}/*,error:d=> {console.log("报错了", d);}*/}
);
$d.getList2({param2: "参数值2"},{success: d => {/* loading.close();//停止加载*/console.log("拿到数据了", d);},fail: d => {console.log("失败了", d);}/*,error: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.headersJSON.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);}*/});}

  相关解决方案