appUpdate.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. import { $post } from './request';
  2. import verConfig from '../ver.config';
  3. import { t } from "@/locale"
  4. import { Toast } from './tool';
  5. const updateConfig = {
  6. url: '/third/check/version ', //检查版本的接口
  7. bgColor: '', //升级主色,按钮背景颜色
  8. iconUrl: '' //升级小图标
  9. }
  10. const platform = uni.getSystemInfoSync().platform;
  11. // 主颜色
  12. const $mainColor = updateConfig.bgColor ? updateConfig.bgColor : "FF5B78";
  13. // 弹窗图标url
  14. const $iconUrl = updateConfig.iconUrl ? updateConfig.iconUrl : "/static/login.png";
  15. const $checkUpdateUrl = updateConfig.url ? updateConfig.url : "";
  16. // 获取当前应用的版本号
  17. export const getCurrentNo = (callback) => {
  18. // 获取本地应用资源版本号
  19. plus.runtime.getProperty(plus.runtime.appid, (inf) => {
  20. callback && callback({
  21. versionCode: inf.versionCode,
  22. versionName: inf.version
  23. });
  24. });
  25. }
  26. // 从服务器下载应用资源包(wgt文件)
  27. const getDownload = (data) => {
  28. let dtask;
  29. if (data.updateType == 'forcibly' || data.updateType == 'solicit') {
  30. let popupData = {
  31. progress: true,
  32. buttonNum: 2
  33. };
  34. if (data.updateType == 'forcibly') {
  35. popupData.buttonNum = 0;
  36. }
  37. let lastProgressValue = 0;
  38. let popupObj = downloadPopup(popupData);
  39. dtask = plus.downloader.createDownload(data.downloadUrl, {
  40. filename: "_doc/update/"
  41. }, function (download, status) {
  42. if (status == 200) {
  43. popupObj.change({
  44. progressValue: 100,
  45. progressTip: t('正在安装文件'),
  46. progress: true,
  47. buttonNum: 0
  48. });
  49. plus.runtime.install(download.filename, {}, function () {
  50. popupObj.change({
  51. contentText: t("应用资源更新完成"),
  52. buttonNum: 1,
  53. progress: false
  54. });
  55. }, function (e) {
  56. popupObj.cancel();
  57. plus.nativeUI.alert(t('安装文件失败') + "[" + e.code + "]:" + e.message);
  58. });
  59. } else {
  60. popupObj.change({
  61. contentText: t("文件下载失败"),
  62. buttonNum: 1,
  63. progress: false
  64. });
  65. }
  66. });
  67. dtask.start();
  68. dtask.addEventListener("statechanged", function (task, status) {
  69. switch (task.state) {
  70. case 1: // 开始
  71. popupObj.change({
  72. progressValue: 0,
  73. progressTip: t("准备下载"),
  74. progress: true
  75. });
  76. break;
  77. case 2: // 已连接到服务器
  78. popupObj.change({
  79. progressValue: 0,
  80. progressTip: t("开始下载"),
  81. progress: true
  82. });
  83. break;
  84. case 3:
  85. const progress = parseInt(task.downloadedSize / task.totalSize * 100);
  86. if (progress - lastProgressValue >= 2) {
  87. lastProgressValue = progress;
  88. popupObj.change({
  89. progressValue: progress,
  90. progressTip: t("已下载") + progress + "%",
  91. progress: true
  92. });
  93. }
  94. break;
  95. }
  96. });
  97. // 取消下载
  98. popupObj.cancelDownload = function () {
  99. dtask && dtask.abort();
  100. uni.showToast({
  101. title: t("已取消下载"),
  102. icon: "none"
  103. });
  104. }
  105. // 重启APP
  106. popupObj.reboot = function () {
  107. plus.runtime.restart();
  108. }
  109. } else if (data.updateType == "silent") {
  110. dtask = plus.downloader.createDownload(data.downloadUrl, {
  111. filename: "_doc/update/"
  112. }, function (download, status) {
  113. if (status == 200) {
  114. plus.runtime.install(download.filename, {}, function () {
  115. console.log("应用资源更新完成");
  116. }, function (e) {
  117. plus.nativeUI.alert(t('安装文件失败') + "[" + e.code + "]:" + e.message);
  118. });
  119. } else {
  120. plus.nativeUI.alert(t('文件下载失败'));
  121. }
  122. });
  123. dtask.start();
  124. }
  125. }
  126. // 文字换行
  127. const drawtext = (text, maxWidth) => {
  128. let textArr = text.split("");
  129. let len = textArr.length;
  130. // 上个节点
  131. let previousNode = 0;
  132. // 记录节点宽度
  133. let nodeWidth = 0;
  134. // 文本换行数组
  135. let rowText = [];
  136. // 如果是字母,侧保存长度
  137. let letterWidth = 0;
  138. // 汉字宽度
  139. let chineseWidth = 14;
  140. // otherFont宽度
  141. let otherWidth = 7;
  142. for (let i = 0; i < len; i++) {
  143. if (/[\u4e00-\u9fa5]|[\uFE30-\uFFA0]/g.test(textArr[i])) {
  144. if (letterWidth > 0) {
  145. if (nodeWidth + chineseWidth + letterWidth * otherWidth > maxWidth) {
  146. rowText.push({
  147. type: "text",
  148. content: text.substring(previousNode, i)
  149. });
  150. previousNode = i;
  151. nodeWidth = chineseWidth;
  152. letterWidth = 0;
  153. } else {
  154. nodeWidth += chineseWidth + letterWidth * otherWidth;
  155. letterWidth = 0;
  156. }
  157. } else {
  158. if (nodeWidth + chineseWidth > maxWidth) {
  159. rowText.push({
  160. type: "text",
  161. content: text.substring(previousNode, i)
  162. });
  163. previousNode = i;
  164. nodeWidth = chineseWidth;
  165. } else {
  166. nodeWidth += chineseWidth;
  167. }
  168. }
  169. } else {
  170. if (/\n/g.test(textArr[i])) {
  171. rowText.push({
  172. type: "break",
  173. content: text.substring(previousNode, i)
  174. });
  175. previousNode = i + 1;
  176. nodeWidth = 0;
  177. letterWidth = 0;
  178. } else if (textArr[i] == "\\" && textArr[i + 1] == "n") {
  179. rowText.push({
  180. type: "break",
  181. content: text.substring(previousNode, i)
  182. });
  183. previousNode = i + 2;
  184. nodeWidth = 0;
  185. letterWidth = 0;
  186. } else if (/[a-zA-Z0-9]/g.test(textArr[i])) {
  187. letterWidth += 1;
  188. if (nodeWidth + letterWidth * otherWidth > maxWidth) {
  189. rowText.push({
  190. type: "text",
  191. content: text.substring(previousNode, i + 1 - letterWidth)
  192. });
  193. previousNode = i + 1 - letterWidth;
  194. nodeWidth = letterWidth * otherWidth;
  195. letterWidth = 0;
  196. }
  197. } else {
  198. if (nodeWidth + otherWidth > maxWidth) {
  199. rowText.push({
  200. type: "text",
  201. content: text.substring(previousNode, i)
  202. });
  203. previousNode = i;
  204. nodeWidth = otherWidth;
  205. } else {
  206. nodeWidth += otherWidth;
  207. }
  208. }
  209. }
  210. }
  211. if (previousNode < len) {
  212. rowText.push({
  213. type: "text",
  214. content: text.substring(previousNode, len)
  215. });
  216. }
  217. return rowText;
  218. }
  219. // 是否更新弹窗
  220. const updatePopup = (data, callback) => {
  221. // 弹窗遮罩层
  222. let maskLayer = new plus.nativeObj.View("maskLayer", { //先创建遮罩层
  223. top: '0px',
  224. left: '0px',
  225. height: '100%',
  226. width: '100%',
  227. backgroundColor: 'rgba(0,0,0,0.5)'
  228. });
  229. // 以下为计算菜单的nview绘制布局,为固定算法,使用者无关关心
  230. const screenWidth = plus.screen.resolutionWidth;
  231. const screenHeight = plus.screen.resolutionHeight;
  232. //弹窗容器宽度
  233. const popupViewWidth = screenWidth * 0.7;
  234. // 弹窗容器的Padding
  235. const viewContentPadding = 20;
  236. // 弹窗容器的宽度
  237. const viewContentWidth = parseInt(popupViewWidth - (viewContentPadding * 2));
  238. // 描述的列表
  239. const descriptionList = drawtext(data.versionInfo, viewContentWidth);
  240. // 弹窗容器高度
  241. let popupViewHeight = 80 + 20 + 20 + 90 + 10;
  242. let popupViewContentList = [{
  243. src: $iconUrl,
  244. id: "logo",
  245. tag: "img",
  246. position: {
  247. top: "0px",
  248. left: (popupViewWidth - 80) / 2 + "px",
  249. width: "80px",
  250. height: "80px",
  251. }
  252. },
  253. {
  254. tag: 'font',
  255. id: 'title',
  256. text: t("发现新版本") + data.versionName,
  257. textStyles: {
  258. size: '18px',
  259. color: "#333",
  260. weight: "bold",
  261. whiteSpace: "normal"
  262. },
  263. position: {
  264. top: '90px',
  265. left: viewContentPadding + "px",
  266. width: viewContentWidth + "px",
  267. height: "30px",
  268. }
  269. }];
  270. const textHeight = 18;
  271. let contentTop = 130;
  272. descriptionList.forEach((item, index) => {
  273. if (index > 0) {
  274. popupViewHeight += textHeight;
  275. contentTop += textHeight;
  276. }
  277. popupViewContentList.push({
  278. tag: 'font',
  279. id: 'content' + index + 1,
  280. text: item.content,
  281. textStyles: {
  282. size: '14px',
  283. color: "#666",
  284. lineSpacing: "50%",
  285. align: "left"
  286. },
  287. position: {
  288. top: contentTop + "px",
  289. left: viewContentPadding + "px",
  290. width: viewContentWidth + "px",
  291. height: textHeight + "px",
  292. }
  293. });
  294. if (item.type == "break") {
  295. contentTop += 10;
  296. popupViewHeight += 10;
  297. }
  298. });
  299. if (data.updateType == "forcibly") {
  300. popupViewContentList.push({
  301. tag: 'rect', //绘制底边按钮
  302. rectStyles: {
  303. radius: "6px",
  304. color: $mainColor
  305. },
  306. position: {
  307. bottom: viewContentPadding + 'px',
  308. left: viewContentPadding + "px",
  309. width: viewContentWidth + "px",
  310. height: "30px"
  311. }
  312. });
  313. popupViewContentList.push({
  314. tag: 'font',
  315. id: 'confirmText',
  316. text: t("立即升级"),
  317. textStyles: {
  318. size: '14px',
  319. color: "#FFF",
  320. lineSpacing: "0%",
  321. },
  322. position: {
  323. bottom: viewContentPadding + 'px',
  324. left: viewContentPadding + "px",
  325. width: viewContentWidth + "px",
  326. height: "30px"
  327. }
  328. });
  329. } else {
  330. // 绘制底边按钮
  331. popupViewContentList.push({
  332. tag: 'rect',
  333. id: 'cancelBox',
  334. rectStyles: {
  335. radius: "3px",
  336. borderColor: "#f1f1f1",
  337. borderWidth: "1px",
  338. },
  339. position: {
  340. bottom: viewContentPadding + 'px',
  341. left: viewContentPadding + "px",
  342. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  343. height: "30px",
  344. }
  345. });
  346. popupViewContentList.push({
  347. tag: 'rect',
  348. id: 'confirmBox',
  349. rectStyles: {
  350. radius: "3px",
  351. color: $mainColor,
  352. },
  353. position: {
  354. bottom: viewContentPadding + 'px',
  355. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  356. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  357. height: "30px",
  358. }
  359. });
  360. popupViewContentList.push({
  361. tag: 'font',
  362. id: 'cancelText',
  363. text: t("暂不升级"),
  364. textStyles: {
  365. size: '14px',
  366. color: "#666",
  367. lineSpacing: "0%",
  368. whiteSpace: "normal"
  369. },
  370. position: {
  371. bottom: viewContentPadding + 'px',
  372. left: viewContentPadding + "px",
  373. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  374. height: "30px",
  375. }
  376. });
  377. popupViewContentList.push({
  378. tag: 'font',
  379. id: 'confirmText',
  380. text: t("立即升级"),
  381. textStyles: {
  382. size: '14px',
  383. color: "#FFF",
  384. lineSpacing: "0%",
  385. whiteSpace: "normal"
  386. },
  387. position: {
  388. bottom: viewContentPadding + 'px',
  389. left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px",
  390. width: (viewContentWidth - viewContentPadding) / 2 + "px",
  391. height: "30px",
  392. }
  393. });
  394. }
  395. // 弹窗内容
  396. let popupView = new plus.nativeObj.View("popupView", { //创建底部图标菜单
  397. tag: "rect",
  398. top: (screenHeight - popupViewHeight) / 2 + "px",
  399. left: '15%',
  400. height: popupViewHeight + "px",
  401. width: "70%"
  402. });
  403. // 绘制白色背景
  404. popupView.drawRect({
  405. color: "#FFFFFF",
  406. radius: "8px"
  407. }, {
  408. top: "40px",
  409. height: popupViewHeight - 40 + "px",
  410. });
  411. popupView.draw(popupViewContentList);
  412. popupView.addEventListener("click", function (e) {
  413. let maxTop = popupViewHeight - viewContentPadding;
  414. let maxLeft = popupViewWidth - viewContentPadding;
  415. let buttonWidth = (viewContentWidth - viewContentPadding) / 2;
  416. if (e.clientY > maxTop - 30 && e.clientY < maxTop) {
  417. if (data.updateType == "forcibly") {
  418. if (e.clientX > viewContentPadding && e.clientX < maxLeft) {
  419. // 立即升级
  420. maskLayer.hide();
  421. popupView.hide();
  422. callback && callback();
  423. }
  424. } else {
  425. // 暂不升级
  426. if (e.clientX > viewContentPadding && e.clientX < maxLeft - buttonWidth - viewContentPadding) {
  427. maskLayer.hide();
  428. popupView.hide();
  429. } else if (e.clientX > maxLeft - buttonWidth && e.clientX < maxLeft) {
  430. // 立即升级
  431. maskLayer.hide();
  432. popupView.hide();
  433. callback && callback();
  434. }
  435. }
  436. }
  437. });
  438. if (data.updateType == "solicit") {
  439. // 点击遮罩层
  440. maskLayer.addEventListener("click", function () { //处理遮罩层点击
  441. maskLayer.hide();
  442. popupView.hide();
  443. });
  444. }
  445. // 显示弹窗
  446. maskLayer.show();
  447. popupView.show();
  448. }
  449. // 文件下载的弹窗绘图
  450. const 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 || t('准备下载');
  463. let contentText = data.contentText || t('正在为您更新请耐心等待');
  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: t("升级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: t("取消下载"),
  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: t("后台下载"),
  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: t("关闭"),
  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. const 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 (isPrompt = false) => {
  800. getCurrentNo(async (versionInfo) => {
  801. let httpData = {
  802. release: versionInfo.versionCode,
  803. // 版本名称
  804. version: versionInfo.versionName,
  805. // setupPage参数说明(判断用户是不是从设置页面点击的更新,如果是设置页面点击的更新,有不要用静默更新了,不然用户点击没反应很奇怪的)
  806. setupPage: isPrompt
  807. };
  808. if (platform == "android") {
  809. httpData.type = 1101;
  810. } else {
  811. httpData.type = 1102;
  812. }
  813. /* 接口入参说明
  814. * version: 应用当前版本号(已自动获取)
  815. * versionName: 应用当前版本名称(已自动获取)
  816. * type:平台(1101是安卓,1102是IOS)
  817. */
  818. /****************以下是示例*******************/
  819. // 可以用自己项目的请求方法(接口自己找后台要,插件不提供)
  820. try {
  821. const e = await $post($checkUpdateUrl, httpData)
  822. let res = e.data;
  823. if (res && res.downloadUrl) {
  824. if (res.updateType == "forcibly" || res.updateType == "silent") {
  825. if (/\.wgt$/i.test(res.downloadUrl)) {
  826. getDownload(res);
  827. } else if (/\.html$/i.test(res.downloadUrl)) {
  828. plus.runtime.openURL(res.downloadUrl);
  829. } else {
  830. if (platform == "android") {
  831. getDownload(res);
  832. } else {
  833. plus.runtime.openURL(res.downloadUrl);
  834. }
  835. }
  836. } else if (res.updateType == "solicit") {
  837. updatePopup(res, function () {
  838. if (/\.wgt$/i.test(res.downloadUrl)) {
  839. getDownload(res);
  840. } else if (/\.html$/i.test(res.downloadUrl)) {
  841. plus.runtime.openURL(res.downloadUrl);
  842. } else {
  843. if (platform == "android") {
  844. getDownload(res);
  845. } else {
  846. plus.runtime.openURL(res.downloadUrl);
  847. }
  848. }
  849. });
  850. }
  851. } else if (isPrompt) {
  852. Toast(t("暂无新版本"));
  853. }
  854. } catch (error) {
  855. console.error(error);
  856. }
  857. });
  858. }