Files
ostandari/pages/admin/users/index.vue
T
2024-10-21 10:22:26 +03:30

308 lines
10 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton size="sm" color="success" :to="{name: 'admin-users-profile',params:{profile: 'new'}}" class="mr-auto">
افزودن
</CButton>
</CustomSubHeader>
<CCard>
<CCardHeader>
<slot name="header">
<i class="fal fa-filter"></i>
<span style="color:#000">فیلتر</span>
</slot>
</CCardHeader>
<CCardBody>
<CRow>
<CCol md="6">
<CRow style="flex-direction : column;">
<CCol class="mt-3">
<CRow style="align-items: baseline;">
<CCol col="md-3" class="mb-2">
<span>نام و نام خانوادگی</span>
</CCol>
<CCol col="md-9">
<el-input style="width : 100%" placeholder="" v-model="search.name"></el-input>
</CCol>
</CRow>
</CCol>
<CCol class="mt-3">
<CRow style="align-items: baseline;">
<CCol col="md-3" class="mb-2">
<span>ایمیل</span>
</CCol>
<CCol col="md-9">
<el-input style="width : 100%" placeholder="" v-model="search.email"></el-input>
</CCol>
</CRow>
</CCol>
</CRow>
</CCol>
<CCol md="6">
<CRow style="flex-direction : column;">
<CCol class="mt-3">
<CRow style="align-items: baseline;">
<CCol col="md-3" class="mb-2">
<span>محدوده دسترسی</span>
</CCol>
<CCol col="md-9">
<el-select v-model="search.portals" clearable style="width : 100%">
<el-option v-for="item in portals" :key="item.value" :label="item.name" :value="item.value">
</el-option>
</el-select>
</CCol>
</CRow>
</CCol>
<CCol class="mt-3">
<CRow style="align-items: baseline;">
<CCol col="md-3" class="mb-2">
<span>وضعیت کاربر</span>
</CCol>
<CCol col="md-9">
<el-select v-model="search.status" clearable style="width : 100%">
<el-option :key="true" label="فعال" :value="true">
</el-option>
<el-option :key="false" label="غیر فعال" :value="false">
</el-option>
</el-select>
</CCol>
</CRow>
</CCol>
</CRow>
</CCol>
</CRow>
<CRow>
<el-button :loading="loading" @click="handelSearch" size="small" style="width: auto;margin: 25px auto auto auto;" type="primary" icon="el-icon-search">
جست و جو
</el-button>
</CRow>
</CCardBody>
</CCard>
<CCard>
<CCardHeader>
<slot name="header">
<i class="fal fa-bars"></i>
<span>{{ list_title }}</span>
</slot>
<slot>
</slot>
</CCardHeader>
<CCardBody>
<el-table v-loading="loading" :data="users.docs" style="width: 100%">
<el-table-column type="index" label="#">
</el-table-column>
<el-table-column label="عکس پروفایل" width="170">
<template slot-scope="scope">
<img :src="scope.row.profilePic || '/img/avatar.png'" style="width: 50px; border-radius: 50em;"
:alt="scope.row.firstName + ' ' + scope.row.lastName">
</template>
</el-table-column>
<el-table-column label="نام و نام خانوادگی">
<template slot-scope="scope">
<span>{{ scope.row.firstName + ' ' + scope.row.lastName }}</span>
</template>
</el-table-column>
<el-table-column prop="username" label="نام کاربری" width="">
</el-table-column>
<el-table-column label="وضعیت کاربر" width="">
<template slot-scope="scope">
<el-tag :type="scope.row.registration_done?'primary' : 'danger'">
{{ scope.row.registration_done ? 'فعال' : 'غیر فعال' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="ویرایش" width="110" align="center">
<template slot-scope="scope">
<CButton :color="scope.row.registration_done ? 'danger' : 'success'" :title="scope.row.registration_done ? 'غیرفعال کردن کاربر' : 'فعال کردن کاربر'" variant="outline"
:key="scope.row._id + Math.random()"
@click="changeUserStatus(scope.row)">
<i class="fas fa-power-off"></i>
</CButton>
<CButton color="success" variant="outline" :key="scope.row._id"
:to="{name: 'admin-users-profile',params: {profile: scope.row._id}}">
<i class="far fa-eye"></i>
</CButton>
</template>
</el-table-column>
</el-table>
</CCardBody>
</CCard>
<CCard class="col" v-if="users.totalDocs>5">
<CRow alignHorizontal="center" style="padding : 10px">
<el-pagination :hide-on-single-page="true" layout="prev, pager, next" :page-size="users.limit"
@current-change="handleCurrentChange" :current-page.sync="users.page" :total="users.totalDocs">
</el-pagination>
</CRow>
</CCard>
</div>
</template>
<script>
const _ = require('lodash');
export default {
data() {
return {
title: 'لیست کاربران',
list_title: 'لیست',
users: null,
search: {},
portals: [],
loading: false
}
},
computed: {
pageQuery() {
return this.$route.query.page;
}
},
watch: {
pageQuery(newPage, oldPage) {
this.$nuxt.refresh();
}
},
methods: {
handleCurrentChange(page) {
if (!_.isEmpty(this.search)) {
this.handelSearch(page);
} else {
this.$router.push({
name: "admin-users",
query: {
page: page
}
});
}
},
handelSearch(page) {
this.loading = true;
const data = {
name: this.search.name,
email: this.search.email,
status: this.search.status,
portals: this.search.portals
}
this.$axios.post(`/api/admin/user/search?page=${page}`, data, this.axiosConfig)
.then((result) => {
this.users = result.data;
this.loading = false;
})
.catch(err => {
this.loading = false;
if (err.response.status === 401) {
return this.$message({
type: 'error',
message: 'لطفا دوباره وارد سیستم شوید.'
})
}
if (err.response.status === 422) this.validation = err.response.data.validation
else console.log(err.response.data)
})
},
changeUserStatus(user) {
this.$confirm(user.registration_done ? 'کاربر غیرفعال شود؟' : 'کاربر فعال شود؟', 'هشدار', {
confirmButtonText: 'بله',
cancelButtonText: 'لغو',
type: user.registration_done ? 'error' : 'warning'
}).then(async () => {
this.$axios.put(`/api/admin/user/changeStatus/${user._id}`)
.then(res => {
this.$message({
type: 'success',
message: 'وضعیت حساب کاربر با موفقیت تغییر کرد'
})
this.$nuxt.refresh()
})
.catch(err => {
if (err.response.status === 401) {
return this.$message({
type: 'error',
message: 'لطفا دوباره وارد سیستم شوید.'
})
}
if (err.response.status === 403) {
return this.$message({
type: 'error',
message: err.response.data.message
})
}
if (err.response.status === 422) {
return this.$message({
type: 'error',
message: err.response.data.message
})
}
console.log(err.response.data)
})
}).catch(() => {
this.$message({
type: 'warning',
message: 'عملیات لغو شد'
})
})
}
},
head() {
return {
title: this.title
}
},
layout: 'admin',
async asyncData({
$axios,
query,
redirect,
error
}) {
try {
// const users = await $axios.get(`/api/admin/user?page=${query.page}` , { progress: false })
// const portals = await $axios.get('/api/admin/portals' , { progress: false });
const requests = [
$axios.get(`/api/admin/user?page=${query.page}`, {progress: false}),
$axios.get('/api/admin/portals', {progress: false})
];
const fetch = await Promise.all(requests);
let [users, portals] = fetch;
if (query.page > 1 && users.data.totalPages < query.page) {
redirect('/admin/users');
} else {
return {
users: users.data,
portals: portals.data
}
}
} catch (e) {
if (e?.response?.status === 401) {
error({
status: 401,
message: 'شما اجازه دسترسی به این صفحه را ندارید'
})
} else {
error({
status: 500,
message: "مشکلی در گرفتن اطلاعات بوجود آمده است",
});
}
}
}
}
</script>