BaseFilter.vue 6.0 KB

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