updateManager.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var updateManager = {};
  2. /**
  3. * 本API返回全局唯一的版本更新管理器对象: updateManager,用于管理小程序更新。
  4. * vk.updateManager.updateReady();
  5. */
  6. updateManager.updateReady = function(obj) {
  7. // #ifdef MP
  8. updateManagerByMP(obj);
  9. // #endif
  10. };
  11. export default updateManager;
  12. function updateManagerByMP (obj={}){
  13. let {
  14. title = "更新提示",
  15. content = "新版本已经准备好,点击更新!",
  16. autoUpdate = true,
  17. showCancel = false,
  18. confirmText = "一键更新"
  19. } = obj;
  20. const updateManager = uni.getUpdateManager();
  21. updateManager.onCheckForUpdate(function (res) {
  22. // 请求完新版本信息的回调
  23. // console.log(res.hasUpdate);
  24. });
  25. updateManager.onUpdateReady(function (res) {
  26. uni.showModal({
  27. title,
  28. content,
  29. showCancel,
  30. confirmText,
  31. success(res) {
  32. if (res.confirm) {
  33. if(typeof obj.success === "function"){
  34. obj.success({
  35. applyUpdate : updateManager.applyUpdate
  36. });
  37. }
  38. if(typeof obj.complete === "function") obj.complete();
  39. if(autoUpdate){
  40. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  41. updateManager.applyUpdate();
  42. }
  43. }
  44. },
  45. });
  46. });
  47. updateManager.onUpdateFailed(function (res) {
  48. // 新的版本下载失败
  49. console.error("onUpdateFailed",res);
  50. if(typeof obj.fail === "function") obj.fail(res);
  51. if(typeof obj.complete === "function") obj.complete();
  52. });
  53. }