admin.vue 13.4 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
<template>
  <div class="admin">
    <el-button @click="add" plain type="success" style="float: right;margin-bottom:10px;" v-if="!$store.state.readonly">新增角色</el-button>
    <el-form ref="searchFrom" :model="searchFrom" inline label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="searchFrom.user_name"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="searchFrom.status" placeholder="请选择用户状态" @change="getList">
              <el-option label="全部" value=""></el-option>
              <el-option label="启用" :value="0"></el-option>
              <el-option label="冻结" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="角色">
            <el-select v-model="searchFrom.role_id" placeholder="请选择角色类型" @change="getList">
              <el-option label="全部" value=""></el-option>
              <el-option
                v-for="data in roleList"
                :key="data.id"
                :label="data.name"
                :value="data.id" ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain @click="getList">搜索</el-button>
          </el-form-item>
    </el-form>
    <!--<div class="head clear-both">-->
      <!---->
    <!--</div>-->
    <el-table
      :data="adminList"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="账号ID">
      </el-table-column>
      <el-table-column
        prop="user_name"
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="role_name"
        label="角色">
      </el-table-column>
      <el-table-column
        label="状态">
        <template slot-scope="scope">
          {{scope.row.status | adminStatus}}
        </template>
      </el-table-column>
      <el-table-column
        prop="desc"
        label="备注">
      </el-table-column>
      <el-table-column
        prop="last_login_at"
        label="最后登录时间" sortable>
      </el-table-column>
      <el-table-column
        v-if="!$store.state.readonly"
        label="操作">
        <template slot-scope="scope">
          <el-popover
            placement="top"
            width="280">
            <div style="text-align: center">
              <el-button size="mini" plain type="primary" @click="edit(scope.row)">
                编辑
              </el-button>
              <el-button size="mini" type="warning" plain @click="editPW(scope.row)">
                修改密码
              </el-button>
              <el-button size="mini" type="danger" plain @click="del(scope.row)" v-if="$store.state.deletePermission">
                删除
              </el-button>
            </div>
            <el-button slot="reference" size="mini" type="text" >操作</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <page :nowPage="nowPage" :total="total" :limit="limit" @pageChange="onPageChange" @sizeChange="onSizeChange"/>
    <el-dialog
      :title="dialog.title"
      center
      append-to-body
      :visible.sync="dialog.show"
      width="30%">
      <el-form ref="form" :rules="dialog.rules" :model="dialog.form" label-width="80px">
        <el-form-item label="账号" prop="username" v-if="dialog.dialogType === 0">
          <el-input v-model="dialog.form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="dialog.dialogType === 2 || dialog.dialogType === 0">
          <el-input type="password" v-model="dialog.form.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="surePassword" v-if="dialog.dialogType === 2 || dialog.dialogType === 0 ">
          <el-input type="password" v-model="dialog.form.surePassword"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role_id" v-if="dialog.dialogType !== 2">
          <el-select v-model="dialog.form.role_id" placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="简述" prop="desc" v-if="dialog.dialogType !== 2">
          <el-input v-model="dialog.form.desc"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status" v-if="dialog.dialogType === 1">
          <el-select v-model="dialog.form.status" placeholder="请选择">
            <el-option
              v-for="item in dialog.select"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.show = false">取 消</el-button>
        <el-button type="primary" @click="onSub">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {getAdminListApi,editAdminListApi,addAdminListApi,delAdminListApi,editPasswordApi,getRoleListApi} from "../../service/api";
  import {ADMINSTATUS} from "../../util/wordbook";
  import page from '../framework/page'
  import md5 from 'js-md5';
  export default {
    name: "admin",
    components:{
      page
    },
    data(){
      // 表单验证
      let usernameCheck=(rule, value, callback)=>{
        if (value === '' && this.dialog.dialogType === 0) {
          callback(new Error('请输入账号'));
        } else {
          callback();
        }
      };
      let passCheck= (rule, value, callback) => {
        if (value === '' && (this.dialog.dialogType === 0 || this.dialog.dialogType === 2 )) {
          callback(new Error('请输入密码'));
        } else {
          if(this.dialog.form.surePassword !== '' && (this.dialog.dialogType === 0 || this.dialog.dialogType === 2 )){
            this.$refs['form'].validateField('surePassword');
          }
          callback();
        }
      };
      let surePassCheck= (rule, value, callback) => {
        if(this.dialog.dialogType === 0 || this.dialog.dialogType === 2 ){
          if (value === '') {
            callback(new Error('请再次输入密码'));
          } else if (value !== this.dialog.form.password) {
            callback(new Error('两次输入密码不一致!'));
          }else{
            callback();
          }
        } else {
          callback();
        }
      };
      let roleIdCheck= (rule, value, callback) => {
          if(this.dialog.dialogType === 0 || this.dialog.dialogType === 1 ){
            if (value === '') {
              callback(new Error('请输入角色'));
            }
          }else{
            callback()
          }
      };
      let descCheck= (rule, value, callback) => {
        if(this.dialog.dialogType === 0 || this.dialog.dialogType === 1 ){
          if (value === '') {
            callback(new Error('请输入简述'));
          }
        }else{
          callback()
        }
      };
      return {
        nowPage:1,
        total:0,
        limit: 10,
        adminList:[],
        searchFrom:{
          user_name: '',
          status: '',
          role_id:""
        },
        dialog:{
          dialogType:0,
          title:'新增账号',
          show:false,
          select:[{
            code:0,
            value:ADMINSTATUS['0']
          },{
            code:1,
            value:ADMINSTATUS['1']
          }],
          rules:{
            username:[{ validator: usernameCheck, trigger: 'blur' }],
            password:[{ validator: passCheck, trigger: 'blur' }],
            surePassword:[{ validator: surePassCheck, trigger: 'blur' }],
            role_id:[{ validator: roleIdCheck, trigger: 'blur' }],
            desc:[{ validator: descCheck, trigger: 'blur' }],
          },
          form:{
            id:'',
            username:'',
            password:'',
            surePassword:'',
            role_id:'',
            status:0,
            desc:''
          },
        },
        roleList: []
      }
    },
    filters:{
      adminStatus:function (value) {
        return ADMINSTATUS[value]
      },
    },
    created(){
       if(this.$route.query.roleId){
         this.searchFrom.role_id = parseInt(this.$route.query.roleId)
       }
      this.getList();
      this.getRoleList();
    },
    methods:{
      getRoleList(){
        getRoleListApi({page: 1,limit: 100}).then(res=>{
          if (res) {
            this.roleList = res.list;
          }
        })
      },
      onPageChange(val){
        this.nowPage = val;
        this.getList()
      },
      onSizeChange(val){
        this.nowPage = 1;
        this.limit = val;
        this.getList()
      },
      getList(){
        let json = {
          limit: this.limit,
          page: this.nowPage
        }
        if (this.searchFrom.user_name){
          json.user_name = this.searchFrom.user_name
        }
        if (this.searchFrom.status !== ''){
          json.status = this.searchFrom.status
        }
         if (this.searchFrom.role_id !== ''){
          json.role_id = this.searchFrom.role_id
        }
        getAdminListApi(json).then(res=>{
          this.adminList = res.list;
          this.total=Number(res.total)
          this.$store.commit('mainCanShow')
        })
      },
      editPW(data){
        this.dialog.form.id = data.id;
        this.dialog.dialogType = 2;
        this.dialog.title = '修改密码';
        this.dialog.form.password = '';
        this.dialog.form.surePassword = '';
        this.dialog.show = true;
      },
      edit(data){
        this.dialog.show = true;
        this.dialog.dialogType=1;
        this.dialog.form.id = data.id;
        this.dialog.title = '编辑';
        this.dialog.form.username = data.user_name;
        this.dialog.form.password = '';
        this.dialog.form.status = data.status;
        this.dialog.form.role_id = data.role_id;
        this.dialog.form.desc = data.desc
      },
      add(){
        this.dialog.show = true;
        this.dialog.dialogType=0;
        this.dialog.form.id = '';
        this.dialog.title = '新增';
        this.dialog.form.username = '';
        this.dialog.form.status = 0;
        this.dialog.form.password = '';
        this.dialog.form.role_id = '';
        this.dialog.form.desc = ''
      },
      del(data){
        this.$confirm('此操作将删除该账号?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delAdminListApi(data.id).then(res=>{
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          });
          this.getList()
        });
      },
      onSub(){
        // this.$refs['form'].validate((valid) => {
        //   if(valid){
            let dia = this.dialog;
            if(dia.dialogType === 1){
              let json = {
                role_id:dia.form.role_id,
                desc:dia.form.desc,
                status:dia.form.status
              };
              this.$confirm('此操作将编辑该账号?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                editAdminListApi(dia.form.id,json).then(()=>{
                  this.$message({
                    type: 'success',
                    message: '修改成功!'
                  });
                  dia.show = false;
                  this.getList()
                })
              })
            }else if(dia.dialogType === 0){
              let json = {
                role_id:dia.form.role_id,
                desc:dia.form.desc,
                passwd:md5(dia.form.password),
                username:dia.form.username
              };
              this.$confirm('此操作将添加新账号?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                addAdminListApi(json).then(()=>{
                  this.$message({
                    type: 'success',
                    message: '添加成功!'
                  });
                  dia.show = false;
                  this.getList()
                })
              })
            }else if(dia.dialogType === 2){
              let json = {
                passwd_new:md5(dia.form.password)
              }
              this.$confirm('此操作将修改此账号密码?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                editPasswordApi(dia.form.id,json).then(()=>{
                  this.$message({
                    type: 'success',
                    message: '修改成功!'
                  });
                  dia.show = false;
                  this.getList()
                })
              })
            }
        //   }
        // })
      }
    }
  }
</script>

<style scoped lang="less">
  .admin{
    .head{
      margin-bottom: 10px;
    }
    width: 100%;
    padding: 20px 0;
    .page-div{
      text-align: center;
      padding-top: 20px
    }
  }
  .clear-both{
  &:after{
     content: '';
     display: block;
     clear: both;
   }
  }
</style>