You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
4.6 KiB

4 years ago
  1. <template>
  2. <view class="app column">
  3. <view class="search-wrap center">
  4. <view class="input-box row">
  5. <text class="mix-icon icon-sousuo"></text>
  6. <input
  7. class="input"
  8. type="text"
  9. placeholder="请输入搜索关键字"
  10. maxlength="20"
  11. v-model="keyword"
  12. @confirm="search"
  13. confirm-type="search"
  14. />
  15. <text v-if="keyword" class="mix-icon icon-guanbi2" @click="clearInput"></text>
  16. </view>
  17. <view class="search-btn center" @click="search">
  18. <text>搜索</text>
  19. </view>
  20. </view>
  21. <view class="content">
  22. <view v-if="historyList.length > 0" class="s-header row">
  23. <text class="tit">历史搜索</text>
  24. <text class="mix-icon icon-lajitong" @click="showPopup('mixModal')"></text>
  25. </view>
  26. <view v-if="historyList.length > 0" class="list" style="margin-bottom: 20rpx;">
  27. <view class="item center" v-for="(item, index) in historyList" :key="index" @click="search(item)">
  28. <text>{{ item }}</text>
  29. </view>
  30. </view>
  31. <view v-if="hotList.length > 0" class="s-header row">
  32. <text class="tit">热门搜索</text>
  33. </view>
  34. <view v-if="hotList.length > 0" class="list">
  35. <view class="item center" v-for="(item, index) in hotList" :key="index" @click="search(item.name)">
  36. <text>{{ item.name }}</text>
  37. </view>
  38. </view>
  39. </view>
  40. <mix-modal ref="mixModal" text="您确定要清除搜索历史吗" @onConfirm="delHistory"></mix-modal>
  41. </view>
  42. </template>
  43. <script>
  44. export default {
  45. data() {
  46. return {
  47. keyword: '',
  48. hotList: [],
  49. historyList: []
  50. }
  51. },
  52. onLoad(options) {
  53. this.sourcePage = options.sourcePage;
  54. this.loadHotKeywords();
  55. this.loadHistory();
  56. },
  57. methods: {
  58. //加载热搜关键词
  59. async loadHotKeywords(){
  60. const response = await this.$request('search', 'get');
  61. this.hotList = response.data;
  62. },
  63. //加载历史搜索
  64. loadHistory() {
  65. uni.getStorage({
  66. key: 'keywordHistoryList',
  67. success: res => {
  68. this.historyList = res.data || [];
  69. }
  70. });
  71. },
  72. //执行搜索
  73. search(keyword) {
  74. if(typeof keyword !== 'string'){
  75. keyword = this.keyword;
  76. }else{
  77. keyword = keyword.trim();
  78. this.keyword = keyword;
  79. }
  80. if(!keyword){
  81. this.$util.msg('请输入搜索关键字,如 狗粮');
  82. return;
  83. }
  84. this.saveKeyword(); //保存为历史
  85. this.$request('search', 'update', {
  86. keyword
  87. });
  88. if(this.sourcePage === 'productList'){
  89. this.$util.prePage().keyword = keyword;
  90. this.$util.prePage().refreshList();
  91. uni.navigateBack();
  92. }else{
  93. this.navTo(`/pages/product/list?keyword=${keyword}&sourcePage=search`);
  94. }
  95. },
  96. //保存关键字到历史记录
  97. saveKeyword() {
  98. let list = uni.getStorageSync('keywordHistoryList');
  99. if(!list){
  100. list = [];
  101. }
  102. const index = list.findIndex(item=>item === this.keyword);
  103. if(index > -1){
  104. list.splice(index, 1);
  105. }
  106. list.unshift(this.keyword);
  107. //只保存30条记录
  108. if(list.length > 30){
  109. list.length = 30;
  110. }
  111. this.historyList = list;
  112. uni.setStorage({
  113. key: 'keywordHistoryList',
  114. data: list
  115. })
  116. },
  117. //清除历史搜索
  118. delHistory() {
  119. this.historyList = [];
  120. uni.removeStorage({
  121. key: 'keywordHistoryList'
  122. });
  123. },
  124. //清空输入框
  125. clearInput(){
  126. this.keyword = '';
  127. }
  128. }
  129. }
  130. </script>
  131. <style scoped lang='scss'>
  132. .search-wrap{
  133. padding-left: 24rpx;
  134. height: 100rpx;
  135. .icon-sousuo{
  136. padding: 0 12rpx 0 20rpx;
  137. font-size: 40rpx;
  138. color: #999;
  139. }
  140. .input-box {
  141. width: 604rpx;
  142. height: 80rpx;
  143. border-radius: 100rpx;
  144. background: #f5f6f7;
  145. }
  146. .input{
  147. flex: 1;
  148. font-size: 30rpx;
  149. color: #333;
  150. }
  151. .icon-guanbi2{
  152. padding: 10rpx 20rpx;
  153. font-size: 32rpx;
  154. color: #999;
  155. }
  156. .search-btn {
  157. flex-shrink: 0;
  158. padding: 0 24rpx 0 20rpx;
  159. font-size: 32rpx;
  160. color: #007aff;
  161. }
  162. }
  163. .content {
  164. flex: 1;
  165. padding-top: 24rpx;
  166. border-radius: 28rpx 28rpx 0 0;
  167. background-color: #fff;
  168. }
  169. .s-header{
  170. height: 80rpx;
  171. padding: 0 32rpx 0 40rpx;
  172. .tit{
  173. flex: 1;
  174. font-size: 30rpx;
  175. color: #333;
  176. font-weight: 700;
  177. }
  178. .icon-lajitong{
  179. padding: 10rpx;
  180. font-size: 36rpx;
  181. color: #333;
  182. }
  183. }
  184. .list{
  185. display: flex;
  186. flex-wrap: wrap;
  187. padding: 10rpx 0 0 36rpx;
  188. .item{
  189. min-width: 60rpx;
  190. height: 58rpx;
  191. padding: 0 24rpx;
  192. margin: 0 24rpx 24rpx 0;
  193. border-radius: 100rpx;
  194. background-color: #f5f6f7;
  195. font-size: 26rpx;
  196. color: #666;
  197. }
  198. }
  199. </style>