// 如果带token请求则为绑定，如果不带则为新用户扫码
// 取 Authorization - Bearer 后面的Token值
let userToken = payload.get().headers["Authorization"] || payload.get().query["token"] || payload.get().headers["X-Token"] || (payload.get().cookies ? payload.get().cookie["token"] : null);
if (!userToken) {
    // 通过openid查询用户信息
    let ses = SQL.query('system_sql', 'SELECT * FROM sys_users WHERE user_id = (SELECT user_id FROM sys_users_oauth WHERE open_id = ?)', res.openid);
    if (ses && ses.length) {
        const userData = ses[0]

        let token = jwt_issue(userData.user_id, userData.nickname);
        response.headers.set('Authorization', 'Bearer ' + token)
        return okMsg({ expire: sevenDays, token: token, userId: userData.user_id });
    }
    // SYSTEM_CONFIG.find(item => item.name === 'oauth_safe_secret').value
    const chiper = crypto.aes.encrypt('ECB', 'xMagic-oAuth.Secret.1', JSON.stringify(res))
    return okMsg({ create: true, token: chiper });
}
// 判断userToken是否是数组
if (Array.isArray(userToken)) {
    userToken = userToken[0]
}

// 删除 Bearer 部分
userToken = userToken.replace("Bearer ", "").replace(/\s/g, "");

let tokenInfo = jwt_parse(userToken) // 这里需要密钥进行验证
if (!tokenInfo) {
    return errMsg(511, "身份验证失败");
}

// 判断tokenInfo.exp 是否过期
if (tokenInfo.exp < Date.now() / 1000) {
    return errMsg(511, "身份凭证已过期");
}