// GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
// {"access_token":"ACCESS_TOKEN","expires_in":7200}

function main() {
    let access_token = cache.get('oauth', `wechat_access_token`)
    if (access_token == null) {
        const getAccessTokenFromDB = SQL.query(
            'system_sql',
            "SELECT `value` FROM `sys_configs` WHERE `delete_time` IS NULL AND `name` = 'wechat_access_token' LIMIT 1;",
        );
        if (getAccessTokenFromDB.length == 0) {
            console.mod('user').fn('oauth').proc('wechat').error(`[getQrCode] - AccessToken不存在`)
            return false
        }
        access_token = getAccessTokenFromDB[0].value
        cache.set('oauth', 'wechat_access_token', access_token, 7000 * 1000)

        // console.mod('user').fn('oauth').proc('wechat').error(`[getQrCode] - AccessToken已失效或不存在`)
        // return false
    }

    let eventId = ex.suid().base58()

    const req = {
        url: `https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=${access_token}`,
        method: 'POST',
        data: JSON.stringify({
            action_name: "QR_STR_SCENE",
            expire_seconds: 180,
            action_info: {
                scene: { scene_str: `login-${eventId}` }
            }
        }),
        content_type: 'application/json',
    }

    const res = request.parse(req).body.toObject()
    if (res.url) {
        res.id = eventId
        return okMsg(res)
    }
    return errMsg(503, res)
}