index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {
  2. getSearchHistory,
  3. getSearchPopular,
  4. } from '../../../services/good/fetchSearchHistory';
  5. Page({
  6. data: {
  7. historyWords: [],
  8. popularWords: [],
  9. searchValue: '',
  10. dialog: {
  11. title: '确认删除当前历史记录',
  12. showCancelButton: true,
  13. message: '',
  14. },
  15. dialogShow: false,
  16. },
  17. deleteType: 0,
  18. deleteIndex: '',
  19. onShow() {
  20. this.queryHistory();
  21. this.queryPopular();
  22. },
  23. async queryHistory() {
  24. try {
  25. const data = await getSearchHistory();
  26. const code = 'Success';
  27. if (String(code).toUpperCase() === 'SUCCESS') {
  28. const { historyWords = [] } = data;
  29. this.setData({
  30. historyWords,
  31. });
  32. }
  33. } catch (error) {
  34. console.error(error);
  35. }
  36. },
  37. async queryPopular() {
  38. try {
  39. const data = await getSearchPopular();
  40. const code = 'Success';
  41. if (String(code).toUpperCase() === 'SUCCESS') {
  42. const { popularWords = [] } = data;
  43. this.setData({
  44. popularWords,
  45. });
  46. }
  47. } catch (error) {
  48. console.error(error);
  49. }
  50. },
  51. confirm() {
  52. const { historyWords } = this.data;
  53. const { deleteType, deleteIndex } = this;
  54. historyWords.splice(deleteIndex, 1);
  55. if (deleteType === 0) {
  56. this.setData({
  57. historyWords,
  58. dialogShow: false,
  59. });
  60. } else {
  61. this.setData({ historyWords: [], dialogShow: false });
  62. }
  63. },
  64. close() {
  65. this.setData({ dialogShow: false });
  66. },
  67. handleClearHistory() {
  68. const { dialog } = this.data;
  69. this.deleteType = 1;
  70. this.setData({
  71. dialog: {
  72. ...dialog,
  73. message: '确认删除所有历史记录',
  74. },
  75. dialogShow: true,
  76. });
  77. },
  78. deleteCurr(e) {
  79. const { index } = e.currentTarget.dataset;
  80. const { dialog } = this.data;
  81. this.deleteIndex = index;
  82. this.setData({
  83. dialog: {
  84. ...dialog,
  85. message: '确认删除当前历史记录',
  86. deleteType: 0,
  87. },
  88. dialogShow: true,
  89. });
  90. },
  91. handleHistoryTap(e) {
  92. const { historyWords } = this.data;
  93. const { dataset } = e.currentTarget;
  94. const _searchValue = historyWords[dataset.index || 0] || '';
  95. if (_searchValue) {
  96. wx.navigateTo({
  97. url: `/pages/goods/result/index?searchValue=${_searchValue}`,
  98. });
  99. }
  100. },
  101. handleSubmit(e) {
  102. const { value } = e.detail.value;
  103. if (value.length === 0) return;
  104. wx.navigateTo({
  105. url: `/pages/goods/result/index?searchValue=${value}`,
  106. });
  107. },
  108. });