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