Browse Source

修改详情页面

wangxy 1 month ago
parent
commit
4f784bbc0e
3 changed files with 244 additions and 20 deletions
  1. 64 10
      pages/chanpinneirong/index.vue
  2. 43 10
      pages/login/index.vue
  3. 137 0
      utils/versionUpdate.js

+ 64 - 10
pages/chanpinneirong/index.vue

@@ -4,6 +4,12 @@
 			<text class="nav-bar-title">学习</text>
 		</view>
 		<chanpin1 v-if="chanpinId==1" ref="chanpin1Ref"></chanpin1>
+
+		<tipMiddleDialog ref="tipDialogRef" :closeFlag='false' :title="tipTitle" @confirm-btn="BanbenConfirmBtn"
+			:notClose="true" :content="tipContent"></tipMiddleDialog>
+		<tipMiddleDialog ref="tipDialogRef2" :closeFlag='false' :title="tipTitle" @confirm-btn="BanbenConfirmBtn"
+			:showTip="true" :notClose="true" :content="tipContentAndroid">
+		</tipMiddleDialog>
 		<!-- 底部 tabBar -->
 		<custom-tab-bar :show="true" :current-index="currentTabIndex" />
 	</view>
@@ -25,19 +31,44 @@
 	import {
 		banbenImpinfo
 	} from "@/api/login.js"
+	import tipMiddleDialog from '@/components/dialog/tipMiddleDialog.vue';
+	import {
+		useVersionUpdate
+	} from "@/utils/versionUpdate.js";
+	import {
+		getVersion
+	} from "../../api/login";
+	import config from '../../config.js'
+
 	export default {
+		setup() {
+			const {
+				initDownload
+			} = useVersionUpdate()
+
+			return {
+				initDownload
+			}
+		},
 		data() {
 			return {
 				currentTabIndex: 1,
 				banbenId: '',
-				chanpinId: ''
+				chanpinId: '',
+				tipTitle: '升级提醒',
+				tipContentAndroid: '您的APP不是最新版本,部分功能不能使用,请升级至最新版本!',
+				tipContent: '您的APP不是最新版本,部分功能不能使用,请升级至最新版本!',
+				updateUrl: '',
+				version: config.appInfo.version
 			}
 		},
 		components: {
 			CustomTabBar,
-			chanpin1
+			chanpin1,
+			tipMiddleDialog
 		},
 		onLoad(options) {
+			this.getLoginInit()
 			uni.hideTabBar()
 		},
 		onShow() {
@@ -45,8 +76,8 @@
 			const cacheData = cacheManager.get('contentInfo');
 			if (cacheData) {
 				console.log('使用缓存数据');
-			this.chanpinId = cacheData.chanpinId
-			this.switchUseCache(cacheData)
+				this.chanpinId = cacheData.chanpinId
+				this.switchUseCache(cacheData)
 			} else {
 				console.log('重新请求数据,请求版本内容详情接口');
 				this.getBanbenInfo();
@@ -59,7 +90,30 @@
 			// 页面卸载无需特殊处理
 		},
 		methods: {
-			switchUseCache(data){
+			getLoginInit() {
+				const systemInfo = uni.getSystemInfoSync();
+				getVersion({}).then(res => {
+					if (this.version != res.data.version) {
+						this.updateUrl = res.data.updateUrl
+
+						if (systemInfo.platform == 'ios') {
+							this.$refs.tipDialogRef.handleShow();
+						} else {
+							this.$refs.tipDialogRef2.handleShow();
+						}
+					}
+				})
+			},
+			BanbenConfirmBtn() {
+				const systemInfo = uni.getSystemInfoSync();
+				if (systemInfo.platform == 'ios') {
+					const appStoreUrl = 'itms-apps://itunes.apple.com/app/6741551741'
+					plus.runtime.openURL(appStoreUrl)
+				} else {
+					this.initDownload(this.updateUrl)
+				}
+			},
+			switchUseCache(data) {
 				switch (Number(data.chanpinId)) {
 					case 1:
 						this.$nextTick(() => {
@@ -67,10 +121,10 @@
 						})
 						break;
 					case 2:
-				
+
 						break;
 					default:
-				
+
 				}
 			},
 			switchGetNeiRongInfo(data) {
@@ -91,7 +145,7 @@
 			getBanbenInfo() {
 				banbenImpinfo({}).then(res => {
 					if (res.code == 0) {
-						console.log('res',res)
+						console.log('res', res)
 						if (res.data.chanpinId == 0) {
 							toast('请先进行选课!')
 							uni.switchTab({
@@ -99,8 +153,8 @@
 							})
 							return;
 						}
-						
-						
+
+
 						this.chanpinId = res.data.chanpinId
 						this.switchGetNeiRongInfo(res.data)
 					} else {

+ 43 - 10
pages/login/index.vue

@@ -46,6 +46,12 @@
 		<agree-dialog ref="agreeWxDialog" @confirm-btn="wxConfirmBtn"></agree-dialog>
 		<tel-dialog ref="telDialogRef" @telClose="telClose" @bindBtn="bindBtn" v-if="telDialogFlag"></tel-dialog>
 		<tip-dialog ref="tipDialogRef" :title="tipTitle" :content="tipContent"></tip-dialog>
+
+    <tipMiddleDialog ref="tipDialogRef" :closeFlag='false' :title="tipTitle" @confirm-btn="BanbenConfirmBtn"
+                     :notClose="true" :content="tipContent"></tipMiddleDialog>
+    <tipMiddleDialog ref="tipDialogRef2" :closeFlag='false' :title="tipTitle" @confirm-btn="BanbenConfirmBtn"
+                     :showTip="true" :notClose="true" :content="tipContentAndroid">
+    </tipMiddleDialog>
 	</view>
 </template>
 
@@ -79,6 +85,10 @@
 		error
 	} from "uview-plus";
 	import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
+  import tipMiddleDialog from '@/components/dialog/tipMiddleDialog.vue';
+  import {
+    useVersionUpdate
+  } from "@/utils/versionUpdate.js";
 
 	let indexData = reactive({
 		phoneNumber: null,
@@ -100,8 +110,14 @@
 	const version = config.appInfo.version;
 	const tipDialogRef = ref(null);
 	const uuid = ref(null);
-	const tipTitle = '升级提醒';
-	const tipContent = '您的APP不是最新版本,部分功能不能使用,请升级至最新版本!';
+  const tipTitle = '升级提醒';
+  const tipContentAndroid = '您的APP不是最新版本,部分功能不能使用,请升级至最新版本!'
+  const tipContent = '您的APP不是最新版本,部分功能不能使用,请升级至最新版本!'
+  const tipDialogRef2 = ref(null)
+  const updateUrl = ref(null)
+  const {
+    initDownload
+  } = useVersionUpdate()
 
 	onLoad((options) => {
 		showAppleLogin.value = isIOS13OrAbove();
@@ -120,12 +136,29 @@
 
 	})
 
+  function BanbenConfirmBtn() {
+    const systemInfo = uni.getSystemInfoSync();
+    if (systemInfo.platform == 'ios') {
+      const appStoreUrl = 'itms-apps://itunes.apple.com/app/6741551741'
+      plus.runtime.openURL(appStoreUrl)
+    } else {
+      initDownload(updateUrl.value)
+    }
+  }
+
 	function getLoginInit() {
-		getVersion({}).then(res => {
-			if (version != res.data.version) {
-				tipDialogRef.value.handleShow();
-			}
-		})
+    const systemInfo = uni.getSystemInfoSync();
+    getVersion({}).then(res => {
+      if (version != res.data.version) {
+        updateUrl.value = res.data.updateUrl
+
+        if (systemInfo.platform == 'ios') {
+          tipDialogRef.value.handleShow();
+        } else {
+          tipDialogRef2.value.handleShow();
+        }
+      }
+    })
 	}
 	const getYzmBtn = () => {
 		if (indexData.telStatus === 'login-btn-normal') {
@@ -168,8 +201,8 @@
 		}
 	}
 
-	const ykConfirmBtn = () => {
-		indexData.isAgreed = true;
+	const ykConfirmBtn = () => {
+		indexData.isAgreed = true;
 		ykBtn()
 		// uni.redirectTo({
 		// 	url: `/pages/selectGradesTerms/index`
@@ -241,7 +274,7 @@
 		if (indexData.isAgreed === true) {
 
 			if (!uuid.value) {
-				toast("设备id丢失")
+				toast("设备id丢失")
 				getUUid()
 				return false
 			}

+ 137 - 0
utils/versionUpdate.js

@@ -0,0 +1,137 @@
+import {ref} from "vue"
+import cacheManager from "./cacheManager";
+
+export function useVersionUpdate() {
+
+    const progress = ref(0);
+    let downloadTask = null;
+
+    function checkInstallPermission() {
+        // 获取主Activity对象
+        const mainActivity = plus.android.runtimeMainActivity();
+        // 获取PackageManager对象
+        const packageManager = mainActivity.getPackageManager();
+
+        // 正确方式:使用plus.android.invoke调用方法
+        const canRequest = plus.android.invoke(packageManager, "canRequestPackageInstalls");
+
+        if (canRequest) {
+            console.log("应用有安装权限");
+            return true;
+        } else {
+            console.log("应用无安装权限,需要请求");
+            return false;
+        }
+    }
+
+
+    function requestInstallPermission() {
+        return new Promise((resolve) => {
+            // 跳转到允许安装未知来源应用的设置界面
+            const Intent = plus.android.importClass("android.content.Intent");
+            const Settings = plus.android.importClass("android.provider.Settings");
+
+            const mainActivity = plus.android.runtimeMainActivity();
+            const intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
+
+            // 设置应用的包名
+            const uri = plus.android.invoke(
+                plus.android.invoke("android.net.Uri", "parse", "package:" + plus.runtime.appid)
+            );
+            intent.setData(uri);
+
+            // 启动Activity并处理结果
+            mainActivity.startActivityForResult(intent, 1001);
+
+            // 监听Activity结果(需要在App.vue的onActivityResult中全局监听)
+            plus.globalEvent.addEventListener("onActivityResult", (result) => {
+                if (result.requestCode === 1001) {
+                    // 再次检查权限状态
+                    checkInstallPermission().then(hasPermission => {
+                        resolve(hasPermission);
+                    });
+                }
+            });
+        });
+    }
+
+    function downloadAndInstallApk(apkUrl) {
+        return new Promise((resolve, reject) => {
+            uni.showLoading({ title: '下载中...', mask: true });
+
+            uni.downloadFile({
+                url: apkUrl,
+                success: (downloadResult) => {
+                    uni.hideLoading();
+                    // 清理原始缓存 防止新版本缓存 遭遇原始缓存问题
+                    cacheManager.clearAll();
+                    if (downloadResult.statusCode === 200) {
+                        const tempFilePath = downloadResult.tempFilePath;
+
+                        // 安装APK
+                        plus.runtime.install(tempFilePath, { force: true }, () => {
+                            uni.showToast({ title: '安装成功', icon: 'success' });
+                            setTimeout(() => {
+                                plus.runtime.restart(); // 重启应用
+                            }, 1500);
+                            resolve();
+                        }, (err) => {
+                            console.error('安装失败: ', err);
+                            uni.showToast({ title: '安装失败', icon: 'none' });
+                            reject(err);
+                        });
+                    }
+                },
+                fail: (error) => {
+                    uni.hideLoading();
+                    console.error('下载失败: ', error);
+                    uni.showToast({ title: '下载失败', icon: 'none' });
+                    reject(error);
+                }
+            });
+        });
+    }
+
+
+    async function initDownload(url) {
+        console.log('url下载', url)
+        try {
+            // 1. 检查安装权限
+            const hasPermission = await checkInstallPermission();
+
+            console.log('权限', hasPermission)
+
+            if (!hasPermission) {
+                // 2. 如果没有权限,请求权限
+                uni.showModal({
+                    title: '权限申请',
+                    content: '需要您允许应用安装权限,以便完成版本更新。',
+                    confirmText: '去设置',
+                    success: async (res) => {
+                        if (res.confirm) {
+                            const permissionGranted = await requestInstallPermission();
+                            if (permissionGranted) {
+                                // 3. 权限获取成功,开始下载安装
+                                await downloadAndInstallApk(url);
+                            } else {
+                                uni.showToast({ title: '权限获取失败', icon: 'none' });
+                            }
+                        }
+                    }
+                });
+            } else {
+                // 已经有权限,直接下载安装
+                await downloadAndInstallApk(url);
+            }
+        } catch (error) {
+            console.error('更新流程出错: ', error);
+            uni.showToast({ title: '更新失败', icon: 'none' });
+        }
+    }
+
+    return {
+        requestInstallPermission,
+        initDownload,
+        progress
+    }
+}