jy-safe-app/utils/checkappupdate.js

373 lines
8.5 KiB
JavaScript
Raw Permalink Normal View History

2025-10-14 15:17:30 +08:00
// checkappupdate.js
import config from '../config/common.js'
const $iconUrl = "/static/huojian2.png";
const $closeUrl = "/static/modalclose1.png";
const $mainColor = "#FF5B78";
function check(param = {}) {
if (!param.appurl) return false;
let datass = {
versionContent: param.versionContent,
versionNumber: param.versionNumber,
versionNow:param.versionNow,
updateType: 'solicit', //solicity
appurl: param.appurl,
apk:param.apk
}
updatePopup(datass, '')
}
function updatePopup(data, callback) {
// 弹窗遮罩层
let maskLayer = new plus.nativeObj.View("maskLayer", { //先创建遮罩层
top: '0px',
left: '0px',
height: '100%',
width: '100%',
backgroundColor: 'rgba(0,0,0,0.5)'
});
// 以下为计算菜单的nview绘制布局为固定算法使用者无关关心
const screenWidth = plus.screen.resolutionWidth;
const screenHeight = plus.screen.resolutionHeight;
//弹窗容器宽度
const popupViewWidth = screenWidth * 0.7;
// 弹窗容器的Padding
const viewContentPadding = 20;
// 弹窗容器的宽度
const viewContentWidth = parseInt(popupViewWidth - (viewContentPadding * 2));
// 描述的列表
const descriptionList = drawtext(data.versionContent, viewContentWidth);
// 弹窗容器高度
let popupViewHeight = 80 + 20 + 20 + 90 + 10 + 60 + 40;
let popupViewContentList = [{
src: $iconUrl,
id: "logo",
tag: "img",
position: {
top: "-70px",
left: "0px",
width: popupViewWidth + 'px',
height: (screenHeight / 3) + "px",
}
},
{
tag: 'font',
id: 'title',
text: "发现新版本",
textStyles: {
size: '22px',
color: "#5F94F9",
// weight: "bold",
whiteSpace: "normal"
},
position: {
top: '140px',
left: (viewContentPadding * 2) + "px",
width: viewContentWidth - viewContentPadding * 2 + "px",
height: "30px",
}
}
];
popupViewContentList.push({
tag: 'rect', //绘制版本号
rectStyles: {
radius: "3px",
color: "#5F94F9"
},
position: {
top: '180px',
left: ((viewContentWidth - 150) / 2 + viewContentPadding) + "px",
width: 150 + "px",
height: "20px"
}
})
popupViewContentList.push({
tag: 'font',
id: 'versionText',
text: 'V' + data.versionNow+ ' -> ' +'V' + data.versionNumber,
textStyles: {
size: '14px',
color: "#FFF",
lineSpacing: "0%",
},
position: {
top: '180px',
left: ((viewContentWidth - 150) / 2 + viewContentPadding) + "px",
width: 150 + "px",
height: "20px"
}
})
const textHeight = 18;
let contentTop = 190;
descriptionList.forEach((item, index) => {
if (index > 0) {
popupViewHeight += textHeight;
contentTop += textHeight;
}
popupViewContentList.push({
tag: 'font',
id: 'content' + index + 1,
text: item.content,
textStyles: {
size: '15px',
color: "#666",
lineSpacing: "50%",
align: "left",
},
position: {
top: (contentTop + 30) + "px",
left: viewContentPadding + "px",
width: viewContentWidth + "px",
height: textHeight + "px",
}
});
if (item.type == "break") {
contentTop += 10;
popupViewHeight += 10;
}
});
popupViewContentList.push({ // 立即升级按钮
// src: $gotoUrl,
id: "gotos",
// tag: "rect",
tag: 'rect',
rectStyles: {
radius: "20px",
color: "#5F94F9"
},
position: {
bottom: (viewContentPadding) + 'px',
left: (viewContentPadding * 2) + "px",
width: (viewContentWidth - viewContentPadding * 2) + "px",
height: "35px"
},
})
popupViewContentList.push({ // 立即升级按钮
// src: $gotoUrl,
id: "gotoText",
// tag: "rect",
tag: 'font',
text: '立即升级',
textStyles: {
size: '14px',
color: "#FFF",
lineSpacing: "0%",
},
position: {
bottom: (viewContentPadding) + 'px',
left: (viewContentPadding * 2) + "px",
width: (viewContentWidth - viewContentPadding * 2) + "px",
height: "35px"
},
})
// 弹窗内容
let popupView = new plus.nativeObj.View("popupView", { //创建底部图标菜单
tag: "rect",
top: (screenHeight - popupViewHeight) / 2 + "px",
left: '15%',
height: popupViewHeight + "px",
width: "70%"
});
// 绘制白色背景
popupView.drawRect({
color: "#ffffff",
radius: "8px"
}, {
top: "40px",
height: (popupViewHeight - 40) + "px",
});
popupView.draw(popupViewContentList);
// 绘制关闭按钮
const errViewWidth = screenWidth * 0.8;
let errViewHeight = 80 + 20 + 20 + 90 + 10 + 60;
let errTop = (screenHeight - errViewHeight) / 3.3 + "px"
let errView = new plus.nativeObj.View('errView', {
top: errTop,
left: errViewWidth,
height: '40px',
width: '100%'
},
[{
tag: 'img',
id: 'img',
src: $closeUrl,
position: {
top: '0px',
left: '0px',
width: '40px',
height: '40px'
},
}],
);
popupView.addEventListener("click", function(e) {
let maxTop = popupViewHeight - viewContentPadding;
let maxLeft = popupViewWidth - viewContentPadding;
let buttonWidth = (viewContentWidth - viewContentPadding) / 2;
if (e.clientY > maxTop - 30 && e.clientY < maxTop) {
if (e.clientX > viewContentPadding && e.clientX < maxLeft) {
maskLayer.hide();
popupView.hide();
errView.hide();
newUpdateDownload(data)
}
}
});
errView.addEventListener("click", function() { //处理点击关闭按钮点击
maskLayer.hide();
popupView.hide();
errView.hide();
});
// 显示弹窗
maskLayer.show();
popupView.show();
errView.show();
}
// 文字换行
function drawtext(text, maxWidth) {
let textArr = text.split("");
let len = textArr.length;
// 上个节点
let previousNode = 0;
// 记录节点宽度
let nodeWidth = 0;
// 文本换行数组
let rowText = [];
// 如果是字母,侧保存长度
let letterWidth = 0;
// 汉字宽度
let chineseWidth = 15;
// otherFont宽度
let otherWidth = 7;
for (let i = 0; i < len; i++) {
if (/[\u4e00-\u9fa5]|[\uFE30-\uFFA0]/g.test(textArr[i])) {
if (letterWidth > 0) {
if (nodeWidth + chineseWidth + letterWidth * otherWidth > maxWidth) {
rowText.push({
type: "text",
content: text.substring(previousNode, i)
});
previousNode = i;
nodeWidth = chineseWidth;
letterWidth = 0;
} else {
nodeWidth += chineseWidth + letterWidth * otherWidth;
letterWidth = 0;
}
} else {
if (nodeWidth + chineseWidth > maxWidth) {
rowText.push({
type: "text",
content: text.substring(previousNode, i)
});
previousNode = i;
nodeWidth = chineseWidth;
} else {
nodeWidth += chineseWidth;
}
}
} else {
if (/\n/g.test(textArr[i])) {
rowText.push({
type: "break",
content: text.substring(previousNode, i)
});
previousNode = i + 1;
nodeWidth = 0;
letterWidth = 0;
} else if (textArr[i] == "\\" && textArr[i + 1] == "n") {
rowText.push({
type: "break",
content: text.substring(previousNode, i)
});
previousNode = i + 2;
nodeWidth = 0;
letterWidth = 0;
} else if (/[a-zA-Z0-9]/g.test(textArr[i])) {
letterWidth += 1;
if (nodeWidth + letterWidth * otherWidth > maxWidth) {
rowText.push({
type: "text",
content: text.substring(previousNode, i + 1 - letterWidth)
});
previousNode = i + 1 - letterWidth;
nodeWidth = letterWidth * otherWidth;
letterWidth = 0;
}
} else {
if (nodeWidth + otherWidth > maxWidth) {
rowText.push({
type: "text",
content: text.substring(previousNode, i)
});
previousNode = i;
nodeWidth = otherWidth;
} else {
nodeWidth += otherWidth;
}
}
}
}
if (previousNode < len) {
rowText.push({
type: "text",
content: text.substring(previousNode, len)
});
}
return rowText;
}
// 新的下载
function newUpdateDownload(data) {
if (data.apk == 'true') {
var appurl = ''
if (plus.os.name === 'Android') {
appurl = data.appurl;
} else {
// 其它平台
// appurl = versionInfo.pathIos;
}
plus.runtime.openURL(appurl); // 跳转应用发布平台
}else{
uni.showLoading({
title: "下载资源包",
mask: true,
})
uni.downloadFile({ // 下载资源包
url: data.appurl,
success: (downloadResult) => {
uni.hideLoading()
if (downloadResult.statusCode ===
200) {
plus.runtime.install(downloadResult
.tempFilePath, { // 安装资源包
force: false
},
function() {
console.log(
'install success...'
);
plus.runtime
.restart(); // 重启APP
},
function(e) {
console.error(e,
'install fail...'
);
})
}
}
})
}
}
export default {
check
}