appUpdate.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. // #ifdef APP-PLUS
  2. import componentConfig from "@/common/updateConfig"
  3. const platform = uni.getSystemInfoSync().platform;
  4. // 主颜色
  5. const $mainColor = componentConfig.appUpdateColor ? componentConfig.appUpdateColor : "FF5B78";
  6. // 弹窗图标url
  7. const $iconUrl = componentConfig.appUpdateIcon ? componentConfig.appUpdateIcon : "/uni_modules/APPUpdate/static/logo.png";
  8. // 获取当前应用的版本号
  9. export const getCurrentNo = function(callback) {
  10. // 获取本地应用资源版本号
  11. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  12. callback && callback({
  13. versionCode: inf.versionCode,
  14. versionName: inf.version
  15. });
  16. });
  17. }
  18. // 从服务器下载应用资源包(wgt文件)
  19. const getDownload = function(data) {
  20. let dtask;
  21. if(data.updateType == 'forcibly' || data.updateType == 'solicit'){
  22. let popupData = {
  23. progress: true,
  24. buttonNum: 2
  25. };
  26. if(data.updateType == 'forcibly'){
  27. popupData.buttonNum = 0;
  28. }
  29. let lastProgressValue = 0;
  30. let popupObj = downloadPopup(popupData);
  31. dtask = plus.downloader.createDownload(data.downloadUrl, {
  32. filename: "_doc/update/"
  33. }, function(download, status) {
  34. if (status == 200) {
  35. popupObj.change({
  36. progressValue: 100,
  37. progressTip:"正在安装文件...",
  38. progress: true,
  39. buttonNum: 0
  40. });
  41. plus.runtime.install(download.filename, {}, function() {
  42. popupObj.change({
  43. contentText: "应用资源更新完成!",
  44. buttonNum: 1,
  45. progress: false
  46. });
  47. }, function(e) {
  48. popupObj.cancel();
  49. plus.nativeUI.alert("安装文件失败[" + e.code + "]:" + e.message);
  50. });
  51. } else {
  52. popupObj.change({
  53. contentText: "文件下载失败...",
  54. buttonNum: 1,
  55. progress: false
  56. });
  57. }
  58. });
  59. dtask.start();
  60. dtask.addEventListener("statechanged", function(task, status) {
  61. switch (task.state) {
  62. case 1: // 开始
  63. popupObj.change({
  64. progressValue:0,
  65. progressTip:"准备下载...",
  66. progress: true
  67. });
  68. break;
  69. case 2: // 已连接到服务器
  70. popupObj.change({
  71. progressValue:0,
  72. progressTip:"开始下载...",
  73. progress: true
  74. });
  75. break;
  76. case 3:
  77. const progress = parseInt(task.downloadedSize / task.totalSize * 100);
  78. if(progress - lastProgressValue >= 2){
  79. lastProgressValue = progress;
  80. popupObj.change({
  81. progressValue:progress,
  82. progressTip: "已下载" + progress + "%",
  83. progress: true
  84. });
  85. }
  86. break;
  87. }
  88. });
  89. // 取消下载
  90. popupObj.cancelDownload = function(){
  91. dtask && dtask.abort();
  92. uni.showToast({
  93. title: "已取消下载",
  94. icon:"none"
  95. });
  96. }
  97. // 重启APP
  98. popupObj.reboot = function(){
  99. plus.runtime.restart();
  100. }
  101. } else if(data.updateType == "silent"){
  102. dtask = plus.downloader.createDownload(data.downloadUrl, {
  103. filename: "_doc/update/"
  104. }, function(download, status) {
  105. if (status == 200) {
  106. plus.runtime.install(download.filename, {}, function() {
  107. console.log("应用资源更新完成");
  108. }, function(e) {
  109. plus.nativeUI.alert("安装文件失败[" + e.code + "]:" + e.message);
  110. });
  111. } else {
  112. plus.nativeUI.alert("文件下载失败...");
  113. }
  114. });
  115. dtask.start();
  116. }
  117. }
  118. // 文字换行
  119. function drawtext(text, maxWidth) {
  120. let textArr = text.split("");
  121. let len = textArr.length;
  122. // 上个节点
  123. let previousNode = 0;
  124. // 记录节点宽度
  125. let nodeWidth = 0;
  126. // 文本换行数组
  127. let rowText = [];
  128. // 如果是字母,侧保存长度
  129. let letterWidth = 0;
  130. // 汉字宽度
  131. let chineseWidth = 14;
  132. // otherFont宽度
  133. let otherWidth = 7;
  134. for (let i = 0; i < len; i++) {
  135. if (/[\u4e00-\u9fa5]|[\uFE30-\uFFA0]/g.test(textArr[i])) {
  136. if(letterWidth > 0){
  137. if(nodeWidth + chineseWidth + letterWidth * otherWidth > maxWidth){
  138. rowText.push({
  139. type: "text",
  140. content: text.substring(previousNode, i)
  141. });
  142. previousNode = i;
  143. nodeWidth = chineseWidth;
  144. letterWidth = 0;
  145. } else {
  146. nodeWidth += chineseWidth + letterWidth * otherWidth;
  147. letterWidth = 0;
  148. }
  149. } else {
  150. if(nodeWidth + chineseWidth > maxWidth){
  151. rowText.push({
  152. type: "text",
  153. content: text.substring(previousNode, i)
  154. });
  155. previousNode = i;
  156. nodeWidth = chineseWidth;
  157. }else{
  158. nodeWidth += chineseWidth;
  159. }
  160. }
  161. } else {
  162. if(/\n/g.test(textArr[i])){
  163. rowText.push({
  164. type: "break",
  165. content: text.substring(previousNode, i)
  166. });
  167. previousNode = i + 1;
  168. nodeWidth = 0;
  169. letterWidth = 0;
  170. }else if(textArr[i] == "\\" && textArr[i + 1] == "n"){
  171. rowText.push({
  172. type: "break",
  173. content: text.substring(previousNode, i)
  174. });
  175. previousNode = i + 2;
  176. nodeWidth = 0;
  177. letterWidth = 0;
  178. }else if(/[a-zA-Z0-9]/g.test(textArr[i])){
  179. letterWidth += 1;
  180. if(nodeWidth + letterWidth * otherWidth > maxWidth){
  181. rowText.push({
  182. type: "text",
  183. content: text.substring(previousNode, i + 1 - letterWidth)
  184. });
  185. previousNode = i + 1 - letterWidth;
  186. nodeWidth = letterWidth * otherWidth;
  187. letterWidth = 0;
  188. }
  189. } else{
  190. if(nodeWidth + otherWidth > maxWidth){
  191. rowText.push({
  192. type: "text",
  193. content: text.substring(previousNode, i)
  194. });
  195. previousNode = i;
  196. nodeWidth = otherWidth;
  197. }else{
  198. nodeWidth += otherWidth;
  199. }
  200. }
  201. }
  202. }
  203. if (previousNode < len) {
  204. rowText.push({
  205. type: "text",
  206. content: text.substring(previousNode, len)
  207. });
  208. }
  209. return rowText;
  210. }
  211. // 是否更新弹窗
  212. function updatePopup(data, callback) {
  213. // 弹窗遮罩层
  214. let maskLayer = new plus.nativeObj.View("maskLayer", { //先创建遮罩层
  215. top: '0px',
  216. left: '0px',
  217. height: '100%',
  218. width: '100%',
  219. backgroundColor: 'rgba(0,0,0,0.5)'
  220. });
  221. // 以下为计算菜单的nview绘制布局,为固定算法,使用者无关关心
  222. const screenWidth = plus.screen.resolutionWidth;
  223. const screenHeight = plus.screen.resolutionHeight;
  224. //弹窗容器宽度
  225. const popupViewWidth = screenWidth * 0.7;
  226. // 弹窗容器的Padding
  227. const viewContentPadding = 20;
  228. // 弹窗容器的宽度
  229. const viewContentWidth = parseInt(popupViewWidth - (viewContentPadding * 2));
  230. // 描述的列表
  231. const descriptionList = drawtext(data.versionInfo, viewContentWidth);
  232. // 弹窗容器高度
  233. let popupViewHeight = 80 + 20 + 20 + 90 + 10;
  234. let popupViewContentList = [
  235. // {
  236. // src: $iconUrl,
  237. // id: "logo",
  238. // tag: "img",
  239. // position: {
  240. // top: "0px",
  241. // left: (popupViewWidth - 124) / 2 + "px",
  242. // width: "124px",
  243. // height: "80px",
  244. // }
  245. // },
  246. {
  247. tag: 'font',
  248. id: 'title',
  249. text: "发现新版本" + data.versionName,
  250. textStyles: {
  251. size: '18px',
  252. color: "#333",
  253. weight: "bold",
  254. whiteSpace: "normal"
  255. },
  256. position: {
  257. top: '90px',
  258. left: viewContentPadding + "px",
  259. width: viewContentWidth + "px",
  260. height: "30px",
  261. }
  262. }];
  263. const textHeight = 18;
  264. let contentTop = 130;
  265. descriptionList.forEach((item,index) => {
  266. if(index > 0){
  267. popupViewHeight += textHeight;
  268. contentTop += textHeight;
  269. }
  270. popupViewContentList.push({
  271. tag: 'font',
  272. id: 'content' + index + 1,
  273. text: item.content,
  274. textStyles: {
  275. size: '14px',
  276. color: "#666",
  277. lineSpacing: "50%",
  278. align: "left"
  279. },
  280. position: {
  281. top: contentTop + "px",
  282. left: viewContentPadding + "px",
  283. width: viewContentWidth + "px",
  284. height: textHeight + "px",
  285. }
  286. });
  287. if(item.type == "break"){
  288. contentTop += 10;
  289. popupViewHeight += 10;
  290. }
  291. });
  292. if(data.updateType == "forcibly"){
  293. popupViewContentList.push({
  294. tag: 'rect', //绘制底边按钮
  295. rectStyles:{
  296. radius: "6px",
  297. color: $mainColor
  298. },
  299. position:{
  300. bottom: viewContentPadding + 'px',
  301. left: viewContentPadding + "px",
  302. width: viewContentWidth + "px",
  303. height: "30px"
  304. }
  305. });
  306. popupViewContentList.push({
  307. tag: 'font',
  308. id: 'confirmText',
  309. text: "立即升级",
  310. textStyles: {
  311. size: '14px',
  312. color: "#FFF",
  313. lineSpacing: "0%",
  314. },
  315. position: {
  316. bottom: viewContentPadding + 'px',
  317. left: viewContentPadding + "px",
  318. width: viewContentWidth + "px",
  319. height: "30px"
  320. }
  321. });
  322. } else {
  323. // 绘制底边按钮
  324. popupViewContentList.push({
  325. tag: 'rect',
  326. id: 'cancelBox',
  327. rectStyles: {
  328. radius: "3px",
  329. borderColor: "#f1f1f1",
  330. borderWidth: "1px",
  331. },
  332. position: {
  333. bottom: viewContentPadding + 'px',
  334. left: viewContentPadding + "px",
  335. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  336. height: "30px",
  337. }
  338. });
  339. popupViewContentList.push({
  340. tag: 'rect',
  341. id: 'confirmBox',
  342. rectStyles: {
  343. radius: "3px",
  344. color: $mainColor,
  345. },
  346. position: {
  347. bottom: viewContentPadding + 'px',
  348. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  349. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  350. height: "30px",
  351. }
  352. });
  353. popupViewContentList.push({
  354. tag: 'font',
  355. id: 'cancelText',
  356. text: "暂不升级",
  357. textStyles: {
  358. size: '14px',
  359. color: "#666",
  360. lineSpacing: "0%",
  361. whiteSpace: "normal"
  362. },
  363. position: {
  364. bottom: viewContentPadding + 'px',
  365. left: viewContentPadding + "px",
  366. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  367. height: "30px",
  368. }
  369. });
  370. popupViewContentList.push({
  371. tag: 'font',
  372. id: 'confirmText',
  373. text: "立即升级",
  374. textStyles: {
  375. size: '14px',
  376. color: "#FFF",
  377. lineSpacing: "0%",
  378. whiteSpace: "normal"
  379. },
  380. position: {
  381. bottom: viewContentPadding + 'px',
  382. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  383. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  384. height: "30px",
  385. }
  386. });
  387. }
  388. // 弹窗内容
  389. let popupView = new plus.nativeObj.View("popupView", { //创建底部图标菜单
  390. tag: "rect",
  391. top: (screenHeight - popupViewHeight) / 2 + "px",
  392. left: '15%',
  393. height: popupViewHeight + "px",
  394. width: "70%"
  395. });
  396. // 绘制白色背景
  397. popupView.drawRect({
  398. color: "#FFFFFF",
  399. radius: "8px"
  400. }, {
  401. top: "40px",
  402. height: popupViewHeight - 40 + "px",
  403. });
  404. popupView.draw(popupViewContentList);
  405. popupView.addEventListener("click", function(e) {
  406. let maxTop = popupViewHeight - viewContentPadding;
  407. let maxLeft = popupViewWidth - viewContentPadding;
  408. let buttonWidth = (viewContentWidth - viewContentPadding) / 2;
  409. if (e.clientY > maxTop - 30 && e.clientY < maxTop) {
  410. if(data.updateType == "forcibly"){
  411. if(e.clientX > viewContentPadding && e.clientX < maxLeft){
  412. // 立即升级
  413. maskLayer.hide();
  414. popupView.hide();
  415. callback && callback();
  416. }
  417. } else {
  418. // 暂不升级
  419. if (e.clientX > viewContentPadding && e.clientX < maxLeft - buttonWidth - viewContentPadding) {
  420. maskLayer.hide();
  421. popupView.hide();
  422. } else if (e.clientX > maxLeft - buttonWidth && e.clientX < maxLeft) {
  423. // 立即升级
  424. maskLayer.hide();
  425. popupView.hide();
  426. callback && callback();
  427. }
  428. }
  429. }
  430. });
  431. if(data.updateType == "solicit"){
  432. // 点击遮罩层
  433. maskLayer.addEventListener("click", function() { //处理遮罩层点击
  434. maskLayer.hide();
  435. popupView.hide();
  436. });
  437. }
  438. // 显示弹窗
  439. maskLayer.show();
  440. popupView.show();
  441. }
  442. // 文件下载的弹窗绘图
  443. function downloadPopupDrawing(data){
  444. // 以下为计算菜单的nview绘制布局,为固定算法,使用者无关关心
  445. const screenWidth = plus.screen.resolutionWidth;
  446. const screenHeight = plus.screen.resolutionHeight;
  447. //弹窗容器宽度
  448. const popupViewWidth = screenWidth * 0.7;
  449. // 弹窗容器的Padding
  450. const viewContentPadding = 20;
  451. // 弹窗容器的宽度
  452. const viewContentWidth = popupViewWidth - (viewContentPadding * 2);
  453. // 弹窗容器高度
  454. let popupViewHeight = viewContentPadding * 3 + 60;
  455. let progressTip = data.progressTip || "准备下载...";
  456. let contentText = data.contentText || "正在为您更新,请耐心等待";
  457. let elementList = [
  458. {
  459. tag: 'rect', //背景色
  460. color: '#FFFFFF',
  461. rectStyles:{
  462. radius: "8px"
  463. }
  464. },
  465. {
  466. tag: 'font',
  467. id: 'title',
  468. text: "升级APP",
  469. textStyles: {
  470. size: '16px',
  471. color: "#333",
  472. weight: "bold",
  473. verticalAlign: "middle",
  474. whiteSpace: "normal"
  475. },
  476. position: {
  477. top: viewContentPadding + 'px',
  478. height: "30px",
  479. }
  480. },
  481. {
  482. tag: 'font',
  483. id: 'content',
  484. text: contentText,
  485. textStyles: {
  486. size: '14px',
  487. color: "#333",
  488. verticalAlign: "middle",
  489. whiteSpace: "normal"
  490. },
  491. position: {
  492. top: viewContentPadding * 2 + 30 + 'px',
  493. height: "20px",
  494. }
  495. }
  496. ];
  497. // 是否有进度条
  498. if(data.progress){
  499. popupViewHeight += viewContentPadding + 40;
  500. elementList = elementList.concat([
  501. {
  502. tag: 'font',
  503. id: 'progressValue',
  504. text: progressTip,
  505. textStyles: {
  506. size: '14px',
  507. color: $mainColor,
  508. whiteSpace: "normal"
  509. },
  510. position: {
  511. top: viewContentPadding * 4 + 20 + 'px',
  512. height: "30px"
  513. }
  514. },
  515. {
  516. tag: 'rect', //绘制进度条背景
  517. id: 'progressBg',
  518. rectStyles:{
  519. radius: "4px",
  520. borderColor: "#f1f1f1",
  521. borderWidth: "1px",
  522. },
  523. position:{
  524. top: viewContentPadding * 4 + 60 + 'px',
  525. left: viewContentPadding + "px",
  526. width: viewContentWidth + "px",
  527. height: "8px"
  528. }
  529. },
  530. ]);
  531. }
  532. if (data.buttonNum == 2) {
  533. popupViewHeight += viewContentPadding + 30;
  534. elementList = elementList.concat([
  535. {
  536. tag: 'rect', //绘制底边按钮
  537. rectStyles:{
  538. radius: "3px",
  539. borderColor: "#f1f1f1",
  540. borderWidth: "1px",
  541. },
  542. position:{
  543. bottom: viewContentPadding + 'px',
  544. left: viewContentPadding + "px",
  545. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  546. height: "30px"
  547. }
  548. },
  549. {
  550. tag: 'rect', //绘制底边按钮
  551. rectStyles:{
  552. radius: "3px",
  553. color: $mainColor
  554. },
  555. position:{
  556. bottom: viewContentPadding + 'px',
  557. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  558. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  559. height: "30px"
  560. }
  561. },
  562. {
  563. tag: 'font',
  564. id: 'cancelText',
  565. text: "取消下载",
  566. textStyles: {
  567. size: '14px',
  568. color: "#666",
  569. lineSpacing: "0%",
  570. whiteSpace: "normal"
  571. },
  572. position: {
  573. bottom: viewContentPadding + 'px',
  574. left: viewContentPadding + "px",
  575. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  576. height: "30px",
  577. }
  578. },
  579. {
  580. tag: 'font',
  581. id: 'confirmText',
  582. text: "后台下载",
  583. textStyles: {
  584. size: '14px',
  585. color: "#FFF",
  586. lineSpacing: "0%",
  587. whiteSpace: "normal"
  588. },
  589. position: {
  590. bottom: viewContentPadding + 'px',
  591. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  592. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  593. height: "30px",
  594. }
  595. }
  596. ]);
  597. }
  598. if (data.buttonNum == 1) {
  599. popupViewHeight += viewContentPadding + 40;
  600. elementList = elementList.concat([
  601. {
  602. tag: 'rect', //绘制底边按钮
  603. rectStyles:{
  604. radius: "6px",
  605. color: $mainColor
  606. },
  607. position:{
  608. bottom: viewContentPadding + 'px',
  609. left: viewContentPadding + "px",
  610. width: viewContentWidth + "px",
  611. height: "40px"
  612. }
  613. },
  614. {
  615. tag: 'font',
  616. id: 'confirmText',
  617. text: "关闭",
  618. textStyles: {
  619. size: '14px',
  620. color: "#FFF",
  621. lineSpacing: "0%",
  622. },
  623. position: {
  624. bottom: viewContentPadding + 'px',
  625. left: viewContentPadding + "px",
  626. width: viewContentWidth + "px",
  627. height: "40px"
  628. }
  629. }
  630. ]);
  631. }
  632. return {
  633. popupViewHeight:popupViewHeight,
  634. popupViewWidth:popupViewWidth,
  635. screenHeight:screenHeight,
  636. viewContentWidth:viewContentWidth,
  637. viewContentPadding:viewContentPadding,
  638. elementList: elementList
  639. };
  640. }
  641. // 文件下载的弹窗
  642. function downloadPopup(data) {
  643. // 弹窗遮罩层
  644. let maskLayer = new plus.nativeObj.View("maskLayer", { //先创建遮罩层
  645. top: '0px',
  646. left: '0px',
  647. height: '100%',
  648. width: '100%',
  649. backgroundColor: 'rgba(0,0,0,0.5)'
  650. });
  651. let popupViewData = downloadPopupDrawing(data);
  652. // 弹窗内容
  653. let popupView = new plus.nativeObj.View("popupView", { //创建底部图标菜单
  654. tag: "rect",
  655. top: (popupViewData.screenHeight - popupViewData.popupViewHeight) / 2 + "px",
  656. left: '15%',
  657. height: popupViewData.popupViewHeight + "px",
  658. width: "70%",
  659. });
  660. let progressValue = 0;
  661. let progressTip = 0;
  662. let contentText = 0;
  663. let buttonNum = 2;
  664. if(data.buttonNum >= 0){
  665. buttonNum = data.buttonNum;
  666. }
  667. popupView.draw(popupViewData.elementList);
  668. let callbackData = {
  669. change: function(res) {
  670. let progressElement = [];
  671. if(res.progressValue){
  672. progressValue = res.progressValue;
  673. // 绘制进度条
  674. progressElement.push({
  675. tag: 'rect', //绘制进度条背景
  676. id: 'progressValueBg',
  677. rectStyles:{
  678. radius: "4px",
  679. color: $mainColor
  680. },
  681. position:{
  682. top: popupViewData.viewContentPadding * 4 + 60 + 'px',
  683. left: popupViewData.viewContentPadding + "px",
  684. width: popupViewData.viewContentWidth * (res.progressValue / 100) + "px",
  685. height: "8px"
  686. }
  687. });
  688. }
  689. if(res.progressTip){
  690. progressTip = res.progressTip;
  691. progressElement.push({
  692. tag: 'font',
  693. id: 'progressValue',
  694. text: res.progressTip,
  695. textStyles: {
  696. size: '14px',
  697. color: $mainColor,
  698. whiteSpace: "normal"
  699. },
  700. position: {
  701. top: popupViewData.viewContentPadding * 4 + 20 + 'px',
  702. height: "30px"
  703. }
  704. });
  705. }
  706. if(res.contentText){
  707. contentText = res.contentText;
  708. progressElement.push({
  709. tag: 'font',
  710. id: 'content',
  711. text: res.contentText,
  712. textStyles: {
  713. size: '16px',
  714. color: "#333",
  715. whiteSpace: "normal"
  716. },
  717. position: {
  718. top: popupViewData.viewContentPadding * 2 + 30 + 'px',
  719. height: "30px",
  720. }
  721. });
  722. }
  723. if(res.buttonNum >= 0 && buttonNum != res.buttonNum){
  724. buttonNum = res.buttonNum;
  725. popupView.reset();
  726. popupViewData = downloadPopupDrawing(Object.assign({
  727. progressValue:progressValue,
  728. progressTip:progressTip,
  729. contentText:contentText,
  730. },res));
  731. let newElement = [];
  732. popupViewData.elementList.map((item,index) => {
  733. let have = false;
  734. progressElement.forEach((childItem,childIndex) => {
  735. if(item.id == childItem.id){
  736. have = true;
  737. }
  738. });
  739. if(!have){
  740. newElement.push(item);
  741. }
  742. });
  743. progressElement = newElement.concat(progressElement);
  744. popupView.setStyle({
  745. tag: "rect",
  746. top: (popupViewData.screenHeight - popupViewData.popupViewHeight) / 2 + "px",
  747. left: '15%',
  748. height: popupViewData.popupViewHeight + "px",
  749. width: "70%",
  750. });
  751. popupView.draw(progressElement);
  752. }else{
  753. popupView.draw(progressElement);
  754. }
  755. },
  756. cancel: function() {
  757. maskLayer.hide();
  758. popupView.hide();
  759. }
  760. }
  761. popupView.addEventListener("click", function(e) {
  762. let maxTop = popupViewData.popupViewHeight - popupViewData.viewContentPadding;
  763. let maxLeft = popupViewData.popupViewWidth - popupViewData.viewContentPadding;
  764. if (e.clientY > maxTop - 40 && e.clientY < maxTop) {
  765. if(buttonNum == 1){
  766. // 单按钮
  767. if (e.clientX > popupViewData.viewContentPadding && e.clientX < maxLeft) {
  768. maskLayer.hide();
  769. popupView.hide();
  770. callbackData.reboot();
  771. }
  772. }else if(buttonNum == 2){
  773. // 双按钮
  774. let buttonWidth = (popupViewData.viewContentWidth - popupViewData.viewContentPadding) / 2;
  775. if (e.clientX > popupViewData.viewContentPadding && e.clientX < maxLeft - buttonWidth - popupViewData.viewContentPadding) {
  776. maskLayer.hide();
  777. popupView.hide();
  778. callbackData.cancelDownload();
  779. } else if (e.clientX > maxLeft - buttonWidth && e.clientX < maxLeft) {
  780. maskLayer.hide();
  781. popupView.hide();
  782. }
  783. }
  784. }
  785. });
  786. // 显示弹窗
  787. maskLayer.show();
  788. popupView.show();
  789. // 改变进度条
  790. return callbackData;
  791. }
  792. export default function(isPrompt = false) {
  793. getCurrentNo(versionInfo => {
  794. componentConfig.getServerNo(versionInfo, isPrompt, res => {
  795. if (res.updateType == "forcibly" || res.updateType == "silent") {
  796. if (/\.wgt$/i.test(res.downloadUrl)) {
  797. getDownload(res);
  798. } else if(/\.html$/i.test(res.downloadUrl)){
  799. plus.runtime.openURL(res.downloadUrl);
  800. } else {
  801. if (platform == "android") {
  802. getDownload(res);
  803. } else {
  804. plus.runtime.openURL(res.downloadUrl);
  805. }
  806. }
  807. } else if(res.updateType == "solicit"){
  808. updatePopup(res, function() {
  809. if (/\.wgt$/i.test(res.downloadUrl)) {
  810. getDownload(res);
  811. } else if(/\.html$/i.test(res.downloadUrl)){
  812. plus.runtime.openURL(res.downloadUrl);
  813. } else {
  814. if (platform == "android") {
  815. getDownload(res);
  816. } else {
  817. plus.runtime.openURL(res.downloadUrl);
  818. }
  819. }
  820. });
  821. }
  822. });
  823. });
  824. }
  825. // #endif