Commit d73aedd9 authored by JoyJ's avatar JoyJ

fix: 瀑布流;整合search

parent d51c7c48
......@@ -4,10 +4,14 @@
import { _category, _detailWindow, _env, _tagTrans } from './stores';
import { fade } from 'svelte/transition';
function debugMessage(...str) {
if (process.env.APP_ENV === 'dev') console.log(...str);
}
//NextPage指示瀑布流下次应请求的页码数
let nextPage = 1;
//NextPage指示瀑布流下次应请求的limit数
let limit = 20;
let globalPageLimit = 20;
//若isEnd(已经没有新的结果了)则滚动到最底不再请求下一页的数据
let isEnd = false;
......@@ -27,19 +31,20 @@
};
/**请求封装头 -> 获取所有资料*/
const getContentsRequest = {
url: `${$_env}/query.php?action=get&limit=${limit}&page=${nextPage}`,
params: { method: 'GET' }
const getContentsRequest = _ => {
return {
url: `${$_env}/query.php?action=get&limit=${globalPageLimit}&page=${nextPage}`,
params: { method: 'GET' }
};
};
/**请求封装头 -> 搜索对应 Tag (函数形式)
* @param {string} tag TAG
* @return {object} 请求封装对象
*/
const searchTagRequest = tag => {
return {
url: `${$_env}/query.php?action=get&limit=${limit}&page=${nextPage}&tag=${encodeURIComponent(tag)}`,
url: `${$_env}/query.php?action=get&limit=${globalPageLimit}&page=${nextPage}&tag=${encodeURIComponent(tag)}`,
params: { method: 'GET' }
};
};
......@@ -50,7 +55,7 @@
*/
const searchCategoryRequest = category => {
return {
url: `${$_env}/query.php?action=get&limit=${limit}&page=${nextPage}&category=${category}`,
url: `${$_env}/query.php?action=get&limit=${globalPageLimit}&page=${nextPage}&category=${category}`,
params: { method: 'GET' }
};
};
......@@ -61,7 +66,7 @@
*/
const searchTextRequest = text => {
return {
url: `${$_env}/query.php?action=get&limit=${limit}&page=${nextPage}&search=${encodeURIComponent(text)}`,
url: `${$_env}/query.php?action=get&limit=${globalPageLimit}&page=${nextPage}&search=${encodeURIComponent(text)}`,
params: { method: 'GET' }
};
};
......@@ -97,7 +102,7 @@
// console.log(data);
if (!data.length) {
isEnd = true;
toast.push('没有搜索到东西捏~');
toast.push('没有更多数据了捏~');
return;
}
......@@ -138,61 +143,58 @@
}
let lastSearchFunc = null;
let lastSearchType = '';
let lastSearchParam = null;
let isSearching = false;
function continueLastSearch() {
if (lastSearchFunc) {
lastSearchFunc(lastSearchParam, limit, nextPage);
}
performSearch(lastSearchType, lastSearchParam, globalPageLimit, nextPage);
}
/**根据 TAG 搜索游戏
* @param {string} tag TAG
*/
function searchTag(tag, pageLimit = 20, page = 1) {
function performSearch(searchType, searchParam, limit = 20, page = 1) {
if (isSearching) {
return;
}
isSearching = true;
isEnd = false;
limit = pageLimit;
nextPage = page;
lastSearchParam = tag;
if (page > 0) {
nextPage = page;
globalPageLimit = limit;
nextPage = page + 1;
lastSearchType = searchType;
lastSearchParam = searchParam;
let requestType = getContentsRequest;
switch(searchType) {
case 'category':
requestType = searchCategoryRequest;
break;
case 'tag':
requestType = searchTagRequest;
break;
case 'title':
requestType = searchTextRequest;
break;
}
lastSearchFunc = searchTag;
if (process.env.APP_ENV === 'dev') console.log(tag, encodeURIComponent(tag));
createFetch(searchTagRequest(tag), data => {
if (process.env.APP_ENV === 'dev') console.log(data);
createFetch(requestType(searchParam), data => {
debugMessage(data);
page == 1 ? showList(data) : appendList(data);
});
nextPage++;
}
/**根据 TAG 搜索游戏
* @param {string} tag TAG
*/
function searchTag(tag, limit = 20, page = 1) {
performSearch('tag', tag, limit, page);
}
/**根据 category 搜索游戏
* @param {string} category category
*/
function searchCategory(category, pageLimit = 20, page = 1) {
if (isSearching) {
return;
}
isSearching = true;
isEnd = false;
limit = pageLimit;
nextPage = page;
lastSearchParam = category;
if (page > 0) {
nextPage = page;
}
lastSearchFunc = searchCategory;
if (process.env.APP_ENV === 'dev') console.log('执行搜索:\t', category);
createFetch(searchCategoryRequest(title), data => {
if (process.env.APP_ENV === 'dev') console.log(data);
page == 1 ? showList(data) : appendList(data);
});
nextPage++;
function searchCategory(category, limit = 20, page = 1) {
performSearch('category', category, limit, page);
}
/**@var {string} 上次搜索内容*/
......@@ -200,25 +202,8 @@
/**根据 title 搜索游戏
* @param {string} title 名称
*/
export function searchTitle(title, pageLimit = 20, page = 1) {
if (isSearching) {
return;
}
isSearching = true;
isEnd = false;
limit = pageLimit;
nextPage = page;
lastSearchParam = title;
if (page > 0) {
nextPage = page;
}
lastSearchFunc = searchTitle;
if (process.env.APP_ENV === 'dev') console.log('执行搜索:\t', title);
createFetch(searchTextRequest(title), data => {
if (process.env.APP_ENV === 'dev') console.log(data);
page == 1 ? showList(data) : appendList(data);
});
nextPage++;
export function searchTitle(title, limit = 20, page = 1) {
performSearch('title', title, limit, page);
}
window.searchTitle = searchTitle;
......@@ -226,21 +211,8 @@
* 默认搜索 -> 暂时是全部搜索
* TODO: 全局内容 30分钟 一次缓存在 localstorage 里
*/
export function searchDefault(_, pageLimit = 20, page = 1) {
if (isSearching) {
return;
}
isSearching = true;
isEnd = false;
limit = pageLimit;
nextPage = page;
lastSearchFunc = searchDefault;
lastSearchParam = "";
createFetch(getContentsRequest, data => {
if (process.env.APP_ENV === 'dev') console.log(data);
page == 1 ? showList(data) : appendList(data);
});
nextPage++;
export function searchDefault(_, limit = 20, page = 1) {
performSearch('', '' , limit, page);
}
/**获取封面图
......@@ -262,7 +234,7 @@
/**关闭详情*/
function key_closePanels(event) {
if (event.key === 'Escape') {
console.log(event);
debugMessage(event);
// $_iframe_switch = 0;
// $_show_configPanel = false;
$_detailWindow = false;
......@@ -275,7 +247,7 @@
// TODO: 搞个记时, 每天一刷 & 做一个主动更新的按钮
if (!$_category.length) {
createFetch(getLanguageCategory, data => {
if (process.env.APP_ENV === 'dev') console.log(data);
debugMessage(data);
$_category = data;
});
}
......@@ -284,7 +256,7 @@
// TODO: 搞个记时, 每天一刷 & 做一个主动更新的按钮
if (!$_tagTrans.length) {
createFetch(getTranslatedTag, data => {
if (process.env.APP_ENV === 'dev') console.log(data);
debugMessage(data);
$_tagTrans = data;
});
}
......
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