配餐项目前端文件
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.1 KiB

2 years ago
import { UserDTO, UserRoleDTO } from "@admin/app/dtos/user.dto";
2 years ago
import { ApiService } from "@admin/app/services";
2 years ago
import {
Component,
EventEmitter,
Input,
OnChanges,
OnInit,
Output,
SimpleChanges,
TemplateRef,
ViewChild,
} from "@angular/core";
2 years ago
import { FormControl, FormGroup } from "@angular/forms";
import { ActivatedRoute } from "@angular/router";
2 years ago
import { AnyObject, FormValidators, TableListOption, Utils } from "@cdk/public-api";
import { MD5 } from "crypto-js";
import { NzMessageService } from "ng-zorro-antd/message";
2 years ago
import { NzModalService } from "ng-zorro-antd/modal";
2 years ago
import { lastValueFrom } from "rxjs";
2 years ago
@Component({
selector: "app-user-list",
templateUrl: "./user-list.component.html",
styleUrls: ["./user-list.component.less"],
})
2 years ago
export class UserListComponent implements OnChanges {
2 years ago
constructor(
private api: ApiService,
private route: ActivatedRoute,
private modal: NzModalService,
private msg: NzMessageService
) {}
2 years ago
@ViewChild("userFormTpl") public userFormTpl!: TemplateRef<void>;
2 years ago
@Input() users: UserDTO[] = [];
2 years ago
2 years ago
@Input() role!: UserRoleDTO;
2 years ago
2 years ago
@Output() onReload = new EventEmitter();
searchValue = "";
visibledUsers: UserDTO[] = [];
2 years ago
public userFrom = new FormGroup({
uid: new FormControl("", [FormValidators.required("请输入账号")]),
name: new FormControl("", [FormValidators.required("请输入姓名")]),
password: new FormControl("", [FormValidators.required("请输入密码")]),
});
2 years ago
2 years ago
ngOnInit(): void {}
2 years ago
2 years ago
ngOnChanges(changes: SimpleChanges): void {
const u = changes["users"]?.currentValue;
if (Array.isArray(u)) {
this.onSearch(this.searchValue);
}
}
onSearch(kw: string) {
this.visibledUsers = this.users.filter((f) => f.name.includes(kw) || f.uid.includes(kw));
}
deleteItem(uid: string) {
this.modal.confirm({
nzTitle: "警告",
nzContent: "是否要删除该用户?",
nzOkDanger: true,
nzOnOk: async () => {
const res = await lastValueFrom(this.api.deleteUser(uid));
if (res.success) {
this.msg.success(res.desc);
this.onReload.emit();
return true;
}
return false;
},
});
}
resetUserForm() {
this.userFrom.reset();
}
2 years ago
openForm(item?: any) {
2 years ago
if (item) {
this.userFrom.patchValue(item);
}
2 years ago
this.modal.create({
nzTitle: item ? "编辑用户" : "新增用户",
nzContent: this.userFormTpl,
2 years ago
nzOnCancel: () => {
this.resetUserForm();
},
2 years ago
nzOnOk: async () => {
if (Utils.validateFormGroup(this.userFrom)) {
const user = { ...this.userFrom.value, roleId: this.role.id };
user["password"] = MD5(user.password!).toString().substring(16).toUpperCase();
2 years ago
let notExist = true;
if ((item && item.uid !== user.uid) || !item) {
const check = await lastValueFrom(this.api.checkUid(user.uid!));
notExist = check.body;
}
if (notExist) {
const res = await lastValueFrom(this.api.saveUser(user, Boolean(item)));
this.msg.success(res.desc);
this.onReload.emit();
this.resetUserForm();
return true;
} else {
this.msg.error("账号重复");
return false;
}
2 years ago
}
return false;
},
2 years ago
});
}
}