index.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <template>
  2. <div class="app-container">
  3. <el-row :gutter="20">
  4. <splitpanes :horizontal="appStore.device === 'mobile'" class="default-theme">
  5. <!--机构数据-->
  6. <!-- <pane size="16">-->
  7. <!-- <el-col>-->
  8. <!-- <div class="head-container">-->
  9. <!-- <el-input v-model="deptName" placeholder="请输入机构名称" clearable prefix-icon="Search" style="margin-bottom: 20px" />-->
  10. <!-- </div>-->
  11. <!-- <div class="head-container">-->
  12. <!-- <el-tree :data="deptOptions" :props="{ label: 'label', children: 'children' }" :expand-on-click-node="false" :filter-node-method="filterNode" ref="deptTreeRef" node-key="id" highlight-current default-expand-all @node-click="handleNodeClick" />-->
  13. <!-- </div>-->
  14. <!-- </el-col>-->
  15. <!-- </pane>-->
  16. <pane size="100">
  17. <el-col>
  18. <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
  19. <el-form-item label="学校名称" prop="name">
  20. <el-select
  21. v-model="queryParams.id"
  22. placeholder="请选择或输入学校名称"
  23. filterable
  24. clearable
  25. style="width: 200px"
  26. >
  27. <el-option
  28. v-for="school in allSchoolList"
  29. :key="school.id"
  30. :label="school.name"
  31. :value="school.id"
  32. />
  33. </el-select>
  34. </el-form-item>
  35. <!-- <el-form-item label="省份" prop="location">-->
  36. <!-- <el-input-->
  37. <!-- v-model="queryParams.location"-->
  38. <!-- placeholder="请输入省份"-->
  39. <!-- clearable-->
  40. <!-- @keyup.enter="handleQuery"-->
  41. <!-- />-->
  42. <!-- </el-form-item>-->
  43. <!-- <el-form-item label="状态" prop="status">
  44. <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 170px">
  45. <el-option
  46. v-for="dict in bool_values"
  47. :key="dict.value"
  48. :label="dict.label"
  49. :value="dict.value"
  50. />
  51. </el-select>
  52. </el-form-item> -->
  53. <el-form-item label="学校类型" prop="examTypes">
  54. <el-select v-model="queryParams.examTypes" placeholder="请选择学校类型" clearable style="width: 170px">
  55. <el-option
  56. v-for="dict in exam_type"
  57. :key="dict.value"
  58. :label="dict.label"
  59. :value="dict.value"
  60. />
  61. </el-select>
  62. </el-form-item>
  63. <el-form-item label="省市区" prop="areaIds">
  64. <AddressSelect class="w-[198px]" v-model="areaIds" />
  65. </el-form-item>
  66. <el-form-item>
  67. <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
  68. <el-button icon="Refresh" @click="resetQuery">重置</el-button>
  69. </el-form-item>
  70. </el-form>
  71. <el-row :gutter="10" class="mb8">
  72. <el-col :span="1.5">
  73. <el-button
  74. type="primary"
  75. plain
  76. icon="Plus"
  77. @click="handleAdd"
  78. v-hasPermi="['dz:school:add']"
  79. >新增</el-button>
  80. </el-col>
  81. <el-col :span="1.5">
  82. <el-button
  83. type="success"
  84. plain
  85. icon="Edit"
  86. :disabled="single"
  87. @click="handleUpdate"
  88. v-hasPermi="['dz:school:edit']"
  89. >修改</el-button>
  90. </el-col>
  91. <el-col :span="1.5">
  92. <el-button
  93. type="danger"
  94. plain
  95. icon="Delete"
  96. :disabled="multiple"
  97. @click="handleDelete"
  98. v-hasPermi="['dz:school:remove']"
  99. >删除</el-button>
  100. </el-col>
  101. <el-col :span="1.5">
  102. <el-button
  103. type="warning"
  104. plain
  105. icon="Download"
  106. @click="handleExport"
  107. v-hasPermi="['dz:school:export']"
  108. >导出</el-button>
  109. </el-col>
  110. <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
  111. </el-row>
  112. <el-table v-loading="loading" :data="schoolList" @selection-change="handleSelectionChange">
  113. <el-table-column type="selection" width="55" align="center" />
  114. <el-table-column label="ID" align="center" prop="id" width="80" />
  115. <el-table-column label="名称" align="center" prop="name" />
  116. <!-- <el-table-column label="归属机构" align="center" key="deptName" prop="dept.deptName" :show-overflow-tooltip="true" />-->
  117. <!-- <el-table-column label="省份" align="center" prop="location" />-->
  118. <el-table-column label="所属地区" align="center" prop="proCityAreaName" />
  119. <!-- <el-table-column label="状态" align="center" prop="status">-->
  120. <!-- <template #default="scope">-->
  121. <!-- <dict-tag :options="bool_values" :value="scope.row.status"/>-->
  122. <!-- </template>-->
  123. <!-- </el-table-column>-->
  124. <el-table-column label="学校类型" align="center" prop="examTypes">
  125. <template #default="scope">
  126. <span>{{ formatExamTypes(scope.row.examTypes) }}</span>
  127. </template>
  128. </el-table-column>
  129. <el-table-column label="班级修改" align="center" prop="classSelect" width="100">
  130. <template #default="scope">
  131. <el-switch
  132. v-model="scope.row.classSelect"
  133. active-value="1"
  134. inactive-value="0"
  135. @change="handleClassSelectChange(scope.row)"
  136. ></el-switch>
  137. </template>
  138. </el-table-column>
  139. <!-- <el-table-column label="备注" align="center" prop="remark" />-->
  140. <el-table-column label="班级管理" align="center" width="120">
  141. <template #default="scope">
  142. <el-button link type="primary" @click="handleViewClasses(scope.row)">点击查看</el-button>
  143. </template>
  144. </el-table-column>
  145. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  146. <template #default="scope">
  147. <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['dz:school:edit']">修改</el-button>
  148. <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['dz:school:remove']">删除</el-button>
  149. </template>
  150. </el-table-column>
  151. </el-table>
  152. <pagination
  153. v-show="total>0"
  154. :total="total"
  155. v-model:page="queryParams.pageNum"
  156. v-model:limit="queryParams.pageSize"
  157. @pagination="getList"
  158. />
  159. </el-col>
  160. </pane>
  161. </splitpanes>
  162. </el-row>
  163. <!-- 班级管理对话框 -->
  164. <el-dialog title="班级管理" v-model="classesDialogOpen" width="90%" append-to-body>
  165. <Classes :key="selectedSchoolId" :externalSchoolId="selectedSchoolId" />
  166. </el-dialog>
  167. <!-- 添加或修改机构校区对话框 -->
  168. <el-dialog :title="title" v-model="open" width="500px" append-to-body>
  169. <el-form ref="schoolRef" :model="form" :rules="rules" label-width="80px">
  170. <el-form-item label="名称" prop="name">
  171. <el-input v-model="form.name" placeholder="请输入名称" />
  172. </el-form-item>
  173. <!-- <el-form-item label="归属机构" prop="deptId">-->
  174. <!-- <el-tree-select v-model="form.deptId" :data="enabledDeptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择归属机构" clearable check-strictly />-->
  175. <!-- </el-form-item>-->
  176. <!-- <el-form-item label="省份" prop="location">
  177. <el-input v-model="form.location" placeholder="请输入省份" />
  178. </el-form-item> -->
  179. <el-form-item label="省市区" prop="areaIds">
  180. <AddressSelect v-model="formAreaIds" style="width: 100%" />
  181. </el-form-item>
  182. <el-form-item label="学校类型" prop="examTypes">
  183. <el-select v-model="form.examTypes" placeholder="请选择学校类型" multiple>
  184. <el-option
  185. v-for="dict in exam_type"
  186. :key="dict.value"
  187. :label="dict.label"
  188. :value="dict.value"
  189. ></el-option>
  190. </el-select>
  191. </el-form-item>
  192. <!-- <el-form-item label="状态" prop="status">-->
  193. <!-- <el-select v-model="form.status" placeholder="请选择状态">-->
  194. <!-- <el-option-->
  195. <!-- v-for="dict in bool_values"-->
  196. <!-- :key="dict.value"-->
  197. <!-- :label="dict.label"-->
  198. <!-- :value="parseInt(dict.value)"-->
  199. <!-- ></el-option>-->
  200. <!-- </el-select>-->
  201. <!-- </el-form-item>-->
  202. <!-- <el-form-item label="备注" prop="remark">-->
  203. <!-- <el-input v-model="form.remark" placeholder="请输入备注" />-->
  204. <!-- </el-form-item>-->
  205. </el-form>
  206. <template #footer>
  207. <div class="dialog-footer">
  208. <el-button type="primary" @click="submitForm">确 定</el-button>
  209. <el-button @click="cancel">取 消</el-button>
  210. </div>
  211. </template>
  212. </el-dialog>
  213. </div>
  214. </template>
  215. <script setup name="School">
  216. import { listSchool, getSchool, delSchool, addSchool, updateSchool, checkSchoolName, listAllSchool } from "@/api/dz/school"
  217. import { deptTreeSelect } from "@/api/system/user"
  218. import useAppStore from '@/store/modules/app'
  219. import { Splitpanes, Pane } from "splitpanes"
  220. import "splitpanes/dist/splitpanes.css"
  221. import AddressSelect from '@/components/AddressSelect';
  222. import { debounce } from '@/utils/index';
  223. import Classes from '@/views/dz/classes/index.vue';
  224. const { proxy } = getCurrentInstance()
  225. const appStore = useAppStore()
  226. const { bool_values, exam_type } = proxy.useDict('bool_values', 'exam_type')
  227. const deptName = ref("")
  228. const deptOptions = ref(undefined)
  229. const enabledDeptOptions = ref(undefined)
  230. const schoolList = ref([])
  231. const allSchoolList = ref([]) // 所有学校列表(用于下拉选择)
  232. const open = ref(false)
  233. const loading = ref(true)
  234. const showSearch = ref(true)
  235. const ids = ref([])
  236. const single = ref(true)
  237. const multiple = ref(true)
  238. const total = ref(0)
  239. const title = ref("")
  240. const classesDialogOpen = ref(false) // 班级管理对话框显示状态
  241. const selectedSchoolId = ref(null) // 选中的学校ID
  242. const dataList = ref([]);
  243. const areaIds = ref([]); // 筛选条件中的省市区
  244. const formAreaIds = ref([]); // 表单对话框中的省市区
  245. let selectedIds = [];
  246. const schoolRef = ref(null);
  247. // 先创建 data 对象,但先不定义 rules
  248. const data = reactive({
  249. form: {},
  250. queryParams: {
  251. pageNum: 1,
  252. pageSize: 10,
  253. id: null, // 学校ID(用于精确查询)
  254. name: null,
  255. deptId: null,
  256. location: null,
  257. pro: null,
  258. city: null,
  259. area: null,
  260. status: null,
  261. examTypes: null,
  262. },
  263. rules: {}
  264. })
  265. // 名称校验函数(在 data 定义之后,可以访问 data.form)
  266. const validateSchoolName = (rule, value, callback) => {
  267. if (!value) {
  268. callback()
  269. return
  270. }
  271. const schoolId = data.form.id || null
  272. checkSchoolName(value, schoolId).then(response => {
  273. if (response.data) {
  274. callback()
  275. } else {
  276. callback(new Error('学校名称已存在,请使用其他名称'))
  277. }
  278. }).catch(() => {
  279. callback()
  280. })
  281. }
  282. // 设置 rules(在 validateSchoolName 定义之后)
  283. data.rules = {
  284. name: [
  285. { required: true, message: "名称不能为空", trigger: "blur" },
  286. { validator: validateSchoolName, trigger: "blur" }
  287. ]
  288. // status: [
  289. // { required: true, message: "状态不能为空", trigger: "change" },
  290. // { required: true, message: "机构不能为空", trigger: "change" }
  291. // ],
  292. }
  293. const { queryParams, form, rules } = toRefs(data)
  294. const queryRef = ref(null);
  295. const dateRange = ref([]);
  296. // 防抖校验函数
  297. const debouncedValidateName = debounce((name) => {
  298. if (!name || !schoolRef.value) return
  299. const schoolId = form.value.id || null
  300. checkSchoolName(name, schoolId).then(response => {
  301. if (!response.data) {
  302. // 名称不唯一,触发校验错误
  303. schoolRef.value.validateField('name', () => {})
  304. } else {
  305. // 名称唯一,清除错误
  306. schoolRef.value.clearValidate('name')
  307. }
  308. })
  309. }, 500)
  310. // 监听名称变化(仅在对话框打开时校验)
  311. watch(() => form.value.name, (newName) => {
  312. if (open.value && newName && newName.trim()) {
  313. debouncedValidateName(newName.trim())
  314. }
  315. })
  316. /** 查询机构校区列表 */
  317. function getList() {
  318. loading.value = true
  319. let areaObj = { pro: areaIds.value?.[0] || '', city: areaIds.value?.[1] || '', area: areaIds.value?.[2] || '' };
  320. // 如果选择了学校ID,则根据ID精确查询,清空name参数
  321. let params = { ...proxy.addDateRange(queryParams.value), ...areaObj }
  322. if (params.id) {
  323. params.name = null // 如果选择了ID,则不再使用name查询
  324. } else {
  325. params.id = null // 如果没有选择ID,则清空id参数
  326. }
  327. listSchool(params).then(response => {
  328. // 将 classSelect 转换为字符串,以便 el-switch 正确显示
  329. schoolList.value = response.rows.map(item => ({
  330. ...item,
  331. classSelect: item.classSelect !== null && item.classSelect !== undefined ? String(item.classSelect) : "0"
  332. }))
  333. total.value = response.total
  334. loading.value = false
  335. })
  336. }
  337. /** 加载所有学校列表(用于下拉选择) */
  338. function loadAllSchoolList() {
  339. listAllSchool({ pageNum: 1, pageSize: 9999 }).then(response => {
  340. allSchoolList.value = response.data || []
  341. })
  342. }
  343. /** 通过条件过滤节点 */
  344. const filterNode = (value, data) => {
  345. if (!value) return true
  346. return data.label.indexOf(value) !== -1
  347. }
  348. /** 根据名称筛选机构树 */
  349. watch(deptName, val => {
  350. proxy.$refs["deptTreeRef"].filter(val)
  351. })
  352. /** 查询机构下拉树结构 */
  353. function getDeptTree() {
  354. deptTreeSelect().then(response => {
  355. deptOptions.value = response.data
  356. enabledDeptOptions.value = filterDisabledDept(JSON.parse(JSON.stringify(response.data)))
  357. })
  358. }
  359. /** 过滤禁用的机构 */
  360. function filterDisabledDept(deptList) {
  361. return deptList.filter(dept => {
  362. if (dept.disabled) {
  363. return false
  364. }
  365. if (dept.children && dept.children.length) {
  366. dept.children = filterDisabledDept(dept.children)
  367. }
  368. return true
  369. })
  370. }
  371. /** 节点单击事件 */
  372. function handleNodeClick(data) {
  373. queryParams.value.deptId = data.id
  374. handleQuery()
  375. }
  376. /** 查询机构代理下拉树结构 */
  377. function getTreeselect() {
  378. listAgent().then(response => {
  379. agentOptions.value = []
  380. const data = { agentId: 0, name: '顶级节点', children: [] }
  381. data.children = proxy.handleTree(response.data, "agentId", "parentId")
  382. agentOptions.value.push(data)
  383. })
  384. }
  385. // 取消按钮
  386. function cancel() {
  387. open.value = false
  388. reset()
  389. }
  390. // 表单重置
  391. function reset() {
  392. form.value = {
  393. id: null,
  394. name: null,
  395. deptId: null,
  396. location: null,
  397. remark: null,
  398. pro: null,
  399. city: null,
  400. area: null,
  401. status: null,
  402. examTypes: [],
  403. createTime: null,
  404. updateTime: null
  405. }
  406. // 清空表单中的省市区选择
  407. formAreaIds.value = []
  408. if (schoolRef.value) {
  409. schoolRef.value.resetFields()
  410. schoolRef.value.clearValidate()
  411. }
  412. }
  413. /** 搜索按钮操作 */
  414. function handleQuery() {
  415. queryParams.value.pageNum = 1
  416. getList()
  417. }
  418. /** 重置按钮操作 */
  419. function resetQuery() {
  420. // 清空机构ID
  421. // queryParams.value.deptId = undefined
  422. // 清空机构名称
  423. // deptName.value = ""
  424. // 取消树节点的选中状态
  425. // proxy.$refs.deptTreeRef.setCurrentKey(null)
  426. proxy.resetForm("queryRef")
  427. // 清空学校ID(下拉选择框的值)
  428. queryParams.value.id = null
  429. // 清空省市区选择
  430. areaIds.value = []
  431. handleQuery()
  432. }
  433. // 多选框选中数据
  434. function handleSelectionChange(selection) {
  435. ids.value = selection.map(item => item.id)
  436. single.value = selection.length != 1
  437. multiple.value = !selection.length
  438. }
  439. /** 新增按钮操作 */
  440. function handleAdd() {
  441. reset()
  442. open.value = true
  443. title.value = "添加机构校区"
  444. }
  445. /** 修改按钮操作 */
  446. function handleUpdate(row) {
  447. reset()
  448. const _id = row.id || ids.value
  449. getSchool(_id).then(response => {
  450. form.value = response.data
  451. // 将 examTypes 字符串转换为数组(如果是逗号分隔的字符串)
  452. if (form.value.examTypes && typeof form.value.examTypes === 'string') {
  453. form.value.examTypes = form.value.examTypes.split(',').filter(item => item.trim())
  454. } else if (!form.value.examTypes) {
  455. form.value.examTypes = []
  456. }
  457. // 将省市区数据转换为 formAreaIds 数组格式,用于 AddressSelect 组件回显
  458. // 按照顺序构建数组:[省, 市, 区],只包含有值的部分
  459. const areaArray = []
  460. if (form.value.pro) {
  461. areaArray.push(form.value.pro)
  462. if (form.value.city) {
  463. areaArray.push(form.value.city)
  464. if (form.value.area) {
  465. areaArray.push(form.value.area)
  466. }
  467. }
  468. }
  469. formAreaIds.value = areaArray
  470. open.value = true
  471. title.value = "修改机构校区"
  472. })
  473. }
  474. /** 提交按钮 */
  475. function submitForm() {
  476. schoolRef.value.validate(valid => {
  477. if (valid) {
  478. // 构建省市区对象(使用表单中的 formAreaIds)
  479. let areaObj = {
  480. pro: formAreaIds.value?.[0] || '',
  481. city: formAreaIds.value?.[1] || '',
  482. area: formAreaIds.value?.[2] || ''
  483. };
  484. // 处理 examTypes:如果是数组,转换为逗号分隔的字符串
  485. let examTypesValue = form.value.examTypes
  486. if (Array.isArray(examTypesValue)) {
  487. examTypesValue = examTypesValue.length > 0 ? examTypesValue.join(',') : null
  488. } else if (!examTypesValue) {
  489. examTypesValue = null
  490. }
  491. // 合并表单数据和省市区数据
  492. const submitData = {
  493. ...form.value,
  494. ...areaObj,
  495. examTypes: examTypesValue
  496. };
  497. if (form.value.id != null) {
  498. // updateSchool(form.value).then(response => {
  499. updateSchool(submitData).then(response => {
  500. proxy.$modal.msgSuccess("修改成功")
  501. open.value = false
  502. getList()
  503. })
  504. } else {
  505. // addSchool(form.value).then(response => {
  506. addSchool(submitData).then(response => {
  507. proxy.$modal.msgSuccess("新增成功")
  508. open.value = false
  509. getList()
  510. })
  511. }
  512. }
  513. })
  514. }
  515. /** 删除按钮操作 */
  516. function handleDelete(row) {
  517. const _ids = row.id || ids.value
  518. proxy.$modal.confirm('是否确认删除机构校区编号为"' + _ids + '"的数据项?').then(function() {
  519. return delSchool(_ids)
  520. }).then(() => {
  521. getList()
  522. proxy.$modal.msgSuccess("删除成功")
  523. }).catch(() => {})
  524. }
  525. /** 导出按钮操作 */
  526. function handleExport() {
  527. proxy.download('dz/school/export', {
  528. ...queryParams.value
  529. }, `school_${new Date().getTime()}.xlsx`)
  530. }
  531. /** 格式化学校类型显示 */
  532. function formatExamTypes(examTypes) {
  533. if (!examTypes) return ''
  534. // 将字符串按逗号分割
  535. const types = String(examTypes).split(',').map(item => item.trim()).filter(item => item)
  536. if (types.length === 0) return ''
  537. // 根据字典查找对应的标签
  538. const labels = types.map(type => {
  539. const dictItem = exam_type.value.find(item => item.value === type)
  540. return dictItem ? dictItem.label : type
  541. })
  542. // 用英文逗号连接
  543. return labels.join(',')
  544. }
  545. /** 查看班级管理 */
  546. function handleViewClasses(row) {
  547. selectedSchoolId.value = row.id
  548. classesDialogOpen.value = true
  549. }
  550. /** 班级选择状态修改 */
  551. function handleClassSelectChange(row) {
  552. let text = row.classSelect === "1" ? "开启" : "关闭"
  553. proxy.$modal.confirm('确认要"' + text + '""' + row.name + '"的班级修改吗?').then(function () {
  554. return updateSchool({
  555. id: row.id,
  556. classSelect: row.classSelect === "1" ? 1 : 0
  557. })
  558. }).then(() => {
  559. proxy.$modal.msgSuccess(text + "成功")
  560. getList()
  561. }).catch(function () {
  562. row.classSelect = row.classSelect === "1" ? "0" : "1"
  563. })
  564. }
  565. onMounted(() => {
  566. // getDeptTree()
  567. loadAllSchoolList() // 加载所有学校列表
  568. getList()
  569. })
  570. </script>