index.vue 18.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
<template>
  <div class="NCC-common-layout workbench-admin">
    <div class="NCC-common-layout-center">
      <el-card shadow="never" class="admin-card">
        <div class="admin-hero">
          <div>
            <div class="admin-hero__title">工作台配置</div>
            <div class="admin-hero__desc">管理组件库、角色默认布局,以及「需授权」组件的角色可见范围</div>
          </div>
          <el-tag type="warning" effect="plain" size="small">所有组件均需在「角色组件授权」中分配后,对应用户才能添加与查看</el-tag>
        </div>

        <el-tabs v-model="activeTab">
          <el-tab-pane label="组件库" name="widgets">
            <el-table :data="widgetList" v-loading="widgetLoading" size="small" border>
              <el-table-column prop="code" label="编码" width="180" show-overflow-tooltip />
              <el-table-column prop="name" label="名称" width="140" />
              <el-table-column prop="category" label="分组" width="100" />
              <el-table-column prop="dataScope" label="数据范围" width="100" />
              <el-table-column label="需授权" width="80" align="center">
                <template slot-scope="{ row }">
                  <el-tag :type="row.needAuth ? 'danger' : 'info'" size="mini">
                    {{ row.needAuth ? '是' : '否' }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column label="状态" width="80" align="center">
                <template slot-scope="{ row }">
                  <el-tag :type="row.enabled ? 'success' : 'info'" size="mini">{{ row.enabled ? '启用' : '停用' }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="description" label="描述" min-width="200" show-overflow-tooltip />
              <el-table-column label="操作" width="100" fixed="right">
                <template slot-scope="{ row }">
                  <el-button type="text" size="mini" @click="toggleWidgetRow(row)">
                    {{ row.enabled ? '停用' : '启用' }}
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>

          <el-tab-pane label="角色默认模板" name="template">
            <div class="template-toolbar">
              <el-form :inline="true" size="small">
                <el-form-item label="角色">
                  <el-select v-model="templateRoleId" filterable style="width: 220px" @change="loadTemplate">
                    <el-option label="全局默认" value="GLOBAL" />
                    <el-option v-for="r in roleOptions" :key="r.id" :label="r.fullName" :value="r.id" />
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" icon="el-icon-plus" @click="templateDrawer = true">添加组件</el-button>
                  <el-button type="success" :loading="templateSaving" @click="saveTemplate">保存模板</el-button>
                </el-form-item>
              </el-form>
            </div>
            <grid-layout
              v-if="templateLayout.length"
              :layout.sync="templateLayout"
              :col-num="12"
              :row-height="40"
              :margin="[12, 12]"
              :is-draggable="true"
              :is-resizable="true"
            >
              <grid-item
                v-for="item in templateLayout"
                :key="item.i"
                :x="item.x"
                :y="item.y"
                :w="item.w"
                :h="item.h"
                :i="item.i"
                :min-w="2"
                :min-h="2"
              >
                <div class="tpl-item">
                  <span>{{ widgetName(item.i) }}</span>
                  <el-button type="text" icon="el-icon-close" class="tpl-remove" @click="removeTplItem(item.i)" />
                </div>
              </grid-item>
            </grid-layout>
            <div v-else class="empty-tip">模板为空,请添加组件</div>
          </el-tab-pane>

          <el-tab-pane label="角色组件授权" name="auth">
            <div class="auth-toolbar">
              <el-form :inline="true" size="small">
                <el-form-item label="角色">
                  <el-select v-model="authRoleId" filterable style="width: 220px" @change="loadRoleWidgets">
                    <el-option v-for="r in roleOptions" :key="r.id" :label="r.fullName" :value="r.id" />
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button size="small" @click="selectAllAuth">全选组件</el-button>
                  <el-button size="small" @click="clearAuth">清空</el-button>
                  <el-button type="primary" :loading="authSaving" @click="saveRoleAuth">保存授权</el-button>
                </el-form-item>
              </el-form>
              <el-alert
                title="说明:所有工作台组件均需授权。请按角色勾选可见组件后保存;未授权角色在「添加组件」中看不到对应组件。系统管理员默认可见全部启用组件。"
                type="info"
                :closable="false"
                show-icon
                class="auth-alert"
              />
            </div>

            <div v-if="!authWidgetOptions.length" class="empty-tip">当前没有已启用的组件</div>
            <div v-else class="auth-groups">
              <div v-for="group in authGroups" :key="group.category" class="auth-group">
                <div class="auth-group__head">
                  <span class="auth-group__title">{{ group.category }}</span>
                  <span class="auth-group__count">{{ group.items.length }}</span>
                  <el-button type="text" size="mini" @click="toggleGroup(group)">
                    {{ isGroupAllSelected(group) ? '取消本组' : '全选本组' }}
                  </el-button>
                </div>
                <el-checkbox-group v-model="authWidgetCodes" class="auth-list">
                  <el-checkbox
                    v-for="w in group.items"
                    :key="w.code"
                    :label="w.code"
                    class="auth-check"
                  >
                    <span class="auth-check__name">{{ w.name }}</span>
                    <span class="auth-check__code">{{ w.code }}</span>
                    <span class="auth-check__scope">{{ w.dataScope || '无' }}</span>
                  </el-checkbox>
                </el-checkbox-group>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <el-drawer
      title="添加组件到模板"
      :visible.sync="templateDrawer"
      size="480px"
      append-to-body
      custom-class="workbench-pick-drawer"
    >
      <div v-for="w in widgetList.filter(x => x.enabled)" :key="w.code" class="pick-row" @click="addToTemplate(w)">
        <i :class="w.icon || 'el-icon-menu'" />
        <div class="pick-row__text">
          <div>{{ w.name }}</div>
          <div class="pick-row__sub">{{ w.category || '其他' }} · {{ w.description || '无' }}</div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import VueGridLayout from 'vue-grid-layout'
import {
  getWidgetList,
  toggleWidget,
  getTemplate,
  saveTemplate,
  getRoleOptions,
  getRoleWidgets,
  saveRoleWidgets
} from '@/api/extend/workbench'

export default {
  name: 'WorkbenchAdmin',
  components: {
    GridLayout: VueGridLayout.GridLayout,
    GridItem: VueGridLayout.GridItem
  },
  data() {
    return {
      activeTab: 'auth',
      widgetList: [],
      widgetLoading: false,
      roleOptions: [],
      templateRoleId: 'GLOBAL',
      templateLayout: [],
      templateSaving: false,
      templateDrawer: false,
      authRoleId: '',
      authWidgetCodes: [],
      authSaving: false
    }
  },
  computed: {
    authWidgetOptions() {
      return this.widgetList.filter(w => w.enabled)
    },
    authGroups() {
      const map = {}
      this.authWidgetOptions.forEach(w => {
        const cat = w.category || '其他'
        if (!map[cat]) map[cat] = []
        map[cat].push(w)
      })
      return Object.keys(map).map(k => ({ category: k, items: map[k] }))
    }
  },
  mounted() {
    const tab = this.$route.query && this.$route.query.tab
    if (tab && ['widgets', 'template', 'auth'].includes(tab)) {
      this.activeTab = tab
    }
    this.init()
  },
  methods: {
    async init() {
      await Promise.all([this.loadWidgets(), this.loadRoles()])
      await this.loadTemplate()
    },
    widgetName(code) {
      const w = this.widgetList.find(x => x.code === code)
      return w ? w.name : code
    },
    async loadWidgets() {
      this.widgetLoading = true
      try {
        const res = await getWidgetList()
        this.widgetList = res.data || []
      } finally {
        this.widgetLoading = false
      }
    },
    async loadRoles() {
      const res = await getRoleOptions()
      this.roleOptions = res.data || []
      if (!this.authRoleId && this.roleOptions.length) {
        this.authRoleId = this.roleOptions[0].id
        await this.loadRoleWidgets()
      }
    },
    async toggleWidgetRow(row) {
      await toggleWidget(row.id)
      this.$message.success('已更新')
      await this.loadWidgets()
    },
    async loadTemplate() {
      const res = await getTemplate(this.templateRoleId)
      const data = res.data || {}
      this.templateLayout = (data.layout || []).map(x => ({ ...x }))
    },
    async saveTemplate() {
      this.templateSaving = true
      try {
        const role = this.roleOptions.find(r => r.id === this.templateRoleId)
        await saveTemplate({
          roleId: this.templateRoleId,
          roleName: this.templateRoleId === 'GLOBAL' ? '全局默认' : (role && role.fullName),
          layout: this.templateLayout
        })
        this.$message.success('模板已保存')
        await this.loadRoles()
      } finally {
        this.templateSaving = false
      }
    },
    addToTemplate(w) {
      if (this.templateLayout.some(l => l.i === w.code)) return
      const maxY = this.templateLayout.reduce((m, l) => Math.max(m, l.y + l.h), 0)
      this.templateLayout.push({
        i: w.code,
        x: 0,
        y: maxY,
        w: w.defaultW || 4,
        h: w.defaultH || 3
      })
      this.templateDrawer = false
    },
    removeTplItem(code) {
      this.templateLayout = this.templateLayout.filter(l => l.i !== code)
    },
    async loadRoleWidgets() {
      if (!this.authRoleId) return
      const res = await getRoleWidgets(this.authRoleId)
      this.authWidgetCodes = res.data || []
    },
    selectAllAuth() {
      this.authWidgetCodes = this.authWidgetOptions.map(w => w.code)
    },
    clearAuth() {
      this.authWidgetCodes = []
    },
    isGroupAllSelected(group) {
      return group.items.every(w => this.authWidgetCodes.includes(w.code))
    },
    toggleGroup(group) {
      const codes = group.items.map(w => w.code)
      if (this.isGroupAllSelected(group)) {
        this.authWidgetCodes = this.authWidgetCodes.filter(c => !codes.includes(c))
      } else {
        const set = new Set(this.authWidgetCodes.concat(codes))
        this.authWidgetCodes = Array.from(set)
      }
    },
    async saveRoleAuth() {
      if (!this.authRoleId) {
        this.$message.warning('请选择角色')
        return
      }
      this.authSaving = true
      try {
        await saveRoleWidgets({ roleId: this.authRoleId, widgetCodes: this.authWidgetCodes })
        this.$message.success('授权已保存')
      } finally {
        this.authSaving = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.workbench-admin { height: 100%; }
.admin-card { min-height: calc(100vh - 140px); }
.admin-hero {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #ebeef5;
}
.admin-hero__title {
  font-size: 18px;
  font-weight: 700;
}
.admin-hero__desc {
  margin-top: 4px;
  font-size: 13px;
}
.template-toolbar, .auth-toolbar { margin-bottom: 12px; }
.auth-alert { margin-bottom: 14px; }
.tpl-item {
  height: 100%;
  border: 1px dashed #409eff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  font-size: 13px;
}
.tpl-remove { color: #f56c6c; }
.empty-tip { text-align: center; padding: 40px; }
.auth-groups {
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-height: calc(100vh - 320px);
  overflow: auto;
}
.auth-group {
  border-radius: 10px;
  padding: 12px 14px;
}
.auth-group__head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.auth-group__title { font-size: 14px; font-weight: 600; }
.auth-group__count {
  min-width: 18px;
  height: 18px;
  padding: 0 6px;
  border-radius: 9px;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.auth-list { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.auth-check {
  margin-left: 0 !important;
  margin-right: 0 !important;
  min-width: 240px;
  padding: 6px 10px;
  border-radius: 6px;
}
.auth-check__name { font-weight: 600; margin-right: 6px; }
.auth-check__code { font-size: 12px; margin-right: 6px; }
.auth-check__scope { font-size: 12px; }
.pick-row {
  display: flex;
  align-items: center;
  padding: 10px;
  margin-bottom: 8px;
  border-radius: 6px;
  cursor: pointer;
  i { margin-right: 8px; font-size: 18px; }
}
.pick-row__text { flex: 1; min-width: 0; }
.pick-row__sub {
  margin-top: 2px;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>

<style lang="scss">
/* 浅色默认(不写进 scoped,避免暗黑被 chunk 顺序盖掉) */
.workbench-admin {
  .admin-hero__title { color: #1e293b; }
  .admin-hero__desc { color: #909399; }
  .tpl-item {
    background: #fff;
    color: #303133;
  }
  .empty-tip { color: #909399; }
  .auth-group {
    border: 1px solid #ebeef5;
    background: #fafbfc;
  }
  .auth-group__title { color: #303133; }
  .auth-group__count {
    background: #ecf5ff;
    color: #409eff;
  }
  .auth-check {
    background: #fff;
    border: 1px solid #ebeef5;
  }
  .auth-check__name { color: #303133; }
  .auth-check__code { color: #909399; }
  .auth-check__scope { color: #67c23a; }
  .pick-row {
    border: 1px solid #ebeef5;
    i { color: #409eff; }
    &:hover { background: #ecf5ff; }
  }
  .pick-row__sub { color: #909399; }
}

/* 工作台配置 · 暗黑强覆盖 */
body.ncc-content-dark .workbench-admin,
.ncc-content-dark .workbench-admin {
  .admin-card.el-card,
  .admin-card .el-card__body {
    background: #1f1f1f !important;
    background-color: #1f1f1f !important;
    border-color: rgba(255, 255, 255, 0.08) !important;
    color: rgba(255, 255, 255, 0.85) !important;
  }

  .admin-hero {
    border-bottom-color: rgba(255, 255, 255, 0.08) !important;
  }

  .admin-hero__title {
    color: rgba(255, 255, 255, 0.88) !important;
  }

  .admin-hero__desc {
    color: rgba(255, 255, 255, 0.55) !important;
  }

  .el-tabs__item {
    color: rgba(255, 255, 255, 0.65) !important;

    &.is-active {
      color: #1890ff !important;
    }
  }

  .el-tabs__nav-wrap::after {
    background-color: rgba(255, 255, 255, 0.08) !important;
  }

  .auth-toolbar .el-form-item__label,
  .template-toolbar .el-form-item__label {
    color: rgba(255, 255, 255, 0.85) !important;
  }

  .auth-toolbar .el-button:not(.el-button--primary),
  .template-toolbar .el-button:not(.el-button--primary):not(.el-button--success) {
    background: #262626 !important;
    border-color: rgba(255, 255, 255, 0.12) !important;
    color: rgba(255, 255, 255, 0.75) !important;
  }

  .auth-alert.el-alert--info {
    background-color: rgba(24, 144, 255, 0.12) !important;
    border-color: rgba(24, 144, 255, 0.25) !important;

    .el-alert__title,
    .el-alert__description {
      color: rgba(255, 255, 255, 0.85) !important;
    }
  }

  .auth-group {
    background: #141414 !important;
    background-color: #141414 !important;
    border: 1px solid rgba(255, 255, 255, 0.08) !important;
    box-shadow: none !important;
  }

  .auth-group__title {
    color: rgba(255, 255, 255, 0.88) !important;
  }

  .auth-group__count {
    background: rgba(24, 144, 255, 0.18) !important;
    color: #69b1ff !important;
  }

  .auth-group__head .el-button--text {
    color: #69b1ff !important;
  }

  .auth-check.el-checkbox,
  .auth-check {
    background: #1f1f1f !important;
    background-color: #1f1f1f !important;
    border: 1px solid rgba(255, 255, 255, 0.08) !important;
  }

  .auth-check .el-checkbox__label {
    color: rgba(255, 255, 255, 0.85) !important;
  }

  .auth-check__name {
    color: rgba(255, 255, 255, 0.88) !important;
  }

  .auth-check__code {
    color: rgba(255, 255, 255, 0.55) !important;
  }

  .auth-check__scope {
    color: #73d13d !important;
  }

  .auth-check .el-checkbox__inner {
    background-color: #141414 !important;
    border-color: rgba(255, 255, 255, 0.25) !important;
  }

  .auth-check.is-checked .el-checkbox__inner,
  .auth-check .el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: #1890ff !important;
    border-color: #1890ff !important;
  }

  .tpl-item {
    background: #141414 !important;
    border-color: rgba(64, 158, 255, 0.45) !important;
    color: rgba(255, 255, 255, 0.85) !important;
  }

  .empty-tip {
    color: rgba(255, 255, 255, 0.55) !important;
  }

  .el-table,
  .el-table__header-wrapper,
  .el-table__body-wrapper {
    background: transparent !important;
    color: rgba(255, 255, 255, 0.85) !important;
  }

  .el-table th.el-table__cell {
    background: #262626 !important;
    color: rgba(255, 255, 255, 0.85) !important;
    border-color: rgba(255, 255, 255, 0.12) !important;
  }

  .el-table td.el-table__cell {
    background: transparent !important;
    color: rgba(255, 255, 255, 0.85) !important;
    border-color: rgba(255, 255, 255, 0.12) !important;
  }
}

body.ncc-content-dark .workbench-pick-drawer,
.ncc-content-dark .workbench-pick-drawer {
  background: #1f1f1f !important;

  .el-drawer__header {
    color: rgba(255, 255, 255, 0.85) !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }

  .el-drawer__body {
    background: #1f1f1f !important;
  }

  .pick-row {
    background: #141414 !important;
    border-color: rgba(255, 255, 255, 0.08) !important;
    color: rgba(255, 255, 255, 0.85) !important;

    i {
      color: #69b1ff !important;
    }

    &:hover {
      background: rgba(24, 144, 255, 0.12) !important;
      border-color: rgba(24, 144, 255, 0.25) !important;
    }
  }

  .pick-row__sub {
    color: rgba(255, 255, 255, 0.55) !important;
  }
}
</style>