user.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import store from '@/store'
  2. import { Role } from '@/constant'
  3. import request, { keycloakRequest } from '@/utils/request'
  4. import {
  5. send,
  6. add,
  7. update,
  8. messageSend
  9. } from './base'
  10. // 公众号二维码
  11. export function getTicket (id) {
  12. return request({
  13. url: `keycloak/query/ticket/${id}`,
  14. method: 'GET'
  15. })
  16. }
  17. // 小程序二维码
  18. export function getQrcode (data) {
  19. return request({
  20. url: `wxapplet/qrcode`,
  21. method: 'GET',
  22. params: data
  23. })
  24. }
  25. export function userinfo (silence) {
  26. const config = {
  27. // url: `${baseUrl}/realms/${realm}/account`,
  28. url: `/users/${store.getters.userId}`,
  29. method: 'GET'
  30. }
  31. if (silence) {
  32. config.custom = true
  33. return keycloakRequest(config)
  34. }
  35. return send(config, keycloakRequest)
  36. }
  37. export function addUser (data) {
  38. return add({
  39. url: `/users`,
  40. method: 'POST',
  41. data
  42. }, keycloakRequest)
  43. }
  44. export function updateUser (id, data, message) {
  45. return update({
  46. url: `/users/${id}`,
  47. method: 'PUT',
  48. data
  49. }, message, keycloakRequest)
  50. }
  51. export function deleteUser ({ id }) {
  52. return keycloakRequest({
  53. url: `/users/${id}`,
  54. method: 'DELETE'
  55. })
  56. }
  57. export function getUserCredentials ({ id }) {
  58. return keycloakRequest({
  59. url: `/users/${id}/credentials`,
  60. method: 'GET'
  61. })
  62. }
  63. export function deleteUserCredentials (userId, credentialId) {
  64. return send({
  65. url: `/users/${userId}/credentials/${credentialId}`,
  66. method: 'DELETE'
  67. }, keycloakRequest)
  68. }
  69. export function resetPassword ({ id }) {
  70. return messageSend({
  71. url: `/users/${id}/reset-password`,
  72. method: 'PUT',
  73. data: {
  74. type: 'password',
  75. value: process.env.VUE_APP_USER_PASSWORD,
  76. temporary: true
  77. }
  78. }, '重置', keycloakRequest)
  79. }
  80. export function getTenantsByQuery (query, onlyOnce) {
  81. const { pageSize: max, pageNum, name, ...params } = query
  82. return keycloakRequest({
  83. url: `/groups`,
  84. method: 'GET',
  85. params: {
  86. briefRepresentation: false,
  87. max,
  88. first: pageNum ? (pageNum - 1) * max : void 0,
  89. search: name || void 0,
  90. ...params
  91. }
  92. }).then(data => {
  93. return { data: normalizeGroups(data, null, onlyOnce) }
  94. })
  95. }
  96. function normalizeGroups (groups, parentGroup, onlyOnce) {
  97. return groups.map(({ id, name, path, attributes, subGroups }) => {
  98. const remark = attributes.remark?.[0] || ''
  99. const group = {
  100. parentGroup,
  101. id,
  102. path,
  103. name,
  104. remark,
  105. label: remark || name
  106. }
  107. if (onlyOnce) {
  108. return group
  109. }
  110. group.subGroups = normalizeGroups(subGroups.sort((a, b) => a.name <= b.name ? -1 : 1), group)
  111. return group
  112. })
  113. }
  114. function getTenantCount (name) {
  115. return keycloakRequest({
  116. url: `/groups/count`,
  117. method: 'GET',
  118. params: {
  119. top: true,
  120. search: name || void 0
  121. }
  122. })
  123. }
  124. export async function getTenants (query) {
  125. const { count } = await getTenantCount(query.name)
  126. const { data } = await getTenantsByQuery(query)
  127. return {
  128. data,
  129. totalCount: count
  130. }
  131. }
  132. export function getTenantTree () {
  133. if (store.getters.isSuperAdmin) {
  134. return getTenantsByQuery({})
  135. }
  136. return getGroups().then(data => {
  137. const groups = normalizeGroups([data])
  138. groups[0].label = '根部门'
  139. return { data: groups }
  140. })
  141. }
  142. export function addTenant ({ name, remark }) {
  143. return add({
  144. url: `/groups`,
  145. method: 'POST',
  146. data: {
  147. name,
  148. attributes: { remark: [remark] }
  149. }
  150. }, keycloakRequest)
  151. }
  152. export function getTopGroups () {
  153. if (store.getters.isSuperAdmin) {
  154. return getTenantsByQuery({}, true)
  155. }
  156. return getGroups().then(data => {
  157. return { data: normalizeGroups(data.subGroups, true) }
  158. })
  159. }
  160. async function getGroups () {
  161. if (!store.getters.tenantId) {
  162. const groups = await getUserGroups(store.getters.userId)
  163. store.commit('user/SET_TENANT_ID', groups[0].id)
  164. }
  165. return keycloakRequest({
  166. url: `/groups/${store.getters.tenantId}`,
  167. method: 'GET'
  168. })
  169. }
  170. export function addSubGroup ({ id }, { name, remark }) {
  171. return add({
  172. url: `/groups/${id}/children`,
  173. method: 'POST',
  174. data: {
  175. name,
  176. attributes: { remark: [remark] }
  177. }
  178. }, keycloakRequest)
  179. }
  180. export function updateGroup ({ id, name, remark }) {
  181. return update({
  182. url: `/groups/${id}`,
  183. method: 'PUT',
  184. data: {
  185. name,
  186. attributes: { remark: [remark] }
  187. }
  188. }, null, keycloakRequest)
  189. }
  190. export function deleteGroup ({ id }) {
  191. return keycloakRequest({
  192. url: `/groups/${id}`,
  193. method: 'DELETE'
  194. })
  195. }
  196. export function getUsersByGroup (query) {
  197. const { id, pageSize: max, pageNum, ...params } = query
  198. return keycloakRequest({
  199. url: `/groups/${id}/members`,
  200. method: 'GET',
  201. params: {
  202. briefRepresentation: true,
  203. max,
  204. first: pageNum ? (pageNum - 1) * max : void 0,
  205. ...params
  206. }
  207. }).then(data => { return { data } })
  208. }
  209. export function getUserGroups (id, briefRepresentation = true) {
  210. return keycloakRequest({
  211. url: `/users/${id}/groups`,
  212. method: 'GET',
  213. params: { briefRepresentation }
  214. }).then(data => data.sort((a, b) => a.path.length - b.path.length))
  215. }
  216. function addUserToGroup (userId, groupId) {
  217. return keycloakRequest({
  218. url: `/users/${userId}/groups/${groupId}`,
  219. method: 'PUT'
  220. })
  221. }
  222. function removeUserFromGroup (userId, groupId) {
  223. return keycloakRequest({
  224. url: `/users/${userId}/groups/${groupId}`,
  225. method: 'DELETE'
  226. })
  227. }
  228. export async function moveUserGroup (userId, from, to) {
  229. const fromGroups = getGroupList(from)
  230. const toGroups = getGroupList(to)
  231. let fromLast = fromGroups.length - 1
  232. const toLast = toGroups.length - 1
  233. let start = 0
  234. while (start <= fromLast && start <= toLast) {
  235. if (fromGroups[start].id === toGroups[start].id) {
  236. start += 1
  237. } else {
  238. break
  239. }
  240. }
  241. const changed = start <= fromLast || start <= toLast
  242. for (; fromLast >= start; fromLast--) {
  243. await removeUserFromGroup(userId, fromGroups[fromLast].id)
  244. }
  245. for (; start <= toLast; start++) {
  246. await addUserToGroup(userId, toGroups[start].id)
  247. }
  248. return changed
  249. }
  250. function getGroupList (group) {
  251. const groups = [group]
  252. let parentGroup = group.parentGroup
  253. while (parentGroup) {
  254. groups.unshift(parentGroup)
  255. parentGroup = parentGroup.parentGroup
  256. }
  257. return groups
  258. }
  259. export function getUserRoleMapping (id) {
  260. return getUserRoles(id).then(
  261. roles => getRoles().then(
  262. available => {
  263. return {
  264. available,
  265. roles
  266. }
  267. }
  268. )
  269. )
  270. }
  271. function getUserRoles (id) {
  272. return keycloakRequest({
  273. url: `/users/${id}/role-mappings/realm`,
  274. method: 'GET'
  275. })
  276. }
  277. function getRoles () {
  278. return keycloakRequest({
  279. url: `/roles`,
  280. method: 'GET'
  281. }).then(filterRoles)
  282. }
  283. function filterRoles (roles) {
  284. const isSuperAdmin = store.getters.isSuperAdmin
  285. return roles.filter(({ name }) => name !== Role.SUPER_ADMIN && (isSuperAdmin || name !== Role.ADMIN) && name.startsWith('ROLE_'))
  286. }
  287. export async function updateUserRoles (userId, available, fromKeys, toKeys) {
  288. const delRoles = []
  289. const toSet = new Set(toKeys)
  290. fromKeys.forEach(id => {
  291. if (!toSet.has(id)) {
  292. delRoles.push(available.find(role => role.id === id))
  293. }
  294. })
  295. if (delRoles.length) {
  296. await removeUserRoles(userId, delRoles)
  297. }
  298. const addRoles = []
  299. const fromSet = new Set(fromKeys)
  300. toKeys.forEach(id => {
  301. if (!fromSet.has(id)) {
  302. addRoles.push(available.find(role => role.id === id))
  303. }
  304. })
  305. if (addRoles.length) {
  306. await addUserRoles(userId, addRoles)
  307. }
  308. }
  309. function removeUserRoles (userId, roles) {
  310. return keycloakRequest({
  311. url: `/users/${userId}/role-mappings/realm`,
  312. method: 'DELETE',
  313. data: roles
  314. })
  315. }
  316. function addUserRoles (userId, roles) {
  317. return keycloakRequest({
  318. url: `/users/${userId}/role-mappings/realm`,
  319. method: 'POST',
  320. data: roles
  321. })
  322. }