Files
ys-app/src/utils/common.js

208 lines
7.1 KiB
JavaScript
Raw Normal View History

2025-08-15 11:16:08 +08:00
import { showAlert } from '@/utils/message.js'
2025-08-14 20:26:26 +08:00
2025-08-13 18:15:04 +08:00
// 递归算法
2025-08-14 20:26:26 +08:00
export const initTree = (arr, parentId = '0', id) => {
2025-08-13 18:15:04 +08:00
const tree = [];
arr.filter(item => item.parentId === parentId).forEach(item => {
2025-08-14 20:26:26 +08:00
const children = initTree(arr, item[id]);
if (children.length > 0) {
item.children = children;
}
tree.push(item);
2025-08-13 18:15:04 +08:00
});
return tree;
2025-08-14 20:26:26 +08:00
}
2025-08-15 11:16:08 +08:00
2025-08-14 20:26:26 +08:00
// 安卓异步调用
2025-08-15 11:16:08 +08:00
export const requestAndroidPermissionAsync = (systemInfo, permissions) => {
2025-08-14 20:26:26 +08:00
return new Promise((resolve, reject) => {
// if (uni.getSystemInfoSync().platform !== 'android') {
// resolve({ granted: true, nonSystem: true })
// return
// }
2025-08-15 11:16:08 +08:00
plus.android.requestPermissions(permissions, (result) => {
showAlert("result=>" + JSON.stringify(result))
2025-08-14 20:26:26 +08:00
// const granted = Object.values(result.granted).every(Boolean);
let granted = false;
2025-08-15 11:16:08 +08:00
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
2025-08-14 20:26:26 +08:00
}
resolve({ ...result, granted })
2025-08-15 11:16:08 +08:00
}, (error) => {
2025-08-14 20:26:26 +08:00
reject(error)
})
})
}
// 安卓同步调用
export const requestAndroidPermission = (systemInfo) => {
let granted = false;
const Manifest = plus.android.importClass("android.Manifest");
2025-08-15 11:16:08 +08:00
const MainActivity = plus.android.runtimeMainActivity();
if (systemInfo.osVersion < 13) { // 安卓系统版本
let permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
2025-08-14 20:26:26 +08:00
let perStatus = 0;
if (permissionStatus === 0) {
perStatus++;
2025-08-15 11:16:08 +08:00
}
2025-08-14 20:26:26 +08:00
permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionStatus === 0) {
perStatus++;
2025-08-15 11:16:08 +08:00
}
if (perStatus == 2) {
granted = true
} else {
granted = false //"当前文件保存权限被关闭,请到设置中开启才能继续后续操作"
2025-08-15 10:13:39 +08:00
goPermission();//调用授权方法去授权
2025-08-14 20:26:26 +08:00
}
2025-08-15 11:16:08 +08:00
} else if (systemInfo.osVersion == 13) {
let permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_MEDIA_IMAGES);
2025-08-14 20:26:26 +08:00
let perStatus = 0;
if (permissionStatus === 0) {
perStatus++;
2025-08-15 11:16:08 +08:00
}
2025-08-14 20:26:26 +08:00
permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_MEDIA_VIDEO);
if (permissionStatus === 0) {
perStatus++;
}
permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_MEDIA_AUDIO);
if (permissionStatus === 0) {
perStatus++;
}
2025-08-15 11:16:08 +08:00
if (perStatus == 3) {
granted = true
} else {
granted = false //"当前文件保存权限被关闭,请到设置中开启才能继续后续操作"
2025-08-15 10:13:39 +08:00
goPermission();//调用授权方法去授权
2025-08-14 20:26:26 +08:00
}
2025-08-15 11:16:08 +08:00
} else {
let permissionStatus = MainActivity.checkSelfPermission(Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED);
2025-08-14 20:26:26 +08:00
let perStatus = 0;
2025-08-15 11:16:08 +08:00
if (permissionStatus === 0) {
2025-08-14 20:26:26 +08:00
perStatus++;
}
2025-08-15 11:16:08 +08:00
if (perStatus == 1) {
granted = true
} else {
granted = false //"当前文件保存权限被关闭,请到设置中开启才能继续后续操作"
2025-08-15 10:13:39 +08:00
goPermission();//调用授权方法去授权
2025-08-14 20:26:26 +08:00
}
}
2025-08-15 11:16:08 +08:00
2025-08-14 20:26:26 +08:00
return granted
2025-08-15 11:16:08 +08:00
2025-08-14 20:26:26 +08:00
}
2025-08-15 10:13:39 +08:00
// 弹窗跳授权页面
2025-08-15 11:16:08 +08:00
const goPermission = () => {
2025-08-15 10:13:39 +08:00
uni.showModal({
title: '提示',
content: "请进行设备授权",
2025-08-15 11:16:08 +08:00
cancelText: '去授权',
confirmText: '已授权',
2025-08-15 10:13:39 +08:00
success: function (res) {
if (res.confirm) {
uni.reLaunch({
url: '/pages/loading/loading',
});
} else if (res.cancel) {
jumpAuthPermission();
}
}
})
}
2025-08-18 15:50:40 +08:00
// 跳转授权按钮
const jumpAuthPermission=()=>{
2025-08-14 20:26:26 +08:00
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();
2025-08-18 15:50:40 +08:00
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)
}
// 跳转授权
const jumpAuthPermission2 = () => {
let main = plus.android.runtimeMainActivity();
let Intent = plus.android.importClass('android.content.Intent');
let Uri = plus.android.importClass('android.net.Uri');
let Build = plus.android.importClass('android.os.Build');
let Settings = plus.android.importClass('android.provider.Settings');
let PackageManager = plus.android.importClass('android.content.pm.PackageManager');
let pkg = main.getPackageName();
2025-08-18 11:32:35 +08:00
2025-08-18 15:50:40 +08:00
let intent = new Intent();
2025-08-18 11:32:35 +08:00
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
2025-08-18 15:50:40 +08:00
showAlert(Build.VERSION.SDK_INT)
2025-08-18 11:32:35 +08:00
// 尝试直接跳转到存储权限设置Android 13+ 支持)
if (Build.VERSION.SDK_INT >= 33) {
// Android 13+ 尝试直接打开存储权限页面
intent.setAction(Settings.ACTION_MANAGE_APP_PERMISSIONS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, pkg);
// 指定权限类型为存储需要Android 13+支持)
intent.putExtra(Settings.EXTRA_PERMISSION_NAME, "android.permission.READ_EXTERNAL_STORAGE");
}
// Android 11-12 尝试直接跳转到权限列表
else if (Build.VERSION.SDK_INT >= 30) {
intent.setAction(Settings.ACTION_MANAGE_APP_PERMISSIONS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, pkg);
2025-08-18 15:50:40 +08:00
2025-08-18 11:32:35 +08:00
}
// Android 6.0-10 跳转到应用详情页(用户需手动点击权限)
else if (Build.VERSION.SDK_INT >= 23) {
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
2025-08-18 15:50:40 +08:00
let uri = Uri.fromParts("package", pkg, null);
2025-08-18 11:32:35 +08:00
intent.setData(uri);
}
// 低版本系统
else {
intent.setAction(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
}
2025-08-18 15:50:40 +08:00
showAlert("main=>"+main)
showAlert("intent=>"+intent)
2025-08-18 11:32:35 +08:00
main.startActivity(intent);
setTimeout(() => {
uni.reLaunch({
url: '/pages/loading/loading',
});
}, 1000)
} catch (e) {
2025-08-18 15:50:40 +08:00
showAlert(e)
2025-08-18 11:32:35 +08:00
// 异常情况下降级处理
console.error("跳转存储权限页面失败,使用备用方案", e);
2025-08-18 15:50:40 +08:00
let fallbackIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
2025-08-18 11:32:35 +08:00
fallbackIntent.setData(Uri.parse('package:' + pkg));
fallbackIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
main.startActivity(fallbackIntent);
setTimeout(() => {
uni.reLaunch({
url: '/pages/loading/loading',
});
}, 1000)
}
2025-08-14 20:26:26 +08:00
}