1. Just copy and paste it (Haoheng)
Write in app.vue file
//Write in app.vue <script> export default {<!-- --> onShow: function() {<!-- --> console.log('App Show') }, onHide: function() {<!-- --> console.log('App Hide') }, onLaunch: function() {<!-- --> let appVersion = '' uni.getSystemInfo({<!-- --> success: function(e) {<!-- --> appVersion = e.platform } }) let that = this; uni.request({<!-- --> url: 'http://xxx.xxx.xxx:3000/xxx/xxx', //Get version number interface. method: "POST", header: {<!-- --> 'custom-header': 'application/json;' //Custom request header information }, success: (res) => {<!-- --> const arr1 = uni.getSystemInfoSync().appVersion.split('.'); const arr2 = res.data.split('.') if (parseInt(arr2[2]) > parseInt(arr1[2])) {<!-- --> uni.showModal({<!-- --> title: "Version Update", content: 'A new version has been detected, do you want to update it', //Update description confirmText: 'Update now', cancelText: 'Proceed later', success: (r) => {<!-- --> if (r.confirm) {<!-- --> //If it is Android, update and download directly if (appVersion === 'android') {<!-- --> uni.showLoading({<!-- --> title: 'Downloading, please wait' }) uni.downloadFile({<!-- --> //The address where the latest installation package is stored url: 'http://xxx.xxx.xxx:3000/apk/xxx/xxx.apk', success: (downloadResult) => {<!-- --> uni.hideLoading(); if (downloadResult.statusCode ===200) {<!-- --> uni.hideLoading(); plus.runtime.install( downloadResult .tempFilePath, {<!-- --> force: false }, function() {<!-- --> console.log( 'install success...' ); plus.runtime .restart(); }, function(e) {<!-- --> uni.hideLoading(); console.error( 'install fail...' ); }); } } }); //If it is ios, jump to the app store } else {<!-- --> //In the app information under the App Store in App Store Connect, you can find the appleId let appleId = plus.runtime.appid plus.runtime.launchApplication({<!-- --> action: `itms-apps://itunes.apple.com/cn/app/id${<!-- -->appleId}?mt=8` }, function(e) {<!-- --> uni.showToast({<!-- --> title: 'Failed to open app store' }) console.log( 'Failed to open app store: ' + e.message); }); } } else if (res.cancel) {<!-- --> console.log('User clicks to cancel'); } } }) } }, }); }, } </script> <style> /*Public css for each page */ </style>
2. Detailed explanation (copying and pasting won’t work, just look at the comments of each line in detail. Trust me, it takes three minutes, which is better than clicking on a bunch of links to check step by step. Hehe! Unless you want to fish. )
<script> export default {<!-- --> onLaunch: function() {<!-- --> let appVersion = '' uni.getSystemInfo({<!-- --> //Get the current apk whether it is Android or ios, success: function(e) {<!-- --> appVersion = e.platform //The value printed by the console is android } }) let that = this; uni.request({<!-- --> url: 'http://xxx.xxx.xxx:3000/xxx/xxx', //Get version number interface. method: "POST", header: {<!-- --> 'custom-header': 'application/json;' //Custom request header information }, success: (res) => {<!-- --> //uni.getSystemInfoSync() returns the apk package information, which is provided by uni and can be printed and viewed by yourself. const arr1 = uni.getSystemInfoSync().appVersion.split('.'); //The version information (1.0.0) returned by the arr2 interface is processed into an array. const arr2 = res.data.split('.') console.log(uni.getSystemInfoSync(), 'This is uni.getSystemInfoSync()') console.log(arr1, 'This is arr1') console.log(arr2, "This is arr2arr2") //parseInt(arr2[2]) > parseInt(arr1[2]) I want to be lazy and only check the size of the last version number. If you want to check the version number in a very regular way, just write your own js logic , judge the first position first, then the second position, and then the third position if (parseInt(arr2[2]) > parseInt(arr1[2])) {<!-- --> uni.showModal({<!-- --> title: "Version Update", content: 'A new version has been detected, do you want to update it', //Update description confirmText: 'Update now', cancelText: 'Proceed later', success: (r) => {<!-- --> if (r.confirm) {<!-- --> //If it is Android, update and download directly if (appVersion === 'android') {<!-- --> uni.showLoading({<!-- --> title: 'Downloading, please wait' }) //This uses downloadFile to download the latest apk package. uni.downloadFile({<!-- --> //The address where the latest installation package is stored url: 'http://xxx.xxx.xxx:3000/apk/xxx/xxx.apk', success: (downloadResult) => {<!-- --> uni.hideLoading(); if (downloadResult.statusCode ===200) {<!-- --> uni.hideLoading(); //What is plus.runtime.install? Don’t ask. I don’t know if I ask, and it still reports an error when running it in the browser. That’s right. Just package it directly on the real machine. I believe you will be fine. plus.runtime.install( downloadResult .tempFilePath, {<!-- --> force: false }, function() {<!-- --> console.log( 'install success...' ); plus.runtime .restart(); }, function(e) {<!-- --> uni.hideLoading(); console.error( 'install fail...' ); }); } } }); //If it is ios, jump to the app store } else {<!-- --> //In the app information under the App Store in App Store Connect, you can find the appleId //I still don’t know about this ios update, because we only do Android. Whether it works or not, try it yourself. let appleId = plus.runtime.appid plus.runtime.launchApplication({<!-- --> action: `itms-apps://itunes.apple.com/cn/app/id${<!-- -->appleId}?mt=8` }, function(e) {<!-- --> uni.showToast({<!-- --> title: 'Failed to open app store' }) console.log( 'Failed to open app store: ' + e.message); }); } } else if (res.cancel) {<!-- --> console.log('User clicks to cancel'); } } }) } }, }); }, } </script>
If you have any questions or better implementation solutions, please comment and discuss them together.