search.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <view class="page">
  3. <view class="p-bar flex-row">
  4. <view class="align-center justify-between bar">
  5. <view class="align-center">
  6. <image
  7. src="/static/mang-sear.png"
  8. class="image_5"
  9. />
  10. <input type="text" placeholder="请输入商品名称" class="inpt" v-model="keyword" @confirm="search">
  11. </view>
  12. <image
  13. src="/static/mang-quxiao.png"
  14. class="image_6"
  15. v-if="hasKeyword"
  16. @tap='clearKW'
  17. />
  18. </view>
  19. <text class="text_3" @tap="search">搜索</text>
  20. </view>
  21. <view class="p-history" v-if="!hasKeyword && search_lishi.length>0">
  22. <view class="title">
  23. 历史搜索
  24. </view>
  25. <view class="item-wrap align-center">
  26. <view class="item" v-for="(item,i) in search_lishi" :key="i" @tap="tapKeyword(item)">
  27. {{item}}
  28. </view>
  29. </view>
  30. </view>
  31. <singleList ref="listRef" url="goods.list" :sendData="reqData" @reqEnd="searchEnd" v-else>
  32. <template v-slot:items="{list}">
  33. <view class="p-content">
  34. <proItem :info="item" v-for="(item,i) in list" :key="i" />
  35. <empty v-if="sRes && sRes==='empty'"/>
  36. </view>
  37. </template>
  38. </singleList>
  39. </view>
  40. </template>
  41. <script>
  42. import proItem from './components/pro-item.vue'
  43. export default {
  44. components: {
  45. proItem,
  46. },
  47. data() {
  48. return {
  49. keyword:'',
  50. search_lishi:[],
  51. reqData:{},
  52. sRes:'',
  53. };
  54. },
  55. computed:{
  56. hasKeyword(){
  57. if(this.keyword){
  58. return true;
  59. }
  60. return false;
  61. }
  62. },
  63. onShow(){ //这里一进来就查询之前留下的搜索内容 别忘了退出登录的时候删掉
  64. // console.log(uni.getStorageSync('history'))
  65. this.search_lishi = uni.getStorageSync('history') || [] //存放历史记录的数组
  66. },
  67. onReachBottom() {
  68. this.$refs.listRef.getList();
  69. },
  70. methods:{
  71. search(){
  72. if(this.hasKeyword){
  73. this.add_history(this.keyword)
  74. }
  75. this.$nextTick(()=>{
  76. this.reqData = {keyword:this.keyword}
  77. })
  78. },
  79. tapKeyword(item){
  80. this.keyword = item;
  81. this.search();
  82. },
  83. clearKW(){
  84. this.keyword = '';
  85. },
  86. //添加新的历史记录 当有搜索新的历史记录时就会进入这个方法
  87. add_history(e){
  88. //存在,flag1==0
  89. try{
  90. let flag1= this.search_lishi.findIndex(item => item=== e) //判断数组里面有没有重复查询过的内容
  91. if(flag1 != -1){ //有重复内容
  92. // if(flag1 != 0){ //如果删除0会出错
  93. this.search_lishi.splice(flag1,1) //删除重复重新添加一个
  94. this.search_lishi.unshift(e) //添加一个内容
  95. // }
  96. }else{
  97. if(this.search_lishi.length >= 16){
  98. this.search_lishi.splice(15,1) //删除最后一个新添加一个
  99. this.search_lishi.unshift(e) //添加一个内容
  100. }else{
  101. this.search_lishi.unshift(e) //添加一个内容
  102. }
  103. }
  104. uni.setStorageSync('history', this.search_lishi);//保存用户搜索历史
  105. this.search_lishi = uni.getStorageSync('history') //存放历史记录的数组
  106. }catch(e){
  107. this.search_lishi.push(e) //添加一个内容
  108. //TODO handle the exception
  109. }
  110. },
  111. searchEnd({loadingType,pageOneRes}){
  112. if(loadingType === 'noMore' && pageOneRes && pageOneRes.length === 0){
  113. this.sRes='empty';
  114. }else{
  115. this.sRes=loadingType;
  116. }
  117. },
  118. },
  119. }
  120. </script>
  121. <style lang="scss">
  122. page{
  123. background-color: #f8f8f8;
  124. }
  125. .page{
  126. padding: 30rpx;
  127. }
  128. .p-bar{
  129. .bar {
  130. flex: 1 1 auto;
  131. border-radius: 32rpx;
  132. height: 64rpx;
  133. border: solid 2rpx #92b99c;
  134. padding: 0 20rpx;
  135. }
  136. .image_5 {
  137. flex-shrink: 0;
  138. width: 31rpx;
  139. height: 31rpx;
  140. }
  141. .inpt {
  142. margin-left: 17rpx;
  143. height: 60rpx;
  144. line-height: 60rpx;
  145. color: #333;
  146. font-size: 28rpx;
  147. font-family: PingFang;
  148. letter-spacing: 1.12rpx;
  149. }
  150. .image_6 {
  151. margin-bottom: 3rpx;
  152. width: 28rpx;
  153. height: 28rpx;
  154. }
  155. .text_3 {
  156. margin-left: 28rpx;
  157. margin-right: 4rpx;
  158. align-self: center;
  159. color: #92b99c;
  160. font-size: 28rpx;
  161. font-family: PingFang;
  162. line-height: 27rpx;
  163. letter-spacing: 1.12rpx;
  164. }
  165. }
  166. .p-history{
  167. padding-top: 10rpx;
  168. .title{
  169. height: 90rpx;
  170. line-height: 90rpx;
  171. font-size: 32rpx;
  172. font-family: PingFang SC;
  173. font-weight: bold;
  174. color: #333333;
  175. }
  176. .item-wrap{
  177. flex-wrap: wrap;
  178. }
  179. .item{
  180. height: 54rpx;
  181. line-height: 54rpx;
  182. padding: 0 20rpx;
  183. background: #ECEFED;
  184. border-radius: 27px;
  185. margin-right: 24rpx;
  186. margin-bottom: 24rpx;
  187. font-size: 24rpx;
  188. font-family: PingFang SC;
  189. font-weight: 500;
  190. color: #333333;
  191. }
  192. }
  193. .p-content{
  194. padding-top: 40rpx;
  195. .item-wrap{
  196. padding: 24rpx 14rpx 24rpx 24rpx;
  197. background-color: #ffffff;
  198. box-shadow: 0px 6rpx 10rpx #2a2a2a14;
  199. border-radius: 20rpx;
  200. margin-bottom: 24rpx;
  201. .image_7 {
  202. width: 200rpx;
  203. height: 200rpx;
  204. }
  205. .group_8 {
  206. margin-left: 18rpx;
  207. margin-bottom: 3rpx;
  208. }
  209. .text_4 {
  210. color: #333333;
  211. font-size: 30rpx;
  212. font-family: PingFang;
  213. max-width: 400rpx;
  214. }
  215. .image_8 {
  216. margin-top: 14rpx;
  217. width: 28rpx;
  218. height: 28rpx;
  219. margin-right: 4rpx;
  220. }
  221. .text_5 {
  222. margin-left: 3rpx;
  223. margin-top: 18rpx;
  224. color: #999999;
  225. font-size: 24rpx;
  226. font-family: PingFang;
  227. }
  228. .group_10 {
  229. margin-top: 57rpx;
  230. padding: 0 3rpx;
  231. }
  232. .group_11 {
  233. line-height: 28rpx;
  234. height: 29rpx;
  235. }
  236. .text_6 {
  237. color: #ff3e3e;
  238. font-size: 24rpx;
  239. font-family: PingFang;
  240. }
  241. .text_7 {
  242. color: #ff3e3e;
  243. font-size: 36rpx;
  244. font-family: PingFang;
  245. }
  246. .text_8 {
  247. margin-left: 10rpx;
  248. margin-top: 9rpx;
  249. color: #999999;
  250. font-size: 24rpx;
  251. font-family: PingFang;
  252. text-decoration: line-through;
  253. }
  254. .text_9 {
  255. align-self: center;
  256. color: #999999;
  257. font-size: 24rpx;
  258. font-family: PingFang;
  259. }
  260. }
  261. }
  262. </style>