autoUpdate.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { getApkVersion } from '@/api/auth'
  2. import { IMGHOST } from '@/config'
  3. export default {
  4. data() {
  5. return {
  6. version:0
  7. };
  8. },
  9. onLoad(){
  10. // #ifdef APP-PLUS
  11. plus.runtime.getProperty(plus.runtime.appid,(wgtinfo)=>{
  12. this.version = wgtinfo.versionCode
  13. });
  14. uni.getSystemInfo({
  15. success:(res) => {
  16. //检测当前平台,如果是安卓则启动安卓更新
  17. if(res.platform=="android"){
  18. this.androidCheckUpdate()
  19. }
  20. }
  21. })
  22. // #endif
  23. },
  24. methods: {
  25. androidCheckUpdate(){
  26. getApkVersion().then(res=>{
  27. if(res.code == 1){
  28. let version = res.data.version
  29. let mustUpdate = res.data.mustUpdate
  30. let updateText = res.data.updateText ? res.data.updateText : '现在更新?'
  31. let singer = mustUpdate == 1 ? true : false
  32. if(version <= this.version){
  33. return false
  34. }
  35. let that = this
  36. that.$util.confirmModal({content:updateText,cancelText:'稍后',title:'有新版本,请到应用市场更新',okText:'好的'},() => {
  37. })
  38. }
  39. })
  40. },
  41. install(version){
  42. console.log(IMGHOST+'/apk/hd/'+version+'.apk')
  43. uni.showToast({ title: '后台下载中...', mask: false, duration: 1200,icon:"none"})
  44. var dtask = plus.downloader.createDownload( IMGHOST+'/apk/hd/'+version+'.apk', {}, function ( d, status ) {
  45. // 下载完成
  46. if ( status == 200 ) {
  47. plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){
  48. uni.showToast({ title: '安装失败', mask: false, duration: 1500,icon:"none" })
  49. })
  50. } else {
  51. uni.showToast({ title: '更新失败', mask: false, duration: 1500,icon:"none" })
  52. }
  53. });
  54. dtask.start()
  55. }
  56. }
  57. }