appUpdate.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //APP更新
  2. import * as config from '../config'
  3. export default function appUpdate() {
  4. uni.request({
  5. url: 'http://api1.eliangeyun.com/appVersion/selectInfo',
  6. data: {
  7. appid: plus.runtime.appid,
  8. version: plus.runtime.version,
  9. imei: "1"
  10. },
  11. method: 'GET',
  12. success: (res) => {
  13. if (res.statusCode === 200) {
  14. console.log("uni.request update success",res)
  15. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  16. let client_version = wgtinfo.version
  17. var flag_update = client_version.split(".").splice(0, 2).join(".") != res.data.data.version.split(".").splice(0, 2)
  18. .join(".")
  19. var flag_hot = (Number(client_version.split(".")[2]) < Number(res.data.data.version.split(".")[2])) & !flag_update
  20. console.log("client_version",client_version)
  21. console.log("flag_update",flag_update)
  22. console.log("flag_hot",flag_hot)
  23. if (flag_update) {
  24. console.log("更新弹窗")
  25. // 提醒用户更新
  26. uni.showModal({
  27. title: '更新提示',
  28. content: res.data.data.note,
  29. success: (showResult) => {
  30. if (showResult.confirm) {
  31. plus.nativeUI.toast("正在准备环境,请稍后!");
  32. console.log(res.data.data.url, )
  33. var dtask = plus.downloader.createDownload(res.data.data.url, {
  34. method: 'GET',
  35. filename: '_doc/update/'
  36. }, function(d, status) {
  37. if (status == 200) {
  38. var path = d.filename; //下载apk
  39. plus.runtime.install(path); // 自动安装apk文件
  40. } else {
  41. plus.nativeUI.alert('版本更新失败:' + status);
  42. }
  43. });
  44. dtask.start();
  45. }
  46. }
  47. })
  48. } else if (flag_hot) {
  49. console.log("热更新")
  50. uni.downloadFile({
  51. url: res.data.data.wgtUrl,
  52. success: (downloadResult) => {
  53. console.log(downloadResult.tempFilePath)
  54. if (downloadResult.statusCode === 200) {
  55. plus.nativeUI.toast(`正在热更新!${res.data.data.versionCode}`);
  56. plus.runtime.install(downloadResult.tempFilePath, {
  57. force: false
  58. }, function() {
  59. plus.nativeUI.toast("热更新成功");
  60. uni.clearStorageSync();
  61. plus.nativeUI.toast("缓存清除成功");
  62. that.$api.doRequest('post', '/auth/api/logout').then(res => {
  63. if (res.data.data) {
  64. that.$store.commit('logout')
  65. that.$api.logout()
  66. plus.nativeUI.toast("登出成功");
  67. plus.runtime.restart();
  68. plus.nativeUI.toast("重启成功");
  69. }
  70. })
  71. plus.runtime.restart();
  72. }, function(e) {
  73. console.log(e)
  74. plus.nativeUI.toast(`热更新失败:${e.message}`);
  75. });
  76. }
  77. }
  78. });
  79. }
  80. });
  81. }
  82. }
  83. })
  84. }