TimeRange.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <div class="timebox">
  3. <div style="display:flex;">
  4. <div>
  5. <el-date-picker
  6. v-model="value"
  7. type="datetimerange"
  8. range-separator="至"
  9. start-placeholder="开始日期"
  10. end-placeholder="结束日期"
  11. format="yyyy-MM-dd HH:mm:ss"
  12. style="width:100%;max-width: 300px;margin-top: 12px;"
  13. value-format="timestamp"
  14. v-if="timeType=='range'"
  15. size='mini'
  16. @change='selectTime'
  17. :pickerOptions="pickerOptions"
  18. >
  19. </el-date-picker>
  20. </div>
  21. <div>
  22. <el-button-group>
  23. <el-button :type="timeType=='range'?'primary':''" icon="el-icon-date" size='mini' @click="timeChange('range')"></el-button>
  24. <!-- <el-button :type="timeType=='real'?'primary':''" size='mini' @click="timeChange('real')">REAL TIME</el-button> -->
  25. <el-button :type="timeType=='5m'?'primary':''" size='mini' @click="timeChange('5m')">Last 5m</el-button>
  26. <el-button :type="timeType=='20m'?'primary':''" size='mini' @click="timeChange('20m')">20m</el-button>
  27. <el-button :type="timeType=='1h'?'primary':''" size='mini' @click="timeChange('1h')">1h</el-button>
  28. <!-- <el-button :type="timeType=='3h'?'primary':''" size='mini' @click="timeChange('3h')">3h</el-button> -->
  29. <el-button :type="timeType=='6h'?'primary':''" size='mini' @click="timeChange('6h')">6h</el-button>
  30. <el-button :type="timeType=='12h'?'primary':''" size='mini' @click="timeChange('12h')">12h</el-button>
  31. <el-button :type="timeType=='1d'?'primary':''" size='mini' @click="timeChange('1d')">1d</el-button>
  32. <!-- <el-button :type="timeType=='2d'?'primary':''" size='mini' @click="timeChange('2d')">2d</el-button> -->
  33. </el-button-group>
  34. </div>
  35. <div style="margin-left:12px" class='box'>
  36. <!-- <el-button-group> -->
  37. <!-- <i class="el-icon-refresh" @click="changeTimeout('refresh')"></i> -->
  38. <!-- <el-button type="primary" icon="el-icon-refresh" size="mini" style="margin-top: 11px;border-top-left-radius: 3px;
  39. border-bottom-left-radius: 3px;" @click="Refresh()">
  40. </el-button> -->
  41. <el-dropdown size="mini" split-button type="primary">
  42. <!-- <span>
  43. <el-button type="primary" icon="el-icon-refresh" size="mini" style="margin-top: 11px;border-top-left-radius: 3px;
  44. border-bottom-left-radius: 3px;" @click="Refresh()">
  45. </el-button>
  46. </span> -->
  47. <!-- <i class="el-icon-refresh" @click="Refresh()"></i>
  48. <span>{{showValue}}</span> -->
  49. <span class="el-dropdown-link">
  50. <!-- <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh()">
  51. </el-button>
  52. <el-button type="primary" size="mini">{{showValue}}</el-button> -->
  53. <i class="el-icon-refresh" @click="Refresh()"></i>
  54. <span style="margin-left:8px; border-left:1px solid rgba(255, 255, 255, 0.5);padding:2px 5px 1px 7px;">{{showValue}}</span>
  55. </span>
  56. <el-dropdown-menu slot="dropdown">
  57. <!-- {{this.timeObj.timeOut/1000}}s -->
  58. <el-dropdown-item @click.native="changeTimeout('off')">关</el-dropdown-item>
  59. <el-dropdown-item @click.native="changeTimeout('5s')">5s</el-dropdown-item>
  60. <el-dropdown-item @click.native="changeTimeout('10s')">10s</el-dropdown-item>
  61. <el-dropdown-item @click.native="changeTimeout('20s')">20s</el-dropdown-item>
  62. <el-dropdown-item @click.native="changeTimeout('30s')">30s</el-dropdown-item>
  63. <el-dropdown-item @click.native="changeTimeout('1m')">1m</el-dropdown-item>
  64. </el-dropdown-menu>
  65. </el-dropdown>
  66. <!-- </el-button-group> -->
  67. </div>
  68. </div>
  69. </div>
  70. </template>
  71. <script>
  72. import { mapGetters } from 'vuex'
  73. export default {
  74. data(){
  75. return {
  76. timeType:'5m',
  77. value:'',
  78. times:'',
  79. timeObj:{
  80. interval:60,
  81. // startTime:Math.round((new Date().getTime())/1000 - (5*60)),//5分钟
  82. startTime:Math.round((new Date().getTime())/1000 - (1*60*60)), //1小时
  83. endTime:Math.round(new Date().getTime()/1000),
  84. timeOut:1, //默认是5000毫秒也就是5s
  85. },
  86. showValue:'关',
  87. choiceDate: null,
  88. pickerOptions: {
  89. onPick: ({ maxDate, minDate }) => {
  90. // 把选择的第一个日期赋值给一个变量。
  91. this.choiceDate = minDate.getTime();
  92. // 如何你选择了两个日期了,就把那个变量置空
  93. if (maxDate) this.choiceDate = "";
  94. },
  95. disabledDate: (time) => {
  96. // 如何选择了一个日期
  97. if (this.choiceDate) {
  98. // 7天的时间戳
  99. const one = 1 * 24 * 3600 * 1000;
  100. // 当前日期 - one = 7天之前
  101. const minTime = this.choiceDate - one;
  102. // 当前日期 + one = 7天之后
  103. const maxTime = this.choiceDate + one;
  104. return (
  105. time.getTime() < minTime ||
  106. time.getTime() > maxTime
  107. // 限制不能选择今天及以后
  108. // || time.getTime() + 1 * 24 * 3600 * 1000 > Date.now()
  109. );
  110. } else {
  111. // 如果没有选择日期,就要限制不能选择今天及以后
  112. // return time.getTime() + 1 * 24 * 3600 * 1000 > Date.now();
  113. }
  114. },
  115. },
  116. }
  117. },
  118. created(){
  119. this.$store.commit('time/setTimes',this.timeObj)
  120. },
  121. computed: {
  122. ...mapGetters([
  123. 'timesFlag'
  124. ])
  125. },
  126. // watch: {
  127. // '$store.state.time.globalTime': {
  128. // handler(newValue, oldValue) {
  129. // console.log(newValue,'newValue',oldValue,'oldValue')
  130. // this.$store.commit('time/changeGlobalTime',newValue)
  131. // },
  132. // immediate: true,
  133. // deep: true
  134. // }
  135. // },
  136. methods:{
  137. timeChange(val){
  138. console.log(val,'时间改变')
  139. this.timeType = val;
  140. let startTime,endTime;
  141. switch(val){
  142. case 'range':
  143. break;
  144. case 'real':
  145. //当前时间的时间戳
  146. // this.times = Math.round((new Date().getTime())/1000);
  147. // this.$store.commit('time/changeGlobalTime',this.times)
  148. break;
  149. case '5m':
  150. startTime = Math.round((new Date().getTime())/1000 - (5*60));
  151. endTime= Math.round(new Date().getTime()/1000);
  152. // interval = endTime-startTime;
  153. // this.timeObj={
  154. // interval:interval,
  155. // startTime:startTime,
  156. // endTime:endTime,
  157. // }
  158. // this.$store.commit('time/setTimes',this.timeObj)
  159. this.parseTime(startTime,endTime);
  160. break;
  161. case '20m':
  162. startTime = Math.round((new Date().getTime())/1000 - (20*60));
  163. endTime= Math.round(new Date().getTime()/1000);
  164. this.parseTime(startTime,endTime);
  165. break;
  166. case '1h':
  167. startTime = Math.round((new Date().getTime())/1000 - (1*60*60));
  168. endTime= Math.round(new Date().getTime()/1000);
  169. this.parseTime(startTime,endTime);
  170. break;
  171. case '3h':
  172. startTime = Math.round((new Date().getTime())/1000 - (3*60*60));
  173. endTime= Math.round(new Date().getTime()/1000);
  174. this.parseTime(startTime,endTime);
  175. break;
  176. case '6h':
  177. startTime = Math.round((new Date().getTime())/1000 - (6*60*60));
  178. endTime= Math.round(new Date().getTime()/1000);
  179. this.parseTime(startTime,endTime);
  180. break;
  181. case '12h':
  182. startTime = Math.round((new Date().getTime())/1000 - (12*60*60));
  183. endTime= Math.round(new Date().getTime()/1000);
  184. this.parseTime(startTime,endTime);
  185. break;
  186. case '1d':
  187. startTime = Math.round((new Date().getTime())/1000 - (24*60*60));
  188. endTime= Math.round(new Date().getTime()/1000);
  189. this.parseTime(startTime,endTime);
  190. break;
  191. case '2d':
  192. startTime = Math.round((new Date().getTime())/1000 - (48*60*60));
  193. endTime= Math.round(new Date().getTime()/1000);
  194. this.parseTime(startTime,endTime);
  195. break;
  196. }
  197. },
  198. selectTime(val){
  199. console.log(val)
  200. this.times = val
  201. console.log(this.times,'this.times')
  202. let a = Math.round(this.times[1]/1000/60) - Math.round(this.times[0]/1000/60);
  203. console.log(a,'range')
  204. this.timeObj={
  205. interval:a,
  206. startTime:this.times[0],
  207. endTime:this.times[1]
  208. }
  209. this.$store.commit('time/setTimes',this.timeObj)
  210. },
  211. parseTime(startTime,endTime){
  212. let interval = (endTime - startTime)/60;
  213. this.timeObj={
  214. interval:interval,
  215. startTime:startTime,
  216. endTime:endTime,
  217. }
  218. console.log(this.timeObj)
  219. this.$store.commit('time/setTimes',this.timeObj)
  220. },
  221. changeTimeout(val){
  222. console.log(val,'下拉选择时间');
  223. if(val == 'refresh'){
  224. // this.timeObj.timeOut = 0;
  225. // this.showValue =
  226. }else if(val == 'off'){
  227. this.timeObj.timeOut = 1;
  228. this.showValue = '关'
  229. }else if(val == '1s'){
  230. this.timeObj.timeOut = 1*1000;
  231. this.showValue = '1s'
  232. }else if(val == "5s"){
  233. this.timeObj.timeOut = 5*1000;
  234. this.showValue = '5s'
  235. }else if(val == '10s'){
  236. this.timeObj.timeOut = 10*1000;
  237. this.showValue = '10s'
  238. }else if(val == '20s'){
  239. this.timeObj.timeOut = 20*1000;
  240. this.showValue = '20s'
  241. }else if(val == '30s'){
  242. this.timeObj.timeOut = 30*1000;
  243. this.showValue = '30s'
  244. }else if(val == '1m'){
  245. this.timeObj.timeOut = 60*1000;
  246. this.showValue = '1m'
  247. }
  248. this.$store.commit('time/setTimes',this.timeObj)
  249. },
  250. Refresh(){
  251. location.reload()
  252. }
  253. },
  254. }
  255. </script>
  256. <style lang="scss" scoped>
  257. .timebox{
  258. height: 50px;
  259. line-height: 50px;
  260. overflow: hidden;
  261. position: relative;
  262. background: #fff;
  263. // box-shadow: 0 1px 4px rgba(0,21,41,.08);
  264. // margin-top:50px;
  265. display: flex;
  266. align-items: center;
  267. justify-content: flex-end;
  268. box-sizing: border-box;
  269. padding:0 20px;
  270. display: inline-block;
  271. }
  272. // .box ::v-deep .el-button-group > .el-button:first-child {
  273. // border-top-right-radius: 0;
  274. // border-bottom-right-radius: 0;
  275. // border-top-left-radius: 0;
  276. // border-bottom-left-radius: 0;
  277. // }el-button el-button--primary
  278. .box ::v-deep .el-dropdown .el-button-group>.el-button.el-button--mini:first-child {
  279. // padding: 7px 7px;
  280. padding: 7px 3px 7px 7px;
  281. }
  282. </style>