Newer
Older
import {Injectable} from "@angular/core";
import {AlertController, ToastController} from "@ionic/angular";
import {File} from "@ionic-native/file/ngx";
import {FileOpener} from "@ionic-native/file-opener/ngx";
6
7
8
9
10
11
12
13
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
48
49
50
51
52
@Injectable()
export class CommonService{
constructor(public toastCtrl:ToastController,public alertCtrl:AlertController,
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 提示信息之后执行的方法
*/
async toast(message, callback?) {
let toast = await this.toastCtrl.create({
message: message,
if (callback) {
callback();
}
}
/**
* alert弹窗
* @param message 弹窗内的文字
* @param callback 如果有回调方法 就有确定、取消两个按钮,没有回调方法 则只有确认一个按钮
*/
async alert(message, callback?) {
if (callback) {
let alert = await this.alertCtrl.create({
message: message,
buttons: ['取消', {
text: "确定",
handler: data => {
callback();
}
}]
});
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
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
}
}
/**
* 下载文件
* @param url 文件URL
*/
downloadFile(fileID,fileName) {
const xhr = new XMLHttpRequest();
const fileType = this.getFileMimeType(fileName);
const url = encodeURI(environment.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;
}
/**
* 定义全局的log日志 开发模式 打印log 生产环境 关闭
* @param message 日志信息
*/
log(message){
if(environment.domain === ""){ //开发环境时
console.log(message);
}
}
}