UNPKG

6.34 kBPlain TextView Raw
1import { HttpClient, HttpHeaders } from '@angular/common/http';
2import { Observable, of, throwError } from 'rxjs';
3import { catchError, map, tap } from 'rxjs/operators';
4
5import { PagedQueryResult, PagedQueryParam } from './models';
6import { Utils } from './utils';
7
8declare var isRunning: number; //用于记录有多少ajax请求正在运行
9declare var lastActiveTime: Date; // 用于记录最后活动时间
10export abstract class BaseService {
11 protected headers = new HttpHeaders({ 'Content-Type': 'application/json' });
12
13 constructor(private http: HttpClient) {
14 lastActiveTime = new Date();
15 }
16 /**
17 * 分页查询
18 * 参数:url URL地址, queryParam:用于查询的参数。waitLoading: 是否要显示等待加载(缺省true)
19 */
20 protected searchPage(url: string, queryParam: PagedQueryParam, waitLoading: boolean = true): Observable<PagedQueryResult<any>> {
21 if (waitLoading) {
22 isRunning++;
23 }
24
25 if (!queryParam.pageNumber) {
26 queryParam.pageNumber = 1;
27 }
28 if (!queryParam.pageSize) {
29 queryParam.pageSize = 10;
30 }
31 if (url.indexOf("?") < 0) {
32 url += "?x=1";
33 }
34 url += this.toQueryString(queryParam, "");
35 return this.http.get<PagedQueryResult<any>>(url).pipe(
36 tap(response => {
37 if (waitLoading) {
38 isRunning--;
39 }
40 lastActiveTime = new Date();
41 let rst: PagedQueryResult<any> = response;
42 this.processDate(rst.datas);
43 }),
44 catchError(err => this.handleError(err, waitLoading))
45 );
46 }
47 /**
48 * 把输入的Object转换成请求参数串。
49 * 如 obj = {a:1, b:[{i:3, k:4},{i:5}],则返回的请求串为:
50 * "&a=1&b[0].i=3&b[0].k=4&b[1].i=6"
51 *
52 */
53 toQueryString(obj: any, propPrefix: string): string {
54 let url = "";
55 let preParam = propPrefix;
56 if (preParam) {
57 preParam += ".";
58 }
59 if (obj == undefined) {
60 return url;
61 }
62 if (typeof obj == 'object') {
63 if (obj instanceof Array) {
64 for (let i = 0; i < obj.length; i++) {
65 if (typeof (obj[i]) == 'object') {
66 url = url + this.toQueryString(obj[i], propPrefix + "[" + i + "]");
67 } else {
68 url = url + "&" + propPrefix + "[" + i + "]" + "=" + this.formatValue(obj[i]);
69 }
70 }
71 } else {
72 for (let prop in obj) {
73 if (prop != "constructor") {
74 let pv = obj[prop];
75 if (pv) {
76 if (typeof pv == 'function') continue;
77 if (typeof pv == 'object') {
78 url = url + this.toQueryString(pv, preParam + prop);
79 } else {
80 url = url + "&" + preParam + prop + "=" + this.formatValue(obj[prop]);
81 }
82 }
83 }
84 }
85 }
86 } else {
87 url = url + "&" + propPrefix + "=" + this.formatValue(obj);
88 }
89 return url;
90 }
91 private formatValue(val: any) {
92 if (val instanceof Date) {
93 return val.getTime();
94 }
95 return encodeURI(val);
96 }
97 /**
98 * GET请求
99 * @param url URL地址
100 * @param data:用于查询的参数
101 * @param waitLoading: 是否要显示等待加载(缺省true)
102 */
103 protected doGet(url: string, data?: any, waitLoading: boolean = true): Observable<any> {
104 if (data) {
105 if (url.indexOf("?") < 0) {
106 url += "?a=1";
107 }
108 }
109 url += this.toQueryString(data, "");
110 if (waitLoading) {
111 isRunning++;
112 }
113 return this.http.get(url)
114 .pipe(
115 tap(resp => {
116 if (waitLoading) {
117 isRunning--;
118 }
119 lastActiveTime = new Date();
120 this.processDate(resp);
121 }),
122 catchError(err => this.handleError(err, waitLoading)));
123 }
124
125 /**
126 * PUT请求
127 * @param url URL地址,
128 * @param data:发送到服务器的数据
129 * @param waitLoading: 是否要显示等待加载(缺省true)
130 */
131 protected doPut(url: string, data: any, waitLoading: boolean = true): Observable<any> {
132 if (waitLoading) {
133 isRunning++;
134 }
135
136 return this.http
137 .put(url, data, { headers: this.headers })
138 .pipe(
139 tap(resp => {
140 if (waitLoading) {
141 isRunning--;
142 }
143 lastActiveTime = new Date();
144 this.processDate(resp);
145 }),
146 catchError(err => this.handleError(err, waitLoading)));
147 }
148 /**
149 * POST请求
150 * @param url URL地址
151 * @param data:要post的数据
152 * @param waitLoading: 是否要显示等待加载(缺省true)
153 *
154 */
155 protected doPost(url: string, data: any, waitLoading: boolean = true): Observable<any> {
156 if (waitLoading) {
157 isRunning++;
158 }
159
160 return this.http
161 .post(url, JSON.stringify(data), { headers: this.headers })
162 .pipe(
163 tap(response => {
164 if (waitLoading) {
165 isRunning--;
166 }
167 lastActiveTime = new Date();
168 this.processDate(response);
169 return response;
170 }),
171 catchError(err => this.handleError(err, waitLoading)));
172 }
173 /**
174 * DELETE请求
175 * @param url URL地址,
176 * @param waitLoading: 是否要显示等待加载(缺省true)
177 */
178 protected doDelete(url: string, waitLoading: boolean = true): Observable<any> {
179 if (waitLoading) {
180 isRunning++;
181 }
182 return this.http.delete(url, { headers: this.headers })
183 .pipe(
184 tap(response => this.processResponse(response, waitLoading)),
185 catchError(err => this.handleError(err, waitLoading)));
186 }
187 protected processResponse(response: any, waitLoading: boolean) {
188 if (waitLoading) {
189 isRunning--;
190 }
191 lastActiveTime = new Date();
192 return this.processDate(response);
193 }
194 protected processDate(rst: any): any {
195 if (rst) {
196 if (Array.isArray(rst)) {
197 for (let r of rst) {
198 this.processDate(r);
199 }
200 } else {
201 for (let p in rst) {
202 if (typeof (rst[p]) == "object") {
203 this.processDate(rst[p]);
204 } else if (p.endsWith("Date") || p.endsWith("Time") || p.endsWith("Timestamp")) {
205 if (typeof (rst[p]) == "number") {
206 rst[p] = new Date(rst[p]);
207 } else if (rst[p].length == 28 && rst[p].indexOf("T") == 10) {//"2017-11-29T12:18:54.921+0000"
208 rst[p] = new Date(rst[p]);
209 }
210 }
211 }
212 }
213 }
214 return rst;
215 }
216 protected handleError(error: any, waitLoading: boolean): Observable<any> {
217 //console.error('An error occurred:'+ error);
218 if (waitLoading) {
219 isRunning--;
220 }
221 lastActiveTime = new Date();
222 let msg = Utils.resolveJsonErrorMessage(error);
223 return throwError(msg);
224 }
225}
\No newline at end of file