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

View File

@ -1,13 +1,5 @@
<view class="search-page">
<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-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-icon slot="left-icon" prefix="wr" name="search" size="40rpx" color="#bbb" />
</t-search>
<view class="search-wrap">
@ -17,45 +9,32 @@
<text class="search-clear" bind:tap="handleClearHistory">清除</text>
</view>
<view class="search-content">
<view
class="search-item"
hover-class="hover-history-item"
wx:for="{{historyWords}}"
bind:tap="handleHistoryTap"
bindlongpress="deleteCurr"
data-index="{{index}}"
wx:key="*this"
>
{{item}}
<view wx:if="{{historyWords.length > 0}}">
<view class="search-item" hover-class="hover-history-item" wx:for="{{historyWords}}" bind:tap="handleHistoryTap" bindlongpress="deleteCurr" data-index="{{index}}" wx:key="*this">
{{item.historySearchContent}}
</view>
</view>
<view wx:else class="search-item">
暂无数据
</view>
</view>
</view>
<view class="popular-wrap">
<view class="search-header">
<text class="search-title">热门搜索</text>
</view>
<view class="search-content">
<view
class="search-item"
hover-class="hover-history-item"
wx:for="{{popularWords}}"
bind:tap="handleHistoryTap"
data-index="{{index}}"
wx:key="*this"
>
{{item}}
<view wx:if="{{popularWords.length > 0}}">
<view class="search-item" hover-class="hover-history-item" wx:for="{{popularWords}}" bind:tap="handleHistoryTap" data-index="{{index}}" wx:key="*this">
{{item.searchTerm}}
</view>
</view>
<view wx:else class="search-item">
暂无数据
</view>
</view>
</view>
</view>
<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"
/>
</view>
<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" />
</view>

View File

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