import qywx from './qywx';

const NODE_ENV = process.env.NODE_ENV;

console.log('当前环境： ', NODE_ENV);

/**
 * 基础环境配置
 * @type {{BASE_API: string}}
 */
const BASE_CONF = {
    BASE_API: 'http://127.0.0.1:11001',
    PT_NAME: '管理平台',
    CLIENT_ID: 'sys',
    CLIENT_SECRET: 'dafadghfgfdghghdwe',
    WX_OAUTH_STATE: 'wx_oauth_',
};

/**
 * 生成环境下的环境覆盖
 * @type {{}}
 */
const PROD_CONF = {};

let CONF = BASE_CONF;
if (NODE_ENV === 'development') {
    CONF = { ...CONF, ...PROD_CONF };
}
export default CONF;


/**
 * 构造企业微信Oauth认证授权链接
 * @param uri 因为是前端采用hash，#xxx的方式跳转页面，所以把最终跳转的页面放在state中，原样返回
 * @returns {string}
 */
export const qyWxOauthUrl = (uri) => {
    return `${qywx.path}appid=${qywx.appId}&redirect_uri=${encodeURI(qywx.redirect_url)}
    &response_type=code&scope=snsapi_base&state=${CONF.WX_OAUTH_STATE}${uri}#wechat_redirect`;
};

/**
 * 企业微信二维码登录
 * @param uri 登录成功后跳转的路径
 * @returns {string}
 */
export const qyWxQrCodeUri = (uri) => {
    return `https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=${qywx.appId}&agentid=${qywx.agentId}
    &redirect_uri=${qywx.redirect_url}&state=${CONF.WX_OAUTH_STATE}${uri}`;
};

export const getStateUri = (state) => {
    if (!!state) {
        return state.replace(CONF.WX_OAUTH_STATE, '');
    } else {
        return '';
    }
};

export function wxConfig() {
    console.log('wx=>', window.wx);
    window.wx.config({
        beta: true,
        debug: true,
        appId: qywx.appId,
    });
}
