base.service.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { HttpClient, HttpParams } from '@angular/common/http';
  17. import { NzSafeAny } from 'ng-zorro-antd/core/types';
  18. import { Observable } from 'rxjs';
  19. import { Message } from '../entity/Message';
  20. import { PageResults } from '../entity/PageResults';
  21. const nullValueHandling = 'include';
  22. const dateValueHandling = 'timestamp';
  23. export class BaseService<T> {
  24. http: HttpClient;
  25. server: {
  26. urls: {
  27. base: string;
  28. fetch: string;
  29. get: string;
  30. load: String;
  31. add: string;
  32. update: string;
  33. delete: string;
  34. tree: string;
  35. member: string;
  36. memberOut: string;
  37. };
  38. } = {
  39. urls: {
  40. base: '',
  41. fetch: '/fetch',
  42. add: '/add',
  43. get: '/get',
  44. load: '/load',
  45. update: '/update',
  46. delete: '/delete',
  47. tree: '/tree',
  48. member: '/member',
  49. memberOut: '/memberOut'
  50. }
  51. };
  52. constructor(httpClient: HttpClient, baseURL: string) {
  53. this.server.urls.base = baseURL;
  54. this.http = httpClient;
  55. }
  56. fetch(params: NzSafeAny, fetchURL?: string): Observable<Message<PageResults>> {
  57. let _fetchURL = '';
  58. if (fetchURL) {
  59. _fetchURL = fetchURL;
  60. } else {
  61. _fetchURL = this.server.urls.base + this.server.urls.fetch;
  62. }
  63. return this.http.get<Message<PageResults>>(_fetchURL, { params: this.parseParams(params) });
  64. }
  65. member(params: NzSafeAny): Observable<Message<PageResults>> {
  66. return this.http.get<Message<PageResults>>(this.server.urls.base + this.server.urls.member, {
  67. params: this.parseParams(params)
  68. });
  69. }
  70. memberOut(params: NzSafeAny): Observable<Message<PageResults>> {
  71. return this.http.get<Message<PageResults>>(this.server.urls.base + this.server.urls.memberOut, {
  72. params: this.parseParams(params)
  73. });
  74. }
  75. get(id: String): Observable<Message<T>> {
  76. if (id === null || id === '') {
  77. return this.http.get<Message<T>>(`${this.server.urls.base + this.server.urls.get}`);
  78. } else {
  79. return this.http.get<Message<T>>(`${this.server.urls.base + this.server.urls.get}/${id}`);
  80. }
  81. }
  82. getByParams(params: NzSafeAny, getURL?: string): Observable<Message<T>> {
  83. let _getURL = '';
  84. if (getURL) {
  85. _getURL = getURL;
  86. } else {
  87. _getURL = `${this.server.urls.base + this.server.urls.get}`;
  88. }
  89. return this.http.get<Message<T>>(_getURL, { params: this.parseParams(params) });
  90. }
  91. add(body: any): Observable<Message<T>> {
  92. return this.http.post<Message<T>>(`${this.server.urls.base + this.server.urls.add}`, body);
  93. }
  94. update(body: any): Observable<Message<T>> {
  95. return this.http.put<Message<T>>(`${this.server.urls.base + this.server.urls.update}`, body);
  96. }
  97. delete(ids: String): Observable<Message<T>> {
  98. return this.http.delete<Message<T>>(`${this.server.urls.base + this.server.urls.delete}?ids=${ids}`);
  99. }
  100. tree(params: NzSafeAny): Observable<Message<any>> {
  101. return this.http.get<Message<any>>(this.server.urls.base + this.server.urls.tree, {
  102. params: this.parseParams(params)
  103. });
  104. }
  105. parseParams(params: NzSafeAny): HttpParams {
  106. const newParams: NzSafeAny = {};
  107. if (params instanceof HttpParams) {
  108. return params;
  109. }
  110. Object.keys(params).forEach(key => {
  111. let _data = params[key];
  112. // 忽略空值
  113. if (_data == null) {
  114. } else {
  115. // 将时间转化为:时间戳 (秒)
  116. if (dateValueHandling === 'timestamp' && _data instanceof Date) {
  117. _data = _data.valueOf();
  118. }
  119. newParams[key] = _data;
  120. }
  121. });
  122. return new HttpParams({ fromObject: newParams });
  123. }
  124. }