Files
boursecu_admin/src/pages/menuPages/users/index.vue
T

303 lines
8.8 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div>
<!-- <BasicMainCard>-->
<div class="flex w-full h-full">
<tbl
ref="table"
class="w-full h-full"
@info="info"
@edit="edit"
@add="add"
@delete="deleteManager"
/>
<!-- <BasicMenu :menu="menu" width="13rem" @onChange="tabSelected" />
<component :is="selectedComponent" class="mr-5" />-->
<!-- <BasicShowContext title="پروژه ها"> <Search /> </BasicShowContext>-->
</div>
<!-- </BasicMainCard>-->
<BasicModal
v-model:visible="addUserVisible"
width="min(52rem, 96vw)"
:title="`${editId ? 'ویرایش' : 'افزودن'} کاربر`"
@ok="ok"
:loading="loading"
@close="addUserVisible = false"
>
<a-form :model="form" ref="formRef" autocomplete="off">
<div class="grid md:grid-cols-2 gap-4">
<a-form-item label="نام" name="fname" :rules="inputRules()">
<BasicInput v-model:value="form.fname" />
</a-form-item>
<a-form-item label="نام خانوادگی" name="lname" :rules="inputRules()">
<BasicInput v-model:value="form.lname" />
</a-form-item>
<a-form-item label="شماره همراه" name="mobile" :rules="inputLenRules(11)">
<BasicInput v-model:value="form.mobile" type="number" />
</a-form-item>
<a-form-item v-if="editId" label="رمز عبور" name="password">
<BasicInput v-model:value="form.password" is-password type="number" />
</a-form-item>
<a-form-item v-if="editId" label="تکرار رمز عبور">
<BasicInput v-model:value="form.repassword" is-password type="number" />
</a-form-item>
<a-form-item v-if="editId" label="ارسال پیامک">
<a-switch v-model:checked="form.send_sms" >
<template #checkedChildren>فعال</template>
<template #unCheckedChildren>غیر فعال</template>
</a-switch>
</a-form-item>
</div>
<div v-if="canAssignAccess" class="mt-1 flex flex-col gap-2">
<div>
<div class="text-sm font-medium">دسترسی صفحات</div>
<p class="mt-0.5 text-xs leading-5 opacity-70">
هر صفحه را باز کنید و مشاهده و عملیات جدول همان صفحه را مشخص کنید. بسته می‌ماند تا وقتی لازم است.
</p>
</div>
<div class="flex items-center justify-between gap-2">
<span class="text-sm">دسترسی کامل به همهٔ صفحات</span>
<a-switch v-model:checked="fullAccess" />
</div>
<ManagerPageAccessTree v-if="!fullAccess" v-model="form.page_access" />
</div>
</a-form>
</BasicModal>
<!-- <BasicInfoModal
v-model:visible="userInfoVisible"
@close="userInfoVisible = false"
:title="infoParams.title"
:date="infoParams.date"
:time="infoParams.time"
:type="infoParams.type"
itemsTitle="اطلاعات فردی"
:name="infoParams.name"
:items="infoParams.items"
/>-->
</div>
</template>
<script setup lang="ts">
// import BasicInfoModal from '@/components/BasicInfoModal'
import { inputLenRules, inputRules } from '@/utils/inputRules'
import { useStaticApiListsStore } from '@/store/staticApisStore'
import type { Const } from '@/model/const'
import type { User } from '@/model/user'
import { createUser, deleteUser, updateUser } from '@/api/user'
import { ref } from 'vue'
import tbl from './components/table.vue'
import type { FormInstance } from 'ant-design-vue'
import BasicInfoModal from '@/components/BasicInfoModal/index.vue'
import { message } from 'ant-design-vue/es'
import type { Info } from '@/components/BasicInfoModal/model/Info'
import { toJalaliNoTime, toTime } from '@core/utils/useDataTime'
import { confirmDeleteItem } from '@/utils/confirmModal'
import { cloneDeep } from 'lodash'
import type { Item } from '@/model/Item'
import ManagerPageAccessTree from './components/ManagerPageAccessTree.vue'
import { allGrantedMap, hasAnyView, parsePageAccess } from '@/utils/adminPageAccess'
import { useUserStore } from '@/store/userStore'
//loading
const loading = ref<boolean>(false)
//define store
const store = useStaticApiListsStore()
const userStore = useUserStore()
const canAssignAccess = computed(() => userStore.hasFullPageAccess)
const fullAccess = ref(true)
//define tbl
const table = ref(null)
const refreshTable = () => table?.value?.fetch()
let updateKey = ref<number>(0)
//role confirm items
const confirmItems: Array<Item> = [
{
value: 'بله',
key: true
},
{
value: 'خیر',
key: false
}
]
//form
const form = ref<User>({
active: true
})
const formRef = ref<FormInstance>()
watch(fullAccess, (on) => {
if (on) return
if (form.value.page_access == null) form.value.page_access = allGrantedMap()
})
//info
let infoParams = reactive<Info>({})
function info(val: User) {
infoParams.name = val.name
infoParams.type = val.mobile
infoParams.date = toJalaliNoTime(val.created_at)
infoParams.time = toTime(val.created_at)
infoParams.itemsTitle = 'اطلاعات کاربری'
infoParams.items = [
{
key: 'تلفن همراه',
value: val.mobile ?? '-'
},
{
key: 'کد کاربری',
value: val.id ?? '-'
}
]
userInfoVisible.value = true
}
//edit
let editId: number | undefined = undefined
async function edit(record: User) {
addUserVisible.value = true
editId = record.id
form.value = cloneDeep(record)
form.value.page_access = parsePageAccess(record.page_access)
fullAccess.value = form.value.page_access == null
}
//add
async function add() {
loading.value = true
editId = undefined
// await store.getUsersList()
addUserVisible.value = true
updateKey.value++
resetData()
loading.value = false
}
//reset data
const resetData = () => {
formRef.value?.resetFields()
form.value = {
fname: '',
lname: '',
active: false,
avatar_id: undefined,
sts: undefined,
id: undefined,
mobile: undefined,
page_access: null
}
fullAccess.value = true
}
//delete — DELETE /admin/user/:id
const deleteManager = (id: number) => confirmDeleteItem('مدیر', id, deleteUser, refreshTable)
/* Modal.confirm({
title: 'آیا از حذف کاربر اطمینان دارید؟',
icon: createVNode(ExclamationCircleOutlined),
okText: 'تایید',
okType: 'danger',
centered: true,
cancelText: 'انصراف',
async onOk() {
// console.log('OK');
await deleteItem(id)
},
onCancel() {
console.log('Cancel')
}
})*/
/*async function deleteItem(id: number) {
try {
loading.value = true
// table?.value?.spinTable(true)
await deleteUser(id)
message.success('با موفقیت حذف شد')
table?.value?.fetch()
} catch (e) {
console.log(e)
message.error('خطا در حذف')
} finally {
loading.value = false
// table?.value?.spinTable(false)
}
}*/
//define selects
onMounted(async () => {
loading.value = true
// await getResponsible()
// await getQC()
// await getManager()
loading.value = false
})
interface selectsItem {
responsibleItems: Array<User> | undefined
users: Array<Const> | undefined
}
//add project modal
const addUserVisible = ref<boolean>(false)
const userInfoVisible = ref<boolean>(false)
//submit user
async function ok() {
formRef.value?.validateFields().then(async () => {
// console.log('salam', form.value)
// return
const payload = cloneDeep(form.value)
delete payload.repassword
if (canAssignAccess.value) {
payload.page_access = fullAccess.value ? null : payload.page_access ?? allGrantedMap()
if (!fullAccess.value && !hasAnyView(payload.page_access)) {
message.error('حداقل مشاهدهٔ یک صفحه را انتخاب کنید')
return
}
} else {
delete payload.page_access
}
loading.value = true
try {
if (editId) {
if (form.value.password) {
if (form.value.password === form.value.repassword) {
await updateUser(editId, payload)
} else {
message.error('پسورد وارد شده با تکرار پسورد وارد شده یکسان نیست')
return
}
} else {
delete payload.password
await updateUser(editId, payload)
}
} else {
await createUser(payload)
}
addUserVisible.value = false
refreshTable()
resetData()
message.success('عملیات با موفقیت انجام شد')
// await getResponsible()
// await getQC()
// await getManager()
} catch (e) {
console.error(e)
// message.error('خطا در بارگذاری اطلاعات')
} finally {
loading.value = false
}
})
}
</script>
<style scoped></style>