//通用接口封装
import Http from "./axios";
import { Message, MessageBox } from "element-ui";
export default {
// 确认弹层
comfirmDeleteMessage(
title = "提示",
text = "是否确认删除",
confirmBtn = "确定",
cancelBtn = "取消"
) {
return MessageBox.confirm(text, title, {
confirmButtonText: confirmBtn,
cancelButtonText: cancelBtn,
type: "warning",
})
// .catch(() => new Promise(()=>{}));
.catch(() => Promise.reject());
},
// GET-不带弹层显示结果
async baseGet(url, params) {
return await Http.get(url, params).then(res=>res.data).catch(()=>{});
},
// GET-带弹层显示结果
async messageGet(url, params, SucMes = "操作成功", failMes = "操作失败") {
return await Http.get(url, params)
.then(res => {
let { code, message, result } = res.data;
if (code === 0) {
Message.success(SucMes || message);
} else {
Message.error(failMes || message);
}
return result;
})
.catch(()=>{});
},
// POST-不带弹层显示结果
async basePost(url, params) {
return await Http.post(url, params).then(res=>res.data).catch(()=>{});
},
// POST-带弹层显示结果
async messagePost(url, params, SucMes = "操作成功", failMes = "操作失败") {
return await Http.post(url, params)
.then(res => {
let { code, message, result } = res.data;
if (code === 0) {
Message.success(SucMes || message);
} else {
Message.error(failMes || message);
}
return result;
})
.catch(()=>{});
},
};
// 二次封装
import commonApi from "../common";
export default {
// +++++ SAMPLES ++++++
// ++ ConfirmPost ++
async testConfirmPost() {
return await commonApi.comfirmDeleteMessage()
.then(async () => {
return await commonApi.messagePost("/api/Role/RoleList", {
PageIndex: 0,
PageSize: 10,
RoleName: "",
})
}
).catch(()=>{})
},
};
// +++++ SAMPLE ++++++
// 页面调用
import api from "@/serve/api/customerApi";
api.testConfirmPost()
.then(res => {
console.log(res)
this.testData = res
console.log('after tespost')
})
.catch(err=>{})
// ==通用 server.js
function post (url,type,data){
return new Promise((resolve,reject)=>{
$.ajax({
url: url,
type: type,
data: data,
dataType: "json",
success: function (res) {
if (res.IsSuccess) {
// console.log(res)
resolve(res.Data,res.TotalCount)
} else {
reject(res.Msg)
}
},
error: function (err) {
console.log(err)
reject(err)
window.location.href = url;
}
});
})
}
// 业务封装 server.js
async function postGetNewCustomerData(data = null) {
return await commonPost('/Crm/GetNewCustomerPager','post',data)
}
// 页面使用
postGetNewCustomerData(data).then((res)=>{
// callback
})