SearchTimeScopeSelector.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <div class="search-time-scope-selector">
  3. <TimeSelectorContent
  4. ref="content"
  5. :options="options"
  6. :beforeChange="beforeChange"
  7. :selectorTime="type"
  8. selectorType="line"
  9. :defaultSelectedKey="value"
  10. :exactCanHalf="exactCanHalf"
  11. :showConfirmButton="showConfirmButton"
  12. :startPlaceholder="startPlaceholder"
  13. :endPlaceholder="endPlaceholder"
  14. :isDateRange="isDateRange"
  15. @onChange="onChange"
  16. />
  17. </div>
  18. </template>
  19. <script>
  20. import TimeSelectorContent from '@/components/selector/TimeSelectorContent.vue'
  21. export default {
  22. name: 'search-time-scope-selector',
  23. components: {
  24. TimeSelectorContent
  25. },
  26. model: {
  27. prop: 'value',
  28. event: 'input'
  29. },
  30. props: {
  31. beforeChange: Function,
  32. options: {
  33. type: Array,
  34. default() {
  35. return []
  36. }
  37. },
  38. splitter: {
  39. type: String,
  40. default: '_'
  41. },
  42. value: {
  43. type: String,
  44. default: 'thisyear'
  45. },
  46. // 时间配置项类型
  47. type: {
  48. type: String,
  49. default: 'bidSearch'
  50. },
  51. // 自定义选项是否可以选择一半值
  52. exactCanHalf: {
  53. type: Boolean,
  54. default: false
  55. },
  56. // 是否展示自定义自动按钮
  57. showConfirmButton: {
  58. type: Boolean,
  59. default: false
  60. },
  61. startPlaceholder: {
  62. type: String,
  63. default: '开始日期'
  64. },
  65. endPlaceholder: {
  66. type: String,
  67. default: '结束日期'
  68. },
  69. // 显示类型是否为日期范围(一个弹框内选择开始日期结束日期)
  70. isDateRange: {
  71. type: Boolean,
  72. default: false
  73. }
  74. },
  75. data() {
  76. return {
  77. selectTime: {
  78. start: 0,
  79. end: 0,
  80. exact: ''
  81. }
  82. }
  83. },
  84. watch: {
  85. value: {
  86. handler(val) {
  87. this.syncState(val)
  88. },
  89. deep: true
  90. }
  91. },
  92. mounted() {
  93. this.syncState(this.value)
  94. },
  95. methods: {
  96. syncState(val) {
  97. const splitter = this.splitter // 默认_
  98. const time = this.transformTimeBefore(val)
  99. if (time.indexOf(splitter) === -1) {
  100. this.selectTime = {
  101. start: 0,
  102. end: 0,
  103. exact: time
  104. }
  105. } else {
  106. const times = time.split(splitter)
  107. this.selectTime = {
  108. start: times[0] * 1000,
  109. end: times[1] * 1000,
  110. exact: 'exact'
  111. }
  112. }
  113. if (this.$refs.content) {
  114. this.$refs.content.setState(this.selectTime)
  115. }
  116. },
  117. setState(data) {
  118. return this.$refs.content.setState(data)
  119. },
  120. getState() {
  121. return this.$refs.content.getState()
  122. },
  123. transformTimeBefore(time) {
  124. // 转换成组件需要的数据类型
  125. const timeMap = {
  126. 'lately-7': 'lately7',
  127. 'lately-30': 'lately30',
  128. thisyear: 'sinceLastYear',
  129. threeyear: 'sinceLastThreeYear',
  130. fiveyear: 'sinceLastFiveYear'
  131. }
  132. return timeMap[time] || time
  133. },
  134. transformTimeAfter(time) {
  135. // 转换成接口需要的数据类型
  136. const timeMap = {
  137. lately7: 'lately-7',
  138. lately30: 'lately-30',
  139. sinceLastYear: 'thisyear',
  140. sinceLastThreeYear: 'threeyear',
  141. sinceLastFiveYear: 'fiveyear'
  142. }
  143. return timeMap[time] || time
  144. },
  145. onChange(state) {
  146. const split = this.splitter
  147. if (state.exact === 'exact') {
  148. this.$emit('input', `${state.start / 1000}${split}${state.end / 1000}`)
  149. } else {
  150. this.$emit('input', this.transformTimeAfter(state.exact))
  151. }
  152. }
  153. }
  154. }
  155. </script>
  156. <style lang="scss" scoped>
  157. .search-time-scope-selector {
  158. &::v-deep {
  159. .fw-bold {
  160. font-weight: bold;
  161. }
  162. // 子组件按钮公共样式
  163. .j-button-item {
  164. display: flex;
  165. align-items: center;
  166. margin: 0 5px;
  167. padding: 2px 6px;
  168. line-height: 20px;
  169. border-radius: 4px;
  170. font-size: 14px;
  171. text-align: center;
  172. background-color: transparent;
  173. cursor: pointer;
  174. &:first-child {
  175. margin-left: 0;
  176. }
  177. &.global {
  178. // s-card中的全部按钮使用
  179. padding: 6px 8px;
  180. height: 24px;
  181. line-height: 24px;
  182. font-weight: 700;
  183. color: inherit;
  184. border-color: rgba(0, 0, 0, 0.05);
  185. }
  186. &.all {
  187. // s-line中的全部按钮使用
  188. font-weight: 700;
  189. border-color: transparent;
  190. }
  191. &.hover:hover {
  192. color: #2abed1;
  193. }
  194. // 选中状态
  195. &.active {
  196. // 默认蓝色边框蓝色字体
  197. color: #2abed1;
  198. border-color: #2abed1;
  199. &.bgc {
  200. // 默认蓝色背景白色字体
  201. color: #fff;
  202. background-color: $color-text--highlight;
  203. }
  204. &.bgc-opacity {
  205. // 默认蓝色边框蓝色字体蓝色半透明背景
  206. background-color: rgba(44, 183, 202, 0.1);
  207. }
  208. }
  209. }
  210. [class^='el-icon-'] {
  211. transition: transform 0.2s ease;
  212. }
  213. .rotate180 {
  214. transform: rotate(180deg);
  215. }
  216. .date-time-container .date-time-item {
  217. width: 130px;
  218. height: 24px;
  219. line-height: 24px;
  220. }
  221. .date-time-container .date-time-item.left::after {
  222. background-color: #e0e0e0;
  223. }
  224. .select-group-container {
  225. margin-right: 10px;
  226. }
  227. .date-time-container {
  228. padding: 0;
  229. background-color: transparent !important;
  230. &.active {
  231. background-color: transparent !important;
  232. input,
  233. .date-time-item.left::after {
  234. background-color: $color_main;
  235. color: #fff;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. </style>