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