autoUpdate.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. console.log(res)
  28. if(res.code == 1){
  29. let version = res.data.version
  30. let mustUpdate = res.data.mustUpdate
  31. let singer = mustUpdate == 1 ? true : false
  32. if(version <= this.version){
  33. return false
  34. }
  35. if(plus.networkinfo.getCurrentType()!=3){
  36. let that = this
  37. that.$util.confirmModal({content:'新版本更新',cancelText:'稍后',okText:'使用流量更新'},() => {
  38. this.install(version)
  39. })
  40. }else{
  41. let that = this
  42. that.$util.confirmModal({content:'新版本更新',singer:singer},() => {
  43. this.install(version)
  44. })
  45. }
  46. }
  47. })
  48. },
  49. install(version){
  50. console.log(IMGHOST+'/apk/hd/'+version+'.apk')
  51. uni.showToast({ title: '后台下载中...', mask: false, duration: 1200,icon:"none"})
  52. var dtask = plus.downloader.createDownload( IMGHOST+'/apk/hd/'+version+'.apk', {}, function ( d, status ) {
  53. // 下载完成
  54. if ( status == 200 ) {
  55. plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){
  56. uni.showToast({ title: '安装失败', mask: false, duration: 1500,icon:"none" })
  57. })
  58. } else {
  59. uni.showToast({ title: '更新失败', mask: false, duration: 1500,icon:"none" })
  60. }
  61. });
  62. dtask.start()
  63. }
  64. }
  65. }