appUpdate.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //APP更新
  2. import * as config from '../config'
  3. export default function appUpdate() {
  4. uni.request({
  5. url: 'https://api2.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.showToast({
  51. icon:'none',
  52. title:'正在热更新'
  53. })
  54. uni.downloadFile({
  55. url: res.data.data.wgtUrl,
  56. success: (downloadResult) => {
  57. console.log(downloadResult.tempFilePath)
  58. if (downloadResult.statusCode === 200) {
  59. plus.nativeUI.toast(`正在热更新!${res.data.data.versionCode}`);
  60. plus.runtime.install(downloadResult.tempFilePath, {
  61. force: false
  62. }, function() {
  63. plus.nativeUI.toast("热更新成功");
  64. uni.clearStorageSync();
  65. plus.nativeUI.toast("缓存清除成功");
  66. that.$api.doRequest('post', '/auth/api/logout').then(res => {
  67. if (res.data.data) {
  68. that.$store.commit('logout')
  69. that.$api.logout()
  70. plus.nativeUI.toast("登出成功");
  71. plus.runtime.restart();
  72. plus.nativeUI.toast("重启成功");
  73. }
  74. })
  75. plus.runtime.restart();
  76. }, function(e) {
  77. console.log(e)
  78. plus.nativeUI.toast(`热更新失败:${e.message}`);
  79. });
  80. }
  81. }
  82. });
  83. }
  84. });
  85. }
  86. }
  87. })
  88. }