|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import { EventEmitter, Injectable, TemplateRef, Type } from '@angular/core';
- import { FormArray, FormGroup } from '@angular/forms';
- import { FormProperty, retrieveSchema } from '@delon/form';
- import { _HttpClient } from '@delon/theme';
- import { NzSafeAny } from 'ng-zorro-antd/core/types';
- import { NzMessageService } from 'ng-zorro-antd/message';
- import { ModalButtonOptions, NzModalService } from 'ng-zorro-antd/modal';
-
- import { MESSAGE_UTIL } from '../../conf/message';
-
- declare var FakeServer: any;
-
- @Injectable({
- providedIn: 'root'
- })
- export class BaseService {
- public drawerWidth = 550;
-
- deptmentServiceUrl = ''; //
-
- constructor(
- private http: _HttpClient,
- private modalService: NzModalService,
- private message: NzMessageService
- ) {}
-
- hasError(...args: Array<FormGroup | FormProperty | null>): boolean {
- const result = this.hasErrorBase(args);
-
- return result;
- }
-
- hasErrorBase(args: Array<FormGroup | FormProperty | null>): boolean {
- let result = false;
- if (!args) {
- return result;
- }
- args.forEach((val: any) => {
- if (val instanceof FormGroup) {
- for (const key in val.controls) {
- const ctl = val.controls[key];
- if (ctl instanceof FormGroup) {
- result = this.hasErrorBase([ctl]) || result;
- } else if (ctl instanceof FormArray) {
- (ctl as FormArray).controls.forEach(c => {
- c.markAsDirty();
- c.updateValueAndValidity();
- result = c.invalid || result;
- });
- } else {
- ctl.markAsDirty();
- ctl.updateValueAndValidity();
- result = ctl.invalid || result;
- }
- }
- } else if (val instanceof FormProperty) {
- const properties = val.root.properties as FormProperty[];
- if (properties) {
- for (const key in properties) {
- const ctl = properties[key];
- if (ctl instanceof FormProperty) {
- ctl.updateValueAndValidity();
- result = ctl.valid || result;
- }
- }
- }
- }
- });
- return result;
- }
-
- public getMessage(messageId: string, ...params: string[]): string {
- return this.getMessageBase(messageId, params);
- }
-
- private getMessageBase(messageId: string, params: string[]): string {
- return MESSAGE_UTIL.getMessage(messageId, params);
- }
-
- /**
- * ocean 2023-4-17
- * message提示框
- */
- public showMessage(state: { messageId: string; content?: string }) {
- if (state.content) {
- this.message.info(state.content);
- } else {
- this.message.info(this.getMessage(state.messageId));
- }
- }
-
- public showConfirm(state: { title?: string; message?: string; okCallback?: () => void; cancelCallback?: () => void }) {
- console.log(state.message, ' state.message');
- this.modalService.create({
- nzTitle: state.title,
- nzContent: state.message,
- nzClosable: false,
- nzBodyStyle: { 'font-size': '1rem' },
- nzFooter: [
- {
- label: '确定',
- type: 'primary',
- onClick: e => {
- let del: any = state;
- setTimeout(() => {
- del.okCallback();
- }, 100);
- this.modalService.ngOnDestroy();
- }
- },
- {
- label: '取消',
- onClick: () => {
- this.modalService.ngOnDestroy();
- console.log(2);
- }
- }
- ],
-
- nzOnOk: () => {
- if (state.okCallback) {
- state.okCallback();
- }
- },
- nzOnCancel: () => {
- if (state.cancelCallback) {
- state.cancelCallback();
- }
- }
- });
- }
-
- public showModal(config: AppModalConfig) {
- let width = 600;
- switch (config.widthClass) {
- case 'xxl':
- width = 1200;
- break;
- case 'xl':
- width = 1000;
- break;
- case 'lg':
- width = 800;
- break;
- case 'sm':
- width = 600;
- break;
- case 'xs':
- width = 400;
- break;
- }
- const modal = this.modalService.create({
- nzTitle: config.title,
- nzContent: config.component,
- nzData: config.componentParams,
- nzClosable: true,
- nzWidth: width,
- nzOnOk: config.okCallBack,
- nzFooter: config.footer,
- nzStyle: config.style,
- nzMaskClosable: false
- });
- return modal;
- }
-
- public async post(url: string, method: 'put' | 'post' | 'get' | 'delete', params?: any) {
- switch (method) {
- case 'put':
- return this.http.put(url, params).subscribe(res => {});
- break;
- case 'post':
- return this.http.post(url, params).subscribe(res => {});
- break;
- case 'get':
- return this.http.get(url, params).subscribe(res => {});
- break;
- case 'delete':
- return this.http.delete(url, params).subscribe(res => {});
- break;
- }
- }
- }
-
- export class AppModalConfig {
- title?: string | TemplateRef<{}>;
- component?: string | TemplateRef<NzSafeAny> | Type<any>;
- componentParams?: any;
- okCallBack?: () => void;
- widthClass?: 'xxl' | 'xl' | 'lg' | 'sm' | 'xs';
- footer?: string | TemplateRef<{}> | Array<ModalButtonOptions<any>> | null;
- style?: Object;
- }
|