index.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /** 检查是否存在新版本 */
  2. export const useGetUpdateManager = () => {
  3. return new Promise((resolve, reject) => {
  4. uni.getUpdateManager().onCheckForUpdate(function (res) {
  5. // 请求完新版本信息的回调
  6. if (res.hasUpdate) {
  7. //如果有新版本
  8. // 小程序有新版本,会主动触发下载操作(无需开发者触发)
  9. uni.getUpdateManager().onUpdateReady(function () {
  10. //当新版本下载完成,会进行回调
  11. uni.showModal({
  12. title: '更新提示',
  13. content: '新版本已经准备好,单击确定重启应用',
  14. showCancel: false,
  15. success: function (res) {
  16. if (res.confirm) {
  17. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  18. uni.getUpdateManager().applyUpdate();
  19. }
  20. },
  21. });
  22. });
  23. // 小程序有新版本,会主动触发下载操作(无需开发者触发)
  24. uni.getUpdateManager().onUpdateFailed(function () {
  25. //当新版本下载失败,会进行回调
  26. uni.showModal({
  27. title: '提示',
  28. content: '检查到有新版本,但下载失败,请检查网络设置',
  29. showCancel: false,
  30. });
  31. });
  32. }
  33. resolve(res.hasUpdate);
  34. });
  35. });
  36. };