用戶管理

主要功能我們只實(shí)現(xiàn)分配角色,其余功能都是重復(fù)工作,可以快速完成

布局與準(zhǔn)備

在user/index.vue中調(diào)用組件UserList

// user/index.vue
<template>
  <div class="user">
    <user-list></user-list>
  </div>
</template>

<script>
import UserList from './components/list'
export default {
  name: 'UserIndex',
  components: {
    UserList
  }
}
</script>

<style lang="scss" scoped></style>

編輯組件user/components/list.vue,分頁功能待完善

<template>
  <el-card>
    <div slot="header">
      <el-form :inline="true" :model="filterParams" ref="filter-form">
        <el-form-item label="手機(jī)號" prop="phone">
          <el-input v-model="filterParams.phone"></el-input>
        </el-form-item>
        <el-form-item label="注冊時間" prop="rangeDate">
          <el-date-picker
            v-model="filterParams.rangeDate"
            type="datetimerange"
            range-separator="至"
            start-placeholder="開始時間"
            end-placeholder="結(jié)束時間"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button :disabled="isLoading" @click="handleReset">重置</el-button>
          <el-button type="primary" @click="handleQuery" :disabled="isLoading"
            >查詢</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="users" style="width: 100%" v-loading="isLoading">
      <el-table-column prop="id" label="用戶ID" width="150"> </el-table-column>
      <el-table-column prop="name" label="頭像" width="100">
        <template slot-scope="scope">
          <img
            width="30px"
            :src="
              scope.row.portrait ||
              'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'
            "
          />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="用戶名"> </el-table-column>
      <el-table-column prop="phone" label="手機(jī)號"> </el-table-column>
      <el-table-column prop="createTime" label="注冊時間">
        <template slot-scope="scope">
            <span>{{ scope.row.createTime | dateFormat }}</span>
        </template>
        <!-- 用戶狀態(tài)操作(服務(wù)端沒有開放權(quán)限,只能演示) -->
      </el-table-column>
      <el-table-column prop="name" label="狀態(tài)" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="ENABLE"
            inactive-value="DISABLE"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="handleForbidUser(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleSelectRole(scope.row)"
            >分配角色</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
import { getUserPages, forbidUser } from '@/services/user'
import dateFormat from '@/utils/dateformat'
export default {
  name: 'UserList',
  data () {
    return {
      users: [],
      filterParams: {
        currentPage: 1,
        pageSize: 100,
        phone: '',
        startCreateTime: '',
        endCreateTime: '',
        rangeDate: []
      },
      isLoading: true
    }
  },
  created () {
    this.loadUsers()
  },

  methods: {
    // 加載用戶
    async loadUsers () {
      this.isLoading = true
      const { rangeDate } = this.filterParams
      console.log(rangeDate)
      //  將選定的時間范圍存入數(shù)組,再將第一項(xiàng)第二項(xiàng)發(fā)送到接口
      if (rangeDate && rangeDate.length) {
        this.filterParams.startCreateTime = rangeDate[0]
        this.filterParams.endCreateTime = rangeDate[1]
      } else {
        this.filterParams.startCreateTime = ''
        this.filterParams.endCreateTime = ''
      }
      const { data } = await getUserPages(this.filterParams)
      this.users = data.data.records
      this.isLoading = false
    },
    async handleForbidUser (user) {
      const { data } = await forbidUser(user.id)
      // 接口禁用,這里沒法使用
      console.log(data)
    },
    // 將查詢的結(jié)果放置到第一頁
    handleQuery () {
      this.filterParams.currentPage = 1
      this.loadUsers()
    },
    // 重置
    handleReset () {
      this.$refs['filter-form'].resetFields()
      this.loadUsers()
    },
    // 點(diǎn)擊用戶的分配角色按鈕
    handleSelectRole () {}
  },
  filters: {
    dateFormat
  }
}
</script>

<style lang="scss" scoped>

</style>

對應(yīng)的接口封裝:

// 分頁查詢用戶信息 - 用戶管理
export const getUserPages = data => {
  return request({
    method: 'POST',
    url: '/boss/user/getUserPages',
    data
  })
}

// 封禁用戶(服務(wù)端關(guān)閉了權(quán)限,無法進(jìn)行實(shí)機(jī)操作)
export const forbidUser = userId => {
  return request({
    method: 'POST',
    url: 'boss/user/forbidUser',
    params: {
      userId
    }
  })
}

分配角色

重頭戲來了,這里我們依然使用的是Element組件中的Dialog對話框組件
放入頁面最后的位置,修改title,width,去除關(guān)閉處理函數(shù),聲明dialogVisible

// user/components/list.vue
<template>
  <el-card>
    ...
    <!-- 分配角色的 Dialog -->
    <el-dialog
      title="分配角色"
      :visible.sync="dialogVisible"
      width="50%"
        >
      <span>這是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">確 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>
<script>
...
data () {
  return {
    ...
    dialogVisible: false
  }
},
...
</script>

點(diǎn)擊了分配角色按鈕顯示對話框

<el-button
  type="text"
  @click="handleSelectRole(scope.row)"
>分配角色</el-button>
...
<script>
  
</script>

設(shè)置多選下拉菜單,通過Element的Select選擇器的基礎(chǔ)多功能設(shè)置

// list.vue
<el-dialog
  ...
>
  <!-- <span>這是一段信息</span> -->
   <el-select v-model="value1" multiple placeholder="請選擇">
    <!-- 根據(jù) options 遍歷生成選項(xiàng)列表 -->
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
  <span slot="footer" class="dialog-footer">
    ...
  </span>
</el-dialog>

data () {
    return {
      ...
          // 列表數(shù)據(jù)
      options: [{
          value: '選項(xiàng)1',
          label: '黃金糕'
        }, {
          value: '選項(xiàng)2',
          label: '雙皮奶'
        }, {
          value: '選項(xiàng)3',
          label: '蚵仔煎'
        }, {
          value: '選項(xiàng)4',
          label: '龍須面'
        }, {
          value: '選項(xiàng)5',
          label: '北京烤鴨'
        }],
                // 選中的數(shù)據(jù)
        value1: []
    }

布局完成

展示角色列表

封裝所有的角色接口:地址

// services/role.js
...
// 獲取所有角色 - 用戶管理
export const getAllRoles = () => {
  return request({
    method: 'GET',
    url: '/boss/role/all'
  })
}

引入并且使用:

  • 引入
  • 聲明數(shù)據(jù)
  • 綁定給下拉框
  • 點(diǎn)擊分配角色時發(fā)送請求
  • 請求成功設(shè)置數(shù)據(jù)

import { getAllRoles } from '@/services/role'
...
data () {
  return {
    ...
    // 所有角色,對象示例的 option
    roles: [],
    // 選中角色,對應(yīng)示例的 value1
    roleIdList: []
  }
},
...
<el-select v-model="roleIdList" multiple placeholder="請選擇">
  <el-option
    v-for="item in roles"
    :key="item.value"
    :label="item.name"
    :value="item.id">
  </el-option>
</el-select>
...
// 點(diǎn)擊用戶的分配角色按鈕
async handleSelectRole () {
  // 顯示對話框
  this.dialogVisible = true
  // 點(diǎn)擊后加載角色列表
  const { data } = await getAllRoles()
if (data.code === '000000') {
  this.roles = data.data
  }
}

提交分配

設(shè)置點(diǎn)擊事件

<el-button type="primary" @click="handleAllocRole">確 定</el-button>
...
// 提交分配角色操作(等提交完畢再隱藏對話框)
async handleAllocRole () {
  
}

觀察接口給用戶分配角色:地址
需要的請求參數(shù)是用戶ID和分配的角色I(xiàn)D組成的shuju

// services/role.js
...
// 給用戶分配角色 - 用戶掛你
export const allocateUserRoles = data => {
  return request({
    method: 'POST',
    url: '/boss/role/allocateUserRoles',
    data
  })
}

引入并且使用

import { getAllRoles, allocateUserRoles } from '@/services/role'
...
// 提交分配角色操作
async handleAllocRole () {
  // 提交
  allocateUserRoles({
    
  })
}

請求用戶ID可以在顯示對話框時接收到組件傳值,保存即可

// 當(dāng)前分配角色的ID(聲明在 data 中)
currentRoleID: null
...
// 點(diǎn)擊用戶的分配角色按鈕
async handleSelectRole (userData) {
  // 將當(dāng)前操作的用戶ID保存,以便提交使用
  this.currentRoleID = userData.id
  ...
},

傳參發(fā)送

// 提交分配角色操作
async handleAllocRole () {
  // 提交
  //   - 用戶 ID 可以在顯示對話框時接收到組件傳值,保存即可
  const { data } = await allocateUserRoles({
    userId: this.currentRoleID,
    roleIdList: this.roleIdList
  })
  if (data.code === '000000') {
    // 提示
    this.$message.success('分配角色成功')
    // 關(guān)閉對話框
    this.dialogVisible = false
  }
}

默認(rèn)選中已經(jīng)分配的角色

roleList中保存的為已經(jīng)選中的選項(xiàng),可以請求用戶已經(jīng)分配的角色,并且修改roleIdList即可
這里使用查詢用戶角色接口:地址

// services/role.js
...
// 查詢用戶角色
export const getUserRoles = userId => {
  return request({
    method: 'GET',
    url: `/boss/role/user/${userId}`
  })
}

引入

import { getAllRoles, allocateUserRoles, getUserRoles } from '@/services/role'

在點(diǎn)擊角色分配按鈕時,根據(jù)當(dāng)前用戶id查詢角色并更新到roleIdList即可

// 點(diǎn)擊用戶的分配角色按鈕
async handleSelectRole (userData) {
  ...
  // 根據(jù)用戶id請求角色信息(data 被之前的請求使用過了,換個名字)
  const { data2 } = await getUserRoles(userData.id)
  // 遍歷得到的角色列表,將id組成數(shù)據(jù)替換給 roleIdList 即可
  this.roleIdList = data2.data.map(item => item.id)
},

其余的功能都是重復(fù)性工作,可以迅速完成

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容