import { useLogin } from '@/hooks/useLogin'
import { getToken } from '@/utils/common'
import { getNeedLoginPages } from '@/utils/pages'

const loginBack = useLogin()
/**
 * 检测是否需要登录
 */
export function checkNeedLogin(route : AnyObject) {
	const pages = getNeedLoginPages()

	if (pages.includes(route.path) && !getToken()) {
		setTimeout(() => {
			loginBack.setLoginBack({ url: route.path, param: route.query || {} })
		}, 100)
	}
}
// 检查并请求授权
export function checkAndRequestAuth() {
	// scope.camera 摄像头
	uni.getSetting({
		success(res) {
			if (!res.authSetting['scope.camera']) {
				requestCameraAuthorization()
			} else {
				console.log('已有授权');
			}
		}
	});
	 // 请求摄像头授权
	 function requestCameraAuthorization() {
        uni.authorize({
            scope: 'scope.camera',
            success() {
                console.log('用户已授权');
                uni.showToast({ icon: 'none', title: '可以使用系统摄像头权限' });
            },
            fail(err) {
                console.error('用户拒绝授权', err);
                uni.showToast({ icon: 'none', title: '拒绝授权使用系统摄像头权限' });
            }
        });
    }
}