index.vue 12.8 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
<template>
  <div class="navbar">
    <hamburger
      id="hamburger-container"
      :is-active="sidebar.opened"
      class="hamburger-container"
      @toggleClick="toggleSideBar"
    />
    <div class="right-menu">
      <el-dropdown class="useList" @command="handleName">
        <span class="el-dropdown-link">
          <i class="el-icon-s-platform adhibitionIcon"></i>
          {{ projectname }}
          <i class="el-icon-caret-bottom el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown" class="topNav useBox">
          <el-dropdown-item v-for="item in dataTable" :key="item.id" :command="item">{{ item.name }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <div class="avatar-wrapper">
        <span class="userHead"></span>
      </div>

      <el-dropdown @command="logout">
        <span class="el-dropdown-link">
          {{ user.name }}<i class="el-icon-caret-bottom el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown" class="topNav">
          <el-dropdown-item command="0">修改密码</el-dropdown-item>
          <el-dropdown-item v-if="user.id<10" command="2">平台管理</el-dropdown-item>
          <el-dropdown-item command="1">退出系统</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-dialog
      class="user-unfreeze-dialog userInfo"
      title="修改密码"
      :visible.sync="isVisible"
      :before-close="handleClose"
    >
      <div class="unfreeze-form">
        <el-form
          ref="ruleForm"
          :rules="formRules"
          :model="userData"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="旧密码:" prop="oldPassword">
            <el-input v-model="userData.oldPassword" type="password" placeholder="请输入" autocomplete="off" minlength="6" maxlength="12" />
          </el-form-item>
          <el-form-item label="新密码:" prop="password">
            <el-input v-model="userData.password" type="password" placeholder="请输入" autocomplete="off" minlength="6" maxlength="12" />
          </el-form-item>
          <el-form-item label="确认密码:" prop="confirmPassword">
            <el-input v-model="userData.confirmPassword" type="password" placeholder="请输入" autocomplete="off" minlength="6" maxlength="12" />
          </el-form-item>
        </el-form>
        <div class="subBox right">
          <el-button @click="handleClose"> 取 消 </el-button>
          <el-button type="primary" :disabled="isDisable" @click="handleSubmit">
            确 定
          </el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 用户添加、编辑对话框 -->
    <more-dialog
      v-if="isMoreVisible"
      ref="userDialog"
      :is-more-visible="isMoreVisible"
      @close="handleMoreClose"
    />
    <!-- end -->
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import { AppModule } from '@/store/modules/app'
import { UserModule } from '@/store/modules/user'
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import Hamburger from '@/components/Hamburger/index.vue'
import { alterPassword } from '@/pages/base/api/api'
import { ElForm } from 'element-ui/types/form'
import { getUser, setApply, getApply } from '@/utils/cookies'
import { getAllList } from '@/pages/adhibition/api/index'
import moreDialog from '@/pages/adhibition/components/moreList.vue'
import { themeData } from '@/utils/data'
@Component({
  name: 'Navbar',
  components: {
    Breadcrumb,
    Hamburger,
    moreDialog
  }
})
export default class extends Vue {
  private userData={}
  private isVisible =false
  private isDisable = false
  private userInfo = {}
  private dataTable =[]
  private isMoreVisible= false
  private chalk = ''
  private version = require('element-ui/package.json').version
  private formRules = {
    oldPassword: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
    password: [{ required: true, message: '请输入新密码', trigger: 'blur' }]
  }
  get sidebar() {
    return AppModule.sidebar
  }
  // 获取用户
  get user() {
    return getUser() ? JSON.parse(getUser() as any) : null
  }
  get projectname() {
    return getApply() ? JSON.parse(getApply() as any).name : ''
  }

  get themeVal() {
    return this.$store.state.themeColor
  }

  @Watch('themeVal')
  themeValChange(val:any, oldVal:any) {
    this.updateTheme(val, oldVal)
  }
  created() {
   // 加载应用列表
   this.getList()
   // 初始化应用和平台
   this.initPlatform()
  }

  /// // 功能函数 /////
  // 初始化应用和平台
  private initPlatform() {
    // 管理员默认平台
   if (this.user.id < 10 && !getApply()) {
     setApply(JSON.stringify({ 'id': '1', 'createTime': null, 'createUser': null, 'updateTime': null, 'updateUser': null, 'name': '应用选择', 'abbreviation': '权限管家', 'status': null, 'describe': null }))
     this.$store.commit('updatedTheme', '')
     this.$store.commit('updatedThemeColor', '#409EFF')
   } else {
     // 应用选择相应主题
     let platform = getApply() ? JSON.parse(getApply() as any) : null
     if (platform && platform.name !== '应用选择') {
       const theme = platform.color ? platform.color : 'default'
       const color = (themeData.find(item => item.value === theme) as any).color
       this.$store.commit('updatedTheme', theme)
       this.$store.commit('updatedThemeColor', color)
     }
   }
  }
  // 控制左侧菜单
  private toggleSideBar() {
    AppModule.ToggleSideBar(false)
  }
  // 退出
  private logout(command:string) {
    if (command === '1') {
      this.$confirm('', '确认退出系统?', {
          confirmButtonText: '确定',
          type: 'warning'
        }).then(async() => {
          await UserModule.LogOut()
          this.$router.push(`/login?redirect=${this.$route.fullPath}`)
        })
    } else if (command === '0') {
      this.isVisible = true
    } else if (command === '2') {
      // 切换到平台管理
      setApply(JSON.stringify({ 'id': '1', 'createTime': null, 'createUser': null, 'updateTime': null, 'updateUser': null, 'name': '应用选择', 'abbreviation': '权限管家', 'status': null, 'describe': null }))
      location.reload()
    }
  }
  // 修改密码
  handleSubmit() {
    this.isDisable = true
    let userData = this.userData as any
    setTimeout(() => {
      this.isDisable = false // 点击一次时隔两秒后才能再次点击
    }, 2000)
    userData.id = UserModule.id
    if (userData.confirmPassword !== userData.password) {
      this.$message({
        message: '两次密码不一致',
        type: 'error'
      })
      return false
    }
    (this.$refs.ruleForm as ElForm).validate(async(valid: boolean) => {
      if (valid) {
        const { data } = await alterPassword(this.userData)
        if (data.isSuccess) {
          this.isVisible = false;
          (this.$refs.ruleForm as ElForm).resetFields()
          this.$message({
            message: '设置成功!',
            type: 'success'
          })
          this.handleClose()
        } else {
          this.$message({
            message: data.msg,
            type: 'error'
          })
        }
      } else {
        return false
      }
    })
  }
  // 获取数据
  private async getList() {
    const { data } = await getAllList({})
    if (data.isSuccess === true) {
      this.dataTable = data.data
      if (this.user.id >= 10 && !getApply()) {
        setApply(JSON.stringify(this.dataTable[0]))
      }
    }
  }
  handleClose() {
    this.isVisible = false
  }
  handleMoreClose() {
    this.isMoreVisible = false
  }
  // 应用筛选
  handleName(val:any) {
    if (val !== 0) {
      setApply(JSON.stringify(val))
      location.reload()
    } else {
      this.isMoreVisible = true
    }
  }

  // 变更主题色
  updateTheme(val:any, oldVal:any) {
    if (typeof val !== 'string') return
    const head = document.getElementsByTagName('head')[0]
    const themeCluster = this.getThemeCluster(val.replace('#', ''))
    const getHandler = (variable:any, id:any) => {
      return () => {
        const originalCluster = this.getThemeCluster(
          oldVal.replace('#', '')
        )
        const newStyle = this.updateStyle(
          this.chalk,
          originalCluster,
          themeCluster
        )

        let styleTag = document.getElementById(id)
        if (!styleTag) {
          styleTag = document.createElement('style')
          styleTag.setAttribute('id', id)
          head.appendChild(styleTag)
        }
        styleTag.innerText = newStyle
      }
    }
    const chalkHandler = getHandler('chalk', 'chalk-style')
    if (!this.chalk) {
      const url = `https://unpkg.com/element-ui@${this.version}/lib/theme-chalk/index.css`
      this.getCSSString(url, chalkHandler, 'chalk')
    } else {
      chalkHandler()
    }
  }

  updateStyle(style:any, oldCluster:any, newCluster:any) {
    let newStyle = style
    oldCluster.forEach((color:any, index:any) => {
      newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
    })
    return newStyle
  }

  // 获取相应主题
  getCSSString(url:any, callback:any, variable:any) {
    const xhr = new XMLHttpRequest()
    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4 && xhr.status === 200) {
        if (variable) {
          this.chalk = xhr.responseText.replace(/@font-face{[^}]+}/, '')
        }
        callback(xhr.responseText)
      }
    }
    xhr.open('GET', url)
    xhr.send()
  }

  // 获取相关主题所有颜色
  private getThemeCluster(theme: String) {
    const tintColor = (color: any, tint: any) => {
      let red = parseInt(color.slice(0, 2), 16)
      let green = parseInt(color.slice(2, 4), 16)
      let blue = parseInt(color.slice(4, 6), 16)

      if (tint === 0) {
        // when primary color is in its rgb space
        return [red, green, blue].join(',')
      } else {
        red += Math.round(tint * (255 - red))
        green += Math.round(tint * (255 - green))
        blue += Math.round(tint * (255 - blue))

        const newRed = red.toString(16)
        const newGreen = green.toString(16)
        const newBlue = blue.toString(16)

        return `#${newRed}${newGreen}${newBlue}`
      }
    }

    const shadeColor = (color: any, shade: any) => {
      let red = parseInt(color.slice(0, 2), 16)
      let green = parseInt(color.slice(2, 4), 16)
      let blue = parseInt(color.slice(4, 6), 16)

      red = Math.round((1 - shade) * red)
      green = Math.round((1 - shade) * green)
      blue = Math.round((1 - shade) * blue)

      const newRed = red.toString(16)
      const newGreen = green.toString(16)
      const newBlue = blue.toString(16)

      return `#${newRed}${newGreen}${newBlue}`
    }

    const clusters = [theme]
    for (let i = 0; i <= 9; i++) {
      clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
    }
    clusters.push(shadeColor(theme, 0.1))
    return clusters
  }
}

</script>

<style lang="scss" scoped>
.navbar {
  height: 70px;
  overflow: hidden;
  position: relative;
  // background: url('./../../../assets/images/userBg.png') no-repeat 100% 50%;
  // background-size: contain;
  background: #FFFFFF;
  .backOut {
    background: url('./../../../assets/images/icon_arrow2.png') no-repeat center
      center;
    background-size: 8px 5px;
    display: inline-block;
    cursor: pointer;
    margin-top: 22px;
    width: 8px;
    height: 5px;
  }
  .hamburger-container {
    line-height: 70px;
    height: 100%;
    float: left;
    padding: 0 15px;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .right-menu {
    display: flex;
    float: right;
    height: 100%;
    line-height: 70px;
    padding-right: 30px;
    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-wrapper {
      margin-top: 10px;
      margin: 10px 10px 0 0;
      position: relative;
      img{
        width: 29px;
        height: 29px;
        border-radius: 50%;
      }
      .userHead{
        width: 29px;
        height: 29px;
        border-radius: 15px;
      }
      .user-avatar {
        cursor: pointer;
        width: 29px;
        height: 29px;
        border-radius: 29px;
      }

      .el-icon-caret-bottom {
        cursor: pointer;
        position: absolute;
        right: -20px;
        top: 25px;
        font-size: 12px;
      }
    }
  }
}
.el-popper.topNav{
  top: 30px !important;
}
.el-dropdown-link{
  cursor: pointer;
}
.username{
  color: var(--current-color);
}
</style>