const API = require("../../../lib/API");
const { BaseStdResponse } = require("../../../BaseStdResponse");
const config = require("../../../config.json");

// 构造网页授权链接
class WXWorkUrl extends API {
    constructor() {
        super();

        this.setMethod("GET");
        this.setPath("/User/WXWorkUrl");
    }

    async onRequest(req, res) {
        const { num, type } = req.query;

        let redirect;
        if(type === 'login')
            redirect = encodeURIComponent(`${config.url}/#/Login/Stage2?num=${num}`);
        else
            redirect = encodeURIComponent(`${config.url}/#/UpdateInfo/BindWXWork?num=${num}`);

        const url = `https://login.work.weixin.qq.com/wwlogin/sso/login?login_type=CorpApp&appid=${config.wxwork.corpid}&redirect_uri=${redirect}&state=STATE&agentid=${config.wxwork.agentid}`
        return res.json({
            ...BaseStdResponse.OK,
            data: { url: url }
        });
    }
}

module.exports.WXWorkUrl = WXWorkUrl;
