前言
功能描述:
1.获取列表参数code:请求A接口(getDataList函数),获取code,code是实时更新的
2. 请求B接口,获取table列表,由于后端是调第三方接口,无法返回一个状态,标记是否给前端返会数据还是空数据
3. 1分钟内,5秒钟调一次接口,直到请求到数据后,清楚定时器
data() {
return {
codeTimer: null,//定时器
codeTime: 12, // 限制1分钟后没请求到erp数据,不再继续请求
searchEmptyText: '暂无数据',
isClickQueary: false //是否查询
};
},
methods: {
// 查询
getFilterParams() {
**//初始化定时器的值,不然上次请求到数据后,开启定时器codeTime等于0**
this.codeTime = 12;
this.getDataList();
},
// 获取erp的code
getDataList() {
this.tableConfig.loading = true;
//清空表格提示
this.searchEmptyText = '';
const params = {
pageNumber: 1,
pageSize: 10
};
API.getAccountsPayableCode(params)
.then(res => {
if (res.status) {
this.erpId = res.data;
//请求列表的接口,同时开启定时器
this.getErpTableDataList();
this._codeCount();
}
})
.catch(err => {
this.tableConfig.loading = false;
this.$message.error(err.msg || '请求失败');
});
},
// 列表
getErpTableDataList() {
const params = {
zzid: this.erpId,
pageNumber: 1,
pageSize: 10
};
params.pageNumber = this.tableConfig.pageOption.pageIndex;
params.pageSize = this.tableConfig.pageOption.pageSize;
API.getSelectAccountsPayableList(params)
.then(res => {
this.tableConfig.data = res.data.records;
this.tableConfig.total = res.data.total;
**//直到请求到数据,清楚定时器**
if (res.status && res.data && res.data.records.length !== 0) {
this._clearCount();
this.tableConfig.loading = false;
}
})
.catch(err => {
this.tableConfig.loading = false;
this.$message.error(err.msg || '请求失败');
});
},
_codeCount() {
this.codeTimer = window.setInterval(() => {
if (this.codeTime <= 0) {
console.log('清楚定时器');
window.clearInterval(this.codeTimer);
this.codeTimer = null;
this.codeTime = 0;
this.tableConfig.loading = false;
if (this.tableConfig.data.length === 0) {
this.searchEmptyText = this.isClickQueary ? '未查询到数据' : '暂无数据';
}
} else {
this.getErpTableDataList();
this.codeTime--;
}
console.log(this.codeTime, 'this.codeTime');
}, 5000);
},
//清楚定时器
_clearCount() {
window.clearInterval(this.codeTimer);
console.log(window, 'window');
this.codeTimer = null;
this.codeTime = 0;
},
// 分页
nextPage(page) {
**// 每次分页请求接口,需要回复初始值**
this.codeTime = 12;
this.tableConfig.pageOption.pageIndex = page.pageIndex;
this.tableConfig.pageOption.pageSize = page.pageSize;
this.tableConfig.pageOption.start = (page.pageIndex - 1) * page.pageSize;
this.getDataList();
},
}
总计:1.多次开启定时器,关闭定时器,需要一个值来记录判断 2.查询和分页时,初始化记录定时器的codeTime=12 ,不然上次已经请求到数据codeTime是0,开启定时器就直接关闭了