appUpdate.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if (res.statusCode === 200) {
  15. console.log("uni.request update success",res)
  16. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  17. let client_version = wgtinfo.version
  18. var flag_update = client_version.split(".").splice(0, 2).join(".") != res.data.data.version.split(".").splice(0, 2)
  19. .join(".")
  20. var flag_hot = (Number(client_version.split(".")[2]) < Number(res.data.data.version.split(".")[2])) & !flag_update
  21. console.log("client_version",client_version)
  22. console.log("flag_update",flag_update)
  23. console.log("flag_hot",flag_hot)
  24. if (flag_update) {
  25. console.log("更新弹窗")
  26. // 提醒用户更新
  27. uni.showModal({
  28. title: '更新提示',
  29. content: res.data.data.note,
  30. success: (showResult) => {
  31. if (showResult.confirm) {
  32. plus.nativeUI.toast("正在准备环境,请稍后!");
  33. console.log(res.data.data.url, )
  34. var dtask = plus.downloader.createDownload(res.data.data.url, {
  35. method: 'GET',
  36. filename: '_doc/update/'
  37. }, function(d, status) {
  38. if (status == 200) {
  39. var path = d.filename; //下载apk
  40. plus.runtime.install(path); // 自动安装apk文件
  41. } else {
  42. plus.nativeUI.alert('版本更新失败:' + status);
  43. }
  44. });
  45. dtask.start();
  46. }
  47. }
  48. })
  49. } else if (flag_hot) {
  50. console.log("热更新")
  51. uni.downloadFile({
  52. url: res.data.data.wgtUrl,
  53. success: (downloadResult) => {
  54. console.log(downloadResult.tempFilePath)
  55. if (downloadResult.statusCode === 200) {
  56. plus.nativeUI.toast(`正在热更新!${res.data.data.versionCode}`);
  57. plus.runtime.install(downloadResult.tempFilePath, {
  58. force: false
  59. }, function() {
  60. plus.nativeUI.toast("热更新成功");
  61. plus.runtime.restart();
  62. }, function(e) {
  63. console.log(e)
  64. plus.nativeUI.toast(`热更新失败:${e.message}`);
  65. });
  66. }
  67. }
  68. });
  69. }
  70. });
  71. }
  72. }
  73. })
  74. }