优化热门搜索方案,如果更新了后端最新代码,请pull下更新

master
lemon橪 2021-07-30 16:29:05 +08:00
parent 436dd56e54
commit d19b02a97d
4 changed files with 44 additions and 14 deletions

View File

@ -30,12 +30,12 @@ export function getCategory(parent_id = 0) {
* 获取热门关键词 * 获取热门关键词
* @param num * @param num
*/ */
export function getHotKeywords(start = 0, end = 10) { export function getHotKeywords(count) {
return http.request({ return http.request({
url: "/goods/hot-words", url: "/goods/hot-words",
method: Method.GET, method: Method.GET,
loading: false, loading: false,
params: { start, end }, params: { count },
}); });
} }

View File

@ -274,7 +274,7 @@ import { getGoodsList, getGoodsRelated } from "@/api/goods.js";
import { getHotKeywords } from "@/api/home.js"; import { getHotKeywords } from "@/api/home.js";
import mSearch from "@/components/m-search-revision/m-search-revision.vue"; import mSearch from "@/components/m-search-revision/m-search-revision.vue";
import storage from "@/utils/storage";
export default { export default {
data() { data() {
return { return {
@ -594,15 +594,38 @@ export default {
}, },
}); });
}, },
//
/**
* 加载热门搜索
* 1.30分钟之后更新缓存
* 2.如果有缓存热门关键字则去请求缓存
*/
async loadHotKeyword() { async loadHotKeyword() {
this.hotKeywordList = []; this.hotKeywordList = [];
let res = await getHotKeywords(); if (
!storage.getHotWords().time ||
storage.getHotWords().time <= new Date().getTime() / 1000
) {
//
let res = await getHotKeywords(10);
let keywords = res.data.result; let keywords = res.data.result;
keywords.forEach((item) => { keywords.forEach((item) => {
this.hotKeywordList.push(item); this.hotKeywordList.push(item);
}); });
let hotData = {
time: new Date().getTime() / 1000 + 30 * 60,
keywords: keywords,
};
storage.setHotWords(hotData);
} else {
let keywords = storage.getHotWords().keywords;
keywords.forEach((item) => {
this.hotKeywordList.push(item);
});
}
}, },
// //
async loadData(type, loading) { async loadData(type, loading) {
@ -663,7 +686,6 @@ export default {
content: "确定清除历史搜索记录?", content: "确定清除历史搜索记录?",
success: (res) => { success: (res) => {
if (res.confirm) { if (res.confirm) {
this.oldKeywordList = []; this.oldKeywordList = [];
uni.removeStorage({ uni.removeStorage({
key: "OldKeys", key: "OldKeys",
@ -694,7 +716,6 @@ export default {
}, },
// //
saveKeyword(keyword) { saveKeyword(keyword) {
if (!keyword) return false; if (!keyword) return false;
uni.getStorage({ uni.getStorage({
key: "OldKeys", key: "OldKeys",
@ -715,7 +736,6 @@ export default {
data: JSON.stringify(OldKeys), data: JSON.stringify(OldKeys),
}); });
this.oldKeywordList = OldKeys; // this.oldKeywordList = OldKeys; //
}, },
fail: (e) => { fail: (e) => {
var OldKeys = [keyword]; var OldKeys = [keyword];

View File

@ -103,7 +103,9 @@ export default {
.row { .row {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
padding: 26rpx 32rpx;
} }
.speak-msg { .speak-msg {
padding: 26rpx 32rpx; padding: 26rpx 32rpx;
> span { > span {
@ -120,6 +122,6 @@ export default {
padding: 16rpx; padding: 16rpx;
} }
.tips { .tips {
margin: 40rpx 0; margin: 40rpx 32rpx;
} }
</style> </style>

View File

@ -9,6 +9,14 @@ const FACE_LOGIN = isDev ? "face_login_dev" : "face_login";
const FINGER_LOGIN = isDev ? "finger_login_dev" : "finger_login"; const FINGER_LOGIN = isDev ? "finger_login_dev" : "finger_login";
const CART_BACKBTN = isDev ? "cart_backbtn_dev" : "cart_backbtn"; const CART_BACKBTN = isDev ? "cart_backbtn_dev" : "cart_backbtn";
export default { export default {
// 写入热门搜索时间戳
setHotWords(val) {
uni.setStorageSync("hotWords", val);
},
// 获取热门搜索时间戳
getHotWords() {
return uni.getStorageSync(`hotWords`);
},
// 获取face id登录 // 获取face id登录
getFaceLogin() { getFaceLogin() {