123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- //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/
- // const regeneratorRuntime = require('../lib/runtime');
- import Config from './config';
- import {
- showLoading,
- toast
- } from "../utils/utils";
- class UpdateManager {
- constructor(a, b) {
- this.oldVersion = Config.VERSION_CODE; // 实例化时调用 set 方法
- this.newVersion = '';
- // this.fourceUpdate = false; // 如果通过接口判断出有更新,定义为强制更新
- this.timer = null; //延时器
- }
- checkForUpdate(updateManager) {
- // return new Promise((resolve, reject) => {
- // updateManager.onCheckForUpdate(function (res) {
- // // 请求完新版本信息的回调
- // resolve(res.hasUpdate);
- // });
- // });
- return new Promise(async (resolve, reject) => {
- // 通过 微信检测更新 和 服务端返回的最新版本号 共同检测更新 (然而,在pc端小程序检测更新方法在触发App生命周期的onLaunch和onShow方法中并不生效)
- // Promise.all([this.wxCheckForUpdate(updateManager), this.serviceCheckForUpdate()]).then(res => {
- // // console.log(res);
- // let [wxCheckUpdate, serviceCheckUpdate] = res;
- // let hasUpdate = wxCheckUpdate || serviceCheckUpdate;
- // // this.fourceUpdate = serviceCheckUpdate;
- // resolve(hasUpdate);
- // // resolve(wxCheckUpdate, serviceCheckUpdate);
- // }).catch(err => {
- // console.log('err', err);
- // resolve(false);
- // });
- let hasUpdate = await this.serviceCheckForUpdate().catch(err => {
- resolve(false);
- });
- resolve(hasUpdate);
- });
- }
- serviceCheckForUpdate() {
- let that = this;
- return new Promise(resolve => {
- try {
- wx.request({
- url: `${Config.API}/education/app/home/homeIndex/appInfo/${Config.APP_ID}`,
- method: 'GET',
- header: {
- 'content-type': 'application/json',
- },
- success(res) {
- if (res.statusCode === 200 && res.data.code === 200) {
- let appVersion = res.data.data.appVersion || '';
- let hasUpdate = appVersion.trim() != Config.VERSION_CODE.trim();
- that.newVersion = appVersion;
- // console.log('res is -> ', res);
- console.log('version -> ', res.data.data.appVersion);
- console.log('serviceUpdate', hasUpdate);
- resolve(hasUpdate);
- } else {
- toast(res.data.msg);
- resolve(false);
- }
- },
- fail(err) {
- toast(err.errMsg);
- // 网络错误
- resolve(false);
- }
- });
- } catch (err) {
- resolve(false);
- }
- })
- }
- wxCheckForUpdate(updateManager) {
- return new Promise((resolve, reject) => {
- updateManager.onCheckForUpdate(function (res) {
- // 请求完新版本信息的回调
- console.log('wxCheckUpdate -> ', res.hasUpdate);
- resolve(res.hasUpdate);
- });
- });
- }
- fetchUpdate(updateManager) {
- return new Promise((resolve, reject) => {
- updateManager.onUpdateReady(function () {
- resolve(true);
- });
- updateManager.onUpdateFailed(function () {
- resolve(false);
- });
- // if (this.fourceUpdate) {
- // clearTimeout(this.timer);
- // this.timer = setTimeout(() => {
- // //5s监听不到,就提示用户删除小程序
- // resolve(false);
- // }, 5000);
- // }
- clearTimeout(this.timer);
- this.timer = setTimeout(() => {
- //5s监听不到,就提示用户删除小程序
- resolve(false);
- }, 5000);
- });
- }
- async update(opts = {}) {
- let updateManager;
- let alreadyCheckUpdate = opts.hasUpdate || false; //如果已经判断出有更新,不重复调用接口
- let needShowLoading = opts.loading || false; //是否需要显示加载动画
- if (wx.canIUse('getUpdateManager')) {
- updateManager = wx.getUpdateManager();
- let hasUpdate = alreadyCheckUpdate;
- if(!alreadyCheckUpdate) {
- hasUpdate = await this.checkForUpdate(updateManager);
- }
- console.log('hasUpdate -> ', hasUpdate);
- if (hasUpdate) {
- if(needShowLoading) {
- wx.showLoading({
- title: '正在检测更新',
- });
- }
- const fetchOK = await this.fetchUpdate(updateManager);
- if(needShowLoading) {
- wx.hideLoading({
- success: (res) => {},
- });
- }
- if (fetchOK) {
- wx.showModal({
- title: '已经有新版本了~',
- content: '更新小程序',
- showCancel: false,
- success(res) {
- if (res.confirm) {
- // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
- updateManager.applyUpdate()
- }
- }
- });
- } else {
- // 新的版本下载失败或者不支持updateManager
- const oldVersion = this.oldVersion;
- const newVersion = this.newVersion;
- // ps: 换行: \r\n
- const tipContent = newVersion ? `我的小程序版本:${oldVersion},\r\n最新小程序版本:${newVersion},\r\n请您删除当前小程序,重新搜索打开~` : '新版本已经上线啦~,请您删除当前小程序,重新搜索打开~';
- wx.showModal({
- title: '已经有新版本了~',
- showCancel: false,
- confirmText: '确定',
- // content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开~'
- content: tipContent,
- success(res) {
- if (res.confirm) {
- //点击确定,退出小程序 触发冷启动
- wx.exitMiniProgram();
- }
- }
- });
- }
- }
- }
- }
- }
- export default new UpdateManager();
|