yangchenggong1_wd

desc:完善 搜索历史

1 -import { MineAppointmentItem } from '../../viewmodel/MineAppointmentItem'  
2 -  
3 @CustomDialog 1 @CustomDialog
4 export struct MyCustomDialog { 2 export struct MyCustomDialog {
5 @State title: string = "标题" 3 @State title: string = "标题"
  4 + @State titleShow: boolean = true
6 @State tipValue: string ="提示文字" 5 @State tipValue: string ="提示文字"
  6 + @State tipShow: boolean = true
7 7
8 @State leftText: string = "取消" 8 @State leftText: string = "取消"
9 @State rightText: string = "确认" 9 @State rightText: string = "确认"
@@ -16,16 +16,21 @@ export struct MyCustomDialog { @@ -16,16 +16,21 @@ export struct MyCustomDialog {
16 16
17 build() { 17 build() {
18 Column() { 18 Column() {
19 - Text(this.title)  
20 - .fontSize("32lpx")  
21 - .margin({ top: "40lpx", bottom: "15lpx" })  
22 - .fontColor($r('app.color.color_333333'))  
23 - .fontSize('35lpx')  
24 - .fontWeight('600lpx')  
25 - Text(this.tipValue)  
26 - .margin({ bottom: "30lpx" })  
27 - .fontSize("27lpx")  
28 - .fontColor($r('app.color.color_B0B0B0')) 19 + if(this.titleShow){
  20 + Text(this.title)
  21 + .fontSize("32lpx")
  22 + .margin({ top: "40lpx", bottom: "15lpx" })
  23 + .fontColor($r('app.color.color_333333'))
  24 + .fontSize('35lpx')
  25 + .fontWeight('600lpx')
  26 + }
  27 +
  28 + if(this.tipShow){
  29 + Text(this.tipValue)
  30 + .margin({ bottom: "30lpx" })
  31 + .fontSize("27lpx")
  32 + .fontColor($r('app.color.color_B0B0B0'))
  33 + }
29 34
30 Divider() 35 Divider()
31 .width("100%") 36 .width("100%")
1 import router from '@ohos.router' 1 import router from '@ohos.router'
  2 +import { StringUtils, ToastUtils } from 'wdKit'
2 import SearcherAboutDataModel from '../../model/SearcherAboutDataModel' 3 import SearcherAboutDataModel from '../../model/SearcherAboutDataModel'
3 import { SearchHistoryItem } from '../../viewmodel/SearchHistoryItem' 4 import { SearchHistoryItem } from '../../viewmodel/SearchHistoryItem'
4 import { SearchHistoryComponent } from './SearchHistoryComponent' 5 import { SearchHistoryComponent } from './SearchHistoryComponent'
@@ -24,6 +25,8 @@ export struct SearchComponent { @@ -24,6 +25,8 @@ export struct SearchComponent {
24 this.getSearchHistoryData() 25 this.getSearchHistoryData()
25 } 26 }
26 27
  28 +
  29 +
27 getSearchHint() { 30 getSearchHint() {
28 SearcherAboutDataModel.getSearchHintData(getContext(this)).then((value) => { 31 SearcherAboutDataModel.getSearchHintData(getContext(this)).then((value) => {
29 if (value != null) { 32 if (value != null) {
@@ -44,7 +47,9 @@ export struct SearchComponent { @@ -44,7 +47,9 @@ export struct SearchComponent {
44 if (!this.hasInputContent) { 47 if (!this.hasInputContent) {
45 Scroll(this.scroller) { 48 Scroll(this.scroller) {
46 Column() { 49 Column() {
47 - SearchHistoryComponent({ searchHistoryData: $searchHistoryData }) 50 + if(this.searchHistoryData!=null && this.searchHistoryData.length>0){
  51 + SearchHistoryComponent({ searchHistoryData: $searchHistoryData, onDelHistory: (): void => this.getSearchHistoryData() })
  52 + }
48 53
49 //分隔符 54 //分隔符
50 Divider() 55 Divider()
@@ -101,9 +106,11 @@ export struct SearchComponent { @@ -101,9 +106,11 @@ export struct SearchComponent {
101 .enabled(false) 106 .enabled(false)
102 .focusable(false) 107 .focusable(false)
103 } 108 }
104 - TextInput({ text: this.searchText, placeholder: '', controller: this.controller }) 109 + Row(){
  110 + TextInput({ text: this.searchText, placeholder: '', controller: this.controller })
105 .caretColor(Color.Pink) 111 .caretColor(Color.Pink)
106 .fontSize('27lpx') 112 .fontSize('27lpx')
  113 + .layoutWeight(1)
107 .fontColor(Color.Black) 114 .fontColor(Color.Black)
108 .onChange((value: string) => { 115 .onChange((value: string) => {
109 this.searchText = value 116 this.searchText = value
@@ -114,14 +121,29 @@ export struct SearchComponent { @@ -114,14 +121,29 @@ export struct SearchComponent {
114 } 121 }
115 }) 122 })
116 .backgroundColor($r('app.color.color_transparent')) 123 .backgroundColor($r('app.color.color_transparent'))
  124 + .defaultFocus(true)
  125 + if(this.hasInputContent){
  126 + Image($r('app.media.search_input_del_icon'))
  127 + .width('31lpx')
  128 + .height('31lpx')
  129 + .objectFit(ImageFit.Cover)
  130 + .interpolation(ImageInterpolation.High)
  131 + .onClick(()=>{
  132 + this.searchText = ""
  133 + this.hasInputContent = false
  134 + })
  135 + }
  136 + }.padding({right:'30lpx'})
  137 + .layoutWeight(1)
117 } 138 }
118 .backgroundImage($r('app.media.search_page_input_bg')) 139 .backgroundImage($r('app.media.search_page_input_bg'))
119 .backgroundImageSize(ImageSize.Cover) 140 .backgroundImageSize(ImageSize.Cover)
120 .layoutWeight(1) 141 .layoutWeight(1)
121 .height('69lpx') 142 .height('69lpx')
122 143
  144 + //TODO 需要修改输入法 换行
123 //右 145 //右
124 - Text("取消") 146 + Text(this.hasInputContent?"搜索":"取消")
125 .textAlign(TextAlign.Center) 147 .textAlign(TextAlign.Center)
126 .fontWeight('400lpx') 148 .fontWeight('400lpx')
127 .fontSize('31lpx') 149 .fontSize('31lpx')
@@ -130,14 +152,15 @@ export struct SearchComponent { @@ -130,14 +152,15 @@ export struct SearchComponent {
130 .width('125lpx') 152 .width('125lpx')
131 .height('58lpx') 153 .height('58lpx')
132 .onClick(() => { 154 .onClick(() => {
133 - router.back()  
134 - // SearcherAboutDataModel.putSearchHistoryData(this.searchText)  
135 - // if(StringUtils.isNotEmpty(this.searchText)){  
136 - // if(this.searchHistoryData.length===10){  
137 - // this.searchHistoryData.pop()  
138 - // }  
139 - // this.searchHistoryData.push(new SearchHistoryItem(this.searchText))  
140 - // } 155 + if(this.hasInputContent){
  156 + if(StringUtils.isNotEmpty(this.searchText)){
  157 + SearcherAboutDataModel.putSearchHistoryData(this.searchText)
  158 + this.getSearchHistoryData()
  159 + ToastUtils.shortToast("插入一条搜索记录")
  160 + }
  161 + }else{
  162 + router.back()
  163 + }
141 }) 164 })
142 } 165 }
143 .height('85lpx') 166 .height('85lpx')
  1 +import SearcherAboutDataModel from '../../model/SearcherAboutDataModel'
1 import { SearchHistoryItem } from '../../viewmodel/SearchHistoryItem' 2 import { SearchHistoryItem } from '../../viewmodel/SearchHistoryItem'
  3 +import { MyCustomDialog } from '../reusable/MyCustomDialog'
2 4
3 /** 5 /**
4 * 搜索历史 6 * 搜索历史
@@ -6,6 +8,33 @@ import { SearchHistoryItem } from '../../viewmodel/SearchHistoryItem' @@ -6,6 +8,33 @@ import { SearchHistoryItem } from '../../viewmodel/SearchHistoryItem'
6 @Component 8 @Component
7 export struct SearchHistoryComponent{ 9 export struct SearchHistoryComponent{
8 @Link searchHistoryData:SearchHistoryItem[] 10 @Link searchHistoryData:SearchHistoryItem[]
  11 + onDelHistory?: () => void;
  12 + dialogController: CustomDialogController = new CustomDialogController({
  13 + builder: MyCustomDialog({
  14 + cancel: this.onCancel,
  15 + confirm: () => {
  16 + this.onAccept()
  17 + },
  18 + title: "确认清空历史记录",
  19 + tipShow:false
  20 + }),
  21 + autoCancel: true,
  22 + alignment: DialogAlignment.Center,
  23 + offset: { dx: 0, dy: -20 },
  24 + gridCount: 4,
  25 + customStyle: false
  26 + })
  27 +
  28 + onAccept(){
  29 + console.info('Callback when the second button is clicked')
  30 + //清空记录
  31 + this.searchHistoryData = []
  32 + SearcherAboutDataModel.delSearchHistoryData()
  33 + }
  34 +
  35 + onCancel() {
  36 + console.info('Callback when the first button is clicked')
  37 + }
9 38
10 build(){ 39 build(){
11 Column(){ 40 Column(){
@@ -24,7 +53,8 @@ export struct SearchHistoryComponent{ @@ -24,7 +53,8 @@ export struct SearchHistoryComponent{
24 .interpolation(ImageInterpolation.High) 53 .interpolation(ImageInterpolation.High)
25 .objectFit(ImageFit.Cover) 54 .objectFit(ImageFit.Cover)
26 .onClick(()=>{ 55 .onClick(()=>{
27 - //清空记录 56 + //弹框提示
  57 + this.dialogController.open()
28 }) 58 })
29 }.justifyContent(FlexAlign.SpaceBetween) 59 }.justifyContent(FlexAlign.SpaceBetween)
30 .margin({bottom:'17lpx'}) 60 .margin({bottom:'17lpx'})
@@ -35,9 +65,10 @@ export struct SearchHistoryComponent{ @@ -35,9 +65,10 @@ export struct SearchHistoryComponent{
35 GridItem(){ 65 GridItem(){
36 Row(){ 66 Row(){
37 Text(`${item.searchContent}`) 67 Text(`${item.searchContent}`)
38 - .height('23lpx')  
39 .fontColor($r('app.color.color_222222')) 68 .fontColor($r('app.color.color_222222'))
40 - .fontSize('23lpx') 69 + .fontSize('31lpx')
  70 + .fontWeight('400lpx')
  71 + .lineHeight('46lpx')
41 .maxLines(1) 72 .maxLines(1)
42 .constraintSize({maxWidth:index%2 === 0?'270lpx':'250lpx'}) 73 .constraintSize({maxWidth:index%2 === 0?'270lpx':'250lpx'})
43 .textOverflow({ overflow: TextOverflow.Ellipsis }) 74 .textOverflow({ overflow: TextOverflow.Ellipsis })
@@ -49,6 +80,12 @@ export struct SearchHistoryComponent{ @@ -49,6 +80,12 @@ export struct SearchHistoryComponent{
49 .margin({right:'31lpx',left:'4lpx'}) 80 .margin({right:'31lpx',left:'4lpx'})
50 .interpolation(ImageInterpolation.High) 81 .interpolation(ImageInterpolation.High)
51 .objectFit(ImageFit.Cover) 82 .objectFit(ImageFit.Cover)
  83 + .onClick(()=>{
  84 + SearcherAboutDataModel.delSearchSingleHistoryData(index)
  85 + if (this.onDelHistory !== undefined) {
  86 + this.onDelHistory()
  87 + }
  88 + })
52 89
53 Blank() 90 Blank()
54 91
1 1
2 -import { Logger, ResourcesUtils, UserDataLocal } from 'wdKit'; 2 +import { Logger, ResourcesUtils, SPHelper, UserDataLocal } from 'wdKit';
3 import { HttpUrlUtils, ResponseDTO, WDHttp } from 'wdNetwork'; 3 import { HttpUrlUtils, ResponseDTO, WDHttp } from 'wdNetwork';
4 import HashMap from '@ohos.util.HashMap'; 4 import HashMap from '@ohos.util.HashMap';
5 import { SearchHistoryItem } from '../viewmodel/SearchHistoryItem'; 5 import { SearchHistoryItem } from '../viewmodel/SearchHistoryItem';
@@ -12,7 +12,8 @@ const TAG = "SearcherAboutDataModel" @@ -12,7 +12,8 @@ const TAG = "SearcherAboutDataModel"
12 */ 12 */
13 class SearcherAboutDataModel{ 13 class SearcherAboutDataModel{
14 private static instance: SearcherAboutDataModel; 14 private static instance: SearcherAboutDataModel;
15 - searchHistoryData:SearchHistoryItem[] = [] 15 + public searchHistoryData:SearchHistoryItem[] = []
  16 + public SEARCH_HISTORY_KEY:string = "SEARCH_HISTORY_KEY" + UserDataLocal.userId
16 17
17 private constructor() { } 18 private constructor() { }
18 19
@@ -28,51 +29,55 @@ class SearcherAboutDataModel{ @@ -28,51 +29,55 @@ class SearcherAboutDataModel{
28 } 29 }
29 30
30 /** 31 /**
31 - * 静态搜索历史记录 32 + * 插入搜索记录(单个)
32 */ 33 */
33 - getSearchHistoryData():SearchHistoryItem[]{  
34 - if(this.searchHistoryData.length > 0){  
35 - return this.searchHistoryData 34 + public async putSearchHistoryData(content:string){
  35 + let history = SPHelper.default.getSync(this.SEARCH_HISTORY_KEY,"[]") as string
  36 + this.searchHistoryData = JSON.parse(history)
  37 + this.searchHistoryData.splice(0,0,new SearchHistoryItem(content))
  38 + await SPHelper.default.saveSync(this.SEARCH_HISTORY_KEY, JSON.stringify(this.searchHistoryData));
  39 + }
  40 +
  41 + /**
  42 + * 删除搜索记录(所有)
  43 + */
  44 + public async delSearchHistoryData(){
  45 + SPHelper.default.deleteSync(this.SEARCH_HISTORY_KEY)
  46 + this.searchHistoryData = []
  47 + }
  48 + /**
  49 + * 删除搜索记录(单个)
  50 + */
  51 + public async delSearchSingleHistoryData(index:number){
  52 + if(this.searchHistoryData!=null && this.searchHistoryData.length>0){
  53 + this.searchHistoryData.splice(index,1)
  54 + }else{
  55 + let history = SPHelper.default.getSync(this.SEARCH_HISTORY_KEY,"[]") as string
  56 + this.searchHistoryData = JSON.parse(history)
  57 + this.searchHistoryData.splice(index,1)
36 } 58 }
37 - this.searchHistoryData.push(new SearchHistoryItem("评论",UserDataLocal.userId,""))  
38 - this.searchHistoryData.push(new SearchHistoryItem("关注关注",UserDataLocal.userId,""))  
39 - this.searchHistoryData.push(new SearchHistoryItem("收藏收藏收藏",UserDataLocal.userId,""))  
40 - this.searchHistoryData.push(new SearchHistoryItem("历史",UserDataLocal.userId,""))  
41 - this.searchHistoryData.push(new SearchHistoryItem("消息消息消息消息消息",UserDataLocal.userId,""))  
42 - this.searchHistoryData.push(new SearchHistoryItem("留言板留言板留言板留言板留言板",UserDataLocal.userId,""))  
43 - this.searchHistoryData.push(new SearchHistoryItem("预约",UserDataLocal.userId,""))  
44 - return this.searchHistoryData 59 + SPHelper.default.saveSync(this.SEARCH_HISTORY_KEY, JSON.stringify(this.searchHistoryData))
45 } 60 }
46 61
47 - // async putSearchHistoryData(content:string){  
48 - // let history = await SPHelper.default.get(SearcherAboutDataModel.SEARCH_HISTORY_KEY, '[]') as string;  
49 - // this.searchHistoryData = JSON.parse(history)  
50 - // this.searchHistoryData.push(new SearchHistoryItem(content))  
51 - // await SPHelper.default.save(SearcherAboutDataModel.SEARCH_HISTORY_KEY, JSON.stringify(this.searchHistoryData));  
52 - // }  
53 -  
54 - // getSearchHistoryData():Promise<SearchHistoryItem[]>{  
55 - // return new Promise<SearchHistoryItem[]>(async (success, error) => {  
56 - // if(this.searchHistoryData!=null && this.searchHistoryData.length>0){  
57 - // success(this.searchHistoryData)  
58 - // return  
59 - // }  
60 - // try {  
61 - // let history = await SPHelper.default.get(SearcherAboutDataModel.SEARCH_HISTORY_KEY, '') as string;  
62 - // console.log('ycg',history);  
63 - // }catch (error){  
64 - // console.log('ycg',"1111");  
65 - // }  
66 - //  
67 - // this.searchHistoryData = JSON.parse(history)  
68 - // this.searchHistoryData.push(new SearchHistoryItem("评论"))  
69 - // this.searchHistoryData.push(new SearchHistoryItem("关注关注"))  
70 - // this.searchHistoryData.push(new SearchHistoryItem("收藏收藏收藏"))  
71 - //  
72 - // success(this.searchHistoryData)  
73 - // })  
74 - // } 62 + /**
  63 + * 查询搜索记录(所有)
  64 + */
  65 + public getSearchHistoryData() : SearchHistoryItem[] {
  66 + if(this.searchHistoryData!=null && this.searchHistoryData.length>0){
  67 + if(this.searchHistoryData.length>10){
  68 + this.searchHistoryData.splice(10,this.searchHistoryData.length - 10)
  69 + }
  70 + return this.searchHistoryData
  71 + }
  72 + let history = SPHelper.default.getSync(this.SEARCH_HISTORY_KEY,"[]") as string
75 73
  74 + this.searchHistoryData = JSON.parse(history)
  75 + if(this.searchHistoryData.length>10){
  76 + this.searchHistoryData.splice(10,this.searchHistoryData.length - 10)
  77 + }
  78 +
  79 + return this.searchHistoryData
  80 + }
76 81
77 /** 82 /**
78 * 首页 搜索提示滚动内容 83 * 首页 搜索提示滚动内容
1 export class SearchHistoryItem{ 1 export class SearchHistoryItem{
2 searchContent:string = "" 2 searchContent:string = ""
3 - searchUserId:string = ""  
4 - searchTime:string = ""  
5 3
6 - constructor(searchContent: string,searchUserId:string,searchTime:string) { 4 +
  5 + constructor(searchContent: string) {
7 this.searchContent = searchContent 6 this.searchContent = searchContent
8 - this.searchUserId = searchUserId  
9 - this.searchTime = searchTime  
10 } 7 }
11 } 8 }