一uniapp自动更新流程逻辑

实现检测版本更新并下载新版本:通过后台返回更新版本的版本号和当前版本号做比较,不同则提示有新版本需要更新,下载地址又后台返回 uniapp自动更新设计思路1. 在服务端配置一个最新的应用版本号;
并将打包生成的 apk(安卓应用) 置于服务器,保证可成功访问的链接
2. 在前端 Uniapp 的最常用的访问页面 ;
设置当前应用的版本号;
image.png
并进行代码编写,判断缓存时间和版本号的大小;
进而通过提示窗口,指导用户实现版本的更新下载等 …

1、客户端检查手机型号

  1. let that = this;
  2. uni.getSystemInfo({
  3. success:(res) => {
  4. console.log(res.platform);
  5. //检测当前平台,如果是安卓则启动安卓更新
  6. if(res.platform=="android"){
  7. that.AndroidCheckUpdate();
  8. }
  9. }
  10. })

2、、检查版本更新差异

进入app登录页面检测系统版本是否是新版本,如果不是新版本,下载安装包,安装新版本。

  1. AndroidCheckUpdate() {
  2. let that = this;
  3. plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
  4. that.version = wgtinfo.version //客户端版本号
  5. console.log('当前app版本信息:' + that.version);
  6. })
  7. that.getUpdateVersion()
  8. },
  9. getUpdateVersion() {
  10. let that = this;
  11. // 获取当前app版本信息
  12. that.$req.get("/appUpdate/queryUpdate", {
  13. }, {}).then(function (res) {
  14. console.log('res.data:' + JSON.stringify(res.data))
  15. console.log("现在的版本"+ that.version +"数据库版本"+ res.data.data.version +"进入查找app版本");
  16. if(res.data.data.version>that.version){
  17. // 这里下载apkurl从/appUpdate/queryUpdate接口请求返回数据中获取
  18. that.downloadUrl = BaseUrl + '/' + res.data.data.androidUrl
  19. // 是否强制更新(0 否;1 是)
  20. that.isForceUpdate = res.data.data.isForceUpdate
  21. uni.showModal({
  22. // 更新提醒
  23. title: '发现新版本,是否更新',
  24. content: '此版本号:'+ that.version + '\xa0\xa0\xa0' + '待更新版本号:' + res.data.data.version,
  25. success: res => {
  26. if (res.confirm) {
  27. that.downWgt();//下载文件
  28. // that.showdownLine = true;
  29. // plus.runtime.openURL(androidUrl)
  30. } else if (res.cancel) {
  31. console.log('that.isForceUpdate:' + that.isForceUpdate);
  32. // 不更新强制退出app
  33. if (that.isForceUpdate == 1) {
  34. console.log('that.isForceUpdate1:' + that.isForceUpdate);
  35. uni.showModal({
  36. // 更新提醒
  37. title: '发现新版本,是否更新',
  38. content: '此版本为强制更新版本如不升级将退出APP',
  39. success: res => {
  40. if (res.confirm) {
  41. console.log('不更新强制退出app');
  42. plus.runtime.quit();
  43. } else if (res.cancel) {
  44. that.AndroidCheckUpdate();
  45. }
  46. }
  47. });
  48. }
  49. }
  50. }
  51. });
  52. //dtask.start();
  53. }
  54. }).catch(error => {
  55. uni.showToast({
  56. title: '调用请求失败',
  57. mask: false,
  58. duration: 5000,
  59. icon:"none"
  60. });
  61. });
  62. complete: () => {}
  63. },

/appUpdate/queryUpdate此服务端接口仅仅请求数据库保存版本更新表返回,最新版本数据,请自行编写。接口返回数据

  1. {
  2. "code":0,
  3. "data":{
  4. "androidUrl":"static/appupload/android_debug.apk",
  5. "appUpdateId":"2",
  6. "iosUrl":"static/appupload/android_debug.apk",
  7. "isForceUpdate":"1",
  8. "updateContent":"测试更新",
  9. "version":"1.0.1",
  10. "versionCode":"20220721"
  11. },
  12. "msg":"Ok",
  13. "time":1658817101226
  14. }

我的返回下载地址指向:127.0.0.1:8088/static/appupload/android_debug.apk,resources下static文件夹已在yml中配置暴露直接访问下载。

image.png

3、确认更新后下载安装包

  1. downWgt() {
  2. let that=this;
  3. console.log('url:' + that.downloadUrl)
  4. uni.showLoading({
  5. title: '更新中……'
  6. })
  7. const downloadTask = uni.downloadFile({//执行下载
  8. url: that.downloadUrl, //下载地址
  9. timeout: 1000 * 30, //30秒超时时间
  10. success: downloadResult => {//下载成功
  11. that.showdownLine = false
  12. uni.hideLoading();
  13. console.log('downloadResult.statusCode' + downloadResult.statusCode)
  14. if (downloadResult.statusCode == 200) {
  15. console.log('更新中')
  16. uni.showModal({
  17. title: '',
  18. content: '更新成功,确定现在重启吗?',
  19. confirmText: '重启',
  20. confirmColor: '#EE8F57',
  21. success: function(res) {
  22. if (res.confirm == true) {
  23. plus.runtime.install(//安装
  24. downloadResult.tempFilePath, {
  25. force: true
  26. },
  27. function(res) {
  28. utils.showToast('更新成功,重启中');
  29. plus.runtime.restart();
  30. }
  31. );
  32. }
  33. }
  34. });
  35. }
  36. },
  37. fail: err => {
  38. uni.hideLoading();
  39. that.showdownLine = false
  40. that.$u.toast(err.errMsg)
  41. console.log(err)
  42. },
  43. complete: com => {
  44. console.log(com)
  45. }
  46. });
  47. // 下载进度
  48. downloadTask.onProgressUpdate(res => {
  49. // that.$u.toast(res.progress)
  50. that.downloadNum = res.progress
  51. console.log('下载进度' + that.downloadNum);
  52. // console.log('已经下载的数据长度' + res.totalBytesWritten);
  53. // console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
  54. // 满足测试条件,取消下载任务。
  55. // if (res.progress > 50) {
  56. // downloadTask.abort();
  57. // }
  58. });
  59. },