fxyk-navbar.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <u-navbar :is-back="false" title="" :border-bottom="false" :background="background">
  3. <view class="slot-wrap">
  4. <view class="navbar d-flex a-center j-sb w-100" :class="{'search': isSearch}">
  5. <view v-if="isNeedReturn" class="white-color" :style="{'color': iconColor}" @click="jump_back">
  6. <u-icon name="arrow-left"></u-icon>
  7. </view>
  8. <view v-else class="hide_color">>>></view>
  9. <view v-if="!isSearch" class="white-color d-flex a-center">{{ title }}</view>
  10. <view v-else style="width:70%">
  11. <fxyk-search ref="search" />
  12. </view>
  13. <view v-if="!isSearch" class="hide_color">>></view>
  14. <view v-else ><u-button shape="circle" :custom-style="buttonStyle" :throttle-time="500" @click="search">搜索</u-button></view>
  15. </view>
  16. </view>
  17. <u-toast ref="uToast" />
  18. </u-navbar>
  19. </template>
  20. <script>
  21. import { mapMutations } from 'vuex'
  22. export default {
  23. props: {
  24. // 是否为搜索页
  25. isSearch: {
  26. type: Boolean,
  27. default: false
  28. },
  29. // 标题
  30. title: {
  31. type: String,
  32. default: ''
  33. },
  34. // 是否需要返回箭头
  35. isNeedReturn: {
  36. type: Boolean,
  37. default: true
  38. },
  39. // 返回箭头颜色
  40. iconColor: {
  41. type: String,
  42. default: '#fff'
  43. }
  44. },
  45. data() {
  46. return {
  47. background: {
  48. backgroundColor: "#40349C",
  49. },
  50. value: ''
  51. }
  52. },
  53. computed: {
  54. buttonStyle() {
  55. return {
  56. 'background-color': '#40349C',
  57. 'color': '#fff',
  58. 'height': '60rpx',
  59. 'width': '100rpx',
  60. 'font-size': '12px'
  61. }
  62. },
  63. },
  64. methods: {
  65. ...mapMutations(['updateSearchItemData', 'clearSearchData']),
  66. // 返回上一页
  67. jump_back() {
  68. uni.navigateBack()
  69. },
  70. // 搜索
  71. search() {
  72. let result = this.$refs.search.value
  73. // 获取搜索框中输入的值
  74. if (!result) {
  75. this.$refs.uToast.show({
  76. title: '请输入搜索内容',
  77. type: 'error',
  78. icon: false
  79. })
  80. return
  81. }
  82. let data = []
  83. // 获取历史搜索记录
  84. let searchData = uni.getStorageSync('searchData') ? uni.getStorageSync('searchData') : []
  85. // 该搜索条件是否已经存在
  86. let index = searchData.findIndex( (value) => result.toString() === value )
  87. if(index === -1 ) {
  88. data.push(result, ...searchData)
  89. // 存入本地缓存
  90. uni.setStorageSync('searchData', data)
  91. // 存入vuex
  92. this.clearSearchData(data)
  93. }
  94. uni.navigateTo({
  95. url: '/pages/index/searchData?status=1'
  96. })
  97. // 清空搜索框内数据
  98. this.$refs.search.value = ''
  99. this.updateSearchItemData()
  100. }
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .slot-wrap {
  106. display: flex;
  107. align-items: center;
  108. height: 100%;
  109. /* 如果您想让slot内容占满整个导航栏的宽度 */
  110. flex: 1;
  111. /* 如果您想让slot内容与导航栏左右有空隙 */
  112. // padding: 0 30rpx;
  113. .navbar {
  114. height: 120rpx;
  115. padding: 0 2%;
  116. .white-color {
  117. font-size: 40rpx;
  118. font-family: SimSun;
  119. font-weight: 400;
  120. line-height: 44px;
  121. color: #FFFFFF;
  122. }
  123. .hide_color {
  124. color: $yk-main-bg-color;
  125. }
  126. }
  127. }
  128. .search {
  129. background-color: $yk-bg-color;
  130. margin-top: 80rpx;
  131. }
  132. </style>