update-manager.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //http://xixio.cn/2018/08/18/regeneratorruntime-is-not-defined-%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%B8%AD%E4%BD%BF%E7%94%A8async%E5%87%BD%E6%95%B0/
  2. // const regeneratorRuntime = require('../lib/runtime');
  3. import Config from './config';
  4. import {
  5. showLoading,
  6. toast
  7. } from "../utils/utils";
  8. class UpdateManager {
  9. constructor(a, b) {
  10. this.oldVersion = Config.VERSION_CODE; // 实例化时调用 set 方法
  11. this.newVersion = '';
  12. // this.fourceUpdate = false; // 如果通过接口判断出有更新,定义为强制更新
  13. this.timer = null; //延时器
  14. }
  15. checkForUpdate(updateManager) {
  16. // return new Promise((resolve, reject) => {
  17. // updateManager.onCheckForUpdate(function (res) {
  18. // // 请求完新版本信息的回调
  19. // resolve(res.hasUpdate);
  20. // });
  21. // });
  22. return new Promise(async (resolve, reject) => {
  23. // 通过 微信检测更新 和 服务端返回的最新版本号 共同检测更新 (然而,在pc端小程序检测更新方法在触发App生命周期的onLaunch和onShow方法中并不生效)
  24. // Promise.all([this.wxCheckForUpdate(updateManager), this.serviceCheckForUpdate()]).then(res => {
  25. // // console.log(res);
  26. // let [wxCheckUpdate, serviceCheckUpdate] = res;
  27. // let hasUpdate = wxCheckUpdate || serviceCheckUpdate;
  28. // // this.fourceUpdate = serviceCheckUpdate;
  29. // resolve(hasUpdate);
  30. // // resolve(wxCheckUpdate, serviceCheckUpdate);
  31. // }).catch(err => {
  32. // console.log('err', err);
  33. // resolve(false);
  34. // });
  35. let hasUpdate = await this.serviceCheckForUpdate().catch(err => {
  36. resolve(false);
  37. });
  38. resolve(hasUpdate);
  39. });
  40. }
  41. serviceCheckForUpdate() {
  42. let that = this;
  43. return new Promise(resolve => {
  44. try {
  45. wx.request({
  46. url: `${Config.API}/education/app/home/homeIndex/appInfo/${Config.APP_ID}`,
  47. method: 'GET',
  48. header: {
  49. 'content-type': 'application/json',
  50. },
  51. success(res) {
  52. if (res.statusCode === 200 && res.data.code === 200) {
  53. let appVersion = res.data.data.appVersion || '';
  54. let hasUpdate = appVersion.trim() != Config.VERSION_CODE.trim();
  55. that.newVersion = appVersion;
  56. // console.log('res is -> ', res);
  57. console.log('version -> ', res.data.data.appVersion);
  58. console.log('serviceUpdate', hasUpdate);
  59. resolve(hasUpdate);
  60. } else {
  61. toast(res.data.msg);
  62. resolve(false);
  63. }
  64. },
  65. fail(err) {
  66. toast(err.errMsg);
  67. // 网络错误
  68. resolve(false);
  69. }
  70. });
  71. } catch (err) {
  72. resolve(false);
  73. }
  74. })
  75. }
  76. wxCheckForUpdate(updateManager) {
  77. return new Promise((resolve, reject) => {
  78. updateManager.onCheckForUpdate(function (res) {
  79. // 请求完新版本信息的回调
  80. console.log('wxCheckUpdate -> ', res.hasUpdate);
  81. resolve(res.hasUpdate);
  82. });
  83. });
  84. }
  85. fetchUpdate(updateManager) {
  86. return new Promise((resolve, reject) => {
  87. updateManager.onUpdateReady(function () {
  88. resolve(true);
  89. });
  90. updateManager.onUpdateFailed(function () {
  91. resolve(false);
  92. });
  93. // if (this.fourceUpdate) {
  94. // clearTimeout(this.timer);
  95. // this.timer = setTimeout(() => {
  96. // //5s监听不到,就提示用户删除小程序
  97. // resolve(false);
  98. // }, 5000);
  99. // }
  100. clearTimeout(this.timer);
  101. this.timer = setTimeout(() => {
  102. //5s监听不到,就提示用户删除小程序
  103. resolve(false);
  104. }, 5000);
  105. });
  106. }
  107. async update(opts = {}) {
  108. let updateManager;
  109. let alreadyCheckUpdate = opts.hasUpdate || false; //如果已经判断出有更新,不重复调用接口
  110. let needShowLoading = opts.loading || false; //是否需要显示加载动画
  111. if (wx.canIUse('getUpdateManager')) {
  112. updateManager = wx.getUpdateManager();
  113. let hasUpdate = alreadyCheckUpdate;
  114. if(!alreadyCheckUpdate) {
  115. hasUpdate = await this.checkForUpdate(updateManager);
  116. }
  117. console.log('hasUpdate -> ', hasUpdate);
  118. if (hasUpdate) {
  119. if(needShowLoading) {
  120. wx.showLoading({
  121. title: '正在检测更新',
  122. });
  123. }
  124. const fetchOK = await this.fetchUpdate(updateManager);
  125. if(needShowLoading) {
  126. wx.hideLoading({
  127. success: (res) => {},
  128. });
  129. }
  130. if (fetchOK) {
  131. wx.showModal({
  132. title: '已经有新版本了~',
  133. content: '更新小程序',
  134. showCancel: false,
  135. success(res) {
  136. if (res.confirm) {
  137. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  138. updateManager.applyUpdate()
  139. }
  140. }
  141. });
  142. } else {
  143. // 新的版本下载失败或者不支持updateManager
  144. const oldVersion = this.oldVersion;
  145. const newVersion = this.newVersion;
  146. // ps: 换行: \r\n
  147. const tipContent = newVersion ? `我的小程序版本:${oldVersion},\r\n最新小程序版本:${newVersion},\r\n请您删除当前小程序,重新搜索打开~` : '新版本已经上线啦~,请您删除当前小程序,重新搜索打开~';
  148. wx.showModal({
  149. title: '已经有新版本了~',
  150. showCancel: false,
  151. confirmText: '确定',
  152. // content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开~'
  153. content: tipContent,
  154. success(res) {
  155. if (res.confirm) {
  156. //点击确定,退出小程序 触发冷启动
  157. wx.exitMiniProgram();
  158. }
  159. }
  160. });
  161. }
  162. }
  163. }
  164. }
  165. }
  166. export default new UpdateManager();