SearchHeader2.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <div class="search-content com-mobile-header">
  3. <div class="options" @click="setShowSearchType(!showSearchType, $event)">
  4. {{searchType | typeFilter}}
  5. <i></i>
  6. <ul v-if="showSearchType">
  7. <li v-show="searchType !== type" v-for="type in searchTypeList" @click="setSearchType(type, $event)">{{type | typeFilter}}</li>
  8. </ul>
  9. </div>
  10. <a @click="goLastPage"><i class="iconfont icon-fanhui"></i></a>
  11. <input type="text" v-model="keyword" @input="getSimilarList()" :placeholder="placeholder" @keyup.13="onSearch()">
  12. <span @click="onSearch()"><i class="iconfont icon-sousuo"></i></span>
  13. <ul class="similar-list" v-if="emptyStatus && type == 'supplier' && keyword && keyword !== '' && showSimilarWord">
  14. <template v-if="similarList.pCmpCode && similarList.pCmpCode.length && searchType === 'code'">
  15. <li class="title text-ellipse">型号</li>
  16. <li class="text-ellipse" v-for="code in similarList.pCmpCode.slice(0, 4)" @click="onSearch(code.pCmpCode, 'pCmpCode', $event)">{{code.pCmpCode}}</li>
  17. </template>
  18. <template v-if="similarList.pBrandEn && similarList.pBrandEn.length && searchType === 'brand'">
  19. <li class="title text-ellipse">品牌</li>
  20. <li class="text-ellipse" v-for="brand in similarList.pBrandEn.slice(0, 4)" @click="onSearch(brand.nameCn, 'pBrandEn', $event)">{{brand.nameCn}}</li>
  21. </template>
  22. <template v-if="similarList.kind && similarList.kind.length && searchType === 'kind'">
  23. <li class="title text-ellipse">物料名称</li>
  24. <li class="text-ellipse" v-for="kind in similarList.kind.slice(0, 4)" @click="onSearch(kind.kind, 'kind', $event)">{{kind.kind}}</li>
  25. </template>
  26. </ul>
  27. <ul class="similar-list" v-if="emptyStatus && type == 'default' && keyword && keyword !== '' && showSimilarWord">
  28. <template v-if="similarList.component && similarList.component.length">
  29. <li class="title text-ellipse">型号</li>
  30. <li class="text-ellipse" v-for="code in similarList.component.slice(0, 4)" @click="onSearch(code.code, 'code', $event)">{{code.code}}</li>
  31. </template>
  32. <template v-if="similarList.brand && similarList.brand.length">
  33. <li class="title text-ellipse">品牌</li>
  34. <li class="text-ellipse" v-for="brand in similarList.brand.slice(0, 4)" @click="onSearch(brand.nameCn, 'brand', $event)">{{brand.nameCn}}</li>
  35. </template>
  36. <template v-if="similarList.kind && similarList.kind.length">
  37. <li class="title text-ellipse">物料名称</li>
  38. <li class="text-ellipse" v-for="kind in similarList.kind.slice(0, 4)" @click="onSearch(kind.nameCn, 'kind', $event)">{{kind.nameCn}}</li>
  39. </template>
  40. </ul>
  41. </div>
  42. </template>
  43. <script>
  44. import {scrollTo} from '~utils/scroll'
  45. export default {
  46. props: {
  47. placeholder: {
  48. type: String,
  49. default: '请输入要查找的内容'
  50. },
  51. similarUrl: { // 联想词url
  52. type: String,
  53. default: '/search/similarKeywords'
  54. },
  55. type: { // 搜索类型
  56. type: String,
  57. default: 'default'
  58. },
  59. showSimilar: { // 是否显示联想词
  60. type: Boolean,
  61. default: true
  62. }
  63. },
  64. data () {
  65. return {
  66. keyword: '',
  67. similarList: {},
  68. showSimilarWord: false,
  69. searchKeyword: '',
  70. showSearchType: false,
  71. searchType: 'code',
  72. searchTypeList: ['code', 'brand', 'kind', 'name']
  73. }
  74. },
  75. filters: {
  76. typeFilter: function (val) {
  77. switch (val) {
  78. case 'code':
  79. return '搜型号'
  80. case 'brand':
  81. return '搜品牌'
  82. case 'kind':
  83. return '搜物料名称'
  84. case 'name':
  85. return '供应商'
  86. default:
  87. return '全部'
  88. }
  89. }
  90. },
  91. mounted () {
  92. this.$nextTick(() => {
  93. document.onclick = () => {
  94. this.showSimilarWord = false
  95. this.showSearchType = false
  96. }
  97. })
  98. },
  99. computed: {
  100. emptyStatus () {
  101. let similarList = this.similarList
  102. let searchType = this.searchType
  103. if (this.type === 'supplier') {
  104. return (similarList.pCmpCode && similarList.pCmpCode.length && searchType === 'code') ||
  105. (similarList.pBrandEn && similarList.pBrandEn.length && searchType === 'brand') ||
  106. (similarList.kind && similarList.kind.length && searchType === 'kind')
  107. } else if (this.type === 'default') {
  108. return (similarList.component && similarList.component.length) ||
  109. (similarList.brand && similarList.brand.length) ||
  110. (similarList.kind && similarList.kind.length)
  111. }
  112. }
  113. },
  114. methods: {
  115. onSearch: function (key, type, e) {
  116. if (e) {
  117. e.stopPropagation()
  118. }
  119. // if (key === this.searchKeyword || this.keyword === this.searchKeyword) {
  120. // return
  121. // }
  122. if (key) {
  123. this.keyword = key
  124. this.$emit('searchAction', {
  125. keyword: this.keyword,
  126. type: type
  127. })
  128. } else {
  129. let sType = null
  130. if (this.searchType === 'code' && this.similarList.pCmpCode[0]) {
  131. this.keyword = this.similarList.pCmpCode[0].pCmpCode
  132. sType = 'pCmpCode'
  133. } else if (this.searchType === 'brand' && this.similarList.pBrandEn[0]) {
  134. this.keyword = this.similarList.pBrandEn[0].nameCn
  135. sType = 'pBrandEn'
  136. } else if (this.searchType === 'kind' && this.similarList.kind[0]) {
  137. this.keyword = this.similarList.kind[0].kind
  138. sType = 'kind'
  139. }
  140. this.$emit('searchAction', {
  141. keyword: this.keyword,
  142. type: sType
  143. })
  144. }
  145. this.searchKeyword = this.keyword
  146. scrollTo('body', 10)
  147. this.showSimilarWord = false
  148. },
  149. getSimilarList: function () {
  150. if (this.showSimilar && this.keyword && this.keyword !== '') {
  151. this.$http.get(this.similarUrl, {params: {keyword: this.keyword}}).then(
  152. res => {
  153. this.similarList = res.data
  154. this.showSimilarWord = true
  155. }
  156. )
  157. }
  158. },
  159. setShowSearchType: function (flag, e) {
  160. if (e) {
  161. e.stopPropagation()
  162. }
  163. this.showSearchType = flag
  164. },
  165. setSearchType: function (type, e) {
  166. if (e) {
  167. e.stopPropagation()
  168. }
  169. this.setShowSearchType(false)
  170. this.searchType = type
  171. }
  172. }
  173. }
  174. </script>
  175. <style lang="scss" scoped>
  176. .search-content {
  177. color: #333;
  178. input {
  179. margin: 0 0 0 .5rem;
  180. line-height: normal;
  181. padding-left: 1.1rem;
  182. }
  183. .similar-list {
  184. width: 5.45rem;
  185. background: #fff;
  186. position: absolute;
  187. left: 1.65rem;
  188. top: .72rem;
  189. border: 1px solid #ccc;
  190. border-radius: .05rem;
  191. max-height: 4.5rem;
  192. overflow-y: auto;
  193. li {
  194. height: .6rem;
  195. line-height: .6rem;
  196. padding: 0 .1rem;
  197. font-size: .26rem;
  198. &.title {
  199. color: #666;
  200. border-bottom: 1px solid #ddd;
  201. font-weight: bold;
  202. background: #f6f5f5;
  203. }
  204. }
  205. }
  206. .options {
  207. position: absolute;
  208. left: .45rem;
  209. width: 1.25rem;
  210. padding-left: .25rem;
  211. background: url(/images/mobile/@2x/search/select-arrow-blue.jpg) no-repeat;
  212. background-size: .14rem .12rem;
  213. background-position: 1.02rem .36rem;
  214. color: #3e82f5;
  215. font-size: .26rem;
  216. i {
  217. height: .46rem;
  218. width: .01rem;
  219. background: #eceef0;
  220. display: block;
  221. position: absolute;
  222. left: 1.2rem;
  223. top: .04rem;
  224. margin-top: .18rem;
  225. }
  226. ul {
  227. position: absolute;
  228. left: .15rem;
  229. top: .54rem;
  230. z-index: -1;
  231. width: 1.1rem;
  232. border-radius: .05rem;
  233. li {
  234. height: .99rem;
  235. /*border-radius: .05rem;*/
  236. background: #666;
  237. color: rgba(255, 255, 255, 0.89);
  238. text-align: center;
  239. line-height: 1.2rem;
  240. font-size: .3rem;
  241. }
  242. }
  243. }
  244. }
  245. </style>