修改hotfix
This commit is contained in:
1
src/hotfix-bs.js
Normal file
1
src/hotfix-bs.js
Normal file
@@ -0,0 +1 @@
|
||||
"use strict"; (function () { function a(g, h) { return new Promise(function (i, j) { var k = plus.downloader.createDownload(g, { filename: "_doc/update/" }, function (l, m) { 200 == m ? (console.log("\u4E0B\u8F7D\u6587\u4EF6\u6210\u529F\uFF1A" + l.filename), i(l.filename)) : (console.log("\u4E0B\u8F7D\u6587\u4EF6\u5931\u8D25\uFF01"), j("\u4E0B\u8F7D\u6587\u4EF6\u5931\u8D25\uFF01")) }); k.addEventListener("statechanged", function (l) { h(Number.parseInt(100 * (l.downloadedSize / l.totalSize))) }), k.start() }) } function b(g) { return console.log(g), new Promise(function (h, i) { plus.runtime.install(g, { force: !0 }, function () { h() }, function (j) { return i(j, "installWgt") }) }) } function c(g, h) { return new Promise(function (i) { var k = new XMLHttpRequest; k.open("post", g, !0), k.setRequestHeader("content-type", "application/json"), k.send(JSON.stringify(h)), k.onreadystatechange = function () { 4 == k.readyState && (200 <= k.status && 300 > k.status ? i(JSON.parse(k.responseText)) : console.log(k)) } }) } function e() { return new Promise(function (g) { plus.runtime.getProperty(plus.runtime.appid, function (h) { g(h) }) }) } window.hotfix = function f(_ref) { var g = _ref.url, h = _ref.success, i = _ref.error, j = _ref.before, k = _ref.onProgress, l = void 0; e().then(function (m) { return console.log(m), console.log(plus.runtime.version), c(g, { version: m.version, os: plus.os, device: plus.device }) }).then(function (m) { return l = m, j(l) }).then(function () { return a(l.android_url, k) }).then(function (m) { return b(m) }).then(function () { h() }).catch(i) } })();
|
83
src/hotfix.js
Normal file
83
src/hotfix.js
Normal file
@@ -0,0 +1,83 @@
|
||||
export default function ({ url, success, error, before, onProgress }) {
|
||||
// 对环境的要求
|
||||
let resData;
|
||||
getProperty().then((inf) => {
|
||||
console.log(inf)
|
||||
console.log(plus.runtime.version)
|
||||
return ajax(url, {
|
||||
version: inf.version, // 版本 用于统计
|
||||
os: plus.os, //系统信息 用于统计
|
||||
device: plus.device //设备信息 用于统计
|
||||
})
|
||||
})
|
||||
.then(data => {
|
||||
// 获取到更新信息
|
||||
resData = data;
|
||||
return before(resData);
|
||||
}).then(() => {
|
||||
return downWgt(resData.android_url, onProgress);
|
||||
}).then((localPath) => {
|
||||
return installWgt(localPath);
|
||||
}).then(() => {
|
||||
success();
|
||||
}).catch(error)
|
||||
}
|
||||
// 下载wgt文件
|
||||
function downWgt(url, onProgress) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dtask = plus.downloader
|
||||
.createDownload(url, { filename: "_doc/update/" }, function (d, status) {
|
||||
if (status == 200) {
|
||||
console.log("下载文件成功:" + d.filename);
|
||||
resolve(d.filename)
|
||||
} else {
|
||||
console.log("下载文件失败!");
|
||||
reject('下载文件失败!');
|
||||
}
|
||||
})
|
||||
dtask.addEventListener("statechanged", function (task, status) {
|
||||
onProgress(Number.parseInt(task.downloadedSize / task.totalSize * 100));
|
||||
});
|
||||
dtask.start();
|
||||
})
|
||||
}
|
||||
// 安装应用资源
|
||||
function installWgt(path) {
|
||||
console.log(path)
|
||||
return new Promise((resove, reject) => {
|
||||
{
|
||||
plus.runtime.install(path, { force: true }, function () {
|
||||
resove()
|
||||
}, (err) => reject(err, 'installWgt'));
|
||||
}
|
||||
})
|
||||
}
|
||||
// 请求
|
||||
function ajax(url, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("post", url, true);
|
||||
// 设置请求头 告诉服务器发给他的数据是json格式
|
||||
xhr.setRequestHeader("content-type", "application/json");
|
||||
xhr.send(JSON.stringify(data));
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState == 4) {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve(JSON.parse(xhr.responseText));
|
||||
} else {
|
||||
console.log(xhr)
|
||||
// reject(xhr);
|
||||
// 避免网络错误
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
// 获取设备版本信息
|
||||
function getProperty() {
|
||||
return new Promise(resolve => {
|
||||
plus.runtime.getProperty(plus.runtime.appid, function (inf) {
|
||||
resolve(inf);
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user