cwg-complex-search.vue 19 KB

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