BaseFilter.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. default: 0
  45. },
  46. // 选择属性值
  47. selectOption: {
  48. type: String,
  49. default: 'defaultKey'
  50. }
  51. },
  52. data () {
  53. return {
  54. val: 0,
  55. dateObj: {
  56. fromDate: null,
  57. toDate: null
  58. },
  59. remindText: '',
  60. timeoutCount: ''
  61. }
  62. },
  63. components: {
  64. RemindBox
  65. },
  66. watch: {
  67. val: {
  68. handler: function (val) {
  69. if (val) {
  70. this.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
  106. }
  107. } else if (val === 2) { // 三个月
  108. dateObj = {
  109. fromDate: currentTime - 3 * 30 * 24 * 60 * 60 * 1000,
  110. toDate: currentTime
  111. }
  112. } else if (val === 3) { // 六个月
  113. dateObj = {
  114. fromDate: currentTime - 6 * 30 * 24 * 60 * 60 * 1000,
  115. toDate: currentTime
  116. }
  117. }
  118. return dateObj
  119. },
  120. setDate (type) {
  121. if (this.dateObj[type]) {
  122. this.dateObj[type] = new Date(this.dateObj[type]).getTime() - 8 * 60 * 60 * 1000
  123. if (this.dateObj.fromDate && this.dateObj.toDate && this.dateObj.fromDate > this.dateObj.toDate) {
  124. if (type === 'fromDate') {
  125. this.setRemindText('起始时间不能大于结束时间')
  126. } else {
  127. this.setRemindText('结束时间不能小于起始时间')
  128. }
  129. this.dateObj[type] = null
  130. } else {
  131. if (this.dateObj.fromDate && this.dateObj.toDate && this.dateObj.fromDate === this.dateObj.toDate) {
  132. // 23:59:59
  133. this.dateObj.toDate += 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000
  134. }
  135. }
  136. } else {
  137. this.dateObj[type] = null
  138. }
  139. this.$emit('selectAction', {
  140. key: 'date',
  141. value: this.dateObj
  142. })
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. $base-color: #3f84f6;
  149. .mobile-base-filter {
  150. line-height: .5rem;
  151. .title {
  152. vertical-align: top;
  153. width: 21%;
  154. }
  155. .content {
  156. width: 79%;
  157. .item {
  158. width: 33%;
  159. color: $base-color;
  160. }
  161. }
  162. .date-wrap {
  163. text-align: center;
  164. label {
  165. width: 2.6rem;
  166. height: .5rem;
  167. line-height: .5rem;
  168. border-radius: .04rem;
  169. border: 1px solid #bfbfbf;
  170. background: url(/images/mobile/select-arrow.png) no-repeat;
  171. background-size: .12rem .06rem;
  172. vertical-align: middle;
  173. background-color: #fff;
  174. background-position: 2.1rem .2rem;
  175. position: relative;
  176. margin: .2rem 0 0 0;
  177. input {
  178. opacity: 0;
  179. width: 2.22rem;
  180. height: .5rem;
  181. position: absolute;
  182. left: 0;
  183. z-index: 1;
  184. }
  185. i {
  186. font-size: .28rem;
  187. color: $base-color;
  188. margin-left: .2rem;
  189. float: left;
  190. }
  191. }
  192. p {
  193. font-weight: normal;
  194. font-size: .22rem;
  195. padding-right: .4rem;
  196. }
  197. span {
  198. color: #a0a0a0;
  199. margin: 0 .4rem;
  200. vertical-align: bottom;
  201. width: .5rem;
  202. display: inline-block;
  203. text-align: center;
  204. }
  205. }
  206. }
  207. </style>