index.js 22 KB

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