
const getToken = () => {
    return new Promise((r) => {
        const oauth_consumer_key = 'DBAndroidClient';
        const clientid = 'AndroidClient';
        const clientsecret = '123456';
        const oauth_consumer_secret = '12e390c3d9c1469c7ad9799cdc471e82';
        const sign = window['duiba_md5'](oauth_consumer_key + clientid + clientsecret + oauth_consumer_secret).toUpperCase();
        const sendData = {
            oauth_consumer_key: oauth_consumer_key,
            oauth_consumer_sig: sign,
            clientid: clientid,
            reqEncryptType: "-1",
            clientsecret: clientsecret
        };
        window['$'].ajax({
            url: "https://csapi.tingmall.com/ContentServiceWS/OAuth/getAccessTokenSig",
            type: 'get',
            async: true,//异步请求
            cache: false,
            contentType: "application/x-www-form-urlencoded",
            data: sendData,//使用变量sendData
            //执行成功的回调函数
            success: (data) => {
                r(data.response.docs.access_token);
            },
            //执行失败或错误的回调函数
            error: (data) => {
                console.log("认证失败");
            }
        });
    })

}

const getCategoryItem = (oauth_token, categoryid) => {
    return new Promise((r) => {
        const sendData = {
            categoryid: categoryid
        };
        window['$'].ajax({
            url: "https://csapi.tingmall.com/ContentServiceWS/CategoryExInfo/getCategoryItem",
            type: 'get',
            async: true,//异步请求
            cache: false,
            contentType: "application/x-www-form-urlencoded",
            data: sendData,//使用变量sendData
            //执行成功的回调函数
            success: (data) => {
                r(data);
            },
            //执行失败或错误的回调函数
            error: (data) => {
                console.log("认证失败");
            },
            beforeSend: (xhr) => {
                xhr.setRequestHeader("oauth_token", oauth_token);
            }
        });
    });
}

const requestMusicUrl = (oauth_token, itemid, songlistid) => {
    const date = new Date();
    const y = date.getFullYear();
    const m = (date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1);
    const d = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate();
    const h = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours();
    const min = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes();
    const ms = date.getSeconds() >= 10 ? date.getSeconds() : '0' + date.getSeconds();
    const startdate = `${y}-${m}-${d}T${h}:${min}:${ms}Z`;
    const enddate = `${y + 1}-${m}-${d}T00:00:00Z`;
    console.log(startdate, enddate);
    return new Promise((r) => {
        const sendData = {
            apimode: "2",
            uid: CFG.uid,
            eventmode: "1Y",
            itemid: itemid,
            startdate: startdate,
            enddate: enddate,
            songlistid: songlistid
        };
        window['$'].ajax({
            url: "https://csapi.tingmall.com/ContentServiceWS/H5EventInfo/requestMusicUrl",
            type: 'get',
            async: true,//异步请求
            cache: false,
            contentType: "application/x-www-form-urlencoded",
            data: sendData,//使用变量sendData
            //执行成功的回调函数
            success: (data) => {
                r(data);
            },
            //执行失败或错误的回调函数
            error: (data) => {
                console.log("认证失败");
            },
            beforeSend: (xhr) => {
                xhr.setRequestHeader("oauth_token", oauth_token);
            }
        });
    });
}

export default () => {
    return new Promise(async (r) => {
        const oauth_token = await getToken();
        const categoryData: any = await getCategoryItem(oauth_token, window['categoryid']);
        const itemid = categoryData.response.docs.stationItems[0].itemID;
        const categoryid = categoryData.response.docs.categoryID;
        const musicData:any = await requestMusicUrl(oauth_token, itemid, categoryid);
        const ele = document.createElement('audio');
        ele.setAttribute('autoplay','');
        ele.setAttribute('loop','');
        ele.setAttribute('preload','');
        ele.setAttribute('id','bgmusic');
        ele.setAttribute('src','http://ks.tingmall.com/content/13/972/13972528-MP3-128K-FTD.mp3?wsSecret=c5e3a5a519f9f5b3c484100df9168c72&wsTime=5eff5602&transDeliveryCode=DB@153237649@1562229050@S@');
        // ele.setAttribute('src',musicData.response.docs.playURL);
        // window['musciURL'] = musicData.response.docs.playURL;
        document.body.append(ele);
    })

}