checkVersion.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * 检查app版本是否需要升级
  3. */
  4. const checkVersion = ({
  5. name, //最新版本名称
  6. code, //最新版本号
  7. content, //更新内容
  8. url, //下载链接
  9. forceUpdate //是否强制升级
  10. }) => {
  11. const str = uni.getSystemInfoSync().appWgtVersion; //当前App版本号
  12. //const selfVersionCode =Version.split('.');
  13. const parts = str.split('.').map(Number);
  14. //console.log(parts)
  15. let selfVersionCode = 0;
  16. for (let i = 0; i < parts.length; i++) {
  17. selfVersionCode += parts[i] * Math.pow(10, (parts.length - 1 - i) ); // 乘以10的幂并累加
  18. }
  19. //线上版本号高于当前,进行在线升级
  20. if (code > selfVersionCode) {
  21. let platform = uni.getSystemInfoSync().platform //手机平台
  22. //安卓手机弹窗升级
  23. //console.log(111)
  24. if (platform === 'android') {
  25. // console.log(222)
  26. // console.log(getCurrentPageRoute())
  27. //当前页面不是升级页面跳转防止多次打开
  28. if (getCurrentPageRoute() !== 'pages/lq-upgrade/upgrade') {
  29. //console.log(333)
  30. uni.navigateTo({
  31. url: '/pages/lq-upgrade/upgrade',
  32. success() {
  33. // console.log(444)
  34. uni.$emit('upgrade-app', {
  35. name,
  36. content,
  37. url,
  38. forceUpdate
  39. })
  40. }
  41. })
  42. }
  43. }
  44. //IOS无法在线升级提示到商店下载
  45. else {
  46. uni.showModal({
  47. title: '发现新版本 ' + newVersionName,
  48. content: '请到App store进行升级',
  49. showCancel: false
  50. })
  51. }
  52. }
  53. }
  54. //获取当前页面url
  55. const getCurrentPageRoute = () => {
  56. let currentRoute;
  57. let pages = getCurrentPages() // 获取栈实例
  58. if (pages&&pages.length) {
  59. currentRoute = pages[pages.length - 1].route;
  60. }
  61. return currentRoute
  62. }
  63. export default checkVersion