迁移新平台,移植红墩界
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

base.service.ts 5.1KB

pirms 4 mēnešiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { EventEmitter, Injectable, TemplateRef, Type } from '@angular/core';
  2. import { FormArray, FormGroup } from '@angular/forms';
  3. import { FormProperty, retrieveSchema } from '@delon/form';
  4. import { _HttpClient } from '@delon/theme';
  5. import { NzSafeAny } from 'ng-zorro-antd/core/types';
  6. import { NzMessageService } from 'ng-zorro-antd/message';
  7. import { ModalButtonOptions, NzModalService } from 'ng-zorro-antd/modal';
  8. import { MESSAGE_UTIL } from '../../conf/message';
  9. declare var FakeServer: any;
  10. @Injectable({
  11. providedIn: 'root'
  12. })
  13. export class BaseService {
  14. public drawerWidth = 550;
  15. deptmentServiceUrl = ''; //
  16. constructor(
  17. private http: _HttpClient,
  18. private modalService: NzModalService,
  19. private message: NzMessageService
  20. ) {}
  21. hasError(...args: Array<FormGroup | FormProperty | null>): boolean {
  22. const result = this.hasErrorBase(args);
  23. return result;
  24. }
  25. hasErrorBase(args: Array<FormGroup | FormProperty | null>): boolean {
  26. let result = false;
  27. if (!args) {
  28. return result;
  29. }
  30. args.forEach((val: any) => {
  31. if (val instanceof FormGroup) {
  32. for (const key in val.controls) {
  33. const ctl = val.controls[key];
  34. if (ctl instanceof FormGroup) {
  35. result = this.hasErrorBase([ctl]) || result;
  36. } else if (ctl instanceof FormArray) {
  37. (ctl as FormArray).controls.forEach(c => {
  38. c.markAsDirty();
  39. c.updateValueAndValidity();
  40. result = c.invalid || result;
  41. });
  42. } else {
  43. ctl.markAsDirty();
  44. ctl.updateValueAndValidity();
  45. result = ctl.invalid || result;
  46. }
  47. }
  48. } else if (val instanceof FormProperty) {
  49. const properties = val.root.properties as FormProperty[];
  50. if (properties) {
  51. for (const key in properties) {
  52. const ctl = properties[key];
  53. if (ctl instanceof FormProperty) {
  54. ctl.updateValueAndValidity();
  55. result = ctl.valid || result;
  56. }
  57. }
  58. }
  59. }
  60. });
  61. return result;
  62. }
  63. public getMessage(messageId: string, ...params: string[]): string {
  64. return this.getMessageBase(messageId, params);
  65. }
  66. private getMessageBase(messageId: string, params: string[]): string {
  67. return MESSAGE_UTIL.getMessage(messageId, params);
  68. }
  69. /**
  70. * ocean 2023-4-17
  71. * message提示框
  72. */
  73. public showMessage(state: { messageId: string; content?: string }) {
  74. if (state.content) {
  75. this.message.info(state.content);
  76. } else {
  77. this.message.info(this.getMessage(state.messageId));
  78. }
  79. }
  80. public showConfirm(state: { title?: string; message?: string; okCallback?: () => void; cancelCallback?: () => void }) {
  81. console.log(state.message, ' state.message');
  82. this.modalService.create({
  83. nzTitle: state.title,
  84. nzContent: state.message,
  85. nzClosable: false,
  86. nzBodyStyle: { 'font-size': '1rem' },
  87. nzFooter: [
  88. {
  89. label: '确定',
  90. type: 'primary',
  91. onClick: e => {
  92. let del: any = state;
  93. setTimeout(() => {
  94. del.okCallback();
  95. }, 100);
  96. this.modalService.ngOnDestroy();
  97. }
  98. },
  99. {
  100. label: '取消',
  101. onClick: () => {
  102. this.modalService.ngOnDestroy();
  103. console.log(2);
  104. }
  105. }
  106. ],
  107. nzOnOk: () => {
  108. if (state.okCallback) {
  109. state.okCallback();
  110. }
  111. },
  112. nzOnCancel: () => {
  113. if (state.cancelCallback) {
  114. state.cancelCallback();
  115. }
  116. }
  117. });
  118. }
  119. public showModal(config: AppModalConfig) {
  120. let width = 600;
  121. switch (config.widthClass) {
  122. case 'xxl':
  123. width = 1200;
  124. break;
  125. case 'xl':
  126. width = 1000;
  127. break;
  128. case 'lg':
  129. width = 800;
  130. break;
  131. case 'sm':
  132. width = 600;
  133. break;
  134. case 'xs':
  135. width = 400;
  136. break;
  137. }
  138. const modal = this.modalService.create({
  139. nzTitle: config.title,
  140. nzContent: config.component,
  141. nzData: config.componentParams,
  142. nzClosable: true,
  143. nzWidth: width,
  144. nzOnOk: config.okCallBack,
  145. nzFooter: config.footer,
  146. nzStyle: config.style,
  147. nzMaskClosable: false
  148. });
  149. return modal;
  150. }
  151. public async post(url: string, method: 'put' | 'post' | 'get' | 'delete', params?: any) {
  152. switch (method) {
  153. case 'put':
  154. return this.http.put(url, params).subscribe(res => {});
  155. break;
  156. case 'post':
  157. return this.http.post(url, params).subscribe(res => {});
  158. break;
  159. case 'get':
  160. return this.http.get(url, params).subscribe(res => {});
  161. break;
  162. case 'delete':
  163. return this.http.delete(url, params).subscribe(res => {});
  164. break;
  165. }
  166. }
  167. }
  168. export class AppModalConfig {
  169. title?: string | TemplateRef<{}>;
  170. component?: string | TemplateRef<NzSafeAny> | Type<any>;
  171. componentParams?: any;
  172. okCallBack?: () => void;
  173. widthClass?: 'xxl' | 'xl' | 'lg' | 'sm' | 'xs';
  174. footer?: string | TemplateRef<{}> | Array<ModalButtonOptions<any>> | null;
  175. style?: Object;
  176. }