Commit d73aedd9 authored by JoyJ's avatar JoyJ

fix: 瀑布流;整合search

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