Newer
Older
import {LoadingController, AlertController, ToastController} from 'ionic-angular';
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
//缓存key的配置
static cache: any = {
slides: "_dress_slides",
categories: "_dress_categories",
products: "_dress_products"
}
//接口基地址 //测试环境
// static domain = "http://220.248.107.115:2239";
//正式环境
// static domain = "http://180.153.158.250:3306";
static domain = "http://180.168.156.212:2931";
//static domain = "http://localhost:8080"
//乔坤
//static domain = "http://10.10.202.54:8080";220.248.107.115:2239
//测试
// 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/";
//接口地址
static API: any = {
getCategories: '/api/ionic3/getCategories',
getLogin: '/app/loginpost',
getDetails: '/api/ionic3/details'
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 + '&';
//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> {
//get请求
ObserverHttpGetAdd(url, params): Observable<any> {
// return this.http.get(AppGlobal.domain+url+ params) //app
return this.http.get(url+ 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")) //本地
}
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'
return this.http.post(url, null,{
params:data,
headers: new Headers({
// "Accept": "application/json",
// "Content-Type": "application/json"
'Content-Type': 'application/x-www-form-urlencoded,charset=UTF-8'
})
});
}
//post请求
ObserverHttpPost(url, params) {
// 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'
})
})
}
headers: new Headers({
// "Accept": "application/json",
"Content-Type": "application/json"
// 'Content-Type':'application/x-www-form-urlencoded,charset=UTF-8'
})
})
}
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,
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,
text: "确定",
handler: data => {
callback();
}
}]
});
alert.present();
} else {
let alert = this.alertCtrl.create({
title: '提示',