BaseFilter.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <div class="mobile-base-filter">
  3. <span class="title inline-block" v-if="ShowTitle">{{title}}:</span>
  4. <div class="inline-block content">
  5. <span class="item inline-block" v-for="item in selectItems" @click.stop="setSelect(item)">
  6. <label class="mobile-cart-check" :class="{active: val === item.val}"></label>
  7. {{item.key}}
  8. </span>
  9. </div>
  10. <div class="date-wrap" v-if="selectOption === 'date' && val === 4">
  11. <label>
  12. <i class="iconfont icon-ico-date"></i>
  13. <input type="date" v-model="dateObj.fromDate" @change="setDate('fromDate')">
  14. <p v-if="dateObj.fromDate">{{dateObj.fromDate | date}}</p>
  15. </label>
  16. <span>—</span>
  17. <label>
  18. <i class="iconfont icon-ico-date"></i>
  19. <input type="date" v-model="dateObj.toDate" @change="setDate('toDate')">
  20. <p v-if="dateObj.toDate">{{dateObj.toDate | date}}</p>
  21. </label>
  22. </div>
  23. <remind-box :title="remindText" :timeoutCount="timeoutCount"></remind-box>
  24. </div>
  25. </template>
  26. <script>
  27. import { RemindBox } from '~components/mobile/common'
  28. export default {
  29. props: {
  30. // 筛选标题
  31. title: {
  32. type: String,
  33. default: '标题'
  34. },
  35. // 筛选条件
  36. selectItems: {
  37. type: Array,
  38. default: function () {
  39. return []
  40. }
  41. },
  42. // 默认选择
  43. defaultVal: {},
  44. // 选择属性值
  45. selectOption: {
  46. type: String,
  47. default: 'defaultKey'
  48. },
  49. ShowTitle: {
  50. type: Boolean,
  51. default: true
  52. }
  53. },
  54. data () {
  55. return {
  56. val: null,
  57. dateObj: {
  58. fromDate: null,
  59. toDate: null
  60. },
  61. remindText: '',
  62. timeoutCount: ''
  63. }
  64. },
  65. components: {
  66. RemindBox
  67. },
  68. watch: {
  69. defaultVal: {
  70. handler: function (val) {
  71. this.val = val
  72. this.$emit('valueAction', {
  73. key: this.selectOption,
  74. value: this.selectOption === 'date' ? this.getDateObj(val) : val
  75. })
  76. },
  77. immediate: true
  78. }
  79. },
  80. methods: {
  81. setRemindText: function (str) {
  82. this.remindText = str
  83. this.timeoutCount++
  84. },
  85. setSelect (item) {
  86. this.val = item.val
  87. if (this.selectOption === 'date' && item.val === 4) {
  88. return
  89. }
  90. this.$emit('selectAction', {
  91. key: this.selectOption,
  92. value: this.selectOption === 'date' ? this.getDateObj(item.val) : item.val
  93. })
  94. },
  95. /*
  96. * 通过勾选获取时间
  97. * @val: 0 => 全部
  98. * 1 => 最近一个月
  99. * 2 => 最近三个月
  100. * 3 => 最近六个月
  101. * */
  102. getDateObj (val) {
  103. let dateObj = null
  104. // 当天0点时间戳
  105. let currentTime = this.baseUtils.getClearDay(new Date())
  106. if (val === 1) { // 一个月
  107. dateObj = {
  108. fromDate: currentTime - 30 * 24 * 60 * 60 * 1000,
  109. toDate: currentTime + 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000
  110. }
  111. } else if (val === 2) { // 三个月
  112. dateObj = {
  113. fromDate: currentTime - 3 * 30 * 24 * 60 * 60 * 1000,
  114. toDate: currentTime + 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000
  115. }
  116. } else if (val === 3) { // 六个月
  117. dateObj = {
  118. fromDate: currentTime - 6 * 30 * 24 * 60 * 60 * 1000,
  119. toDate: currentTime + 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000
  120. }
  121. }
  122. return dateObj
  123. },
  124. setDate (type) {
  125. if (this.dateObj[type]) {
  126. // 初始化为00:00:00
  127. this.dateObj[type] = new Date(this.dateObj[type]).getTime() - 8 * 60 * 60 * 1000
  128. if (this.dateObj.fromDate && this.dateObj.toDate && this.dateObj.fromDate > this.dateObj.toDate) {
  129. if (type === 'fromDate') {
  130. this.setRemindText('起始时间不能大于结束时间')
  131. } else {
  132. this.setRemindText('结束时间不能小于起始时间')
  133. }
  134. this.dateObj[type] = null
  135. }
  136. // else {
  137. // if (this.dateObj.fromDate && this.dateObj.toDate && this.dateObj.fromDate === this.dateObj.toDate) {
  138. // // 23:59:59
  139. // this.dateObj.toDate += 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000
  140. // }
  141. // }
  142. // 23:59:59
  143. if (this.dateObj.toDate && type === 'toDate') {
  144. this.dateObj.toDate += (23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000)
  145. }
  146. } else {
  147. this.dateObj[type] = null
  148. }
  149. this.$emit('selectAction', {
  150. key: 'date',
  151. value: this.dateObj
  152. })
  153. }
  154. }
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. $base-color: #3f84f6;
  159. .mobile-base-filter {
  160. line-height: .5rem;
  161. .title {
  162. vertical-align: top;
  163. width: 23%;
  164. }
  165. .content {
  166. width: 77%;
  167. .item {
  168. width: 33%;
  169. color: $base-color;
  170. }
  171. }
  172. .date-wrap {
  173. text-align: center;
  174. label {
  175. width: 2.6rem;
  176. height: .5rem;
  177. line-height: .5rem;
  178. border-radius: .04rem;
  179. border: 1px solid #bfbfbf;
  180. background: url(/images/mobile/select-arrow.png) no-repeat;
  181. background-size: .12rem .06rem;
  182. vertical-align: middle;
  183. background-color: #fff;
  184. background-position: 2.1rem .2rem;
  185. position: relative;
  186. margin: .2rem 0 0 0;
  187. input {
  188. opacity: 0;
  189. width: 2.22rem;
  190. height: .5rem;
  191. position: absolute;
  192. left: 0;
  193. z-index: 1;
  194. }
  195. i {
  196. font-size: .28rem;
  197. color: $base-color;
  198. margin-left: .2rem;
  199. float: left;
  200. }
  201. }
  202. p {
  203. font-weight: normal;
  204. font-size: .22rem;
  205. padding-right: .4rem;
  206. }
  207. span {
  208. color: #a0a0a0;
  209. margin: 0 .4rem;
  210. vertical-align: bottom;
  211. width: .5rem;
  212. display: inline-block;
  213. text-align: center;
  214. }
  215. }
  216. }
  217. </style>