Newer
Older
import {AlertController, LoadingController, ToastController} from "ionic-angular";
import {AppGlobal} from "../service/http.service";
import {File} from "@ionic-native/file";
import {FileOpener} from "@ionic-native/file-opener";
@Injectable()
export class CommonService{
constructor(public toastCtrl:ToastController,public alertCtrl:AlertController,
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
private file: File, private fileOpener: FileOpener){}
/**
* JSON格式数据转化为字符串 接口调用
* @param data json格式的数据
* @returns {string}
*/
toQuery(data){
let str = '';
for (let key in data) {
if (data.hasOwnProperty(key)) {
const value = data[key];
str += key + '=' + value + '&';
}
}
str = str.substring(0, str.length - 1);
return str;
}
/**
* JSON格式对象转为form表单的格式,用来接口传递数据
* @param json 需要转化的json
* @returns {FormData} formData数据
*/
toFormData(json){
let formData = new FormData();
for(let k in json){
formData.append(k,json[k])
}
return formData;
}
/**
* 提示信息 位置:居中,延时2s
* @param message 提示文字
* @param callback 提示信息之后执行的方法
*/
toast(message, callback?) {
let toast = this.toastCtrl.create({
message: message,
position:'middle',
dismissOnPageChange: true,
});
toast.present();
if (callback) {
callback();
}
}
//短暂提示提示
toastTime(message,duration) {
let toast = this.toastCtrl.create({
message: message,
duration: duration,
position:'middle',
dismissOnPageChange: true,
});
toast.present();
}
/**
* alert弹窗
* @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();
}
}
alert2(message,title,callback?){
if (callback) {
let alert = this.alertCtrl.create({
title: title,
message: message,
buttons: [
{
text: "确定",
handler: data => {
callback();
}
}]
});
alert.present();
} else {
let alert = this.alertCtrl.create({
title: title,
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
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
213
214
215
216
217
218
219
220
221
222
message: message,
buttons: ["确定"]
});
alert.present();
}
}
/**
* 下载文件
* @param url 文件URL
*/
downloadFile(fileID,fileName) {
const xhr = new XMLHttpRequest();
const fileType = this.getFileMimeType(fileName);
const url = encodeURI(AppGlobal.domain + '/wisdomgroup/modules/common/file/download/' + fileID);
xhr.open('GET',url);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.responseType = "blob";
xhr.addEventListener("loadstart",(ev)=>{
})
xhr.addEventListener("progress",(ev)=>{
let progress = Math.round(100.0 * ev.loaded / ev.total);
// alert(progress);
})
xhr.addEventListener("load",(ev)=>{
const blob = xhr.response;
if(blob){
let path = this.file.externalDataDirectory;
this.file.writeFile(path,fileName,blob,{ //写入文件
replace:true
}).then(
()=>{
this.fileOpener.open(path + fileName, fileType).catch((err) => {
this.alert('打开文件失败!'+err);
})
}).catch((err)=>{
this.toast("下载文件失败!")
})
}
});
xhr.addEventListener("loadend", (ev) => {
// 结束下载事件
});
xhr.addEventListener("error", (ev) => {
this.alert('下载文件失败!');
});
xhr.addEventListener("abort", (ev) => {
});
xhr.send();
}
//获取文件类型
getFileMimeType(fileName: string): string {
let fileType = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase();
let mimeType: string = '';
switch (fileType) {
case 'txt':
mimeType = 'text/plain';
break;
case 'docx':
mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'doc':
mimeType = 'application/msword';
break;
case 'pptx':
mimeType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
break;
case 'ppt':
mimeType = 'application/vnd.ms-powerpoint';
break;
case 'xlsx':
mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'xls':
mimeType = 'application/vnd.ms-excel';
break;
case 'zip':
mimeType = 'application/x-zip-compressed';
break;
case 'rar':
mimeType = 'application/octet-stream';
break;
case 'pdf':
mimeType = 'application/pdf';
break;
case 'jpg':
mimeType = 'image/jpeg';
break;
case 'png':
mimeType = 'image/png';
break;
default:
mimeType = 'application/' + fileType;
break;
}
return mimeType;
}