cwg-complex-search.vue 20 KB

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