appUpdate.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //APP更新
  2. export default function appUpdate() {
  3. uni.request({
  4. url: 'http://app.jeecg.com/update.json', //检查更新的服务器地址
  5. data: {
  6. appid: plus.runtime.appid,
  7. version: plus.runtime.version,
  8. imei: plus.device.imei
  9. },
  10. success: (res) => {
  11. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  12. let client_version = wgtinfo.version
  13. var flag_update = client_version.split(".").splice(0, 2).join(".") != res.data.version.split(".").splice(0, 2)
  14. .join(".")
  15. var flag_hot = (Number(client_version.split(".")[2]) < Number(res.data.version.split(".")[2])) & !flag_update
  16. console.log(client_version)
  17. console.log(flag_update)
  18. console.log(flag_hot)
  19. if (flag_update) {
  20. // 提醒用户更新
  21. uni.showModal({
  22. title: '更新提示',
  23. content: res.data.note,
  24. success: (showResult) => {
  25. if (showResult.confirm) {
  26. plus.nativeUI.toast("正在准备环境,请稍后!");
  27. console.log(res.data.url, )
  28. var dtask = plus.downloader.createDownload(res.data.url, {
  29. method: 'GET',
  30. filename: '_doc/update/'
  31. }, function(d, status) {
  32. if (status == 200) {
  33. var path = d.filename; //下载apk
  34. plus.runtime.install(path); // 自动安装apk文件
  35. } else {
  36. plus.nativeUI.alert('版本更新失败:' + status);
  37. }
  38. });
  39. dtask.start();
  40. }
  41. }
  42. })
  43. } else if (flag_hot) {
  44. uni.downloadFile({
  45. url: res.data.wgtUrl,
  46. success: (downloadResult) => {
  47. console.log(downloadResult.tempFilePath)
  48. if (downloadResult.statusCode === 200) {
  49. plus.nativeUI.toast(`正在热更新!${res.data.versionCode}`);
  50. plus.runtime.install(downloadResult.tempFilePath, {
  51. force: false
  52. }, function() {
  53. plus.nativeUI.toast("热更新成功");
  54. plus.runtime.restart();
  55. }, function(e) {
  56. console.log(e)
  57. plus.nativeUI.toast(`热更新失败:${e.message}`);
  58. });
  59. }
  60. }
  61. });
  62. }
  63. });
  64. }
  65. })
  66. }