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

491 lines
17 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton size="sm" color="success" :to="{ name: 'admin-news-details', params: { details: '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.term"></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-select filterable clearable style="width : 100%" v-model="search.catId"
placeholder="انتخاب دسته بندی">
<el-option v-for="item in parents" :key="item._id" :label="item.title" :value="item._id">
<!-- <span>{{setTitle(item)}}</span> -->
</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 filterable clearable style="width : 100%" v-model="search.newsFile">
<el-option v-for="item in newsFile" :key="item._id" :label="item.title" :value="item._id">
<!-- <span>{{setTitle(item)}}</span> -->
</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.publish" 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>
<CCol class="mt-3">
<CRow style="align-items: baseline;">
<CCol col="md-3" class="mb-2">
<span>تاریخ</span>
</CCol>
<CCol col="md-9">
<date-picker range clearable format="YYYY-MM-DD" display-format="jMMMM jD"
v-model="search.date"></date-picker>
</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>
<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.notifications" 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>
<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.special" 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>
<CCol class="mt-3" v-if="$auth.user.permissions.includes('superAdmin')">
<CRow style="align-items: baseline;">
<CCol col="md-3" class="mb-2">
<span>نمایش در صفحه اصلی</span>
</CCol>
<CCol col="md-9">
<el-select v-model="search.showInHome" 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="handleSearch" 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="news.docs" :default-sort="{ prop: 'date', order: 'descending' }"
style="width: 100%">
<el-table-column type="index" label="#" :index="1"></el-table-column>
<!-- <el-table-column
label="کاور خبر"
width="200">
<template slot-scope="scope">
<img :src="scope.row.cover || '/img/avatar.png'" style="width: 200px;" :alt="scope.row.title">
</template>
</el-table-column> -->
<el-table-column label="تیتر" width="180px">
<template slot-scope="scope">
<span class="title">
<el-popover placement="top-end" trigger="hover" :content="scope.row.title">
<i class="el-icon-info" slot="reference"></i>
</el-popover>
{{ scope.row.title }}
</span>
</template>
</el-table-column>
<el-table-column label="نویسنده">
<template slot-scope="scope">
<span v-if="!$auth.user.permissions.includes('superAdmin')">{{
scope.row._creator.firstName + " " + scope.row._creator.lastName
}}</span>
<nuxt-link v-else :to="{
name: 'admin-users-profile',
params: { profile: scope.row._creator._id },
}"><span style="color: blue;">{{
scope.row._creator.firstName +
" " +
scope.row._creator.lastName
}}</span></nuxt-link>
</template>
</el-table-column>
<el-table-column prop="catId.title" label="دسته بندی" width="">
<!-- <template slot-scope="scope">-->
<!-- <span>{{ scope.row.catId.title }}</span>-->
<!-- </template>-->
</el-table-column>
<el-table-column label="وضعیت خبر" width="">
<template slot-scope="scope">
<el-tag :type="scope.row.newsStatus ? 'success' : 'danger'">{{
scope.row.newsStatus ? "کامل" : "ناقص"
}}
</el-tag>
</template>
</el-table-column>
<el-table-column label="وضعیت انتشار" width="">
<template slot-scope="scope">
<el-tag :type="scope.row.publish ? 'primary' : 'danger'">{{
scope.row.publish ? "منتشر شده" : "منتشر نشده"
}}
</el-tag>
</template>
</el-table-column>
<el-table-column v-if="$auth.user.permissions.includes('superAdmin')" label="نمایش در صفحه اصلی" width="">
<template slot-scope="scope">
<el-tag :type="scope.row.showInHome ? 'primary' : 'danger'">{{
scope.row.showInHome ? "بله" : "خیر"
}}
</el-tag>
</template>
</el-table-column>
<el-table-column label="بخش" width="">
<template slot-scope="scope">
<el-tag :type="'primary'">{{ getModelName(scope.row.modelType) }}</el-tag>
</template>
</el-table-column>
<el-table-column label="تعداد بازدید" width="">
<template slot-scope="scope">
<span>{{scope.row.userViews}}</span>
</template>
</el-table-column>
<el-table-column label="تاریخ ایجاد" width="">
<template slot-scope="scope">
<span>{{ jDate(scope.row.created_at) }}</span>
</template>
</el-table-column>
<el-table-column label="ویرایش" width="110" align="center">
<template slot-scope="scope">
<CButton color="danger" variant="outline" :key="scope.row._id + Math.random()"
@click="deleteNews(scope.row._id)">
<i class="far fa-trash-alt"></i>
</CButton>
<CButton color="success" variant="outline" :key="scope.row._id" :to="{
name: 'admin-news-details',
params: { details: scope.row._id },
}">
<i class="far fa-eye"></i>
</CButton>
</template>
</el-table-column>
</el-table>
</CCardBody>
</CCard>
<CCard class="col" v-if="news.totalDocs>5">
<CRow alignHorizontal="center" style="padding : 10px">
<el-pagination
:hide-on-single-page="true"
layout="prev, pager, next"
:page-size="news.limit"
@current-change="handleCurrentChange"
:current-page.sync="news.page"
:total="news.totalDocs"
>
</el-pagination>
</CRow>
</CCard>
</div>
</template>
<script>
import moment from "moment-jalaali";
const _ = require('lodash');
export default {
data() {
return {
title: "لیست اخبار",
list_title: "لیست",
portals: [],
filterParent: null,
news: null,
search: {},
newsFile: [],
loading: false
};
},
computed: {
pageQuery() {
return this.$route.query.page;
}
},
watch: {
pageQuery(newPage, oldPage) {
this.$nuxt.refresh();
}
},
methods: {
getModelName(value) {
/** get name of portal */
// for (const item of news.data.docs) {
// item.modelType = portals.data.filter((value) => value.value === item.modelType)[0].name;
// }
return this.portals.find(item => item.value === value).name
},
jDate(date) {
return moment(date).format("jYYYY/jMM/jDD");
},
handleCurrentChange(page) {
if (!_.isEmpty(this.search)) {
this.handleSearch(page);
} else {
this.$router.push({
name: "admin-news",
query: {
page: page
}
});
}
},
handleSearch(page) {
this.loading = true;
const data = {
term: this.search.term,
catId: this.search.catId,
status: this.search.status,
showInHome: this.search.showInHome,
portals: this.search.portals,
publish: this.search.publish,
notifications: this.search.notifications,
special: this.search.special,
newsFile: this.search.newsFile,
date: this.search.date,
}
this.$axios.post(`/api/admin/news/search?page=${page}`, data, this.axiosConfig)
.then((result) => {
/** get name of portal */
// for (const item of result.data.docs) {
// item.modelType = this.portals.filter((value) => value.value === item.modelType)[0].name;
// }
this.news = 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)
})
},
deleteNews(id) {
this.$confirm("خبر حذف شود؟", "هشدار", {
confirmButtonText: "بله",
cancelButtonText: "لغو",
type: "warning",
})
.then(async () => {
this.$axios
.delete(`/api/admin/news/${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, error}) {
try {
const requests = [
$axios.get(`/api/admin/news?page=${query.page}`, { progress: false }),
$axios.get(`/api/admin/portals` ,{ progress: false }),
$axios.get("/api/admin/newsFile" ,{ progress: false }),
$axios.get("/api/admin/category/getParent", {
params: {
type: "news",
},
})
];
const fetch = await Promise.all(requests);
let [news, portals, newsFile , parents] = fetch;
return {
portals: portals.data,
parents: parents.data,
news: news.data,
newsFile: newsFile.data
};
} catch (e) {
console.log(e);
if (e?.response?.status === 401) {
error({
status: 401,
message: 'شما اجازه دسترسی به این صفحه را ندارید'
})
} else {
error({
status: 500,
message: "مشکلی در گرفتن اطلاعات بوجود آمده است",
});
}
}
}
};
</script>