appUpdate.js 2.7 KB

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