import http from './request.js';
|
import {
|
session,
|
} from "@/comm/utils.js"
|
|
|
export const getVersion = (type) => {
|
|
return new Promise((resolve, reject) => {
|
const updateUrl = getApp().globalData.updateUrl || ""
|
const rootUrl = updateUrl.trim()
|
if (!rootUrl) {
|
resolve({})
|
return
|
}
|
var url = rootUrl + '/esgo/version.json';
|
|
uni.request({
|
url: url, // 指定JS文件的完整路径
|
method: 'GET',
|
success(res) {
|
if (res && res.statusCode === 200) {
|
var ret = res.data
|
if (typeof ret == 'string') {
|
try {
|
try {
|
ret = JSON.parse(ret.replace(/\\"/g, '"'));
|
} catch (ex) {
|
ret = JSON.parse(ret.replace(/\\/g, '\\\\'))
|
}
|
} catch (ex) {
|
console.log(ex)
|
}
|
}
|
//获取当前版本号
|
let verInfo = ret[type]; //更新包名称
|
if (!verInfo) {
|
resolve({})
|
}
|
// let versionInfo = verName.split('_');
|
// let verNum = versionInfo[2].replace(".apk", ""); //版本编码
|
// let verCode = versionInfo[2].replace(".apk", "").replace(".", "").replace(".", ""); //服务端版本号
|
if (verInfo.file_name) {
|
verInfo.dld_url = rootUrl + "/esgo/" + verInfo.file_name
|
}
|
|
resolve(verInfo);
|
} else {
|
// return reject("访问失败,状态码:" + res.statusCode);
|
resolve({})
|
}
|
|
},
|
fail: (err) => {
|
return reject(err);
|
},
|
})
|
})
|
}
|
export const getAppVersion = async () => {
|
//return getVersion("app")
|
const rootUrl = getApp().globalData.updateUrl || ""
|
var header = {
|
"Content-Type": "application/json;charset=UTF-8"
|
};
|
var url = `${rootUrl}/api/list_files/`;
|
try {
|
return await http.request({
|
method: "POST",
|
url,
|
header,
|
data: {
|
is_app: 1
|
}
|
})
|
} catch (ex) {
|
console.log("getAppVersion", ex)
|
}
|
|
}
|
export const getServerVersion = async() => {
|
//return getVersion("server")
|
const rootUrl = getApp().globalData.updateUrl || ""
|
var header = {
|
"Content-Type": "application/json;charset=UTF-8"
|
};
|
var url = `${rootUrl}/api/list_files/`;
|
try {
|
return await http.request({
|
method: "POST",
|
url,
|
header,
|
data: {
|
is_app: 0
|
}
|
})
|
} catch (ex) {
|
console.log("getServerVersion", ex)
|
}
|
|
}
|
|
/**
|
* GET api/download/
|
* 下载更新包(服务器接口)
|
* @param
|
* @returns
|
*/
|
|
export const downloadUpdatePackage = (fileName, cbSuccess, cbProgress, cbFail) => {
|
const rootUrl = getApp().globalData.updateUrl || ""
|
var url = `${rootUrl}/api/download/?file_name=${encodeURI(fileName)}`;
|
console.log(url)
|
const downloadTask = uni.downloadFile({
|
url: url,
|
header: {
|
'content-type': 'application/octet-stream',
|
},
|
success: (result) => {
|
console.log("downloadFile", result)
|
const now = new Date()
|
const date = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
|
if (result.statusCode === 200) {
|
http.addLog({
|
date,
|
method: `GET`,
|
url: url,
|
param: {},
|
statusCode: result.statusCode,
|
data: result
|
})
|
cbSuccess(result.tempFilePath)
|
} else {
|
|
http.addLog({
|
date,
|
method: `GET`,
|
url: url,
|
param: {},
|
statusCode: result.statusCode,
|
data: result.data
|
})
|
cbFail(result.data || '下载未知错误!');
|
}
|
},
|
fail: (result) => {
|
console.log("downloadTaskFile fail", result)
|
const now = new Date()
|
const date = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
|
http.addLog({
|
date,
|
method: `GET`,
|
url: url,
|
param: {},
|
statusCode: -1,
|
data: result
|
})
|
cbFail('下载文件失败!');
|
}
|
})
|
downloadTask.onProgressUpdate((res) => {
|
|
cbProgress(res, downloadTask)
|
});
|
return downloadTask
|
}
|
/**
|
* GET api/upload/
|
* 上传日志包( 服务器接口):
|
* @param
|
* @returns
|
*/
|
|
export const uploadLogPackage = (filePath, cbSuccess, cbProgress, cbFail) => {
|
const rootUrl = getApp().globalData.updateUrl || ""
|
var url = `${rootUrl}/api/upload/`;
|
console.log("uploadLogPackage", url, filePath)
|
const uploadTask = uni.uploadFile({
|
url,
|
name: "file",
|
filePath: filePath,
|
success: (result) => {
|
const now = new Date()
|
const date = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
|
console.log("uploadFile", result)
|
http.addLog({
|
date,
|
method: `POST`,
|
url: url,
|
param: {
|
file: filePath
|
},
|
statusCode: 200,
|
data: result
|
})
|
cbSuccess()
|
},
|
fail: (result) => {
|
console.log(result)
|
const now = new Date()
|
const date = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
|
http.addLog({
|
date,
|
method: `GET`,
|
url: url,
|
param: {
|
file: filePath
|
},
|
statusCode: -1,
|
data: result
|
})
|
cbFail('上传文件失败!');
|
},
|
})
|
uploadTask.onProgressUpdate((res) => {
|
console.log(res)
|
cbProgress(res, uploadTask)
|
});
|
}
|