Commit 034ad257 authored by KesaubeEire's avatar KesaubeEire

fix: 翻页的各种问题, 真的是屎山...

parent 165eb7f8
......@@ -115,9 +115,10 @@
/**创建请求底层 fetch 函数
* @param {object} param0 参数对象
* @param {function} callback 回调函数
* @param {function} successCallBack 成功回调函数
* @param {function} failCallBack 失败回调函数
*/
function createFetch({ url, params, payload }, callback = () => {}) {
function createFetch({ url, params, payload }, successCallBack = () => {}, failCallBack = () => {}) {
const msg = {
method: params.method,
headers: {
......@@ -133,6 +134,7 @@
isSearching = false;
if (!isEnd) toast.push('搜索参数重复捏~');
if (process.env.APP_ENV === 'dev') console.log(lastSearchURL, url);
failCallBack();
return;
}
lastSearchURL = url;
......@@ -143,12 +145,13 @@
// console.log(data);
// 处理数据
callback(data);
successCallBack(data);
})
.catch(error => {
// 处理错误
console.error('网络请求错误:', error);
toast.push('网络请求错误');
failCallBack();
})
.finally(() => {
isSearching = false;
......@@ -160,8 +163,9 @@
* @param searchParam
* @param limit
* @param page
* @param {function} failFunction 失败的回调函数
*/
function performSearch(searchType, searchParam, limit = 20, page = 1) {
function performSearch(searchType, searchParam, limit = 20, page = 1, failFunction = () => {}) {
// 瀑布流请求中不能重复请求
if (isSearching) {
return;
......@@ -195,33 +199,40 @@
if (process.env.APP_ENV === 'dev') console.log(requestType(searchParam));
createFetch(requestType(searchParam), data => {
if (process.env.APP_ENV === 'dev') console.log(data);
if (data.length != 0) {
page == 1 ? showList(data) : appendList(data);
} else {
toast.push('没有搜索到数据捏~');
return;
}
if (data.length < globalPageLimit) {
console.log(data.length);
if (sameSearch) isEnd = true;
toast.push('加载完成了捏~');
return;
}
nextPage++;
// console.log(nextPage);
});
createFetch(
requestType(searchParam),
data => {
if (process.env.APP_ENV === 'dev') console.log(data);
if (data.length != 0) {
page == 1 ? showList(data) : appendList(data);
} else {
toast.push('没有搜索到数据捏~');
return;
}
if (data.length < globalPageLimit) {
console.log(data.length);
if (sameSearch) isEnd = true;
toast.push('加载完成了捏~');
return;
}
let current = nextPage;
nextPage++;
console.log('current:\t', current, '\t\tnextPage:\t', nextPage);
},
failFunction
);
}
/**瀑布流: 延续参数继续搜索下一页*/
function continueLastSearch() {
// console.log(nextPage);
performSearch(lastSearchType, lastSearchParam, globalPageLimit, nextPage);
console.log('--------- TurnPage ---------');
}
// -----------------------------
......@@ -254,7 +265,10 @@
* TODO: 全局内容 30分钟 一次缓存在 localstorage 里
*/
export function searchDefault(limit = globalPageLimit, page = 1) {
performSearch('', '', limit, page);
nextPage = 1;
performSearch('', '', limit, page, () => {
nextPage = 2;
});
}
/**拉取静态资源: Category */
......@@ -373,7 +387,7 @@
let pc_debounced = clientHeight > 0 && scrollTop > 0 && scrollTop + clientHeight > scrollHeight - distance;
let phone_debounced = window.scrollY + window.innerHeight >= document.documentElement.scrollHeight - distance;
if (pc_debounced || phone_debounced) {
if ((pc_debounced || phone_debounced) && nextPage != 1 && List.length) {
// toast.push(pc_debounced ? 'pc' : 'phone');
debouncedContinue();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment