import { HttpClient, HttpParams } from "@angular/common/http"; import { Injectable } from "@angular/core"; import { Utils, AnyObject, ResponseType, UserRoleDTO, PermItemDTO, UserDTO, GlobalEnum, OrgConfigDTO, ClientAccountDTO, } from "@cdk/public-api"; import { Observable, map, of, tap } from "rxjs"; @Injectable({ providedIn: "root", }) export class ClientApiServicde { constructor(private http: HttpClient) { try { const strageAccount = localStorage.getItem(this.accountKey); if (strageAccount) { this.account = JSON.parse(strageAccount); } } catch (error) { console.error("获取用户信息失败", error); } } public account!: ClientAccountDTO; public accountKey = "CATERING_ACCOUNT"; globalEnum!: GlobalEnum; page(v: {}, q: {}) { return this.http.get("https://jsonplaceholder.typicode.com/users", v).pipe( map((r) => { return { total: 10, content: r, }; }) ); } getAllEnum(force?: boolean): Observable { if (this.globalEnum && !force) { return of(this.globalEnum); } return this.http.get>("/api/basic/enum").pipe( map((res) => { return res.body; }), tap((r) => { this.globalEnum = r; }) ); } login(v: {}) { const params = Utils.objectToHttpParams(v); return this.http.get("/api/login", { params }); } logout() { return this.http.get("/api/logout"); } getRoleList() { return this.http.get>("/api/role"); } deleteRole(roleId: string) { const params = new HttpParams().set("roleId", roleId); return this.http.delete("/api/role", { params, }); } updateRole(rule: AnyObject) { const body = Utils.objectToFormData(rule); const method = rule["roleId"] ? "post" : "put"; return this.http[method]("/api/role", body); } getRolePerms() { return this.http.get>("/api/role/item"); } getUserList() { return this.http.get>("/api/user"); } checkUid(uid: string) { const params = new HttpParams().set("uid", uid); return this.http.delete("/api/user/check", { params, }); } saveUser(user: AnyObject, edit: boolean) { const body = Utils.objectToFormData(user); const method = edit ? "post" : "put"; return this.http[method]("/api/user", body); } deleteUser(uid: string) { const params = new HttpParams().set("uid", uid); return this.http.delete("/api/user", { params, }); } saveOrg(org: AnyObject) { const body = Utils.objectToFormData(org); return this.http.post(" /api/vender", body); } getOrgConfig() { return this.http.get>("/api/vender/config"); } saveOrgConfig(config: AnyObject) { const body = Utils.objectToFormData(config); return this.http.post("/api/vender/config", body); } }