diff --git a/src/pages/deviceAuth/deviceAuth.vue b/src/pages/deviceAuth/deviceAuth.vue index 3d2cf17..2a2feec 100644 --- a/src/pages/deviceAuth/deviceAuth.vue +++ b/src/pages/deviceAuth/deviceAuth.vue @@ -79,33 +79,73 @@ import { onLoad } from '@dcloudio/uni-app'; import { v4 as uuidv4 } from 'uuid' import { getNavBarPaddingTop} from '@/utils/system.js'; import { getBindStatus,authDeviceApply,authInputCode } from '@/api/auth.js'; -import {showToast} from '@/utils/message.js' +import {showToast,showAlert} from '@/utils/message.js' import { CLIENT_ID,NETWORK_ENV } from '@/enums/cacheEnums'; import encryptObj from '@/utils/encrypt.js' -const { proxy } = getCurrentInstance(); +import { requestAndroidPermissionAsync } from '@/utils/common.js' +const { proxy } = getCurrentInstance(); import { useUserStore } from '@/stores/user'; const userStore = useUserStore(); +// #ifdef APP-PLUS +// 获取 存储手机的module +const safeSave = uni.requireNativePlugin("Tm-TmSafeSaveFileModule"); +// #endif +let systemInfo = {}; +let deviceId = ref(undefined); onLoad(async(opt) => { console.log("deviceAuth=>onLoad"); uni.setStorageSync('page_cache',true); // uni.preloadPage({url: "/pages/login/login"}); // uni.preloadPage({url: "/pages/home/home"}); - deviceId.value = uni.getStorageSync('app_device_id');// 本地设备ID - await selectBindStatus(); - if(bindStatus.value==2){ - // bindStatus=2-----到等待页面(定时任务查询 10秒查询一次) - timer = setInterval(()=>{ - refreshBindStatus() - },1000*10)//10秒刷新一次 + systemInfo = uni.getSystemInfoSync() + // #ifdef APP-PLUS + try { + // // 授权设备存储 + // let res = {granted:false} + // if(systemInfo.osAndroidAPILevel >=30){ + // let permissions = ['android.permission.READ_MEDIA_IMAGES', 'android.permission.READ_MEDIA_VIDEO', 'android.permission.READ_MEDIA_AUDIO']; + // res = await requestAndroidPermissionAsync(permissions); + // }else{ + // let permissions = ['android.permission.READ_EXTERNAL_STORAGE', 'android.permission.WRITE_EXTERNAL_STORAGE']; + // res = await requestAndroidPermissionAsync(permissions); + // } + + // // 读取设备id + // if (result.granted) { + // console.log('所有权限已授予') + safeSave.getSafeFile({ "key": "app_device_id" }, res2 => { + if (res2.code == 1) { + let deviceId = res2.data; + console.log("读取成功=>",deviceId) + deviceId.value = deviceId; // 本地设备ID + selectBindStatus(deviceId); + if(bindStatus.value==2){ + // bindStatus=2-----到等待页面(定时任务查询 10秒查询一次) + timer = setInterval(()=>{ + refreshBindStatus() + },1000*10)//10秒刷新一次 + } + } else { + showAlert('读取失败:'+res2.msg) + } + }) + // } else { + // showAlert("您还没对设备授权,请授权!") + // } + } catch (error) { + console.error('权限请求出错:', error); + showAlert('权限请求出错:', error) } + // #endif + + }) // 绑定状态(1=已提交、2=等待审核、3=审核通过、4=绑定成功、5=审核拒绝) let bindStatus = ref(undefined); -let deviceId = ref(undefined); let timer = null; // 获取导航栏高度用于内容区域padding const navBarPaddingTop = ref(0); @@ -119,9 +159,9 @@ onUnmounted(() => { }) // 根据设备ID查询设备状态 -let selectBindStatus = async ()=>{ - if(deviceId.value){ - let res = await getBindStatus({uniqCode:deviceId.value}) +let selectBindStatus = async (uniqCode)=>{ + if(uniqCode){ + let res = await getBindStatus({uniqCode}) bindStatus.value = res.bindStatus; //bindStatus=4-----已绑定成功,跳转用户登录页面 if(bindStatus.value==4){ @@ -190,8 +230,6 @@ const submitForm = async() => { deviceId.value = uuidv4(); let clientId = `${CLIENT_ID || 2}`; let password = encryptObj.Encrypt(param.username + clientId + "," + param.password); - - let systemInfo = uni.getSystemInfoSync() let params = { username:param.username, password, @@ -208,38 +246,53 @@ const submitForm = async() => { } let res = await authDeviceApply(params); btnLoading.value = false; - uni.setStorageSync('app_device_id', deviceId.value) - bindStatus.value = res.bindStatus;//默认为 2-等待审核 - // 刷新状态 - refreshBindStatus(); - timer = setInterval(()=>{ - refreshBindStatus() - },1000*10)//10秒刷新一次 + + try { + // 授权设备存储 + // let result = {granted:false} + // if(systemInfo.osAndroidAPILevel >=30){ + // let permissions = ['android.permission.READ_MEDIA_IMAGES', 'android.permission.READ_MEDIA_VIDEO', 'android.permission.READ_MEDIA_AUDIO']; + // result = await requestAndroidPermissionAsync(permissions); + // }else{ + // let permissions = ['android.permission.READ_EXTERNAL_STORAGE', 'android.permission.WRITE_EXTERNAL_STORAGE']; + // result = await requestAndroidPermissionAsync(permissions); + // } + + // if (result.granted) { + // 存储设备id + safeSave.saveSafeFile({ "key": "app_device_id","value": deviceId.value}, res3 => { + showAlert("保存成功=>"+JSON.stringify(res3)); + if (res3.code == 1) { + console.log('保存成功:'+res3.msg) + showAlert("保存成功=>"+res3.msg); + // uni.setStorageSync('app_device_id', deviceId.value) + bindStatus.value = res.bindStatus;//默认为 2-等待审核 + // 刷新状态 + refreshBindStatus(); + timer = setInterval(()=>{ + refreshBindStatus() + },1000*10)//10秒刷新一次 + } else { + showAlert('保存失败:'+res3.msg) + } + }) + // } else { + // showAlert("您还没对设备授权,请授权!") + // } + + } catch (error) { + showAlert('设备处理异常:'+error) + } + }).catch(err => { console.log('表单错误11:', err); btnLoading.value = false; }); }; -// 在UniApp的APP端使用plus.netinfo -let getLocalIPInUniApp=()=>{ - return new Promise((resolve) => { - plus.netinfo.getCurrentInfo((info) => { - const ips = []; - if (info.wifi && info.wifi.ipAddress) { - ips.push(info.wifi.ipAddress); - } - if (info.cellular && info.cellular.ipAddress) { - ips.push(info.cellular.ipAddress); - } - resolve(ips.length ? ips : ['无法获取本地IP']); - }); - }); -} - // 刷新 const handleRefresh = ()=>{ - selectBindStatus(); + selectBindStatus(deviceId.value); } const codes = ref(Array(6).fill('')) @@ -295,7 +348,7 @@ const handleSubmit=()=>{ return; } - let uniqCode = uni.getStorageSync('app_device_id'); + let uniqCode = deviceId.value; let param={ uniqCode, authCode:authCode.value diff --git a/src/pages/home/home.vue b/src/pages/home/home.vue index 3fb6e44..965ba9e 100644 --- a/src/pages/home/home.vue +++ b/src/pages/home/home.vue @@ -153,6 +153,9 @@ import { getWeekStr,formatTimestamp } from '@/utils/datetime.js' import { formatMoney } from '@/utils/formatter.js' import {showLoading,hideLoading} from '@/utils/message.js' + + + onLoad(async(opt) => { uni.setStorageSync('page_cache',true); // initLoad(); diff --git a/src/pages/loading/loading.vue b/src/pages/loading/loading.vue index 5a90278..c9ed5dd 100644 --- a/src/pages/loading/loading.vue +++ b/src/pages/loading/loading.vue @@ -4,14 +4,6 @@ Version {{ appVersion }} - - - - - {{content}}--{{ app_device_id }} - - - @@ -32,16 +24,17 @@ import { ref } from 'vue'; import { onLoad } from '@dcloudio/uni-app'; import { versionCheck,getBindStatus } from '@/api/auth.js'; import { formatIOS } from '@/utils/status.js' +import { requestAndroidPermissionAsync,requestAndroidPermission } from '@/utils/common.js' +import {showAlert} from '@/utils/message.js' import { useUserStore } from '@/stores/user'; const userStore = useUserStore(); // #ifdef APP-PLUS // 获取 存储手机的module -let safeSave = uni.requireNativePlugin("Tm-TmSafeSaveFileModule"); -let app_device_id = ref("7f47cfb4-59e2-4cb9-ac46-9da5e23c4de2"); -let content = ref('') +const safeSave = uni.requireNativePlugin("Tm-TmSafeSaveFileModule"); // #endif +let systemInfo={}; let appVersion = ref("1.0.0");//当前版本号 let newVersion = ref('1.0.0');//最新版本号 let versionVisible=ref(false);//版本弹窗 @@ -62,7 +55,39 @@ onLoad(async(opt) => { // 不更新版本 执行设备ID查询和绑定操作 if(!versionVisible.value){ - selectDeviceId() + try { + // 授权设备存储 + // let result = {granted:false} + // if(systemInfo.osVersion<13){ + // let permissions = ['android.permission.READ_EXTERNAL_STORAGE', 'android.permission.WRITE_EXTERNAL_STORAGE']; + // result = await requestAndroidPermissionAsync(systemInfo,permissions); + // }else if(systemInfo.osVersion==13){ + // let permissions = ['android.permission.READ_MEDIA_IMAGES', 'android.permission.READ_MEDIA_VIDEO', 'android.permission.READ_MEDIA_AUDIO']; + // result = await requestAndroidPermissionAsync(systemInfo,permissions); + // }else{ + // let permissions = ['android.permission.READ_MEDIA_VISUAL_USER_SELECTED']; + // result = await requestAndroidPermissionAsync(systemInfo,permissions); + // } + + // if (result.granted) { + // showAlert("11授权成功!") + // setTimeout(()=>{ + // selectDeviceId(); + // },500) + // } else { + let granted = requestAndroidPermission(systemInfo); + if(granted){ + showAlert("22授权成功!") + setTimeout(()=>{ + selectDeviceId(); + },500) + } + // showAlert("您还没对设备授权,请授权!") + // } + } catch (error) { + console.error('权限请求出错:', error); + showAlert(JSON.stringify(error)) + } } // #endif @@ -82,60 +107,9 @@ onLoad(async(opt) => { }); -// 保存设备ID -const saveValue = ()=>{ - console.log("saveValue",safeSave) - safeSave.saveSafeFile({ - "key": "app_device_id", - "value": app_device_id.value - }, (res) => { - console.log(res); - if (res.code == 1) { - uni.showModal({ - title: "保存成功", - content:res.msg, - success: function (res) { - if (res.confirm) { - content.value("成功=>") - } - } - }) - } else { - uni.showModal({ - title: "保存失败", - content:res.msg, - }) - } - - }) -} - -// 读取设备ID -const getValue =()=>{ - console.log(safeSave) - safeSave.getSafeFile({ - "key": "app_device_id" - }, (res) => { - console.log(res); - if (res.code == 1) { - app_device_id.value = res.data; - content.value = "aaaa==>" - uni.showModal({ - title: "读取成功", - content:res.data, - }) - } else { - uni.showModal({ - title: "读取失败", - content: res.msg, - }) - } - }) -}; - -// 检查版本是否是最新的 -let getOSVesion = async()=>{ - let systemInfo = uni.getSystemInfoSync(); +// 检查版本是否是最新的s +const getOSVesion = async()=>{ + systemInfo = uni.getSystemInfoSync(); let param = { // #ifdef APP-PLUS verNumber:systemInfo.appWgtVersion,//当前版本号 @@ -177,36 +151,49 @@ let getOSVesion = async()=>{ // 查询设备ID const selectDeviceId = async()=>{ // 查询本地缓存的设备状态是否绑定过 - let deviceId = uni.getStorageSync('app_device_id');// 本地设备ID - console.log("uniqCode=>",deviceId) - if(!deviceId){ - setTimeout(()=>{ - uni.reLaunch({ - url: '/pages/deviceAuth/deviceAuth', - }); - },2000) - }else{ - let res = await getBindStatus({uniqCode:deviceId}); - setTimeout(()=>{ - // 绑定状态(1=已提交、2=等待审核、3=审核通过、4=绑定成功、5=审核拒绝) - if(res.bindStatus == 4){ - // 检查是否已登录 并 获取用户信息 - if (userStore.isLogin) { - uni.reLaunch({ - url: '/pages/home/home', - }); - - }else{ - uni.reLaunch({ - url: '/pages/login/login', - }); - } - }else{ - uni.reLaunch({ - url: '/pages/deviceAuth/deviceAuth', + // let deviceId = uni.getStorageSync('app_device_id');// 本地设备ID + try { + // showAlert("safeSave=>"+safeSave) + safeSave.getSafeFile({ "key": "app_device_id" }, res2 => { + // showAlert("读取成功=>"+JSON.stringify(res2)); + if (res2.code == 1) { + let deviceId = res2.data; + console.log("读取成功=>",deviceId) + showAlert("读取成功=>"+deviceId); + getBindStatus({uniqCode:deviceId}).then(res=>{ + let bindStatus = res.bindStatus + setTimeout(()=>{ + // 绑定状态(1=已提交、2=等待审核、3=审核通过、4=绑定成功、5=审核拒绝) + if(bindStatus == 4){ + // 检查是否已登录 并 获取用户信息 + if (userStore.isLogin) { + uni.reLaunch({ + url: '/pages/home/home', + }); + + }else{ + uni.reLaunch({ + url: '/pages/login/login', + }); + } + }else{ + uni.reLaunch({ + url: '/pages/deviceAuth/deviceAuth', + }); + } + },2000) }); + } else { + showAlert('读取失败:'+res2.msg) + setTimeout(()=>{ + uni.reLaunch({ + url: '/pages/deviceAuth/deviceAuth', + }); + },2000) } - },2000) + }) + } catch (error) { + showAlert("catch=>"+error) } } // 下载最新版本 diff --git a/src/pages/login/login.vue b/src/pages/login/login.vue index 2c805e1..bfcf66e 100644 --- a/src/pages/login/login.vue +++ b/src/pages/login/login.vue @@ -84,7 +84,12 @@ import { getNavBarPaddingTop} from '@/utils/system.js' import { useUserStore } from '@/stores/user'; import encryptObj from '@/utils/encrypt.js' import { CLIENT_ID } from '@/enums/cacheEnums'; +import {showAlert} from '@/utils/message.js' +// #ifdef APP-PLUS +// 获取 存储手机的module +const safeSave = uni.requireNativePlugin("Tm-TmSafeSaveFileModule"); +// #endif const userStore = useUserStore() onLoad(async(opt) => { uni.setStorageSync('page_cache',true); @@ -244,14 +249,60 @@ const submitForm = () => { form2.value.validate().then(async param => { param.loginType = activeTab.value; let clientId = `${CLIENT_ID || "2"}`; + param.clientId = clientId; let password = encryptObj.Encrypt(param.username + clientId + "," + param.password); param.password = password; + + // #ifdef H5 param.uniqCode = uni.getStorageSync('app_device_id');//先从缓存取之后处理 '7f47cfb4-59e2-4cb9-ac46-9da5e23c4de2'// - param.clientId = clientId; let res = await login(param); userStore.login(res); uni.switchTab({ url: '/pages/home/home' }) btnLoading.value = false; + // #endif + + // #ifdef APP-PLUS + try { + // 授权设备存储 + // let systemInfo = uni.getSystemInfoSync(); + // let result = {} + // if(systemInfo.osAndroidAPILevel >=30){ + // let permissions = ['android.permission.READ_MEDIA_IMAGES', 'android.permission.READ_MEDIA_VIDEO', 'android.permission.READ_MEDIA_AUDIO']; + // result = await requestAndroidPermissionAsync(permissions); + // }else{ + // let permissions = ['android.permission.READ_EXTERNAL_STORAGE', 'android.permission.WRITE_EXTERNAL_STORAGE']; + // result = await requestAndroidPermissionAsync(permissions); + // } + + // if (result.granted) { + // console.log('所有权限已授予') + safeSave.getSafeFile({ "key": "app_device_id" }, res3 => { + if (res3.code == 1) { + let deviceId = res3.data; + showAlert("读取成功=>"+deviceId); + param.uniqCode = deviceId; + login(param).then(res=>{ + userStore.login(res); + uni.switchTab({ url: '/pages/home/home' }) + btnLoading.value = false; + }) + + } else { + showAlert('读取失败:'+res3.msg) + btnLoading.value = false; + } + }) + // } else { + // showAlert("您还没对设备授权,请授权!") + // btnLoading.value = false; + // } + } catch (error) { + console.error('出错:', error); + showAlert('出错:', error) + btnLoading.value = false; + } + // #endif + }).catch(err => { console.log('表单错误11:', err); btnLoading.value = false; diff --git a/src/utils/common.js b/src/utils/common.js index 9abe064..cc10f11 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -1,12 +1,161 @@ +import {showAlert} from '@/utils/message.js' + // 递归算法 -export const initTree = (arr, parentId='0',id) => { +export const initTree = (arr, parentId = '0', id) => { const tree = []; arr.filter(item => item.parentId === parentId).forEach(item => { - const children = initTree(arr, item[id]); - if (children.length > 0) { - item.children = children; - } - tree.push(item); + const children = initTree(arr, item[id]); + if (children.length > 0) { + item.children = children; + } + tree.push(item); }); return tree; -} \ No newline at end of file +} + +// 安卓异步调用 +export const requestAndroidPermissionAsync = (systemInfo,permissions) => { + return new Promise((resolve, reject) => { + // if (uni.getSystemInfoSync().platform !== 'android') { + // resolve({ granted: true, nonSystem: true }) + // return + // } + plus.android.requestPermissions(permissions,(result) => { + showAlert("result=>"+JSON.stringify(result)) + // const granted = Object.values(result.granted).every(Boolean); + + let granted = false; + if(systemInfo.osVersion<13){ + granted = result.granted.length==2 + } else if(systemInfo.osVersion==13){ + granted =result.granted.length==3 + } else{ + granted = result.granted.length==1 + } + resolve({ ...result, granted }) + + },(error) => { + reject(error) + }) + }) +} + + + +// 安卓同步调用 +export const requestAndroidPermission = (systemInfo) => { + let granted = false; + const Manifest = plus.android.importClass("android.Manifest"); + const MainActivity = plus.android.runtimeMainActivity(); + if(systemInfo.osVersion <13){ // 安卓系统版本 + let permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE); + let perStatus = 0; + if (permissionStatus === 0) { + perStatus++; + } + permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); + if (permissionStatus === 0) { + perStatus++; + } + if(perStatus ==2){ + granted=true + }else{ + granted=false //"当前文件保存权限被关闭,请到设置中开启才能继续后续操作" + uni.showModal({ + title: '提示', + content: "请点击授权", + cancelText:'去设置', + confirmText:'已设置', + success: function (res) { + if (res.confirm) { + uni.reLaunch({ + url: '/pages/loading/loading', + }); + } else if (res.cancel) { + jumpAuthPermission(); + } + } + }) + } + + }else if(systemInfo.osVersion ==13){ + let permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_MEDIA_IMAGES); + let perStatus = 0; + if (permissionStatus === 0) { + perStatus++; + } + permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_MEDIA_VIDEO); + if (permissionStatus === 0) { + perStatus++; + } + permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_MEDIA_AUDIO); + if (permissionStatus === 0) { + perStatus++; + } + if(perStatus ==3){ + granted=true + }else{ + granted=false //"当前文件保存权限被关闭,请到设置中开启才能继续后续操作" + uni.showModal({ + title: '提示', + content: "请点击授权", + cancelText:'去设置', + confirmText:'已设置', + success: function (res) { + if (res.confirm) { + uni.reLaunch({ + url: '/pages/loading/loading', + }); + } else if (res.cancel) { + jumpAuthPermission(); + } + } + }) + } + }else{ + let permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED); + let perStatus = 0; + if (permissionStatus === 0) { + perStatus++; + } + if(perStatus ==1){ + granted=true + }else{ + granted=false //"当前文件保存权限被关闭,请到设置中开启才能继续后续操作" + uni.showModal({ + title: '提示', + content: "请点击授权", + cancelText:'去设置', + confirmText:'已设置', + success: function (res) { + if (res.confirm) { + uni.reLaunch({ + url: '/pages/loading/loading', + }); + } else if (res.cancel) { + jumpAuthPermission(); + } + } + }) + } + } + + return granted + +} + +// 跳转授权按钮 +const jumpAuthPermission=()=>{ + var main = plus.android.runtimeMainActivity(); + var Intent = plus.android.importClass('android.content.Intent'); + var Uri = plus.android.importClass('android.net.Uri'); + var pkg = main.getPackageName(); + var intent = new Intent('android.settings.APPLICATION_DETAILS_SETTINGS'); + intent.setData(Uri.parse('package:' + pkg)); + main.startActivity(intent); + setTimeout(()=>{ + uni.reLaunch({ + url: '/pages/loading/loading', + }); + },500) +}