appUpdate.js 2.5 KB

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