feat 搜索历史记录 热门搜索 首页轮播图

This commit is contained in:
Cxpller 2023-10-14 16:08:07 +08:00
parent f8b6610566
commit 39ff7198f6
8 changed files with 167 additions and 67 deletions

View File

@ -1,7 +1,7 @@
import { import { getHistorySearchList } from '~/services/home/getHistorySearchList';
getSearchHistory, import { getHotSearchList } from '~/services/home/getHotSearchList';
getSearchPopular, import { addHistorySearch } from '~/services/home/addHistorySearch';
} from '../../../services/good/fetchSearchHistory'; import { deleteHistorySearch } from '~/services/home/deleteHistorySearch';
Page({ Page({
data: { data: {
@ -9,7 +9,6 @@ Page({
popularWords: [], popularWords: [],
searchValue: '', searchValue: '',
dialog: { dialog: {
title: '确认删除当前历史记录',
showCancelButton: true, showCancelButton: true,
message: '', message: '',
}, },
@ -26,50 +25,61 @@ Page({
async queryHistory() { async queryHistory() {
try { try {
const data = await getSearchHistory(); getHistorySearchList().then((res) => {
const code = 'Success'; if (res.code == 200) {
if (String(code).toUpperCase() === 'SUCCESS') { this.setData({
const { historyWords = [] } = data; historyWords: res.data,
this.setData({ });
historyWords, } else {
}); console.log(res.msg);
} }
});
} catch (error) { } catch (error) {
console.error(error); console.log(error);
} }
}, },
async queryPopular() { async queryPopular() {
try { try {
const data = await getSearchPopular(); getHotSearchList().then((res) => {
const code = 'Success'; if (res.code == 200) {
if (String(code).toUpperCase() === 'SUCCESS') { this.setData({
const { popularWords = [] } = data; popularWords: res.data,
this.setData({ });
popularWords, } else {
}); console.log(res.msg);
} }
});
} catch (error) { } catch (error) {
console.error(error); console.log(error);
} }
}, },
confirm() { confirm() {
const { historyWords } = this.data; const { historyWords } = this.data;
const { deleteType, deleteIndex } = this; const { deleteType, deleteIndex } = this;
historyWords.splice(deleteIndex, 1); // console.log('deleteType', deleteType, 'deleteIndex', deleteIndex);
// console.log(historyWords);
if (deleteType === 0) { if (deleteType === 0) {
const deleteWord = historyWords[deleteIndex];
historyWords.splice(deleteIndex, 1);
this.setData({ this.setData({
historyWords, historyWords,
dialogShow: false, dialogShow: false,
}); });
deleteHistorySearch(deleteWord.historySearchId);
} else { } else {
this.setData({ historyWords: [], dialogShow: false }); this.setData({
historyWords: [],
dialogShow: false,
});
} }
}, },
close() { close() {
this.setData({ dialogShow: false }); this.setData({
dialogShow: false,
});
}, },
handleClearHistory() { handleClearHistory() {
@ -109,9 +119,16 @@ Page({
} }
}, },
// TODO:提交搜索
handleSubmit(e) { handleSubmit(e) {
const { value } = e.detail.value; const { value } = e.detail.value;
if (value.length === 0) return; if (value?.length === 0) return;
const data = {
HistorySearchContent: e.detail.value,
};
addHistorySearch(data).then((res) => {
this.queryHistory();
});
wx.navigateTo({ wx.navigateTo({
url: `/pages/goods/result/index?searchValue=${value}`, url: `/pages/goods/result/index?searchValue=${value}`,
}); });

View File

@ -1,13 +1,5 @@
<view class="search-page"> <view class="search-page">
<t-search <t-search t-class-input-container="t-class__input-container" t-class-input="t-search__input" value="{{searchValue}}" leftIcon="" placeholder="iPhone12pro" bind:submit="handleSubmit" focus>
t-class-input-container="t-class__input-container"
t-class-input="t-search__input"
value="{{searchValue}}"
leftIcon=""
placeholder="iPhone12pro"
bind:submit="handleSubmit"
focus
>
<t-icon slot="left-icon" prefix="wr" name="search" size="40rpx" color="#bbb" /> <t-icon slot="left-icon" prefix="wr" name="search" size="40rpx" color="#bbb" />
</t-search> </t-search>
<view class="search-wrap"> <view class="search-wrap">
@ -17,45 +9,32 @@
<text class="search-clear" bind:tap="handleClearHistory">清除</text> <text class="search-clear" bind:tap="handleClearHistory">清除</text>
</view> </view>
<view class="search-content"> <view class="search-content">
<view <view wx:if="{{historyWords.length > 0}}">
class="search-item" <view class="search-item" hover-class="hover-history-item" wx:for="{{historyWords}}" bind:tap="handleHistoryTap" bindlongpress="deleteCurr" data-index="{{index}}" wx:key="*this">
hover-class="hover-history-item" {{item.historySearchContent}}
wx:for="{{historyWords}}" </view>
bind:tap="handleHistoryTap" </view>
bindlongpress="deleteCurr" <view wx:else class="search-item">
data-index="{{index}}" 暂无数据
wx:key="*this"
>
{{item}}
</view> </view>
</view> </view>
</view> </view>
<view class="popular-wrap"> <view class="popular-wrap">
<view class="search-header"> <view class="search-header">
<text class="search-title">热门搜索</text> <text class="search-title">热门搜索</text>
</view> </view>
<view class="search-content"> <view class="search-content">
<view <view wx:if="{{popularWords.length > 0}}">
class="search-item" <view class="search-item" hover-class="hover-history-item" wx:for="{{popularWords}}" bind:tap="handleHistoryTap" data-index="{{index}}" wx:key="*this">
hover-class="hover-history-item" {{item.searchTerm}}
wx:for="{{popularWords}}" </view>
bind:tap="handleHistoryTap" </view>
data-index="{{index}}" <view wx:else class="search-item">
wx:key="*this" 暂无数据
>
{{item}}
</view> </view>
</view> </view>
</view> </view>
</view> </view>
<t-dialog <t-dialog visible="{{dialogShow}}" content="{{dialog.message}}" bindconfirm="confirm" bind:close="close" confirm-btn="确定" cancel-btn="{{dialog.showCancelButton ? '取消' : null}}" t-class-confirm="dialog__button-confirm" t-class-cancel="dialog__button-cancel" />
visible="{{dialogShow}}" </view>
content="{{dialog.message}}"
bindconfirm="confirm"
bind:close="close"
confirm-btn="确定"
cancel-btn="{{dialog.showCancelButton ? '取消' : null}}"
t-class-confirm="dialog__button-confirm"
t-class-cancel="dialog__button-cancel"
/>
</view>

View File

@ -1,5 +1,6 @@
import { fetchHome } from '~/services/home/home'; import { fetchHome } from '~/services/home/home';
import { fetchGoodsList } from '~/services/good/fetchGoods'; import { fetchGoodsList } from '~/services/good/fetchGoods';
import { getbannerList } from '~/services/home/getbannerList';
import Toast from 'tdesign-miniprogram/toast/index'; import Toast from 'tdesign-miniprogram/toast/index';
Page({ Page({
@ -58,10 +59,20 @@ Page({
this.setData({ this.setData({
pageLoading: true, pageLoading: true,
}); });
// 获取轮播图列表
getbannerList().then((res) => {
if (res.code == 200) {
this.setData({
imgSrcs: res.data.map((v) => v.bannerImg),
});
}
});
fetchHome().then(({ swiper, tabList }) => { fetchHome().then(({ swiper, tabList }) => {
this.setData({ this.setData({
tabList, tabList,
imgSrcs: swiper, // imgSrcs: swiper,
pageLoading: false, pageLoading: false,
}); });
this.loadGoodsList(true); this.loadGoodsList(true);

View File

@ -0,0 +1,21 @@
import {
request
} from '~/services/_utils/request';
/** 添加历史搜索 */
export function addHistorySearch(data) {
return new Promise((resolve, reject) => {
request({
url: `HistorySearchApi/addHistorySearch`,
method: 'POST',
data: data,
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
}
});
});
}

View File

@ -0,0 +1,17 @@
import { request } from '~/services/_utils/request';
/** 删除历史搜索 */
export function deleteHistorySearch(ids) {
return new Promise((resolve, reject) => {
request({
url: `HistorySearchApi/` + ids,
method: 'DELETE',
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
},
});
});
}

View File

@ -0,0 +1,19 @@
import {
request
} from '../_utils/request';
/** 获取历史搜索列表 */
export function getHistorySearchList() {
return new Promise((resolve, reject) => {
request({
url: `HistorySearchApi/getHistorySearchList`,
method: 'GET',
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
}
});
});
}

View File

@ -0,0 +1,17 @@
import { request } from '../_utils/request';
/** 获取热门搜索列表 */
export function getHotSearchList() {
return new Promise((resolve, reject) => {
request({
url: `HistorySearchApi/getHotSearchList`,
method: 'GET',
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
},
});
});
}

View File

@ -0,0 +1,19 @@
import {
request
} from '../_utils/request';
/** 获取轮播图列表 */
export function getbannerList() {
return new Promise((resolve, reject) => {
request({
url: `BannerApi/getBannerList?BannerPosition=1`,
method: 'GET',
success: function (res) {
resolve(res);
},
fail: function (error) {
reject(error);
}
});
});
}