Newer
Older
import { LoadingController, AlertController, ToastController } from 'ionic-angular';
import { Injectable } from '@angular/core';
import {Observable} from "rxjs";
@Injectable() export class AppGlobal {
//缓存key的配置
static cache: any = {
slides: "_dress_slides",
categories: "_dress_categories",
products: "_dress_products"
}
//接口基地址 //测试环境
// static domain = "http://180.153.158.250:3306";
static domain = "http://180.168.156.212:2931";
// static picture = "http://220.248.107.115:2239/wisdomgroup/manager/getIcon/";
// static picture = "http://180.153.158.250:3306/wisdomgroup/manager/getIcon/";
static picture = "http://180.168.156.212:2931/wisdomgroup/manager/getIcon/";
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//接口地址
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
for (var key in params) {
if (params.hasOwnProperty(key)) {
var value = params[key];
//str += encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&';
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
return this.http.get(url+this.encode(params,"get")) //本地
//get请求
ObserverHttpGetData(url,params): Observable<any>{
// return this.http.get(AppGlobal.domain+url+this.encode(params,"get")) //app
return this.http.get(url ,params) //本地
}
//get请求带?的
ObserverHttpGetOption(url,params): Observable<any>{
// return this.http.get(AppGlobal.domain+url+this.encode(params,"get")) //app
return this.http.get(url ,{
params:params
}); //本地
}
//delete
ObserverHttpDetelete(url,params): Observable<any>{
// return this.http.get(AppGlobal.domain+url+this.encode(params,"get")) //app
return this.http.delete(url+this.encode(params,"get")) //本地
}
ObserverHttpPostData(url, params) {
// return this.http.post(AppGlobal.domain+url,params,{ //app
return this.http.post(url,params,{ //本地
headers: new Headers({
// "Accept": "application/json",
"Content-Type": "application/json"
// 'Content-Type':'application/x-www-form-urlencoded,charset=UTF-8'
})
})
}
ObserverHttpPostForm(url, params) {
console.log('form');
// return this.http.post(AppGlobal.domain+url,params,{ //app
return this.http.post(url,null,{ //本地
params: params,
// "Content-Type": "application/json"
'Content-Type':'application/x-www-form-urlencoded,charset=UTF-8'
// return this.http.post(AppGlobal.domain+url,null,{ //app
return this.http.post(url,null,{ //本地
params: this.encode(params,'post'),
headers: new Headers({
// "Accept": "application/json",
//"Content-Type": "application/json,charset=UTF-8"
'Content-Type':'application/x-www-form-urlencoded,charset=UTF-8'
})
})
}
//put请求
ObserverHttpPut(url, params,data) {
// return this.http.post(AppGlobal.domain+url,null,{ //app
return this.http.put(url + data,params,{ //本地
headers: new Headers({
// "Accept": "application/json",
"Content-Type": "application/json"
// 'Content-Type':'application/x-www-form-urlencoded,charset=UTF-8'
})
})
}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
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,
//showCloseButton:true,
//closeButtonText:"关闭"
}).present();
}
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();
}
}
}