updateApp.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // 下载并安装最新的APP版本
  2. function downloadApp(appUrl) {
  3. // 下载最新版本APP
  4. var dtask = plus.downloader.createDownload(
  5. appUrl,
  6. {
  7. method: "GET",
  8. },
  9. (d, status) => {
  10. if (status == 200) {
  11. plus.runtime.install(
  12. plus.io.convertLocalFileSystemURL(d.filename),
  13. {},
  14. {},
  15. () => {
  16. uni.showToast({
  17. title: "安装失败",
  18. duration: 1500,
  19. icon: "none",
  20. });
  21. },
  22. );
  23. } else {
  24. plus.nativeUI.alert("安装失败,请稍候重试: " + status);
  25. }
  26. },
  27. );
  28. dtask.start();
  29. var prg = 0;
  30. var showLoading = plus.nativeUI.showWaiting("正在下载");
  31. dtask.addEventListener("statechanged", function (task) {
  32. switch (task.state) {
  33. case 1:
  34. showLoading.setTitle("正在下载");
  35. break;
  36. case 2:
  37. showLoading.setTitle("已连接到服务器");
  38. break;
  39. case 3:
  40. prg = parseInt(
  41. (parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100,
  42. );
  43. showLoading.setTitle("版本更新,正在下载" + prg + "% ");
  44. break;
  45. case 4:
  46. plus.nativeUI.closeWaiting();
  47. break;
  48. }
  49. });
  50. }
  51. export { downloadApp };