searchResult.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <view>
  3. <u-navbar title="发布内容" v-show="false" ref="refNavbar" >
  4. </u-navbar>
  5. <!-- 搜索框 -->
  6. <view class="search" >
  7. <u-icon name="arrow-left" size="36" @tap="goBack" ></u-icon>
  8. <u-search ref="searchinput"
  9. v-model="key" @blur="blur()" @focus="focus()" @custom="submit" @search="submit" @keyup.enter="submit"
  10. search-icon="none"></u-search>
  11. </view>
  12. <!-- 搜索历史 -->
  13. <view class="main" v-show="step">
  14. <view class="search-history" >
  15. <view class="title">
  16. 搜索历史
  17. </view>
  18. <view class="clear" @click="clear()">
  19. 清空历史
  20. </view>
  21. </view>
  22. <view class="history">
  23. <view class="item" v-for="(mod,index) in searchHistory" :key="index" v-text="mod" @click="key=mod,submit()">
  24. 荆州
  25. </view>
  26. </view>
  27. </view>
  28. <!-- 搜索结果 -->
  29. <view class="result" v-show="!step">
  30. <view class="result-item" v-for="(item,i) in list" @click="ckInfo(item.id)" :key="i" >
  31. <view class="content">
  32. <view class="title">
  33. {{item.title}}
  34. </view>
  35. <view class="infos">
  36. <view class="classify">
  37. {{item.typeName}}
  38. </view>
  39. <view class="date">
  40. {{substrDate(item.createTime)}}
  41. </view>
  42. </view>
  43. </view>
  44. <view class="picture">
  45. <img v-if="item.pic" :src="item.pic" alt="">
  46. <img v-else src="@/assets/img/default_img.png" alt="">
  47. </view>
  48. </view>
  49. <u-divider :isnone="list.length==0" nonetext="没有找到相关内容" border-color="#CFD2D5">已经到底了</u-divider>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import * as API from '@/apis/pagejs/news.js'
  55. export default {
  56. data() {
  57. return {
  58. step:true,//步骤 ,true第一步查询,false第二步显示结果
  59. key:"",
  60. recordsTotal:0,
  61. listForm:{
  62. typeId:"",
  63. title:"",
  64. pageIndex: 1,
  65. pageSize: 4,
  66. totalPage: 1,
  67. },
  68. list: [
  69. ],
  70. searchHistory:[],
  71. }
  72. },
  73. computed:{
  74. },
  75. onLoad(op){
  76. if(op.type){
  77. this.listForm.typeId=op.type
  78. }
  79. var sz=this.carhelp.get("setSearchHistory");
  80. if(sz){
  81. this.searchHistory=sz
  82. }else{
  83. this.searchHistory= []
  84. }
  85. },
  86. onReachBottom() {
  87. if (this.list.length < this.recordsTotal) {
  88. this.myLoadmore();
  89. }
  90. },
  91. methods: {
  92. ckInfo(id){
  93. var url="/pages/news/articleDetails?id="+id;
  94. uni.navigateTo({
  95. url:url
  96. })
  97. },
  98. myLoadmore(){
  99. this.listForm.pageIndex += 1;
  100. this.query();
  101. },
  102. setSearchHistory(obj){
  103. this.searchHistory=obj
  104. this.carhelp.set("setSearchHistory",obj)
  105. },
  106. focus(){
  107. this.step=true;
  108. },
  109. blur(){
  110. // if(this.step&&this.$refs.searchinput.getRef()){
  111. // this.$refs.searchinput.getRef().focus()//自动聚焦
  112. // }
  113. },
  114. clear(){
  115. this.setSearchHistory([]);
  116. mui.toast("搜索内容已清空");
  117. },
  118. goBack(){
  119. this.$refs.refNavbar.goBack()
  120. },
  121. setHistory(){
  122. //搜索记录保存
  123. console.log(this.key)
  124. var sz=this.searchHistory;
  125. if(!sz){
  126. sz=[];
  127. }
  128. var temp =[];
  129. //去重 ,后插入的,排队到最前面
  130. if(sz.length){
  131. for(var i in sz){
  132. if(i==0){
  133. temp.push(this.key);
  134. }
  135. if(sz[i]==this.key){
  136. continue;
  137. }
  138. temp.push(sz[i]);
  139. if(temp.length==10){
  140. break
  141. }
  142. }
  143. }else{
  144. temp.push(this.key);
  145. }
  146. this.setSearchHistory(temp);
  147. },
  148. query(){
  149. this.listForm.title=this.key
  150. uni.showLoading({
  151. title: "加载中",
  152. mask: true,
  153. })
  154. API.pageList(this.listForm).then((res) => {
  155. uni.hideLoading();
  156. if(this.listForm.pageIndex==1){
  157. this.list = res.data.data;
  158. }else{
  159. this.list = [
  160. ...this.list,
  161. ...res.data.data
  162. ];
  163. }
  164. this.recordsTotal = res.data.recordsTotal;
  165. }).catch(error => {
  166. uni.showToast({
  167. title: error,
  168. icon: "none"
  169. })
  170. })
  171. }, submit(){
  172. if(!this.key){
  173. return
  174. }
  175. this.setHistory()
  176. this.step=false;
  177. this.listForm.pageIndex=1
  178. // var ref =this.$refs.searchinput.getRef()
  179. // ref.blur()
  180. this.query();
  181. }
  182. }
  183. }
  184. </script>
  185. <style lang="scss" scoped>
  186. page{
  187. background-color: #fff;
  188. font-family: 'Regular';
  189. }
  190. // 搜索框
  191. .search{
  192. padding:12rpx 24rpx;
  193. display: flex;
  194. align-items: center;
  195. .u-icon--right{
  196. margin-right: 24rpx;
  197. }
  198. /deep/.u-content{
  199. border-radius: 8px !important;
  200. }
  201. /deep/.u-action{
  202. color: rgba(31, 74, 153, 1);
  203. font-size: 16px;
  204. }
  205. }
  206. // 搜索历史
  207. .main{
  208. margin: 36rpx;
  209. .search-history{
  210. display: flex;
  211. justify-content: space-between;
  212. align-items: center;
  213. .title{
  214. color: #111111;
  215. }
  216. .clear{
  217. color: rgba(31, 74, 153, 1);
  218. font-size: 12px;
  219. }
  220. }
  221. .history{
  222. // display: flex;
  223. margin-top: 24rpx;
  224. .item{
  225. min-width: 140rpx;
  226. text-align: center;
  227. display: inline-block;
  228. border-radius:8px;
  229. background-color: rgba(239, 240, 245, 1);
  230. color: rgba(88, 88, 88, 1);
  231. height: 66rpx;
  232. line-height: 66rpx;
  233. margin-right: 26rpx;
  234. margin-top:12rpx;
  235. font-size: 28rpx;
  236. padding: 2rpx 48rpx;
  237. }
  238. }
  239. }
  240. // 搜索结果
  241. .result{
  242. padding: 36rpx 32rpx;
  243. .result-item{
  244. display: flex;
  245. justify-content: space-between;
  246. margin-bottom: 40rpx;
  247. .content{
  248. display: flex;
  249. width: 100%;
  250. justify-content: space-between;
  251. flex-direction: column;
  252. .title{
  253. color: rgba(51, 51, 51, 1);
  254. font-size: 32rpx;
  255. text-align: justify;
  256. }
  257. .infos{
  258. display: flex;
  259. justify-content: space-between;
  260. align-items: center;
  261. .classify{
  262. color: rgba(119, 119, 119, 1);
  263. font-size: 24rpx;
  264. }
  265. .date{
  266. color: rgba(119, 119, 119, 1);
  267. font-size:24rpx;
  268. }
  269. }
  270. }
  271. .picture{
  272. margin-left: 24rpx;
  273. img{
  274. width: 224rpx;
  275. height: 172rpx;
  276. border-radius: 5px;
  277. }
  278. }
  279. }
  280. }
  281. </style>