appUpdate.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. // #ifdef APP-PLUS
  2. import componentConfig from "@/config/componentConfig"
  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/zhouWei-APPUpdate/static/ic_ar.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. src: $iconUrl,
  236. id: "logo",
  237. tag: "img",
  238. position: {
  239. top: "0px",
  240. left: (popupViewWidth - 124) / 2 + "px",
  241. width: "124px",
  242. height: "80px",
  243. }
  244. },
  245. {
  246. tag: 'font',
  247. id: 'title',
  248. text: "发现新版本" + data.versionName,
  249. textStyles: {
  250. size: '18px',
  251. color: "#333",
  252. weight: "bold",
  253. whiteSpace: "normal"
  254. },
  255. position: {
  256. top: '90px',
  257. left: viewContentPadding + "px",
  258. width: viewContentWidth + "px",
  259. height: "30px",
  260. }
  261. }];
  262. const textHeight = 18;
  263. let contentTop = 130;
  264. descriptionList.forEach((item,index) => {
  265. if(index > 0){
  266. popupViewHeight += textHeight;
  267. contentTop += textHeight;
  268. }
  269. popupViewContentList.push({
  270. tag: 'font',
  271. id: 'content' + index + 1,
  272. text: item.content,
  273. textStyles: {
  274. size: '14px',
  275. color: "#666",
  276. lineSpacing: "50%",
  277. align: "left"
  278. },
  279. position: {
  280. top: contentTop + "px",
  281. left: viewContentPadding + "px",
  282. width: viewContentWidth + "px",
  283. height: textHeight + "px",
  284. }
  285. });
  286. if(item.type == "break"){
  287. contentTop += 10;
  288. popupViewHeight += 10;
  289. }
  290. });
  291. if(data.updateType == "forcibly"){
  292. popupViewContentList.push({
  293. tag: 'rect', //绘制底边按钮
  294. rectStyles:{
  295. radius: "6px",
  296. color: $mainColor
  297. },
  298. position:{
  299. bottom: viewContentPadding + 'px',
  300. left: viewContentPadding + "px",
  301. width: viewContentWidth + "px",
  302. height: "30px"
  303. }
  304. });
  305. popupViewContentList.push({
  306. tag: 'font',
  307. id: 'confirmText',
  308. text: "立即升级",
  309. textStyles: {
  310. size: '14px',
  311. color: "#FFF",
  312. lineSpacing: "0%",
  313. },
  314. position: {
  315. bottom: viewContentPadding + 'px',
  316. left: viewContentPadding + "px",
  317. width: viewContentWidth + "px",
  318. height: "30px"
  319. }
  320. });
  321. } else {
  322. // 绘制底边按钮
  323. popupViewContentList.push({
  324. tag: 'rect',
  325. id: 'cancelBox',
  326. rectStyles: {
  327. radius: "3px",
  328. borderColor: "#f1f1f1",
  329. borderWidth: "1px",
  330. },
  331. position: {
  332. bottom: viewContentPadding + 'px',
  333. left: viewContentPadding + "px",
  334. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  335. height: "30px",
  336. }
  337. });
  338. popupViewContentList.push({
  339. tag: 'rect',
  340. id: 'confirmBox',
  341. rectStyles: {
  342. radius: "3px",
  343. color: $mainColor,
  344. },
  345. position: {
  346. bottom: viewContentPadding + 'px',
  347. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  348. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  349. height: "30px",
  350. }
  351. });
  352. popupViewContentList.push({
  353. tag: 'font',
  354. id: 'cancelText',
  355. text: "暂不升级",
  356. textStyles: {
  357. size: '14px',
  358. color: "#666",
  359. lineSpacing: "0%",
  360. whiteSpace: "normal"
  361. },
  362. position: {
  363. bottom: viewContentPadding + 'px',
  364. left: viewContentPadding + "px",
  365. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  366. height: "30px",
  367. }
  368. });
  369. popupViewContentList.push({
  370. tag: 'font',
  371. id: 'confirmText',
  372. text: "立即升级",
  373. textStyles: {
  374. size: '14px',
  375. color: "#FFF",
  376. lineSpacing: "0%",
  377. whiteSpace: "normal"
  378. },
  379. position: {
  380. bottom: viewContentPadding + 'px',
  381. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  382. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  383. height: "30px",
  384. }
  385. });
  386. }
  387. // 弹窗内容
  388. let popupView = new plus.nativeObj.View("popupView", { //创建底部图标菜单
  389. tag: "rect",
  390. top: (screenHeight - popupViewHeight) / 2 + "px",
  391. left: '15%',
  392. height: popupViewHeight + "px",
  393. width: "70%"
  394. });
  395. // 绘制白色背景
  396. popupView.drawRect({
  397. color: "#FFFFFF",
  398. radius: "8px"
  399. }, {
  400. top: "40px",
  401. height: popupViewHeight - 40 + "px",
  402. });
  403. popupView.draw(popupViewContentList);
  404. popupView.addEventListener("click", function(e) {
  405. let maxTop = popupViewHeight - viewContentPadding;
  406. let maxLeft = popupViewWidth - viewContentPadding;
  407. let buttonWidth = (viewContentWidth - viewContentPadding) / 2;
  408. if (e.clientY > maxTop - 30 && e.clientY < maxTop) {
  409. if(data.updateType == "forcibly"){
  410. if(e.clientX > viewContentPadding && e.clientX < maxLeft){
  411. // 立即升级
  412. maskLayer.hide();
  413. popupView.hide();
  414. callback && callback();
  415. }
  416. } else {
  417. // 暂不升级
  418. if (e.clientX > viewContentPadding && e.clientX < maxLeft - buttonWidth - viewContentPadding) {
  419. maskLayer.hide();
  420. popupView.hide();
  421. } else if (e.clientX > maxLeft - buttonWidth && e.clientX < maxLeft) {
  422. // 立即升级
  423. maskLayer.hide();
  424. popupView.hide();
  425. callback && callback();
  426. }
  427. }
  428. }
  429. });
  430. if(data.updateType == "solicit"){
  431. // 点击遮罩层
  432. maskLayer.addEventListener("click", function() { //处理遮罩层点击
  433. maskLayer.hide();
  434. popupView.hide();
  435. });
  436. }
  437. // 显示弹窗
  438. maskLayer.show();
  439. popupView.show();
  440. }
  441. // 文件下载的弹窗绘图
  442. function downloadPopupDrawing(data){
  443. // 以下为计算菜单的nview绘制布局,为固定算法,使用者无关关心
  444. const screenWidth = plus.screen.resolutionWidth;
  445. const screenHeight = plus.screen.resolutionHeight;
  446. //弹窗容器宽度
  447. const popupViewWidth = screenWidth * 0.7;
  448. // 弹窗容器的Padding
  449. const viewContentPadding = 20;
  450. // 弹窗容器的宽度
  451. const viewContentWidth = popupViewWidth - (viewContentPadding * 2);
  452. // 弹窗容器高度
  453. let popupViewHeight = viewContentPadding * 3 + 60;
  454. let progressTip = data.progressTip || "准备下载...";
  455. let contentText = data.contentText || "正在为您更新,请耐心等待";
  456. let elementList = [
  457. {
  458. tag: 'rect', //背景色
  459. color: '#FFFFFF',
  460. rectStyles:{
  461. radius: "8px"
  462. }
  463. },
  464. {
  465. tag: 'font',
  466. id: 'title',
  467. text: "升级APP",
  468. textStyles: {
  469. size: '16px',
  470. color: "#333",
  471. weight: "bold",
  472. verticalAlign: "middle",
  473. whiteSpace: "normal"
  474. },
  475. position: {
  476. top: viewContentPadding + 'px',
  477. height: "30px",
  478. }
  479. },
  480. {
  481. tag: 'font',
  482. id: 'content',
  483. text: contentText,
  484. textStyles: {
  485. size: '14px',
  486. color: "#333",
  487. verticalAlign: "middle",
  488. whiteSpace: "normal"
  489. },
  490. position: {
  491. top: viewContentPadding * 2 + 30 + 'px',
  492. height: "20px",
  493. }
  494. }
  495. ];
  496. // 是否有进度条
  497. if(data.progress){
  498. popupViewHeight += viewContentPadding + 40;
  499. elementList = elementList.concat([
  500. {
  501. tag: 'font',
  502. id: 'progressValue',
  503. text: progressTip,
  504. textStyles: {
  505. size: '14px',
  506. color: $mainColor,
  507. whiteSpace: "normal"
  508. },
  509. position: {
  510. top: viewContentPadding * 4 + 20 + 'px',
  511. height: "30px"
  512. }
  513. },
  514. {
  515. tag: 'rect', //绘制进度条背景
  516. id: 'progressBg',
  517. rectStyles:{
  518. radius: "4px",
  519. borderColor: "#f1f1f1",
  520. borderWidth: "1px",
  521. },
  522. position:{
  523. top: viewContentPadding * 4 + 60 + 'px',
  524. left: viewContentPadding + "px",
  525. width: viewContentWidth + "px",
  526. height: "8px"
  527. }
  528. },
  529. ]);
  530. }
  531. if (data.buttonNum == 2) {
  532. popupViewHeight += viewContentPadding + 30;
  533. elementList = elementList.concat([
  534. {
  535. tag: 'rect', //绘制底边按钮
  536. rectStyles:{
  537. radius: "3px",
  538. borderColor: "#f1f1f1",
  539. borderWidth: "1px",
  540. },
  541. position:{
  542. bottom: viewContentPadding + 'px',
  543. left: viewContentPadding + "px",
  544. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  545. height: "30px"
  546. }
  547. },
  548. {
  549. tag: 'rect', //绘制底边按钮
  550. rectStyles:{
  551. radius: "3px",
  552. color: $mainColor
  553. },
  554. position:{
  555. bottom: viewContentPadding + 'px',
  556. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  557. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  558. height: "30px"
  559. }
  560. },
  561. {
  562. tag: 'font',
  563. id: 'cancelText',
  564. text: "取消下载",
  565. textStyles: {
  566. size: '14px',
  567. color: "#666",
  568. lineSpacing: "0%",
  569. whiteSpace: "normal"
  570. },
  571. position: {
  572. bottom: viewContentPadding + 'px',
  573. left: viewContentPadding + "px",
  574. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  575. height: "30px",
  576. }
  577. },
  578. {
  579. tag: 'font',
  580. id: 'confirmText',
  581. text: "后台下载",
  582. textStyles: {
  583. size: '14px',
  584. color: "#FFF",
  585. lineSpacing: "0%",
  586. whiteSpace: "normal"
  587. },
  588. position: {
  589. bottom: viewContentPadding + 'px',
  590. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  591. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  592. height: "30px",
  593. }
  594. }
  595. ]);
  596. }
  597. if (data.buttonNum == 1) {
  598. popupViewHeight += viewContentPadding + 40;
  599. elementList = elementList.concat([
  600. {
  601. tag: 'rect', //绘制底边按钮
  602. rectStyles:{
  603. radius: "6px",
  604. color: $mainColor
  605. },
  606. position:{
  607. bottom: viewContentPadding + 'px',
  608. left: viewContentPadding + "px",
  609. width: viewContentWidth + "px",
  610. height: "40px"
  611. }
  612. },
  613. {
  614. tag: 'font',
  615. id: 'confirmText',
  616. text: "关闭",
  617. textStyles: {
  618. size: '14px',
  619. color: "#FFF",
  620. lineSpacing: "0%",
  621. },
  622. position: {
  623. bottom: viewContentPadding + 'px',
  624. left: viewContentPadding + "px",
  625. width: viewContentWidth + "px",
  626. height: "40px"
  627. }
  628. }
  629. ]);
  630. }
  631. return {
  632. popupViewHeight:popupViewHeight,
  633. popupViewWidth:popupViewWidth,
  634. screenHeight:screenHeight,
  635. viewContentWidth:viewContentWidth,
  636. viewContentPadding:viewContentPadding,
  637. elementList: elementList
  638. };
  639. }
  640. // 文件下载的弹窗
  641. function downloadPopup(data) {
  642. // 弹窗遮罩层
  643. let maskLayer = new plus.nativeObj.View("maskLayer", { //先创建遮罩层
  644. top: '0px',
  645. left: '0px',
  646. height: '100%',
  647. width: '100%',
  648. backgroundColor: 'rgba(0,0,0,0.5)'
  649. });
  650. let popupViewData = downloadPopupDrawing(data);
  651. // 弹窗内容
  652. let popupView = new plus.nativeObj.View("popupView", { //创建底部图标菜单
  653. tag: "rect",
  654. top: (popupViewData.screenHeight - popupViewData.popupViewHeight) / 2 + "px",
  655. left: '15%',
  656. height: popupViewData.popupViewHeight + "px",
  657. width: "70%",
  658. });
  659. let progressValue = 0;
  660. let progressTip = 0;
  661. let contentText = 0;
  662. let buttonNum = 2;
  663. if(data.buttonNum >= 0){
  664. buttonNum = data.buttonNum;
  665. }
  666. popupView.draw(popupViewData.elementList);
  667. let callbackData = {
  668. change: function(res) {
  669. let progressElement = [];
  670. if(res.progressValue){
  671. progressValue = res.progressValue;
  672. // 绘制进度条
  673. progressElement.push({
  674. tag: 'rect', //绘制进度条背景
  675. id: 'progressValueBg',
  676. rectStyles:{
  677. radius: "4px",
  678. color: $mainColor
  679. },
  680. position:{
  681. top: popupViewData.viewContentPadding * 4 + 60 + 'px',
  682. left: popupViewData.viewContentPadding + "px",
  683. width: popupViewData.viewContentWidth * (res.progressValue / 100) + "px",
  684. height: "8px"
  685. }
  686. });
  687. }
  688. if(res.progressTip){
  689. progressTip = res.progressTip;
  690. progressElement.push({
  691. tag: 'font',
  692. id: 'progressValue',
  693. text: res.progressTip,
  694. textStyles: {
  695. size: '14px',
  696. color: $mainColor,
  697. whiteSpace: "normal"
  698. },
  699. position: {
  700. top: popupViewData.viewContentPadding * 4 + 20 + 'px',
  701. height: "30px"
  702. }
  703. });
  704. }
  705. if(res.contentText){
  706. contentText = res.contentText;
  707. progressElement.push({
  708. tag: 'font',
  709. id: 'content',
  710. text: res.contentText,
  711. textStyles: {
  712. size: '16px',
  713. color: "#333",
  714. whiteSpace: "normal"
  715. },
  716. position: {
  717. top: popupViewData.viewContentPadding * 2 + 30 + 'px',
  718. height: "30px",
  719. }
  720. });
  721. }
  722. if(res.buttonNum >= 0 && buttonNum != res.buttonNum){
  723. buttonNum = res.buttonNum;
  724. popupView.reset();
  725. popupViewData = downloadPopupDrawing(Object.assign({
  726. progressValue:progressValue,
  727. progressTip:progressTip,
  728. contentText:contentText,
  729. },res));
  730. let newElement = [];
  731. popupViewData.elementList.map((item,index) => {
  732. let have = false;
  733. progressElement.forEach((childItem,childIndex) => {
  734. if(item.id == childItem.id){
  735. have = true;
  736. }
  737. });
  738. if(!have){
  739. newElement.push(item);
  740. }
  741. });
  742. progressElement = newElement.concat(progressElement);
  743. popupView.setStyle({
  744. tag: "rect",
  745. top: (popupViewData.screenHeight - popupViewData.popupViewHeight) / 2 + "px",
  746. left: '15%',
  747. height: popupViewData.popupViewHeight + "px",
  748. width: "70%",
  749. });
  750. popupView.draw(progressElement);
  751. }else{
  752. popupView.draw(progressElement);
  753. }
  754. },
  755. cancel: function() {
  756. maskLayer.hide();
  757. popupView.hide();
  758. }
  759. }
  760. popupView.addEventListener("click", function(e) {
  761. let maxTop = popupViewData.popupViewHeight - popupViewData.viewContentPadding;
  762. let maxLeft = popupViewData.popupViewWidth - popupViewData.viewContentPadding;
  763. if (e.clientY > maxTop - 40 && e.clientY < maxTop) {
  764. if(buttonNum == 1){
  765. // 单按钮
  766. if (e.clientX > popupViewData.viewContentPadding && e.clientX < maxLeft) {
  767. maskLayer.hide();
  768. popupView.hide();
  769. callbackData.reboot();
  770. }
  771. }else if(buttonNum == 2){
  772. // 双按钮
  773. let buttonWidth = (popupViewData.viewContentWidth - popupViewData.viewContentPadding) / 2;
  774. if (e.clientX > popupViewData.viewContentPadding && e.clientX < maxLeft - buttonWidth - popupViewData.viewContentPadding) {
  775. maskLayer.hide();
  776. popupView.hide();
  777. callbackData.cancelDownload();
  778. } else if (e.clientX > maxLeft - buttonWidth && e.clientX < maxLeft) {
  779. maskLayer.hide();
  780. popupView.hide();
  781. }
  782. }
  783. }
  784. });
  785. // 显示弹窗
  786. maskLayer.show();
  787. popupView.show();
  788. // 改变进度条
  789. return callbackData;
  790. }
  791. export default function(isPrompt = false) {
  792. getCurrentNo(versionInfo => {
  793. componentConfig.getServerNo(versionInfo, isPrompt, res => {
  794. if (res.updateType == "forcibly" || res.updateType == "silent") {
  795. if (/\.wgt$/i.test(res.downloadUrl)) {
  796. getDownload(res);
  797. } else if(/\.html$/i.test(res.downloadUrl)){
  798. plus.runtime.openURL(res.downloadUrl);
  799. } else {
  800. if (platform == "android") {
  801. getDownload(res);
  802. } else {
  803. plus.runtime.openURL(res.downloadUrl);
  804. }
  805. }
  806. } else if(res.updateType == "solicit"){
  807. updatePopup(res, function() {
  808. if (/\.wgt$/i.test(res.downloadUrl)) {
  809. getDownload(res);
  810. } else if(/\.html$/i.test(res.downloadUrl)){
  811. plus.runtime.openURL(res.downloadUrl);
  812. } else {
  813. if (platform == "android") {
  814. getDownload(res);
  815. } else {
  816. plus.runtime.openURL(res.downloadUrl);
  817. }
  818. }
  819. });
  820. }
  821. });
  822. });
  823. }
  824. // #endif