cwg-complex-search.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <template>
  2. <view class="complex-search">
  3. <!-- PC/平板端:直接显示表单 -->
  4. <cwg-match-media :min-width="991">
  5. <view v-if="!isMobile" class="search-bar search-form">
  6. <view v-for="(row, rowIndex) in formRows" :key="rowIndex" class="form-row">
  7. <view v-for="field in row" :key="field.key" class="form-item">
  8. <view class="search-bar">
  9. <!-- 根据字段类型渲染不同组件 -->
  10. <template v-if="field.type === 'input'">
  11. <uni-easyinput v-model="formData[field.key]" :placeholder="field.placeholder || '请输入'"
  12. clearable />
  13. </template>
  14. <template v-else-if="field.type === 'select'">
  15. <cwg-combox v-model:value="formData[field.key]" :options="field.options"
  16. :placeholder="field.placeholder || '请选择'" clearable @change="handleSearch" />
  17. </template>
  18. <template v-else-if="field.type === 'date'">
  19. <uni-datetime-picker v-model="formData[field.key]" type="date"
  20. :placeholder="field.placeholder || '请选择日期'" @change="handleDateChange" />
  21. </template>
  22. <template v-else-if="field.type === 'daterange'">
  23. <uni-datetime-picker v-model="formData[field.key]" type="daterange"
  24. :placeholder="field.placeholder || '请选择日期范围'" @change="handleDateChange" />
  25. </template>
  26. <template v-else-if="field.type === 'number'">
  27. <uni-easyinput v-model="formData[field.key]" type="number"
  28. :placeholder="field.placeholder || '请输入数字'" />
  29. </template>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="form-actions">
  34. <button class="reset-btn" @click="resetForm" v-t="'Documentary.tradingCenter.item4'" />
  35. <button class="search-btn" type="primary" @click="handleSearch" v-t="'Btn.Search'" />
  36. </view>
  37. </view>
  38. </cwg-match-media>
  39. <cwg-match-media :max-width="991">
  40. <!-- 移动端:只显示筛选按钮,点击弹出底部抽屉 -->
  41. <view class="mobile-filter">
  42. <view v-if="dateField" class="mobile-date-wrapper">
  43. <uni-datetime-picker v-model="formData[dateField.key]"
  44. :type="dateField.type === 'daterange' ? 'daterange' : 'date'"
  45. :placeholder="dateField.placeholder || (dateField.type === 'daterange' ? '选择日期范围' : '选择日期')"
  46. @change="handleDateChange" />
  47. </view>
  48. <button class="filter-chip" @click="openFilterPopup">
  49. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none"
  50. stroke="#141d22" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
  51. class="filter-icon">
  52. <path
  53. d="M4 4h16v2.172a2 2 0 0 1 -.586 1.414l-4.414 4.414v7l-6 2v-8.5l-4.48 -4.928a2 2 0 0 1 -.52 -1.345v-2.227z">
  54. </path>
  55. </svg>
  56. <text class="filter-label" v-t="'Documentary.tradingCenter.item3'" />
  57. </button>
  58. </view>
  59. </cwg-match-media>
  60. <cwg-popup v-model:visible="visible" type="center" :mask-click="false" :showFooters="true"
  61. :title="t('Documentary.tradingCenter.item3')">
  62. <scroll-view scroll-y class="drawer-content">
  63. <view v-for="field in nonDateField" :key="field.key" class="filter-item">
  64. <view class="label">{{ field.label }}</view>
  65. <view class="control">
  66. <template v-if="field.type === 'input'">
  67. <uni-easyinput v-model="tempFormData[field.key]" :placeholder="field.placeholder || '请输入'"
  68. clearable />
  69. </template>
  70. <template v-else-if="field.type === 'select'">
  71. <uni-data-select v-model:value="tempFormData[field.key]" :localdata="field.options"
  72. :placeholder="field.placeholder || '请选择'" clearable v-if="shouldUseSelect(field)" />
  73. <view class="chip-group" v-else>
  74. <view class="chip-list">
  75. <view v-for="opt in field.options" :key="opt.value" class="chip" :class="{
  76. 'chip-filled': tempFormData[field.key] === opt.value,
  77. 'chip-outlined': tempFormData[field.key] !== opt.value
  78. }" @click="selectChip(field.key, opt.value)">
  79. {{ opt.text }}
  80. </view>
  81. </view>
  82. </view>
  83. </template>
  84. <template v-else-if="field.type === 'number'">
  85. <uni-easyinput v-model="tempFormData[field.key]" type="number"
  86. :placeholder="field.placeholder || '请输入数字'" />
  87. </template>
  88. </view>
  89. </view>
  90. </scroll-view>
  91. <template #footer>
  92. <button class="reset-btn" @click="resetTempForm" v-t="'Documentary.tradingCenter.item4'" />
  93. <button class="search-btn" type="primary" @click="applyFilter" v-t="'Btn.Search'" />
  94. </template>
  95. </cwg-popup>
  96. </view>
  97. </template>
  98. <script setup>
  99. import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
  100. import { useI18n } from 'vue-i18n'
  101. const { t } = useI18n()
  102. const props = defineProps({
  103. // 字段配置列表,每个字段包含: key, label, type, options(可选), placeholder(可选)
  104. fields: {
  105. type: Array,
  106. required: true
  107. },
  108. // 每行显示的字段数(PC端),默认3个
  109. columns: {
  110. type: Number,
  111. default: 1
  112. },
  113. // 表单初始值 / v-model 绑定值
  114. modelValue: {
  115. type: Object,
  116. default: () => ({})
  117. }
  118. })
  119. const emit = defineEmits(['update:modelValue', 'search', 'reset'])
  120. // 表单数据(PC端)
  121. const formData = ref({})
  122. // 移动端临时数据
  123. const tempFormData = ref({})
  124. // 移动端弹窗是否可见
  125. const visible = ref(false)
  126. // 将字段按列数分组(仅用于PC布局)
  127. const formRows = computed(() => {
  128. const rows = []
  129. for (let i = 0; i < props.fields.length; i += props.columns) {
  130. rows.push(props.fields.slice(i, i + props.columns))
  131. }
  132. return rows
  133. })
  134. const selectChip = (key, value) => {
  135. // 如果点击的是当前已选中的项,可以保持不变,也可以置空(根据需求)
  136. // 此处保持选中该项,不提供取消功能(符合常见筛选行为)
  137. tempFormData.value[key] = value
  138. }
  139. // 初始化表单数据
  140. const initFormData = () => {
  141. const initial = {}
  142. props.fields.forEach(field => {
  143. // 1. 优先使用外部传入的 modelValue
  144. if (props.modelValue && props.modelValue[field.key] !== undefined) {
  145. initial[field.key] = props.modelValue[field.key]
  146. }
  147. // 2. 其次使用字段配置的 defaultValue
  148. else if (field.defaultValue !== undefined) {
  149. initial[field.key] = field.defaultValue
  150. }
  151. // 3. 日期字段特殊处理:默认当前月
  152. else if (field.type === 'date' || field.type === 'daterange') {
  153. initial[field.key] = getDefaultDateValue(field)
  154. }
  155. // 4. 其他字段默认为空字符串
  156. else {
  157. initial[field.key] = ''
  158. }
  159. })
  160. formData.value = initial
  161. tempFormData.value = JSON.parse(JSON.stringify(initial))
  162. }
  163. // 获取第一个日期或日期范围字段(用于移动端顶部快捷显示)
  164. const dateField = computed(() => {
  165. return props.fields.find(field => field.type === 'date' || field.type === 'daterange')
  166. })
  167. // 判断字段是否应该使用下拉选择器(用于排序和渲染)
  168. const shouldUseSelect = (field) => {
  169. if (!field.options) return false
  170. return field.options.length > 10 || field.isSelect === true
  171. }
  172. const handleDateChange = (value) => {
  173. // 日期变化时自动触发搜索
  174. handleSearch()
  175. }
  176. // 排序:输入框 + 下拉选择器 → 前面;Chip 选择器 → 后面
  177. const nonDateField = computed(() => {
  178. const filtered = props.fields.filter(field => field.type !== 'date' && field.type !== 'daterange')
  179. const priorityFields = []
  180. const otherFields = []
  181. filtered.forEach(field => {
  182. if (field.type === 'input' || field.type === 'number') {
  183. priorityFields.push(field)
  184. }
  185. else if (field.type === 'select') {
  186. if (shouldUseSelect(field)) {
  187. priorityFields.push(field)
  188. } else {
  189. otherFields.push(field)
  190. }
  191. }
  192. else {
  193. otherFields.push(field)
  194. }
  195. })
  196. return [...priorityFields, ...otherFields]
  197. })
  198. // 监听外部 modelValue 变化
  199. watch(() => props.modelValue, (newVal) => {
  200. if (newVal) {
  201. props.fields.forEach(field => {
  202. if (newVal[field.key] !== undefined) {
  203. formData.value[field.key] = newVal[field.key]
  204. }
  205. })
  206. }
  207. }, { deep: true, immediate: true })
  208. // 监听内部 formData 变化,同步到外部
  209. watch(formData, (newVal) => {
  210. emit('update:modelValue', newVal)
  211. }, { deep: true })
  212. // 重置表单(清空所有字段)
  213. const resetForm = () => {
  214. const empty = {}
  215. props.fields.forEach(field => {
  216. if (field.defaultValue !== undefined) {
  217. empty[field.key] = field.defaultValue
  218. } else if (field.type === 'date' || field.type === 'daterange') {
  219. empty[field.key] = getDefaultDateValue(field)
  220. } else {
  221. empty[field.key] = ''
  222. }
  223. })
  224. formData.value = empty
  225. emit('reset', empty)
  226. }
  227. // 触发查询
  228. const handleSearch = () => {
  229. emit('search', { ...formData.value })
  230. }
  231. // 移动端:打开弹窗
  232. const openFilterPopup = () => {
  233. // 复制当前表单数据到临时对象
  234. tempFormData.value = JSON.parse(JSON.stringify(formData.value))
  235. visible.value = true
  236. }
  237. const closePopup = () => {
  238. visible.value = false
  239. }
  240. const resetTempForm = () => {
  241. const empty = {}
  242. props.fields.forEach(field => {
  243. if (field.defaultValue !== undefined) {
  244. empty[field.key] = field.defaultValue
  245. } else if (field.type === 'date' || field.type === 'daterange') {
  246. empty[field.key] = getDefaultDateValue(field)
  247. } else {
  248. empty[field.key] = ''
  249. }
  250. })
  251. tempFormData.value = empty
  252. }
  253. // 获取日期字段的默认值(当前月范围或当天)
  254. const getDefaultDateValue = (field) => {
  255. const now = new Date()
  256. const year = now.getFullYear()
  257. const month = now.getMonth()
  258. const firstDay = new Date(year, month, 1)
  259. const lastDay = new Date(year, month + 1, 0)
  260. const formatDate = (date) => {
  261. const y = date.getFullYear()
  262. const m = String(date.getMonth() + 1).padStart(2, '0')
  263. const d = String(date.getDate()).padStart(2, '0')
  264. return `${y}-${m}-${d}`
  265. }
  266. if (field.type === 'daterange') {
  267. return [formatDate(firstDay), formatDate(lastDay)]
  268. } else if (field.type === 'date') {
  269. // 单日期默认设为今天(也可设为第一天,根据需求调整)
  270. return formatDate(now)
  271. }
  272. return ''
  273. }
  274. const applyFilter = () => {
  275. // 将临时数据同步到正式表单
  276. formData.value = JSON.parse(JSON.stringify(tempFormData.value))
  277. closePopup()
  278. handleSearch()
  279. }
  280. // 监听fields变化,重新初始化
  281. watch(() => props.fields, () => {
  282. initFormData()
  283. }, { deep: true, immediate: false })
  284. onMounted(() => {
  285. // 初始化表单数据
  286. initFormData()
  287. })
  288. </script>
  289. <style lang="scss" scoped>
  290. @import "@/uni.scss";
  291. .complex-search {
  292. width: 100%;
  293. }
  294. .search-bar {
  295. .cwg-combox,
  296. .uni-easyinput,
  297. .uni-date {
  298. width: px2rpx(240) !important;
  299. flex: none;
  300. }
  301. .form-actions {
  302. display: flex;
  303. justify-content: flex-end;
  304. gap: px2rpx(10);
  305. .reset-btn,
  306. .search-btn {
  307. line-height: px2rpx(35);
  308. font-size: px2rpx(14);
  309. }
  310. }
  311. }
  312. .mobile-filter {
  313. margin-bottom: px2rpx(10);
  314. display: flex;
  315. align-items: center;
  316. justify-content: center;
  317. gap: px2rpx(12);
  318. .mobile-date-wrapper {
  319. flex: 1;
  320. }
  321. .filter-chip {
  322. display: inline-flex;
  323. align-items: center;
  324. justify-content: center;
  325. gap: px2rpx(12);
  326. background-color: #ffffff;
  327. border: 1px solid #e5e5e5;
  328. border-radius: px2rpx(4);
  329. padding: px2rpx(0) px2rpx(16);
  330. font-size: px2rpx(16);
  331. font-weight: 500;
  332. color: #141d22;
  333. line-height: px2rpx(35);
  334. transition: all 0.2s ease;
  335. box-shadow: none;
  336. .iconfont,
  337. .uni-icons {
  338. font-size: 32rpx;
  339. color: #141d22;
  340. }
  341. /* 按下反馈效果 */
  342. &:active {
  343. transform: scale(0.96);
  344. background-color: #f5f7fa;
  345. /* 轻量点击背景色 */
  346. border-color: #b9bfc7;
  347. }
  348. &::after {
  349. border: none;
  350. }
  351. }
  352. }
  353. /* 弹窗整体优化 */
  354. .drawer-content {
  355. padding: px2rpx(20);
  356. border-radius: px2rpx(16);
  357. box-sizing: border-box;
  358. .label {
  359. font-size: px2rpx(18);
  360. font-weight: 600;
  361. color: #2c3e50;
  362. margin-bottom: px2rpx(20);
  363. letter-spacing: px2rpx(1);
  364. }
  365. :deep(.uni-scroll-view-content) {
  366. display: flex;
  367. flex-direction: column;
  368. gap: px2rpx(20);
  369. }
  370. :deep(uni-button) {
  371. line-height: px2rpx(35);
  372. }
  373. /* ========== Chip 胶囊样式 (移动端筛选弹窗) ========== */
  374. .chip-group {
  375. margin-bottom: px2rpx(20);
  376. .chip-list {
  377. display: flex;
  378. flex-wrap: wrap;
  379. gap: px2rpx(16);
  380. }
  381. .chip {
  382. display: inline-flex;
  383. align-items: center;
  384. justify-content: center;
  385. padding: px2rpx(4) px2rpx(12);
  386. border-radius: px2rpx(64);
  387. font-size: px2rpx(14);
  388. font-weight: 500;
  389. transition: all 0.2s ease;
  390. background-color: #ffffff;
  391. border: px2rpx(1) solid #d1d5db;
  392. color: #141d22;
  393. cursor: pointer;
  394. &:active {
  395. transform: scale(0.96);
  396. }
  397. /* 填充样式(选中) */
  398. &.chip-filled {
  399. background-color: #007aff;
  400. border-color: #007aff;
  401. color: #ffffff;
  402. }
  403. /* 轮廓样式(未选中) */
  404. &.chip-outlined {
  405. background-color: #ffffff;
  406. border-color: #d1d5db;
  407. color: #141d22;
  408. }
  409. }
  410. }
  411. }
  412. </style>