cwg-complex-search.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 === 'picker'">
  27. <uni-data-picker
  28. v-model="formData[field.key]"
  29. :localdata="field.options"
  30. :popup-title="field.popupTitle || t('State.All')"
  31. :map="field.map || { value: 'value', text: 'label' }"
  32. @change="(e) => field.onChange?.(e)"
  33. @nodeclick="(node) => field.onNodeClick?.(node)"
  34. />
  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 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 v-if="shouldUseSelect(field)" />
  83. <view class="chip-group" v-else>
  84. <view class="chip-list">
  85. <view v-for="opt in field.options" :key="opt.value" class="chip" :class="{
  86. 'chip-filled': tempFormData[field.key] === opt.value,
  87. 'chip-outlined': tempFormData[field.key] !== opt.value
  88. }" @click="selectChip(field.key, opt.value)">
  89. {{ opt.text }}
  90. </view>
  91. </view>
  92. </view>
  93. </template>
  94. <template v-else-if="field.type === 'number'">
  95. <uni-easyinput v-model="tempFormData[field.key]" type="number"
  96. :placeholder="field.placeholder || '请输入数字'" />
  97. </template>
  98. <template v-else-if="field.type === 'picker'">
  99. <uni-data-picker
  100. v-model="tempFormData[field.key]"
  101. :localdata="field.options"
  102. :popup-title="field.popupTitle || t('State.All')"
  103. :map="field.map || { value: 'value', text: 'label' }"
  104. @change="(e) => field.onChange?.(e)"
  105. @nodeclick="(node) => field.onNodeClick?.(node)"
  106. />
  107. </template>
  108. </view>
  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, 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 getDefaultDateValue = (field) => {
  142. const now = new Date()
  143. const year = now.getFullYear()
  144. const month = now.getMonth()
  145. const firstDay = new Date(year, month, 1)
  146. const lastDay = new Date(year, month + 1, 0)
  147. const formatDate = (date) => {
  148. const y = date.getFullYear()
  149. const m = String(date.getMonth() + 1).padStart(2, '0')
  150. const d = String(date.getDate()).padStart(2, '0')
  151. return `${y}-${m}-${d}`
  152. }
  153. if (field.type === 'daterange') {
  154. return [formatDate(firstDay), formatDate(lastDay)]
  155. } else if (field.type === 'date') {
  156. // 单日期默认设为今天(也可设为第一天,根据需求调整)
  157. return formatDate(now)
  158. }
  159. return ''
  160. }
  161. // 表单数据(PC端)
  162. const formData = ref({})
  163. // 移动端临时数据
  164. const tempFormData = ref({})
  165. // 移动端弹窗是否可见
  166. const visible = ref(false)
  167. // 将字段按列数分组(仅用于PC布局)
  168. const formRows = computed(() => {
  169. const rows = []
  170. for (let i = 0; i < props.fields.length; i += props.columns) {
  171. rows.push(props.fields.slice(i, i + props.columns))
  172. }
  173. return rows
  174. })
  175. const selectChip = (key, value) => {
  176. // 如果点击的是当前已选中的项,可以保持不变,也可以置空(根据需求)
  177. // 此处保持选中该项,不提供取消功能(符合常见筛选行为)
  178. tempFormData.value[key] = value
  179. }
  180. // 初始化表单数据
  181. const initFormData = () => {
  182. const initial = {}
  183. props.fields.forEach(field => {
  184. // 1. 优先使用外部传入的 modelValue(如果值不为 null 或 undefined)
  185. if (props.modelValue && props.modelValue[field.key] != null) {
  186. initial[field.key] = props.modelValue[field.key]
  187. }
  188. // 2. 其次使用字段配置的 defaultValue
  189. else if (field.defaultValue !== undefined) {
  190. initial[field.key] = field.defaultValue
  191. }
  192. // 3. 日期字段特殊处理:默认当前月(即使 modelValue 中存在但为 null/undefined)
  193. else if (field.type === 'date' || field.type === 'daterange') {
  194. initial[field.key] = getDefaultDateValue(field)
  195. }
  196. // 4. 其他字段默认为空字符串
  197. else {
  198. initial[field.key] = ''
  199. }
  200. })
  201. formData.value = initial
  202. tempFormData.value = JSON.parse(JSON.stringify(initial))
  203. }
  204. // 获取第一个日期或日期范围字段(用于移动端顶部快捷显示)
  205. const dateField = computed(() => {
  206. return props.fields.find(field => field.type === 'date' || field.type === 'daterange')
  207. })
  208. // 判断字段是否应该使用下拉选择器(用于排序和渲染)
  209. const shouldUseSelect = (field) => {
  210. if (!field.options) return false
  211. return field.options.length > 10 || field.isSelect === true
  212. }
  213. const handleDateChange = (value) => {
  214. // 日期变化时自动触发搜索
  215. handleSearch()
  216. }
  217. // 排序:输入框 + 下拉选择器 → 前面;Chip 选择器 → 后面
  218. const nonDateField = computed(() => {
  219. const filtered = props.fields.filter(field => field.type !== 'date' && field.type !== 'daterange')
  220. const priorityFields = []
  221. const otherFields = []
  222. filtered.forEach(field => {
  223. if (field.type === 'input' || field.type === 'number') {
  224. priorityFields.push(field)
  225. }
  226. else if (field.type === 'select') {
  227. if (shouldUseSelect(field)) {
  228. priorityFields.push(field)
  229. } else {
  230. otherFields.push(field)
  231. }
  232. }
  233. else if (field.type === 'picker') {
  234. priorityFields.push(field)
  235. }
  236. else {
  237. otherFields.push(field)
  238. }
  239. })
  240. return [...priorityFields, ...otherFields]
  241. })
  242. // 监听外部 modelValue 变化
  243. watch(() => props.modelValue, (newVal) => {
  244. if (newVal) {
  245. props.fields.forEach(field => {
  246. if (newVal[field.key] != null) {
  247. formData.value[field.key] = newVal[field.key]
  248. } else if (field.type === 'date' || field.type === 'daterange') {
  249. // 当日期字段为 null 或 undefined 时,使用默认值
  250. formData.value[field.key] = getDefaultDateValue(field)
  251. }
  252. })
  253. }
  254. }, { deep: true, immediate: true })
  255. // 监听内部 formData 变化,同步到外部
  256. watch(formData, (newVal) => {
  257. emit('update:modelValue', newVal)
  258. }, { deep: true })
  259. // 重置表单(清空所有字段)
  260. const resetForm = () => {
  261. const empty = {}
  262. props.fields.forEach(field => {
  263. if (field.defaultValue !== undefined) {
  264. empty[field.key] = field.defaultValue
  265. } else if (field.type === 'date' || field.type === 'daterange') {
  266. empty[field.key] = getDefaultDateValue(field)
  267. } else {
  268. empty[field.key] = ''
  269. }
  270. })
  271. formData.value = empty
  272. emit('reset', empty)
  273. }
  274. // 触发查询
  275. const handleSearch = () => {
  276. emit('search', { ...formData.value })
  277. }
  278. // 移动端:打开弹窗
  279. const openFilterPopup = () => {
  280. // 复制当前表单数据到临时对象
  281. tempFormData.value = JSON.parse(JSON.stringify(formData.value))
  282. visible.value = true
  283. }
  284. const closePopup = () => {
  285. visible.value = false
  286. }
  287. const resetTempForm = () => {
  288. const empty = {}
  289. props.fields.forEach(field => {
  290. if (field.defaultValue !== undefined) {
  291. empty[field.key] = field.defaultValue
  292. } else if (field.type === 'date' || field.type === 'daterange') {
  293. empty[field.key] = getDefaultDateValue(field)
  294. } else {
  295. empty[field.key] = ''
  296. }
  297. })
  298. tempFormData.value = empty
  299. }
  300. const applyFilter = () => {
  301. // 将临时数据同步到正式表单
  302. formData.value = JSON.parse(JSON.stringify(tempFormData.value))
  303. closePopup()
  304. handleSearch()
  305. }
  306. // 监听fields变化,重新初始化
  307. watch(() => props.fields, () => {
  308. initFormData()
  309. }, { deep: true, immediate: false })
  310. onMounted(() => {
  311. // 初始化表单数据
  312. initFormData()
  313. })
  314. </script>
  315. <style lang="scss" scoped>
  316. @import "@/uni.scss";
  317. .complex-search {
  318. width: 100%;
  319. }
  320. .search-bar {
  321. .cwg-combox,
  322. .uni-easyinput,
  323. .uni-date {
  324. width: px2rpx(240) !important;
  325. flex: none;
  326. }
  327. .uni-date {
  328. width: px2rpx(250) !important;
  329. flex: none;
  330. }
  331. .form-actions {
  332. display: flex;
  333. justify-content: flex-end;
  334. gap: px2rpx(10);
  335. .reset-btn,
  336. .search-btn {
  337. line-height: px2rpx(35);
  338. font-size: px2rpx(14);
  339. }
  340. }
  341. }
  342. .mobile-filter {
  343. margin-bottom: px2rpx(10);
  344. display: flex;
  345. align-items: center;
  346. justify-content: center;
  347. gap: px2rpx(12);
  348. .mobile-date-wrapper {
  349. flex: 1;
  350. }
  351. .filter-chip {
  352. display: inline-flex;
  353. align-items: center;
  354. justify-content: center;
  355. gap: px2rpx(12);
  356. background-color: #ffffff;
  357. border: 1px solid #e5e5e5;
  358. border-radius: px2rpx(4);
  359. padding: px2rpx(0) px2rpx(16);
  360. font-size: px2rpx(16);
  361. font-weight: 500;
  362. color: #141d22;
  363. line-height: px2rpx(35);
  364. transition: all 0.2s ease;
  365. box-shadow: none;
  366. .iconfont,
  367. .uni-icons {
  368. font-size: 32rpx;
  369. color: #141d22;
  370. }
  371. /* 按下反馈效果 */
  372. &:active {
  373. transform: scale(0.96);
  374. background-color: #f5f7fa;
  375. /* 轻量点击背景色 */
  376. border-color: #b9bfc7;
  377. }
  378. &::after {
  379. border: none;
  380. }
  381. }
  382. }
  383. /* 弹窗整体优化 */
  384. .drawer-content {
  385. padding: px2rpx(20);
  386. border-radius: px2rpx(16);
  387. box-sizing: border-box;
  388. .label {
  389. font-size: px2rpx(18);
  390. font-weight: 600;
  391. color: #2c3e50;
  392. margin-bottom: px2rpx(20);
  393. letter-spacing: px2rpx(1);
  394. }
  395. :deep(.uni-scroll-view-content) {
  396. display: flex;
  397. flex-direction: column;
  398. gap: px2rpx(20);
  399. }
  400. :deep(uni-button) {
  401. line-height: px2rpx(35);
  402. }
  403. /* ========== Chip 胶囊样式 (移动端筛选弹窗) ========== */
  404. .chip-group {
  405. margin-bottom: px2rpx(20);
  406. .chip-list {
  407. display: flex;
  408. flex-wrap: wrap;
  409. gap: px2rpx(16);
  410. }
  411. .chip {
  412. display: inline-flex;
  413. align-items: center;
  414. justify-content: center;
  415. padding: px2rpx(4) px2rpx(12);
  416. border-radius: px2rpx(64);
  417. font-size: px2rpx(14);
  418. font-weight: 500;
  419. transition: all 0.2s ease;
  420. background-color: #ffffff;
  421. border: px2rpx(1) solid #d1d5db;
  422. color: #141d22;
  423. cursor: pointer;
  424. &:active {
  425. transform: scale(0.96);
  426. }
  427. /* 填充样式(选中) */
  428. &.chip-filled {
  429. background-color: #007aff;
  430. border-color: #007aff;
  431. color: #ffffff;
  432. }
  433. /* 轮廓样式(未选中) */
  434. &.chip-outlined {
  435. background-color: #ffffff;
  436. border-color: #d1d5db;
  437. color: #141d22;
  438. }
  439. }
  440. }
  441. }
  442. </style>