index.js 25 KB

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