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

141 lines
4.6 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)
})
})
}
2025-08-15 10:13:39 +08:00
2025-08-14 20:26:26 +08:00
// 安卓同步调用
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-14 20:26:26 +08:00
// 跳转授权按钮
2025-08-15 11:16:08 +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();
var intent = new Intent('android.settings.APPLICATION_DETAILS_SETTINGS');
intent.setData(Uri.parse('package:' + pkg));
2025-08-15 11:16:08 +08:00
main.startActivity(intent);
setTimeout(() => {
2025-08-14 20:26:26 +08:00
uni.reLaunch({
url: '/pages/loading/loading',
});
2025-08-15 11:16:08 +08:00
}, 1000)
2025-08-14 20:26:26 +08:00
}