index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <route lang="yaml">
  2. meta:
  3. enabled: false
  4. </route>
  5. <script lang="ts" setup name="daasObjectEdit">
  6. import Sortable from 'sortablejs'
  7. import type { FormInstance, FormRules } from 'element-plus'
  8. import { ElMessage } from 'element-plus'
  9. import { createObject, getObjectById, updateObject } from '@/api/model/objectApi'
  10. import useSettingsStore from '@/store/modules/settings'
  11. const settingsStore = useSettingsStore()
  12. const route = useRoute()
  13. const modelId = parseInt(route.params.modelId.toString())
  14. const objectId = parseInt(route.params.objectId.toString())
  15. const router = useRouter()
  16. const tabbar = useTabbar()
  17. interface ObjectAttr {
  18. attrId: number
  19. attrTitle: string
  20. attrCode: string
  21. attrType: number
  22. attrLength: number
  23. attrDecimals: number
  24. isNotNull: number
  25. isFixed: number
  26. comment: string
  27. isPKey: number
  28. sortNo: number
  29. }
  30. const dataType = [
  31. { dataType: 1, dataName: '整数' },
  32. { dataType: 2, dataName: '字符串' },
  33. { dataType: 3, dataName: '小数' }]
  34. // 数据
  35. const state = reactive({
  36. title: '',
  37. dialogVisible: false,
  38. loading: false,
  39. search: {
  40. keywords: '',
  41. },
  42. formData: {
  43. modelId,
  44. objectId,
  45. objectType: 1,
  46. objectCode: '',
  47. objectTitle: '',
  48. comment: '',
  49. attrs: [] as ObjectAttr[],
  50. },
  51. actionType: 1,
  52. })
  53. const formRef = ref<FormInstance>()
  54. const formRules = ref<FormRules>({
  55. objectTitle: [
  56. { required: true, message: '请输入对象标题', trigger: 'blur' },
  57. ],
  58. objectType: [
  59. { required: true, message: '请输入对象类型', trigger: 'blur' },
  60. ],
  61. objectCode: [
  62. { required: true, message: '请输入对像代码', trigger: 'blur' },
  63. ],
  64. comment: [
  65. { required: false, message: '请输入简要描述', trigger: 'blur' },
  66. ],
  67. })
  68. // 查询数据
  69. const getData = async () => {
  70. state.loading = true
  71. const { data } = await getObjectById(modelId, objectId)
  72. state.loading = false
  73. state.formData = data
  74. }
  75. const attrsTableRef = ref()
  76. const attrsTableKey = ref(0)
  77. function onAttrAdd() {
  78. state.formData.attrs.push({
  79. attrId: 0,
  80. attrTitle: '', // 名称
  81. attrCode: '', // 代码
  82. attrType: 0, // 类型
  83. attrLength: 0, // 长度
  84. attrDecimals: 0, // 小数位数
  85. isNotNull: 0, // 是否为空
  86. isFixed: 0, // 是否锁定
  87. isPKey: 0, // 主键
  88. comment: '', // 注释
  89. sortNo: 0,
  90. })
  91. nextTick(() => {
  92. attrsTableRef.value.setScrollTop(state.formData.attrs.length * 50)
  93. })
  94. }
  95. // 删除属性
  96. function onAttrDelete(index: number) {
  97. state.formData.attrs.splice(index, 1)
  98. }
  99. function onAttrDrag() {
  100. const tbody = attrsTableRef.value.$el.querySelector('.el-table__body-wrapper tbody')
  101. Sortable.create(tbody, {
  102. handle: '.sortable',
  103. animation: 300,
  104. ghostClass: 'ghost',
  105. onEnd: ({ newIndex, oldIndex }) => {
  106. if (newIndex === undefined || oldIndex === undefined) {
  107. return
  108. }
  109. const currRow = state.formData.attrs.splice(oldIndex, 1)[0]
  110. state.formData.attrs.splice(newIndex, 0, currRow)
  111. attrsTableKey.value += 1
  112. nextTick(() => onAttrDrag())
  113. },
  114. })
  115. }
  116. onMounted(() => {
  117. if (objectId <= 0) {
  118. state.title = '新增对象'
  119. state.actionType = 1
  120. }
  121. else {
  122. state.title = '编辑对象'
  123. state.actionType = 2
  124. getData()
  125. }
  126. nextTick(() => {
  127. onAttrDrag()
  128. })
  129. })
  130. // 保存
  131. function onSubmit() {
  132. formRef.value && formRef.value.validate(async (valid) => {
  133. if (valid) {
  134. state.formData.attrs.forEach((item, index) => item.sortNo = index)
  135. if (state.actionType === 1) {
  136. await createObject(state.formData)
  137. state.dialogVisible = false
  138. ElMessage.success('新增成功')
  139. }
  140. else {
  141. await updateObject(state.formData)
  142. state.dialogVisible = false
  143. ElMessage.success('修改成功')
  144. }
  145. goBack()
  146. }
  147. })
  148. }
  149. function onCancel() {
  150. goBack()
  151. }
  152. // 返回列表页
  153. function goBack() {
  154. if (settingsStore.settings.tabbar.enable && settingsStore.settings.tabbar.mergeTabsBy !== 'activeMenu') {
  155. tabbar.close({ name: 'daasModel' })
  156. }
  157. else {
  158. router.push({ name: 'daasModel' })
  159. }
  160. }
  161. </script>
  162. <template>
  163. <div class="absolute-container">
  164. <page-header title="对象" content="为每个应用配置基于角色的权限。">
  165. <el-button size="default" round @click="goBack">
  166. <template #icon>
  167. <el-icon>
  168. <svg-icon name="i-ep:arrow-left" />
  169. </el-icon>
  170. </template>
  171. 返回
  172. </el-button>
  173. </page-header>
  174. <div class="page-main">
  175. <LayoutContainer hide-left-side-toggle>
  176. <template #leftSide>
  177. <h2>基本信息</h2>
  178. <el-scrollbar class="form-el-scrollbar">
  179. <div class="form-content">
  180. <el-form ref="formRef" :model="state.formData" :rules="formRules" label-position="top">
  181. <el-form-item label="对象名称" prop="objectTitle">
  182. <el-input v-model="state.formData.objectTitle" placeholder="请输入标题" />
  183. </el-form-item>
  184. <el-form-item label="类型" prop="objectType">
  185. <el-radio-group v-model="state.formData.objectType">
  186. <el-radio :label="1">
  187. 实体对象
  188. </el-radio>
  189. <el-radio :label="2">
  190. 视图对象
  191. </el-radio>
  192. </el-radio-group>
  193. </el-form-item>
  194. <el-form-item label="对象代码" prop="objectCode">
  195. <el-input v-model="state.formData.objectCode" placeholder="请输入代码" />
  196. </el-form-item>
  197. <el-form-item label="简要说明" prop="comment">
  198. <el-input v-model="state.formData.comment" type="textarea" rows="10" placeholder="请输入标题" />
  199. </el-form-item>
  200. </el-form>
  201. </div>
  202. </el-scrollbar>
  203. </template>
  204. <div class="container">
  205. <tool-bar>
  206. <template #left>
  207. <h2>属性</h2>
  208. </template>
  209. <template #right>
  210. <el-input v-model="state.search.keywords" placeholder="请输入关键词筛选字典项" clearable style="width: 200px;" />
  211. <el-button style="margin-left:10px" @click="getData">
  212. <template #icon>
  213. <el-icon>
  214. <svg-icon name="i-ep:search" />
  215. </el-icon>
  216. </template>
  217. </el-button>
  218. <el-button type="primary" @click="onAttrAdd">
  219. <template #icon>
  220. <el-icon>
  221. <svg-icon name="i-ep:plus" />
  222. </el-icon>
  223. </template>
  224. </el-button>
  225. </template>
  226. </tool-bar>
  227. <el-table ref="attrsTableRef" :key="attrsTableKey" :data="state.formData.attrs" class="list-table" border stripe highlight-current-row>
  228. <el-table-column width="60" align="center" fixed>
  229. <template #header>
  230. <el-button type="primary" size="small" plain circle @click="onAttrAdd">
  231. <template #icon>
  232. <el-icon>
  233. <svg-icon name="i-ep:plus" />
  234. </el-icon>
  235. </template>
  236. </el-button>
  237. </template>
  238. <template #default="scope">
  239. <span class="index">{{ scope.$index + 1 }}</span>
  240. <el-button type="danger" size="small" plain circle class="delete" @click="onAttrDelete(scope.$index)">
  241. <template #icon>
  242. <el-icon>
  243. <svg-icon name="i-ep:delete" />
  244. </el-icon>
  245. </template>
  246. </el-button>
  247. </template>
  248. </el-table-column>
  249. <el-table-column label="名称" width="150">
  250. <template #default="scope">
  251. <el-input v-model="scope.row.attrTitle" />
  252. </template>
  253. </el-table-column>
  254. <el-table-column label="代码" width="120">
  255. <template #default="scope">
  256. <el-input v-model="scope.row.attrCode" />
  257. </template>
  258. </el-table-column>
  259. <el-table-column label="类型" width="120">
  260. <template #default="scope">
  261. <el-select v-model="scope.row.attrType">
  262. <el-option
  263. v-for="item in dataType"
  264. :key="item.dataType"
  265. :value="item.dataType"
  266. :label="item.dataName"
  267. />
  268. </el-select>
  269. </template>
  270. </el-table-column>
  271. <el-table-column label="长度" width="90">
  272. <template #default="scope">
  273. <el-input v-model="scope.row.attrLength" />
  274. </template>
  275. </el-table-column>
  276. <el-table-column label="小数位数" width="90">
  277. <template #default="scope">
  278. <el-input v-model="scope.row.attrDecimals" />
  279. </template>
  280. </el-table-column>
  281. <el-table-column label="是否为空" width="90">
  282. <template #default="scope">
  283. <el-checkbox v-model="scope.row.isNotNull" :true-label="1" :false-label="0">
  284. {{ scope.row.isNotNull === 1 ? '是' : '否' }}
  285. </el-checkbox>
  286. </template>
  287. </el-table-column>
  288. <el-table-column label="主键" width="70">
  289. <template #default="scope">
  290. <el-checkbox v-model="scope.row.isPKey" :true-label="1" :false-label="0">
  291. {{ scope.row.isPKey === 1 ? '是' : '否' }}
  292. </el-checkbox>
  293. </template>
  294. </el-table-column>
  295. <el-table-column label="锁定" width="70">
  296. <template #default="scope">
  297. <el-checkbox v-model="scope.row.isFixed" :true-label="1" :false-label="0">
  298. {{ scope.row.isFixed === 1 ? '是' : '否' }}
  299. </el-checkbox>
  300. </template>
  301. </el-table-column>
  302. <el-table-column label="注释">
  303. <template #default="scope">
  304. <el-input v-model="scope.row.comment" />
  305. </template>
  306. </el-table-column>
  307. <el-table-column width="80" align="center">
  308. <template #header>
  309. 排序
  310. </template>
  311. <template #default>
  312. <el-tag type="info" class="sortable">
  313. <el-icon>
  314. <svg-icon name="i-ep:d-caret" />
  315. </el-icon>
  316. </el-tag>
  317. </template>
  318. </el-table-column>
  319. </el-table>
  320. </div>
  321. </LayoutContainer>
  322. </div>
  323. <fixed-action-bar>
  324. <el-button type="primary" size="large" @click="onSubmit">
  325. 保存
  326. </el-button>
  327. <el-button size="large" @click="onCancel">
  328. 取消
  329. </el-button>
  330. </fixed-action-bar>
  331. </div>
  332. </template>
  333. <style lang="scss" scoped>
  334. .page-main {
  335. :deep(.left-side) {
  336. display: flex;
  337. flex-direction: column;
  338. height: 100%;
  339. .form-el-scrollbar{
  340. flex: 1;
  341. }
  342. }
  343. :deep(.el-table) {
  344. height: 100%;
  345. margin-top: 15px;
  346. .el-table__row {
  347. .index {
  348. display: inline-block;
  349. }
  350. .delete {
  351. display: none;
  352. }
  353. &:hover {
  354. .index {
  355. display: none;
  356. }
  357. .delete {
  358. display: inline-block;
  359. }
  360. }
  361. .el-tag.sortable,
  362. .el-tag.sortable .el-icon {
  363. cursor: s-resize;
  364. }
  365. }
  366. }
  367. h2{
  368. padding: 0;
  369. margin: 0;
  370. }
  371. }
  372. </style>