autoUpdate.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. let that = this
  27. getApkVersion().then(res=>{
  28. console.log(res)
  29. if(res.code == 1){
  30. let version = res.data.version
  31. let mustUpdate = res.data.mustUpdate
  32. let singer = mustUpdate == 1 ? true : false
  33. if(version <= that.version){
  34. return false
  35. }
  36. if(plus.networkinfo.getCurrentType()!=3){
  37. uni.showModal({
  38. title: '提示',
  39. content: '有新的版本可以更新',
  40. cancelText: "取消",
  41. confirmText: "使用流量更新",
  42. showCancel: false,
  43. success: (res) => {
  44. if(res.confirm) {
  45. that.install(version)
  46. }
  47. }
  48. })
  49. }else{
  50. uni.showModal({
  51. title: '提示',
  52. content: '有新的版本可以更新',
  53. cancelText: "取消",
  54. confirmText: "确认",
  55. showCancel: false,
  56. success: (res) => {
  57. if(res.confirm) {
  58. that.install(version)
  59. }
  60. }
  61. })
  62. }
  63. }
  64. })
  65. },
  66. install(version){
  67. uni.showToast({ title: '后台下载中...', mask: false, duration: 1200,icon:"none"})
  68. console.log(IMGHOST+'/apk/hd/pad'+version+'.apk')
  69. var dtask = plus.downloader.createDownload( IMGHOST+'/apk/hd/pad'+version+'.apk', {}, function ( d, status ) {
  70. // 下载完成
  71. if ( status == 200 ) {
  72. plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){
  73. uni.showToast({ title: '安装失败', mask: false, duration: 1500,icon:"none" })
  74. })
  75. } else {
  76. uni.showToast({ title: '安装失败', mask: false, duration: 1500,icon:"none" })
  77. }
  78. })
  79. dtask.start()
  80. }
  81. }
  82. }