micro_mall_xcx/pages/goods/search/index.js
2023-10-25 17:29:54 +08:00

178 lines
3.4 KiB
JavaScript

import {
getHistorySearchList
} from '~/services/home/getHistorySearchList';
import {
getSearchRecList
} from '~/services/home/getSearchRecList';
import {
getHotSearchList
} from '~/services/home/getHotSearchList';
import {
addHistorySearch
} from '~/services/home/addHistorySearch';
import {
deleteHistorySearch
} from '~/services/home/deleteHistorySearch';
Page({
data: {
historyWords: [],
popularWords: [],
searchValue: '',
dialog: {
showCancelButton: true,
message: '',
},
dialogShow: false,
},
deleteType: 0,
deleteIndex: '',
onShow() {
this.queryHistory();
this.queryPopular();
},
async queryHistory() {
try {
getHistorySearchList().then((res) => {
if (res.code == 200) {
this.setData({
historyWords: res.data,
});
} else {
console.log(res.msg);
}
});
} catch (error) {
console.log(error);
}
},
async queryPopular() {
try {
getSearchRecList().then((res) => {
if (res.code == 200) {
this.setData({
popularWords: res.data,
});
} else {
console.log(res.msg);
}
});
} catch (error) {
console.log(error);
}
},
confirm() {
const {
historyWords
} = this.data;
const {
deleteType,
deleteIndex
} = this;
// console.log('deleteType', deleteType, 'deleteIndex', deleteIndex);
// console.log(historyWords);
if (deleteType === 0) {
const deleteWord = historyWords[deleteIndex];
historyWords.splice(deleteIndex, 1);
this.setData({
historyWords,
dialogShow: false,
});
deleteHistorySearch(deleteWord.historySearchId);
} else {
this.setData({
historyWords: [],
dialogShow: false,
});
}
},
close() {
this.setData({
dialogShow: false,
});
},
handleClearHistory() {
const {
dialog
} = this.data;
this.deleteType = 1;
this.setData({
dialog: {
...dialog,
message: '确认删除所有历史记录',
},
dialogShow: true,
});
},
deleteCurr(e) {
const {
index
} = e.currentTarget.dataset;
const {
dialog
} = this.data;
this.deleteIndex = index;
this.setData({
dialog: {
...dialog,
message: '确认删除当前历史记录',
deleteType: 0,
},
dialogShow: true,
});
},
handleHistoryTap(e) {
const {
historyWords
} = this.data;
const {
dataset
} = e.currentTarget;
const _searchValue = historyWords[dataset.index || 0].historySearchContent || '';
if (_searchValue) {
wx.navigateTo({
url: `/pages/goods/result/index?searchValue=${_searchValue}`,
});
}
},
// 点击搜索推荐
handleRecTap(e) {
const {
dataset
} = e.currentTarget;
const _searchValue = dataset?.value;
if (_searchValue) {
wx.navigateTo({
url: `/pages/goods/result/index?searchValue=${_searchValue}`,
});
}
},
// TODO:提交搜索
handleSubmit(e) {
const {
value
} = e.detail;
if (value?.length === 0 || value == undefined) return;
const data = {
HistorySearchContent: value,
};
addHistorySearch(data).then((res) => {
this.queryHistory();
});
wx.navigateTo({
url: `/pages/goods/result/index?searchValue=${value}`,
});
},
});