|
|
@@ -24,11 +24,6 @@ export default new (class {
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 网络请求对象
|
|
|
- */
|
|
|
- private xhr = new XMLHttpRequest()
|
|
|
-
|
|
|
- /**
|
|
|
* 当前下载进度任务
|
|
|
*/
|
|
|
private progressTask = new Map()
|
|
|
@@ -46,7 +41,6 @@ export default new (class {
|
|
|
|
|
|
constructor() {
|
|
|
this.onPlusReady((plus) => {
|
|
|
- this.xhr = new plus.net.XMLHttpRequest()
|
|
|
this.systemInfo.os = plus.os.name
|
|
|
this.systemInfo.statusBarHeight = plus.navigator.getStatusbarHeight()
|
|
|
|
|
|
@@ -63,7 +57,7 @@ export default new (class {
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
plus.runtime.getProperty(plus.runtime.appid, (info: any) => {
|
|
|
this.systemInfo.version = info.version
|
|
|
this.systemInfo.versionCode = info.versionCode
|
|
|
@@ -194,30 +188,31 @@ export default new (class {
|
|
|
* @returns
|
|
|
*/
|
|
|
httpRequest(config: HttpRequestConfig) {
|
|
|
+ const xhr = this.hasPlus() ? new window.plus.net.XMLHttpRequest() : new XMLHttpRequest()
|
|
|
return new Promise<any>((resolve, reject) => {
|
|
|
- this.xhr.responseType = config.responseType ?? 'json'
|
|
|
+ xhr.responseType = config.responseType ?? 'json'
|
|
|
if (config.header) {
|
|
|
for (const key in config.header) {
|
|
|
- this.xhr.setRequestHeader(key, config.header[key])
|
|
|
+ xhr.setRequestHeader(key, config.header[key])
|
|
|
}
|
|
|
}
|
|
|
- this.xhr.onreadystatechange = () => {
|
|
|
- if (this.xhr.readyState === 4) {
|
|
|
- if (this.xhr.status == 200) {
|
|
|
+ xhr.onreadystatechange = () => {
|
|
|
+ if (xhr.readyState === 4) {
|
|
|
+ if (xhr.status == 200) {
|
|
|
resolve({
|
|
|
code: 200,
|
|
|
- data: this.xhr.response
|
|
|
+ data: xhr.response
|
|
|
})
|
|
|
} else {
|
|
|
reject({
|
|
|
- code: this.xhr.status,
|
|
|
- message: this.xhr.statusText
|
|
|
+ code: xhr.status,
|
|
|
+ message: xhr.statusText
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- this.xhr.open(config.method ?? 'GET', config.url)
|
|
|
- this.xhr.send()
|
|
|
+ xhr.open(config.method ?? 'GET', config.url)
|
|
|
+ xhr.send()
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -408,19 +403,35 @@ export default new (class {
|
|
|
*/
|
|
|
getLocalFileContent(filePath: string) {
|
|
|
return new Promise<any>((resolve, reject) => {
|
|
|
- this.onPlusReady((plus) => {
|
|
|
- plus.io.resolveLocalFileSystemURL('_www/' + filePath, (entry: any) => {
|
|
|
- entry.file((file: any) => {
|
|
|
- const fileReader = new plus.io.FileReader()
|
|
|
- fileReader.readAsText(file, 'utf-8')
|
|
|
- fileReader.onloadend = (evt: any) => {
|
|
|
- resolve(evt.target.result)
|
|
|
- }
|
|
|
+ if (this.hasPlus()) {
|
|
|
+ this.onPlusReady((plus) => {
|
|
|
+ plus.io.resolveLocalFileSystemURL('_www/' + filePath, (entry: any) => {
|
|
|
+ entry.file((file: any) => {
|
|
|
+ const fileReader = new plus.io.FileReader()
|
|
|
+ fileReader.readAsText(file, 'utf-8')
|
|
|
+ fileReader.onloadend = (evt: any) => {
|
|
|
+ const data = evt.target.result
|
|
|
+ try {
|
|
|
+ // 尝试解析内容
|
|
|
+ resolve(JSON.parse(data))
|
|
|
+ } catch (e) {
|
|
|
+ resolve(data)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }, (e: any) => {
|
|
|
+ reject(e.message)
|
|
|
})
|
|
|
- }, (e: any) => {
|
|
|
- reject(e.message)
|
|
|
})
|
|
|
- })
|
|
|
+ } else {
|
|
|
+ this.httpRequest({
|
|
|
+ url: filePath
|
|
|
+ }).then((res) => {
|
|
|
+ resolve(res.data)
|
|
|
+ }).catch((res) => {
|
|
|
+ reject(res.message)
|
|
|
+ })
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
|