修改hotfix

This commit is contained in:
滕亚庆 2018-08-13 21:41:40 +08:00
parent 4bd5e6f5cb
commit 341f38d3a4
8 changed files with 1716 additions and 385 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
.DS_Store
node_modules/
/dist/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
/test/unit/coverage/
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
#Hbuilder
unpackage/debug/
unpackage/release/
/docs/.vuepress/dist

View File

@ -1,37 +1,3 @@
# hotfix[查看文档](https://tyaqing.github.io/mogo-h5plus/hotfix/introduction.html)
让您的Hbuilder APP应用快速拥有更新能力.
# 接入方式
## 引入方式
可以在`main.js`中添加`checkUpdate(URL);`,打开 app 就会自动检测.还可以放在`检查更新`的按钮上触发.
### ES6 Module 引入
首先在`page.json`把用到`checkUpdate`的页面加上管道`|plusReady`.
然后加载使用.
```js
import { checkUpdate } from "./utils/hotfix";
checkUpdate(URL); // 填入您检查api的url地址
```
### `<script>`方式引入
这种用于没有使用脚手架的开发者
```html
<title>APP</title>
<script src="html5plus://ready"></script> // 这段必须加载title底下
....
<script src="path/hotfix-bs.js"></script>
<script>
checkUpdate('https://api.hotfix.femirror.com/public/app/checkUpdate?bundleId=你的appId'); // 填入您检查api的url地址
</script>
```
如果您没有后端接入,可以使用`FEmirror云更新`
### 如果您有自己的后台版本管理,请按照文档配置使用即可

View File

@ -1,183 +0,0 @@
"use strict";
(function() {
var newVersion = void 0,
localVersion = void 0,
downloadUrl = void 0,
updateSilence = false;
// 检查更新
function checkUpdate(updateUrl) {
// 获取当前应用版本信息
getProperty()
.then(function(inf) {
console.log(JSON.stringify(inf));
localVersion = inf.version; //当前版本 // 获取版本信息
return ajax(updateUrl, {
version: plus.runtime.version, // 版本 用于统计
os: plus.os, //系统信息 用于统计
device: plus.device //设备信息 用于统计
});
})
.then(function(data) {
// 查看最新版本信息
newVersion = data.name;
// 如果版本相等
if (!compareVersion(newVersion, localVersion)) return false;
// 处理静默更新/提示更新
downloadUrl = data.android_url;
// 处理不同平台 如果只热更新安卓
if (data.platform !== "both") {
if (isAndroid()) {
if (data.platform !== "android") return false;
}
// 如果只热更新苹果
if (isIos()) {
if (data.platform !== "ios") return false;
}
}
// 如果是apk安装,是没法静默更新的
if (data.type !== "apk" && data.hotupdate_type === "silence") {
updateSilence = true;
downWgt(downloadUrl);
return false;
}
// 如果是苹果系统,且更新为安装则不做处理
if (isIos() && data.type === "apk") {
return false;
}
return confirm(data.description, data.title);
})
.then(function(selected) {
if (selected.index === 0) {
// 如果是苹果系统 然后是安装包
downWgt(downloadUrl);
}
})
.catch(function(error) {
console.log(error);
console.log(JSON.stringify(error));
//即使错误也不做任何处理
});
}
// 下载wgt文件
function downWgt(url) {
!updateSilence && plus.nativeUI.showWaiting("下载更新文件...");
plus.downloader
.createDownload(url, { filename: "_doc/update/" }, function(d, status) {
if (status == 200) {
console.log(2);
console.log("下载wgt成功" + d.filename);
installWgt(d.filename); // 安装wgt包
} else {
console.log(3);
console.log("下载wgt失败");
!updateSilence && plus.nativeUI.alert("下载更新文件失败!");
}
!updateSilence && plus.nativeUI.closeWaiting();
})
.start();
}
// 更新应用资源
function installWgt(path) {
!updateSilence && plus.nativeUI.showWaiting("安装更新文件...");
plus.runtime.install(
path,
{},
function() {
!updateSilence && plus.nativeUI.closeWaiting();
console.log("更新成功!");
!updateSilence && plus.nativeUI.toast("更新完成");
!updateSilence && plus.runtime.restart();
},
function(e) {
!updateSilence && plus.nativeUI.closeWaiting();
console.log("安装wgt文件失败[" + e.code + "]" + e.message);
!updateSilence &&
plus.nativeUI.alert("更新失败[" + e.code + "]" + e.message);
}
);
}
// 判断版本大小 a>=b : true 大于等于
function compareVersion(curV, reqV) {
if (curV && reqV) {
//将两个版本号拆成数字
var arr1 = curV.split("."),
arr2 = reqV.split(".");
var minLength = Math.min(arr1.length, arr2.length),
position = 0,
diff = 0;
//依次比较版本号每一位大小,当对比得出结果后跳出循环(后文有简单介绍)
while (
position < minLength &&
(diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0
) {
position++;
}
diff = diff != 0 ? diff : arr1.length - arr2.length;
//若curV大于reqV则返回true
return diff > 0;
} else {
//输入为空
console.log("版本号不能为空");
return false;
}
}
function isAndroid() {
var ua = navigator.userAgent;
return ua.match(/(Android);?[\s\/]+([\d.]+)?/);
}
function isIos() {
var ua = navigator.userAgent;
return ua.match(/(iPhone\sOS)\s([\d_]+)/);
}
function getProperty() {
return new Promise(function(resolve) {
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
resolve(inf);
});
});
}
function confirm(message) {
var title =
arguments.length > 1 && arguments[1] !== undefined
? arguments[1]
: "确认";
return new Promise(function(resolve) {
// plus.nativeUI.confirm(message, resolve, title, ["确认更新", "取消"]);
plus.nativeUI.confirm(message, resolve, {
title: title,
buttons: ["确认更新", "取消"],
verticalAlign: "bottom"
});
});
}
function ajax(url, data) {
return new Promise(function(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 {
reject(xhr);
}
}
};
});
}
window.checkUpdate = checkUpdate;
})();

168
hotfix.js
View File

@ -1,168 +0,0 @@
let newVersion,
localVersion,
downloadUrl,
updateSilence = false;
// 检查更新
export function checkUpdate(updateUrl) {
// 获取当前应用版本信息
getProperty()
.then(inf => {
localVersion = inf.version; //当前版本 // 获取版本信息
return ajax(updateUrl, {
version: plus.runtime.version, // 版本 用于统计
os: plus.os, //系统信息 用于统计
device: plus.device //设备信息 用于统计
});
})
.then(data => {
// 查看最新版本信息
newVersion = data.name;
// 如果版本相等
if (!compareVersion(newVersion, localVersion)) return false;
// 处理静默更新/提示更新
downloadUrl = data.android_url;
// 处理不同平台 如果只热更新安卓
if (data.platform !== "both") {
if (isAndroid()) {
if (data.platform !== "android") return false;
}
// 如果只热更新苹果
if (isIos()) {
if (data.platform !== "ios") return false;
}
}
// 如果是apk安装,是没法静默更新的
if (data.type !== "apk" && data.hotupdate_type === "silence") {
updateSilence = true;
downWgt(downloadUrl);
return false;
}
// 如果是苹果系统,且更新为安装则不做处理
if (isIos() && data.type === "apk") {
return false;
}
return confirm(data.description, data.title);
})
.then(selected => {
if (selected.index === 0) {
// 如果是苹果系统 然后是安装包
downWgt(downloadUrl);
}
})
.catch(error => {
console.log(error);
//即使错误也不做任何处理
});
}
// 下载wgt文件
function downWgt(url) {
!updateSilence && plus.nativeUI.showWaiting("下载更新文件...");
plus.downloader
.createDownload(url, { filename: "_doc/update/" }, function(d, status) {
if (status == 200) {
console.log("下载wgt成功" + d.filename);
installWgt(d.filename); // 安装wgt包
} else {
console.log("下载wgt失败");
!updateSilence && plus.nativeUI.alert("下载更新文件失败!");
}
!updateSilence && plus.nativeUI.closeWaiting();
})
.start();
}
// 更新应用资源
function installWgt(path) {
!updateSilence && plus.nativeUI.showWaiting("安装更新文件...");
plus.runtime.install(
path,
{},
function() {
!updateSilence && plus.nativeUI.closeWaiting();
console.log("更新成功!");
!updateSilence && plus.nativeUI.toast("更新完成");
!updateSilence && plus.runtime.restart();
},
function(e) {
!updateSilence && plus.nativeUI.closeWaiting();
console.log("安装wgt文件失败[" + e.code + "]" + e.message);
!updateSilence &&
plus.nativeUI.alert("更新失败[" + e.code + "]" + e.message);
}
);
}
// 判断版本大小 a>=b : true 大于等于
function compareVersion(curV, reqV) {
if (curV && reqV) {
//将两个版本号拆成数字
var arr1 = curV.split("."),
arr2 = reqV.split(".");
var minLength = Math.min(arr1.length, arr2.length),
position = 0,
diff = 0;
//依次比较版本号每一位大小,当对比得出结果后跳出循环(后文有简单介绍)
while (
position < minLength &&
(diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0
) {
position++;
}
diff = diff != 0 ? diff : arr1.length - arr2.length;
//若curV大于reqV则返回true
return diff > 0;
} else {
//输入为空
console.log("版本号不能为空");
return false;
}
}
function isAndroid() {
const ua = navigator.userAgent;
return ua.match(/(Android);?[\s\/]+([\d.]+)?/);
}
function isIos() {
const ua = navigator.userAgent;
return ua.match(/(iPhone\sOS)\s([\d_]+)/);
}
function getProperty() {
return new Promise(resolve => {
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
resolve(inf);
});
});
}
function confirm(message, title = "确认") {
return new Promise(resolve => {
// plus.nativeUI.confirm(message, resolve, title, ["确认更新", "取消"]);
plus.nativeUI.confirm(message, resolve, {
title: title,
buttons: ["确认更新", "取消"],
verticalAlign: "bottom"
});
});
}
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 {
reject(xhr);
}
}
};
});
}

1591
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "h5plus-hotfix",
"version": "1.0.0",
"description": "让您的Hbuilder APP应用快速拥有更新能力.",
"main": "src/hotfix.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tyaqing/hotfix.git"
},
"keywords": [],
"author": "ArH<626019610@qq.com>",
"license": "ISC",
"bugs": {
"url": "https://github.com/tyaqing/hotfix/issues"
},
"homepage": "https://github.com/tyaqing/hotfix#readme"
}

1
src/hotfix-bs.js Normal file
View 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
View 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);
});
});
}