// ==========================================================================
// 所属模块：oauth
// 生成日期：2024/10/16
// 生成路径: /api/user/oauth/qrcode/function.js
// 生成人：xScript_Engine
// 数据表：system
// ==========================================================================
response.headers.set("Server-Timing", `PreCheck;dur=${new Date() - payload.get().headers["Timestamp"]}`)

response.headers.add('X-Script-Version', 'login-qrcode-24.10.20')

function main(){
    let ua = payload.get().headers['User-Agent'][0]
    let id = payload.get().path.split('/').pop()
    if (!id){
        return errMsg(400,"错误的参数")
    }
    
    if (ua.includes('DingTalk')){
        let ticket = runtime.call('api/user/oauth/qrcode/dingtalk/getTicket.js',id)
        // console.log(JSON.stringify(ticket))
        runtime.exec('api/user/oauth/qrcode/dingtalk/function.js',id,ticket.code)
        response.headers.set('Location', ticket.url)
        response.status.movedPermanently()
        return okMsg(ticket)
    }
    if (ua.includes('AlipayClient')){
        let ticket = runtime.call('api/user/oauth/qrcode/alipay/getTicket.js',id)
        // console.log(JSON.stringify(ticket))
        runtime.exec('api/user/oauth/qrcode/alipay/function.js',id,ticket.code)
        response.headers.set('Location', ticket.url)
        response.status.movedPermanently()
        return okMsg(ticket)
    }
    return errMsg(405,"不支持的登录方式")
}
