BaseFilter.vue 5.8 KB

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