Newer
Older
import {LoadingController, AlertController, ToastController} from 'ionic-angular';
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import {Observable} from "rxjs";
@Injectable()
export class AppGlobal {
//缓存key的配置
static cache: any = {
slides: "_dress_slides",
categories: "_dress_categories",
products: "_dress_products"
};
//接口基地址
// static defaultVersion = "4.7.0"; //dev
static defaultVersion; //prod
//接口地址
static API: any = {
getCategories: '/api/ionic3/getCategories',
getLogin: '/app/loginpost',
getDetails: '/api/ionic3/details'
};
}
@Injectable()
export class AppService {
constructor(public http: Http,
public loadingCtrl: LoadingController,
private alertCtrl: AlertController,
private toastCtrl: ToastController,) {
}
// 对参数进行编码
encode(params, flag) {
var str = '';
if (params) {
if (flag == 'get') { //get /a/b
for (var key in params) {
if (params.hasOwnProperty(key)) {
var value = params[key];
str += value + '/';
}
}
str = '/' + str.substring(0, str.length - 1);
}
if (flag == 'post') { //post a=b&c=d
str += key + '=' + value + '&';
}
}
str = str.substring(0, str.length - 1);
}
}
return str;
}
//get请求
ObserverHttpGet(url, params): Observable<any> {
return this.http.get(AppGlobal.domain + url + this.encode(params, "get")) //app
}
//get请求
ObserverHttpGetData(url, params): Observable<any> {
}
//get请求
ObserverHttpGetAdd(url, params): Observable<any> {
return this.http.get(AppGlobal.domain + url + params) //app
}
//get请求带?的
ObserverHttpGetOption(url, params): Observable<any> {
return this.http.get(AppGlobal.domain + url, {params: params}); //app
}
//get请求带pararms ,body 的
ObserverHttpGetAddBody(url, params, body): Observable<any> {
return this.http.post(AppGlobal.domain + url + params, null, { //本地
params: body,
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
headers: new Headers({
// "Accept": "application/json",
"Content-Type": "application/json"
// 'Content-Type':'application/x-www-form-urlencoded,charset=UTF-8'
})
})
}
//delete
ObserverHttpDetelete(url, params): Observable<any> {
return this.http.delete(AppGlobal.domain + url + this.encode(params, "get")) //app
}
//delete
ObserverHttpDeteleteOption(url, params): Observable<any> {
return this.http.delete(AppGlobal.domain + url + params) //app
}
//delete
ObserverHttpDeteleteData(url, params, data): Observable<any> {
return this.http.delete(AppGlobal.domain + url + params, { //app
params: this.encode(data, 'post'),
headers: new Headers({
"Content-Type": "application/json"
})
}) //本地
}
ObserverHttpPostData(url, params) {
return this.http.post(AppGlobal.domain + url, params, { //app
headers: new Headers({
"Content-Type": "application/json"
})
})
}
ObserverHttpPostAdd(url, params) {
return this.http.post(AppGlobal.domain + url + params, { //app
headers: new Headers({
"Content-Type": "application/json"
})
})
}
ObserverHttpPostOption(url, data) {
return this.http.post(AppGlobal.domain + url, null, { //app
params: data,
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded,charset=UTF-8'
})
});
}
//post请求
ObserverHttpForm(url, params, body) {
return this.http.post(AppGlobal.domain + url + params, null, {
params: body,
headers: new Headers({
// "Accept": "application/json",
// "Content-Type": "application/json"
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
})
})
}
//post请求
ObserverHttpPost(url, params) {
return this.http.post(AppGlobal.domain + url, null, { //app
params: this.encode(params, 'post'),
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
})
})
}
//post请求
ObserverHttpNoForm(url, params) {
return this.http.post(AppGlobal.domain + url, null, { //app
params: params,
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
})
})
}
//put请求
ObserverHttpPut(url, params, data) {
return this.http.put(AppGlobal.domain + url + params, data, { //app
headers: new Headers({
"Content-Type": "application/json"
})
})
}
private handleError(error: Response | any) {
let msg = '';
if (error.status == 400) {
msg = '请求无效(code:404)';
console.log('请检查参数类型是否匹配');
}
if (error.status == 404) {
msg = '请求资源不存在(code:404)';
console.error(msg + ',请检查路径是否正确');
}
if (error.status == 500) {
msg = '服务器发生错误(code:500)';
console.error(msg + ',请检查路径是否正确');
}
console.log(error);
if (msg != '') {
this.toast(msg);
}
}
toast(message, callback?) {
let toast = this.toastCtrl.create({
message: message,
duration: 2000,
dismissOnPageChange: true,
});
toast.present();
if (callback) {
callback();
}
}
//position:top, bottom and middle
popToastView(message: string, position: string, duration: number) {
this.toastCtrl.create({
message: message,
position: position,
duration: duration,
}).present();
}
/**
*
* @param message
* @param callback
*/
alert(message, callback?) {
if (callback) {
let alert = this.alertCtrl.create({
title: '提示',
message: message,
buttons: ['取消', {
text: "确定",
handler: data => {
callback();
}
}]
});
alert.present();
} else {
let alert = this.alertCtrl.create({
title: '提示',
message: message,
buttons: ["确定"]
});
alert.present();
}
}
}