update.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const update = {
  2. check : function(path, call) {
  3. var self = this;
  4. var source = this.source;
  5. if (source != 'app') {
  6. return;
  7. }
  8. var type = plus.os.name.toLowerCase();
  9. self.post('isUpdate', {t:1}, function(res) {
  10. if (type == 'android') {
  11. res.downloadUrl = res.androidUrl;
  12. } else {
  13. res.downloadUrl = res.iosUrl;
  14. }
  15. self.data('dever_update_link', res.downloadUrl);
  16. plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
  17. var version = widgetInfo.versionCode;
  18. var state = false;
  19. state = widgetInfo.versionCode < res.versionCode;
  20. if (state) {
  21. //强制更新
  22. var txt = '正在为您下载更新,下载完成将重启应用';
  23. if (res.forceUpdate == true) {
  24. self.down(res, res.downloadUrl, txt, type);
  25. } else if(call) {
  26. call(res, txt, type);
  27. } else {
  28. uni.showModal({
  29. title: '发现新版本',
  30. content: '有新版本可用 (版本号:' + res.versionName + '),请问您是否更新?',
  31. success: (t) => {
  32. if (t.confirm) {
  33. if (path) {
  34. self.location(path);
  35. } else {
  36. self.down(res, res.downloadUrl, txt, type);
  37. }
  38. }
  39. }
  40. })
  41. }
  42. }
  43. });
  44. });
  45. },
  46. updateCall : function(config, type, packgePath) {
  47. // 保存更新记录到stroage,下次启动app时安装更新
  48. var self = this;
  49. self.data('dever_update', 1);
  50. self.data('dever_update_down', packgePath);
  51. // 任务完成,关闭下载任务,开始安装
  52. plus.runtime.install(packgePath, {force: true}, function() {
  53. self.data('dever_update', 2);
  54. self.data('dever_update_down', 'del');
  55. uni.showModal({
  56. title: '提示',
  57. content: '应用将重启以完成更新',
  58. showCancel: false,
  59. complete: () => {
  60. plus.runtime.restart();
  61. }
  62. })
  63. });
  64. },
  65. down : function(config, url, txt, type, call) {
  66. if (!url) {
  67. return false;
  68. }
  69. if (txt) {
  70. this.alert(txt);
  71. }
  72. if (type != 'android' && !url.match(RegExp(/.wgt/))) {
  73. plus.runtime.openURL(url);
  74. return false;
  75. }
  76. var self = this;
  77. var packgePath = self.data('dever_update_down');
  78. if (packgePath) {
  79. self.updateCall(config, type, packgePath);
  80. return false;
  81. }
  82. var downloadTask = uni.downloadFile({
  83. url: url,
  84. success: (res) => {
  85. if (res.statusCode === 200) {
  86. // 保存下载的安装包
  87. uni.saveFile({
  88. tempFilePath: res.tempFilePath,
  89. success: (res) => {
  90. var packgePath = res.savedFilePath;
  91. if (call) {
  92. call();
  93. }
  94. self.updateCall(config, type, packgePath);
  95. downloadTask.abort();
  96. downloadTask = null;
  97. }
  98. })
  99. }
  100. }
  101. });
  102. return downloadTask;
  103. },
  104. }
  105. export default update;