index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <template>
  2. <v-card class="card-box d-flex pa-3">
  3. <v-row no-gutters justify="space-between">
  4. <v-col cols="2">
  5. <div class="d-flex justify-start pr-3">
  6. <v-btn prepend-icon="mdi-plus" variant="text" density="compact" color="primary" @click="handleAdd(1)">{{ $t('enterprise.userManagement.addBranchOffice') }}</v-btn>
  7. </div>
  8. <v-treeview
  9. :items="treeData"
  10. activatable
  11. color="primary"
  12. item-value="id"
  13. open-all
  14. open-strategy="single"
  15. density="compact"
  16. @update:activated="handleClick"
  17. @update:opened="handleClick"
  18. >
  19. <template v-slot:title="{ item }">
  20. <div class="treeTitle font-size-15">
  21. {{ formatName(item.anotherName || item.name) }}
  22. <v-tooltip activator="parent" location="end">{{ formatName(item.anotherName || item.name) }}</v-tooltip>
  23. </div>
  24. </template>
  25. </v-treeview>
  26. </v-col>
  27. <v-divider vertical></v-divider>
  28. <v-col class="ml-10" cols="9">
  29. <div class="d-flex justify-space-between px-3">
  30. <TextInput v-model="query.name" :item="textItem" @change="getUserList"></TextInput>
  31. <v-btn prepend-icon="mdi-plus" color="primary" @click="handleAdd(0)">{{ $t('enterprise.userManagement.inviteNewColleagues') }}</v-btn>
  32. </div>
  33. <CtTable
  34. :items="tableData"
  35. :headers="headers"
  36. :loading="loading"
  37. :elevation="0"
  38. :is-tools="false"
  39. :showPage="true"
  40. :total="total"
  41. :page-info="query"
  42. itemKey="id"
  43. @pageHandleChange="handleChangePage"
  44. >
  45. <template #name="{ item }">
  46. <div class="d-flex align-center">
  47. <v-avatar size="40" :image="getUserAvatar(item.avatar, item.sex)"></v-avatar>
  48. <span class="ml-3">{{ item?.name }}</span>
  49. </div>
  50. </template>
  51. <template #actions="{ item }">
  52. <v-btn v-if="item.userType === '0'" color="primary" variant="text" @click="handleEdit(item)">编辑</v-btn>
  53. <v-btn v-if="item.status === '1' && item.userType !== '1'" color="primary" variant="text" @click="handleAction('', 0, item)">{{ $t('enterprise.userManagement.enable') }}</v-btn>
  54. <v-btn v-if="item.status === '0' && item.userType !== '1'" color="primary" variant="text" @click="handleAction('', 1, item)">{{ $t('enterprise.userManagement.disable') }}</v-btn>
  55. <v-btn v-if="item.status === '0' && item.userType !== '1' && item.enterpriseId.toString() === enterpriseInfo?.enterpriseId.toString()" color="primary" variant="text" @click="handleRole(item)">分配角色</v-btn>
  56. </template>
  57. </CtTable>
  58. </v-col>
  59. </v-row>
  60. </v-card>
  61. <CtDialog :visible="showEdit" :widthType="2" titleClass="text-h6" title="编辑员工基本信息" @close="showEdit = false; editId = null" @submit="handleSubmit">
  62. <CtForm ref="CtFormRef" class="mt-3" :items="formItems">
  63. <template #avatar="{ item }">
  64. <div style="width: 100%;">
  65. <div class="d-flex">
  66. <div style="color: #7a7a7a;">头像</div>
  67. <div class="avatarsBox" @mouseover="showIcon = true" @mouseleave="showIcon = false">
  68. <v-avatar class="elevation-5" size=80 :image="getUserAvatar(item.value, item.sex)"></v-avatar>
  69. <div v-show="showIcon" @click="openFileInput" v-bind="$attrs" class="mdi mdi-camera-outline">
  70. <input
  71. type="file"
  72. ref="fileInput"
  73. accept="image/png, image/jpg, image/jpeg"
  74. style="display: none;"
  75. @change="handleUploadFile"
  76. />
  77. </div>
  78. </div>
  79. </div>
  80. <div class="text-center color-999 font-size-14 mb-5">*只支持JPG、JPEG、PNG类型的图片</div>
  81. </div>
  82. </template>
  83. </CtForm>
  84. </CtDialog>
  85. <CtDialog :visible="showRole" :widthType="2" titleClass="text-h6" title="分配角色" @close="showRole = false; roleItem = null" @submit="handleSubmitRole">
  86. <CtForm v-loading="roleLoading" ref="CtFormRef" class="mt-3" :items="roleForm"></CtForm>
  87. </CtDialog>
  88. <ImgCropper :visible="isShowCopper" :image="selectPic" :cropBoxResizable="true" @submit="handleHideCopper" :aspectRatio="1 / 1" @close="isShowCopper = false"></ImgCropper>
  89. </template>
  90. <script setup>
  91. defineOptions({ name: 'group-account'})
  92. import { ref, computed } from 'vue'
  93. import { useI18n } from '@/hooks/web/useI18n'
  94. import { timesTampChange } from '@/utils/date'
  95. import { getEnterpriseTree } from '@/api/recruit/enterprise/system/group'
  96. import { getEnterpriseUserList, systemUserEnable, systemUserDisable } from '@/api/recruit/enterprise/system/user'
  97. import Confirm from '@/plugins/confirm'
  98. import Snackbar from '@/plugins/snackbar'
  99. import { checkCompanyEmail } from '@/utils/validate'
  100. import { updateGroupUserAccount } from '@/api/enterprise'
  101. import { getUserAvatar } from '@/utils/avatar'
  102. import { uploadFile } from '@/api/common'
  103. import { getToken } from '@/utils/auth'
  104. import { formatName } from '@/utils/getText';
  105. import {
  106. getRoleList,
  107. getRolePageSimple,
  108. saveUserRole
  109. } from '@/api/recruit/enterprise/system/role'
  110. const { t } = useI18n()
  111. const total = ref(0)
  112. const loading = ref(false)
  113. const query = ref({
  114. pageSize: 10,
  115. pageNo: 1,
  116. enterpriseId: '',
  117. name: null
  118. })
  119. // 企业基本信息
  120. const enterpriseInfo = ref(localStorage.getItem('entBaseInfo') ? JSON.parse(localStorage.getItem('entBaseInfo')) : {})
  121. const tableData = ref([])
  122. const treeData = ref([])
  123. const headers = [
  124. { title: t('login.username'), key: 'name', sortable: false },
  125. { title: t('enterprise.userManagement.affiliatedEnterprise'), key: 'enterpriseAnotherName', sortable: false, value: item => formatName(item.enterpriseAnotherName || item.enterpriseName) },
  126. { title: t('enterprise.userManagement.post'), key: 'postName', sortable: false },
  127. { title: t('enterprise.userManagement.phone'), key: 'phone', sortable: false },
  128. { title: t('enterprise.userManagement.email'), key: 'email', sortable: false },
  129. { title: t('enterprise.userManagement.accountType'), key: 'userType', value: item => item.userType === '1' ? t('enterprise.userManagement.administrators') : t('enterprise.userManagement.regularUser'), sortable: false },
  130. { title: t('enterprise.userManagement.lastLoginTime'), key: 'loginDate', value: item => timesTampChange(item.loginDate), sortable: false },
  131. { title: t('common.actions'), key: 'actions', sortable: false }
  132. ]
  133. const textItem = ref({
  134. type: 'text',
  135. value: null,
  136. width: 250,
  137. clearable: true,
  138. label: '请输入用户名称搜索'
  139. })
  140. // 角色分配
  141. const showRole = ref(false)
  142. const roleLoading = ref(false)
  143. const roleItem = ref(null)
  144. const roleForm = ref({
  145. options: [
  146. {
  147. type: 'text',
  148. key: 'name',
  149. value: null,
  150. label: t('login.username'),
  151. disabled: true
  152. },
  153. {
  154. type: 'text',
  155. key: 'enterpriseAnotherName',
  156. value: null,
  157. label: t('enterprise.userManagement.affiliatedEnterprise'),
  158. disabled: true
  159. },
  160. {
  161. type: 'autocomplete',
  162. key: 'roleIds',
  163. value: [],
  164. label: '角色选择',
  165. multiple: true,
  166. itemText: 'name',
  167. itemValue: 'id',
  168. items: []
  169. }
  170. ]
  171. })
  172. // 获取用户列表
  173. const getUserList = async () => {
  174. loading.value = true
  175. try {
  176. const { list, total: number } = await getEnterpriseUserList(query.value)
  177. tableData.value = list
  178. total.value = number
  179. } finally {
  180. loading.value = false
  181. }
  182. }
  183. // 获取树形列表
  184. const getTreeData = async () => {
  185. const data = await getEnterpriseTree()
  186. if (!data) return
  187. treeData.value = [data]
  188. query.value.enterpriseId = data.id
  189. // 获取用户列表
  190. getUserList()
  191. }
  192. getTreeData()
  193. // 分页
  194. const handleChangePage = (e) => {
  195. query.value.pageNo = e
  196. getUserList()
  197. }
  198. // 树形click
  199. const handleClick = (e) => {
  200. if (!e.length) return
  201. query.value.enterpriseId = e[0]
  202. getUserList()
  203. }
  204. const apiList = [
  205. { api: systemUserEnable, desc: t('enterprise.userManagement.enableAccount') },
  206. { api: systemUserDisable, desc: t('enterprise.userManagement.disableAccount') }
  207. ]
  208. // 启用、禁用账户
  209. const handleAction = (type, index, item) => {
  210. const ids = [item.id]
  211. Confirm(t('common.confirmTitle'), apiList[index].desc).then(async () => {
  212. await apiList[index].api(ids)
  213. Snackbar.success(t('common.operationSuccessful'))
  214. query.value.pageNo = 1
  215. getUserList()
  216. })
  217. }
  218. // 打开生成邀请链接页面
  219. const handleAdd = (type) => {
  220. if (!getToken(1)) {
  221. window.location.reload()
  222. return
  223. }
  224. // type: 类型(0 邀请同事 | 1 邀请子公司)
  225. window.open(`/recruit/enterprise/systemManagement/groupAccount/invite/${type}`)
  226. }
  227. const formItems = ref({
  228. options: [
  229. {
  230. slotName: 'avatar',
  231. key: 'avatar',
  232. value: null
  233. },
  234. // {
  235. // type: 'ifRadio',
  236. // key: 'sex',
  237. // value: '',
  238. // label: '性别 *',
  239. // width: 90,
  240. // items: []
  241. // },
  242. {
  243. type: 'text',
  244. key: 'name',
  245. value: '',
  246. label: '中文名 *',
  247. rules: [v => !!v || '请填写中文名']
  248. },
  249. {
  250. type: 'phoneNumber',
  251. key: 'phone',
  252. value: '',
  253. clearable: true,
  254. label: '联系手机号 *',
  255. rules: [v => !!v || '请填写联系手机号']
  256. },
  257. {
  258. type: 'text',
  259. key: 'email',
  260. value: '',
  261. label: '电子邮箱 *',
  262. disabled: false,
  263. rules: [
  264. value => {
  265. if (value) return true
  266. return '请输入联系邮箱'
  267. },
  268. value => {
  269. if (checkCompanyEmail(value)) return true
  270. return '请输入正确的电子邮箱'
  271. }
  272. ]
  273. },
  274. {
  275. type: 'text',
  276. key: 'postName',
  277. value: '',
  278. label: '所在岗位'
  279. }
  280. ]
  281. })
  282. // getDict('menduner_sex').then(({ data }) => {
  283. // data = data?.length && data || []
  284. // formItems.value.options.find(e => e.key === 'sex').items = data
  285. // })
  286. // 编辑员工信息
  287. const showEdit = ref(false)
  288. const CtFormRef = ref()
  289. const editId = ref(null)
  290. const showIcon = ref(false)
  291. const handleEdit = async (item) => {
  292. formItems.value.options.forEach(e => { e.value = item[e.key] })
  293. editId.value = item.id
  294. showEdit.value = true
  295. }
  296. // 选择文件
  297. const fileInput = ref()
  298. const clicked = ref(false)
  299. const selectPic = ref('')
  300. const isShowCopper = ref(false)
  301. const openFileInput = () => {
  302. if (clicked.value) return
  303. clicked.value = true
  304. fileInput.value.click()
  305. clicked.value = false
  306. }
  307. // 上传头像
  308. const accept = ['jpg', 'png', 'jpeg']
  309. const handleUploadFile = async (e) => {
  310. const file = e.target.files[0]
  311. if (!file) return
  312. const arr = file.name.split('.')
  313. const fileType = arr?.length ? arr[arr.length-1] : ''
  314. if (!accept.includes(fileType)) return Snackbar.warning('请上传图片格式文件')
  315. const reader = new FileReader()
  316. reader.readAsDataURL(file)
  317. reader.onload = () => {
  318. selectPic.value = String(reader.result)
  319. isShowCopper.value = true
  320. }
  321. }
  322. const handleHideCopper = (data) => {
  323. isShowCopper.value = false
  324. if (data) {
  325. const { file } = data
  326. if (!file) return
  327. const formData = new FormData()
  328. formData.append('file', file)
  329. formData.append('path', 'img')
  330. uploadFile(formData).then(async ({ data }) => {
  331. if (!data) return
  332. formItems.value.options.find(e => e.key === 'avatar').value = data
  333. })
  334. }
  335. }
  336. const handleRole = async (item) => {
  337. roleItem.value = item
  338. showRole.value = true
  339. const list = await getRolePageSimple()
  340. const data = await getRoleList({ bindId: item.id })
  341. roleForm.value.options.forEach(e => {
  342. if (e.key === 'roleIds') {
  343. e.items = list
  344. e.value = data
  345. return
  346. }
  347. e.value = item[e.key]
  348. })
  349. }
  350. const handleSubmitRole = async () => {
  351. roleLoading.value = true
  352. try {
  353. await saveUserRole({ bindId: roleItem.value.id, roleIds: roleForm.value.options.find(e => e.key === 'roleIds').value })
  354. Snackbar.success('保存成功')
  355. showRole.value = false
  356. } finally {
  357. roleLoading.value = false
  358. }
  359. }
  360. // 提交
  361. const handleSubmit = async () => {
  362. const { valid } = await CtFormRef.value.formRef.validate()
  363. if (!valid) return
  364. const obj = {
  365. id: editId.value
  366. }
  367. formItems.value.options.forEach(e => { obj[e.key] = e.value })
  368. // if (!obj.sex || obj.sex === '0') return Snackbar.warning('请选择员工性别')
  369. await updateGroupUserAccount(obj)
  370. showEdit.value = false
  371. editId.value = null
  372. Snackbar.success('编辑成功')
  373. getUserList()
  374. }
  375. </script>
  376. <style scoped lang="scss">
  377. .avatarsBox {
  378. height: 80px;
  379. width: 80px;
  380. position: relative;
  381. cursor: pointer;
  382. margin: 0 0 10px 100px;
  383. .img {
  384. width: 100%;
  385. height: 100%;
  386. }
  387. .mdi {
  388. font-size: 42px;
  389. color: #fff;
  390. }
  391. div {
  392. position: absolute;
  393. top: 50%;
  394. left: 50%;
  395. transform: translate(-50%, -50%);
  396. border-radius: 50%;
  397. }
  398. }
  399. .treeTitle {
  400. width: 100%;
  401. max-width: 100%;
  402. white-space: nowrap;
  403. text-overflow: ellipsis;
  404. overflow: hidden;
  405. }
  406. </style>