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";
export class CommonService {
constructor(public toastCtrl: ToastController, public alertCtrl: AlertController,
public loadCtrl: LoadingController,
private file: File, private fileOpener: FileOpener) {
}
/**
* JSON格式数据转化为字符串 接口调用
* @param data json格式的数据
* @returns {string}
*/
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数据
*/
/**
* 提示信息 位置:居中,延时2s
* @param message 提示文字
* @param callback 提示信息之后执行的方法
*/
toast(message, callback?) {
let toast = this.toastCtrl.create({
message: message,
dismissOnPageChange: true,
});
toast.present();
if (callback) {
callback();
}
}
let toast = this.toastCtrl.create({
message: message,
duration: duration,
/**
* 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();
}
}
if (callback) {
let alert = this.alertCtrl.create({
title: title,
message: message,
buttons: [
{
});
alert.present();
} else {
let alert = this.alertCtrl.create({
title: title,
message: message,
buttons: ["确定"]
});
alert.present();
}
}
/**
* 下载文件
* @param url 文件URL
*/
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");
let progress = Math.round(100.0 * ev.loaded / ev.total);
// alert(progress);
})
this.file.writeFile(path, fileName, blob, { //写入文件
replace: true
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
})
}
});
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;
}
/**
* 对象数组去重
*/
uniArr(arr,key) {
let temp = [], result = [];
arr.map((item) => {
if (!temp[item[key]]) {
result.push(item);
temp[item[key]] = true;
}
});
return result;
}