import { HttpClient, HttpParams } from '@angular/common/http' import { Injectable } from '@angular/core' import { JwResponse, NullableProps } from 'app/types' import { Utils } from 'app/utils' import { NzTreeNodeOptions } from 'ng-zorro-antd/tree' import { BehaviorSubject, map, of, tap } from 'rxjs' import { AuthDTO, AuthorityDTO, AuthorizeItemDTO, ConsoleClientTopDTO, ConsoleCountDTO, ConsoleExpireDTO, EntityDTO, EntityDetailDTO, PermissionDTO, ProductDTO, RoleFormDTO, RoleListDTO, UserDTO, } from './api.dto' import { PermissionService } from 'app/shared/permission/permission.service' export interface UserGroupTreeItem { name: string id: string parentId: string children: UserGroupTreeItem[] } @Injectable({ providedIn: 'root', }) export class ApiService { constructor( private http: HttpClient, private permission: PermissionService, ) {} authInfo: AuthDTO = { createTime: '', enable: false, groupId: '', id: '', name: '', remark: '', roleId: '', roleName: '', updateTime: '', username: '', authorities: [], } getAuthInfo(force?: boolean) { if (this.authInfo && !force) { return of(this.authInfo) } localStorage.setItem('da', 'af') return of(this.authInfo) // return this.http.get>('/api/auth/info').pipe( // map((res) => res.data), // tap((res) => { // this.authInfo = res // this.permission.loadPermission(res.authorities.map((f) => f.authority)) // }), // ) } login(data: {}) { return this.http.post('/api/oauth/login', data) } private _parseUserGroupTree(group: UserGroupTreeItem[]): NzTreeNodeOptions[] { if (group?.length === 0) { return [] } return group.map((i) => { return { ...i, key: i.id, title: i.name, children: this._parseUserGroupTree(i.children ?? []), isLeaf: i.children?.length === 0 || !i.children, } }) } changeUserPassword(id: string, password: string) { return this.http.put('/api/user/reset/password', { id, password, }) } getDashboardCounter() { return this.http.get>(`/api/console/count`) } getDashboardExpire() { return this.http.get>(`/api/console/expire`) } getDashboardClientTop() { return this.http.get>(`/api/console/client/top`) } getUserInfo(id: string) { return this.http.get>(`/api/user/${id}`).pipe() } changePassword(data: {}) { return this.http.put('/api/user/password', data) } getUserPage(p: {}, q: {}) { const params = Utils.objectToURLSearchParams({ ...p, ...q }) return this.http.get(`/api/user/pages?${params}`) } saveUser(user: any) { if (user.id) { return this.http.put('/api/user', user) } else { return this.http.post('/api/user', user) } } deleteUser(id: string) { return this.http.delete(`/api/user/${id}`) } getUserGroupTree() { return this.http.get>('/api/user/group/tree').pipe( map((res) => { return this._parseUserGroupTree(res.data) }), ) } saveUserGroup(group: Partial) { if (group.id) { return this.http.put('/api/user/group', group) } else { return this.http.post('/api/user/group', group) } } updateUserStatus(id: string) { return this.http.put(`/api/user/enable/${id}`, null) } deleteUserGroup(id: string) { return this.http.delete(`/api/user/group/${id}`) } getAllRole() { return this.http.get>(`/api/sysRole/list`) } getRolePage(p: {}, q: {}) { const params = Utils.objectToURLSearchParams({ ...p, ...q }) return this.http.get(`/api/sysRole/pages?${params}`) } getAllPermission() { return this.http.get>('/api/authority/tree') } getRoleById(id: string) { return this.http.get>(`/api/sysRole/${id}`) } deleteRole(id: string) { return this.http.delete(`/api/sysRole/${id}`) } saveRole(role: NullableProps) { if (role.id) { return this.http.put('/api/sysRole', role) } else { return this.http.post('/api/sysRole', role) } } authorize(v: {}) { return this.http.post('/api/client/license', v) } getSysLog(p: {}, q: {}) { const params = Utils.objectToURLSearchParams({ ...p, ...q }) return this.http.get(`/api/sysLog/pages?${params}`) } getEntityPage(p: {}, q: {}) { const params = Utils.objectToURLSearchParams({ ...p, ...q }) return this.http.get(`/api/client/pages?${params}`) } getEntityAuthorizeList(clientId: string) { return this.http.get>(`/api/licenseLog/${clientId}/list`) } getEntityDetail(id: string) { return this.http.get>(`/api/client/${id}`) } saveEntity(entity: Partial>) { if (entity.id) { return this.http.put('/api/client', entity) } else { return this.http.post('/api/client', entity) } } deleteEntity(id: string) { return this.http.delete(`/api/client/${id}`) } getProductPage(p: {}, q: {}) { const params = Utils.objectToURLSearchParams({ ...p, ...q }) return this.http.get(`/api/product/pages?${params}`) } getAllProduct() { return this.http.get>(`/api/product/list`) } saveProduct(entity: Partial>) { if (entity.id) { return this.http.put('/api/product', entity) } else { return this.http.post('/api/product', entity) } } deleteProduct(id: string) { return this.http.delete(`/api/product/${id}`) } getProductVersionPage(p: {}, q: {}) { const params = Utils.objectToURLSearchParams({ ...p, ...q }) return this.http.get(`/api/product/pages?${params}`) } saveVersion(entity: Partial>) { if (entity.id) { return this.http.put('/api/product', entity) } else { return this.http.post('/api/product', entity) } } deleteVersion(id: string, pid: string) { return this.http.delete(`/api/product/${id}`) } getProductDetail(pid: string) { return this.http.get(`/api/product/${pid}`) } }