cwg-complex-search.vue 20 KB

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