appUpdate.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //APP更新
  2. import * as config from '../config'
  3. export default function appUpdate() {
  4. let baseUrlNew = config.def().baseUrlNew
  5. uni.request({
  6. url: 'https://apitest.eliangeyun.com/appVersion/selectInfo',
  7. data: {
  8. appid: plus.runtime.appid,
  9. version: plus.runtime.version,
  10. imei: "1",
  11. type:'sj'
  12. },
  13. method: 'GET',
  14. success: (res) => {
  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. plus.runtime.restart();
  63. }, function(e) {
  64. console.log(e)
  65. plus.nativeUI.toast(`热更新失败:${e.message}`);
  66. });
  67. }
  68. }
  69. });
  70. }
  71. });
  72. }
  73. }
  74. })
  75. }