index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @Descripttion: app检查更新
  3. * @Version: 1.0.0
  4. * @Author: lgen
  5. */
  6. import config from '@/lgen-config.js'
  7. import appDialog from './dialog'
  8. const { timer=24 }=config.upgrade;
  9. class CheckUpate{
  10. // 判断时间戳检测是否更新
  11. judgeTimeStamp(){
  12. uni.getStorage({
  13. key:'appUpdateTime',
  14. success:(res)=>{
  15. const timeLength=1000*60*60*timer;
  16. const currTimeStamp=new Date().getTime();
  17. if((res.data+timeLength)<currTimeStamp){
  18. this.getVersion();
  19. }
  20. },
  21. fail:()=>{
  22. // 获取缓存失败(第一次进入APP)检测更新
  23. this.getVersion();
  24. }
  25. })
  26. }
  27. // 获取当前版本信息
  28. getVersion(){
  29. plus.runtime.getProperty(plus.runtime.appid,(wgtinfo)=>{
  30. CheckUpate.getUrl(wgtinfo.versionCode).then(res=>{
  31. /*
  32. res.status 是否有新版本,0无,1有
  33. res.path 新版 apk 地址
  34. */
  35. if(res.status===1){
  36. const path=res.path || '';
  37. appDialog.show(path)
  38. }
  39. })
  40. this.storageTimeStamp()
  41. });
  42. }
  43. // 缓存检测更新的时间戳
  44. storageTimeStamp(){
  45. const currTimeStamp=new Date().getTime();
  46. uni.setStorage({
  47. key:'appUpdateTime',
  48. data:currTimeStamp
  49. })
  50. }
  51. }
  52. // type检测类型:0自动检测,1用户主动检测
  53. export default function(fn,type=0){
  54. const cu=new CheckUpate()
  55. if(type==1){
  56. cu.getVersion();
  57. }else{
  58. cu.judgeTimeStamp();
  59. }
  60. CheckUpate.getUrl=fn;
  61. }