优化热门搜索方案,如果更新了后端最新代码,请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
*/
export function getHotKeywords(start = 0, end = 10) {
export function getHotKeywords(count) {
return http.request({
url: "/goods/hot-words",
method: Method.GET,
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 mSearch from "@/components/m-search-revision/m-search-revision.vue";
import storage from "@/utils/storage";
export default {
data() {
return {
@ -594,15 +594,38 @@ export default {
},
});
},
//
/**
* 加载热门搜索
* 1.30分钟之后更新缓存
* 2.如果有缓存热门关键字则去请求缓存
*/
async loadHotKeyword() {
this.hotKeywordList = [];
let res = await getHotKeywords();
let keywords = res.data.result;
if (
!storage.getHotWords().time ||
storage.getHotWords().time <= new Date().getTime() / 1000
) {
//
let res = await getHotKeywords(10);
let keywords = res.data.result;
keywords.forEach((item) => {
this.hotKeywordList.push(item);
});
keywords.forEach((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) {
@ -663,7 +686,6 @@ export default {
content: "确定清除历史搜索记录?",
success: (res) => {
if (res.confirm) {
this.oldKeywordList = [];
uni.removeStorage({
key: "OldKeys",
@ -694,7 +716,6 @@ export default {
},
//
saveKeyword(keyword) {
if (!keyword) return false;
uni.getStorage({
key: "OldKeys",
@ -715,7 +736,6 @@ export default {
data: JSON.stringify(OldKeys),
});
this.oldKeywordList = OldKeys; //
},
fail: (e) => {
var OldKeys = [keyword];

View File

@ -103,7 +103,9 @@ export default {
.row {
display: flex;
flex-wrap: wrap;
padding: 26rpx 32rpx;
}
.speak-msg {
padding: 26rpx 32rpx;
> span {
@ -120,6 +122,6 @@ export default {
padding: 16rpx;
}
.tips {
margin: 40rpx 0;
margin: 40rpx 32rpx;
}
</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 CART_BACKBTN = isDev ? "cart_backbtn_dev" : "cart_backbtn";
export default {
// 写入热门搜索时间戳
setHotWords(val) {
uni.setStorageSync("hotWords", val);
},
// 获取热门搜索时间戳
getHotWords() {
return uni.getStorageSync(`hotWords`);
},
// 获取face id登录
getFaceLogin() {