
const https = require('https');
let accessToken = null;
function requestAccessToken(){
    // wxb9db86cc1e1b8ce7
    // 7b693cd53613d50b2fa6a1faaa39b658
    return new Promise((resolve,reject)=>{
        https.get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb9db86cc1e1b8ce7&secret=7b693cd53613d50b2fa6a1faaa39b658',function(res){
            var html = '';
            res.on('data',function(data){
                html += data;
            });
            res.on('end',function(){
                resolve(JSON.parse(html));
            });
        });
    })
}

function init(){
    requestAccessToken().then((res)=>{
        accessToken = res.access_token;
        console.log('accessToken',accessToken);
        setTimeout(()=>{
            init();
        },(res.expires_in-200)*1000)
    })
}

function getAccessToken(){
    return accessToken;
}


module.exports = {
    init,
    getAccessToken
};