#include "xysdk_common.h"
#include "xysdk_http_request.h"

#define DEVICE_INFO_JSON_STR    "{\"deviceName\":\"%s\",\"deviceModel\":\"%s\",\"deviceId\":\"%s\"}"
#define PAY_INFO_JSON_STR       "{\"pay_content\":\"%s\",\"album_id\":\"%s\",\"album_type\":%d,\"distribute_item_type\":%d}"
#define USER_INFO_JSON_STR      "{\"uid\":%d,\"access_token\":\"%s\",\"refresh_token\":\"%s\"}"

static int xy_play_url_parse(char *response_data, int len, xy_get_url_resp_model *resp_data)
{
	int ret = XY_HTTP_NET_ERR;
	char *dest=NULL;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *object = NULL;
	xm_cJSON *pSub = NULL;

	if(!response_data || !resp_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}

    object = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(object)
    {
        if(object->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else if(object->valueint== XY_HTTP_REFRESH_TOKEN_ERR_CODE|| object->valueint == XY_HTTP_ACCESS_TOKEN_ERR_CODE)
        {
            ret = XY_HTTP_TOKEN_INVALID;
        }
        else 
        {
            ret = object->valueint;
        }
        goto exit_play_url;
    }

    ret = XY_HTTP_DATA_PRASE_ERR;
    object = xm_cJSON_GetObjectItem(new_obj, "data");
    if(!object)
    {
        goto exit_play_url;
    }

    pSub = xm_cJSON_GetObjectItem(object, "play_url");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->url, sizeof(resp_data->url), "%s", pSub->valuestring);
		extern void himalaya_http_audio_url(u8 *url);
		himalaya_http_audio_url(resp_data->url);
		//snprintf(resp_data->url, sizeof(resp_data->url), "%s", "http://openaudio.cos.tx.xmcdn.com/storages/fb0e-audiofreehighqps/F8/F0/GKwRIW4KpypSAWPuigMH_rAG.mp3");
 
    }
    else
    {	
        goto exit_play_url;
    }

    pSub = xm_cJSON_GetObjectItem(object, "play_size");
    if(pSub && pSub->valueint != 0)
    {
        resp_data->play_size = pSub->valueint;
    }
    else
    {
        goto exit_play_url;
    }

    pSub = xm_cJSON_GetObjectItem(object, "duration");
    if(pSub && pSub->valueint != 0)
    {
        resp_data->play_duration = pSub->valueint;
    }
    else
    {
        goto exit_play_url;
    }
	ret = XY_HTTP_OK;
	
exit_play_url:
	h_post_ui_msg(2);
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}

int xy_api_http_get_play_url(xy_get_url_req_model* req_param, xy_get_url_resp_model *resp_data)
{
    int ret = -1;
    char source_type_str[4]={0};
    char pay_type_str[4]={0};
    uint8_t cnt=0;
    bool is_bought=false;

    if(!req_param || !resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        return XY_HTTP_PARAM_NULL;
    }

    if(req_param->pay_type == 1)
    {
        if(req_param->album_type == 2 || req_param->album_type == 3)  //vip专享 || vip畅听
        {
            is_bought = xy_common_user_is_vip();
        }

        if(!is_bought)
        {
            if(req_param->album_type == 1 || req_param->album_type == 3)  // 单购 || vip畅听
            {
                is_bought = xy_api_http_get_album_is_bought(req_param->album_id);
                if(!is_bought)
                {
                    is_bought = xy_api_http_get_track_is_bought(req_param->track_id);
                }
            }
        }
        
        if(!is_bought)
        {
            return XY_HTTP_PLEASE_PAY;
        }
    }

    do
    {
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            return XY_HTTP_MALLOC_ERR;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
        xy_http_request_setPath(getRequest, XY_HTTP_PLAY_URL_INTERFACE);

        if(req_param->source_type != 1 && req_param->source_type != 2)
        {
            req_param->source_type = 1;
        }
        snprintf(source_type_str, sizeof(source_type_str), "%d", req_param->source_type);
        snprintf(pay_type_str, sizeof(pay_type_str), "%d", req_param->pay_type);
        xy_http_request_addParam(getRequest, "id", req_param->track_id);
        xy_http_request_addParam(getRequest, "source_type", source_type_str);
        xy_http_request_addParam(getRequest, "pay_type", pay_type_str);
        xy_http_request_addParam(getRequest, "access_token", access_token);
        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_play_url_parse(getRequest->response_buff->data, getRequest->response_buff->length, resp_data);
        }
        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);
    return ret;
}

static int xy_temp_token_parse(char *response_data, int len)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *pSub = NULL;
    xy_temp_token_model resp_data = {0};
	if(!response_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}
	
    pSub = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(pSub)
    {
        if(pSub->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else 
        {
            ret = pSub->valueint;
        }
        goto exit_temp_token_parse;
    }

    ret = XY_HTTP_DATA_PRASE_ERR;
    pSub = xm_cJSON_GetObjectItem(new_obj, "access_token");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data.temp_token, sizeof(resp_data.temp_token), "%s", pSub->valuestring);
        resp_data.temp_token_update_time = xy_api_get_sys_time();
    }
    else
    {	
        goto exit_temp_token_parse;	
    }
    XY_LOG_I("resp_data.temp_token: %s", resp_data.temp_token);
    pSub = xm_cJSON_GetObjectItem(new_obj, "expires_in");
    if(pSub && pSub->valueint != 0)
    {
        resp_data.expires_in = pSub->valueint;
    }
    xy_common_save_temp_token(&resp_data);
	ret = XY_HTTP_OK;
	
exit_temp_token_parse:
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}
int xy_api_http_get_temp_token(void)
{
    int ret = XY_HTTP_UNKNOW_ERR;

    xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        return XY_HTTP_MALLOC_ERR;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
    xy_http_request_setPath(getRequest, XY_HTTP_TEMP_TOKEN_INTERFACE);
    
    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_temp_token_parse(getRequest->response_buff->data, getRequest->response_buff->length);
    }
    xy_http_request_destroy(getRequest);
    
    return ret;
}

static int xy_qrcode_parse(char *response_data, int len, xy_get_qrcode_resp_model *resp_data)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *object = NULL;
	xm_cJSON *pSub = NULL;

	if(!response_data || !resp_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}

    
    object = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(object)
    {
        if(object->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else 
        {
            ret = object->valueint;
        }
        goto exit_qrcode_parse;
    }

	ret = XY_HTTP_DATA_PRASE_ERR;
    object = xm_cJSON_GetObjectItem(new_obj, "data");
    if(!object)
    {
        goto exit_qrcode_parse;
    }

    pSub = xm_cJSON_GetObjectItem(object, "qrcode_content");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->qrcode_url, sizeof(resp_data->qrcode_url), "%s", pSub->valuestring);
        XY_LOG_I("resp_data->qrcode_url: %s",resp_data->qrcode_url);
    }
    else
    {	
        goto exit_qrcode_parse;
    }

    pSub = xm_cJSON_GetObjectItem(object, "sid");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->qrcode_id, sizeof(resp_data->qrcode_id), "%s", pSub->valuestring);
        XY_LOG_I("resp_data->qrcode_id: %s",resp_data->qrcode_id);
    }
    else
    {	
        goto exit_qrcode_parse;
    }

    pSub = xm_cJSON_GetObjectItem(object, "title");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->qrcode_title, sizeof(resp_data->qrcode_title), "%s", pSub->valuestring);
    }

    pSub = xm_cJSON_GetObjectItem(object, "text");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->qrcode_text, sizeof(resp_data->qrcode_text), "%s", pSub->valuestring);
    }
	extern void h_cpy_qr_mess(char * buf,char * name);
	h_cpy_qr_mess(resp_data->qrcode_url,resp_data->qrcode_text);
	ret = XY_HTTP_OK;
	
exit_qrcode_parse:
	h_post_ui_msg(4);//未登录
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}

int xy_api_http_get_login_pay_qrcode(xy_get_qrcode_req_model* req_param, xy_get_qrcode_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_buy = false;
    bool is_login = xy_common_user_is_login();
    char *user_info_str = NULL;
    char *pay_info_str = NULL;
    xy_http_request_model *getRequest = NULL;

    if(!req_param || !resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }

    char *device_info_str=(char *)xy_common_malloc(XY_MAX_STR_LEN_256);
    if(!device_info_str)
    {
        ret = XY_HTTP_MALLOC_ERR;
        goto exit_qrcode;
    }
    snprintf(device_info_str, XY_MAX_STR_LEN_256, DEVICE_INFO_JSON_STR, req_param->device_info.device_name, req_param->device_info.device_model,xy_api_get_device_id());

    if(!strcmp(req_param->action, "buy"))//支付二维码
    {
        is_buy = true;
    }

    if(is_buy)
    {
        pay_info_str = (char *)xy_common_malloc(XY_MAX_STR_LEN_256);
        if(!pay_info_str)
        {
            ret = XY_HTTP_MALLOC_ERR;
            goto exit_qrcode;
        }
        snprintf(pay_info_str, XY_MAX_STR_LEN_256, PAY_INFO_JSON_STR, req_param->pay_info.pay_content, req_param->pay_info.album_id, req_param->pay_info.album_type, req_param->pay_info.distribute_type);
    }

    if(is_login)  //已经登录的需要传入user_info
    {
        xy_user_info_model *user_info = xy_common_get_user_info();
        if(!user_info)
        {
            ret = XY_HTTP_PARAM_NULL;
            goto exit_qrcode;
        }

        user_info_str = (char *)xy_common_malloc(XY_MAX_STR_LEN_256);
        if(!user_info_str)
        {
            ret = XY_HTTP_MALLOC_ERR;
            goto exit_qrcode;
        }
        snprintf(user_info_str, XY_MAX_STR_LEN_256, USER_INFO_JSON_STR,user_info->user_id, user_info->access_token, user_info->refresh_token);
    }

    getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        ret =  XY_HTTP_MALLOC_ERR;
        goto exit_qrcode;;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
    xy_http_request_setPath(getRequest, XY_HTTP_QRCODE_INTERFACE);
    xy_http_request_addParam(getRequest, "action", req_param->action);
    xy_http_request_addParam(getRequest, "device_info", device_info_str);
    if(is_login)
    {
        xy_http_request_addParam(getRequest, "is_login", "true");
        xy_http_request_addParam(getRequest, "user_info", user_info_str);
    }
    else
    {
        xy_http_request_addParam(getRequest, "is_login", "false");
    }
    if(xy_common_user_is_vip())
    {
        xy_http_request_addParam(getRequest, "is_vip", "true");
    }
    else
    {
        xy_http_request_addParam(getRequest, "is_vip", "false");
    }
    if(is_buy)
    {
        xy_http_request_addParam(getRequest, "pay_info", pay_info_str);
    }

    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_qrcode_parse(getRequest->response_buff->data, getRequest->response_buff->length, resp_data);
    }
exit_qrcode:
    xy_http_request_destroy(getRequest);
    
    xy_common_free((void **)&device_info_str);

    if(is_login)
    {
        xy_common_free((void **)&user_info_str);
    }  
    if(is_buy)
    {
        xy_common_free((void **)&pay_info_str);
    } 
    
    return ret;
}

static int xy_qrcode_status_parse(char *response_data, int len, xy_qrcode_status_resp_model *resp_data)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
    xm_cJSON *data_obj = NULL;
    xm_cJSON *info_obj = NULL;
	xm_cJSON *object = NULL;
	xm_cJSON *pSub = NULL;
	xm_cJSON *arrayItem = NULL;
    xm_cJSON *cmd=NULL;
    xm_cJSON *root=NULL;
    xm_cJSON *sub_object=NULL;
    int i=0;
	int size = 0;
    int get_info_type = 0;  // 1:支付  2：登录  3：登录+支付
    int ret_info_type = 0;  // 1:支付  2：登录  3：登录+支付
    
	if(!response_data || !resp_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	root = xm_cJSON_Parse(response_data);
	if(!root)
	{
		return XY_HTTP_NET_ERR;
	}

    new_obj = xm_cJSON_GetObjectItem(root, "error_no");
    if(new_obj)
    {
        if(new_obj->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else 
        {
            ret = new_obj->valueint;
        }
        goto exit_status_parse;
    }

    ret = XY_HTTP_DATA_PRASE_ERR;
    new_obj = xm_cJSON_GetObjectItem(root, "data");
	if(!new_obj)
	{
		goto exit_status_parse;
	}
	
    arrayItem = xm_cJSON_GetObjectItem(new_obj, "wait_cmds");
    if(!arrayItem)
	{
		goto exit_status_parse;
	}
    size = xm_cJSON_GetArraySize(arrayItem);
    for (i = 0; i < size; i++) 
    {
        cmd = xm_cJSON_GetArrayItem(arrayItem, i);
        if(cmd && cmd->valuestring && strlen(cmd->valuestring))
        {
            XY_LOG_I("cmd->valuestring: %s", cmd->valuestring);
            if(!strcmp(cmd->valuestring, "pay"))
            {
                get_info_type |= 0x01; //需要支付信息
            }

            if(!strcmp(cmd->valuestring, "user_login"))
            {
                get_info_type |= 0x02; //需要登录信息
            }
        }
    }
    XY_LOG_I("get_info_type: %d", get_info_type);

    data_obj = xm_cJSON_GetObjectItem(new_obj, "status");
    if(data_obj)
    {
        resp_data->status = data_obj->valueint;
    }

    data_obj = xm_cJSON_GetObjectItem(new_obj, "text");
    if(data_obj && data_obj->valuestring && strlen(data_obj->valuestring))
    {
        snprintf(resp_data->status_text, sizeof(resp_data->status_text), "%s", data_obj->valuestring);
    }


    data_obj = xm_cJSON_GetObjectItem(new_obj, "cmd_data");
    if(!data_obj)
    {
        goto exit_status_parse;
    }
    if(get_info_type == 1 || get_info_type == 3)
    {
        info_obj = xm_cJSON_GetObjectItem(data_obj, "pay");
        if(info_obj && info_obj->valuestring && strlen(info_obj->valuestring))
        {
            sub_object = xm_cJSON_Parse(info_obj->valuestring);
            if(sub_object)
            {
                object = xm_cJSON_GetObjectItem(sub_object, "pay_status");
                if(object)
                {
                    resp_data->pay_status = object->valueint;
                    ret_info_type |= 0x01; //拿到支付信息
                }
            }
        }
        if(get_info_type == 1)
        {
            resp_data->login_status = 2;
        }
    }

    if(get_info_type == 2 || get_info_type == 3)
    {
        info_obj = xm_cJSON_GetObjectItem(data_obj, "user_login");
        if(info_obj && info_obj->valuestring && strlen(info_obj->valuestring))
        {
            sub_object = xm_cJSON_Parse(info_obj->valuestring);
            if(sub_object)
            {
                xy_user_info_model user_info={0};
                pSub = xm_cJSON_GetObjectItem(sub_object, "access_token");
                if(pSub && pSub->valuestring && strlen(pSub->valuestring))
                {
                    snprintf(user_info.access_token, sizeof(user_info.access_token), "%s", pSub->valuestring);
                    user_info.access_token_update_time = xy_api_get_sys_time();
                    user_info.expires_in = XY_ACCESS_TOKEN_TIMEOUT_DEFAULT;
                }
                else
                {
                    goto exit_status_parse;
                }

                pSub = xm_cJSON_GetObjectItem(sub_object, "refresh_token");
                if(pSub && pSub->valuestring && strlen(pSub->valuestring))
                {
                    snprintf(user_info.refresh_token, sizeof(user_info.refresh_token), "%s", pSub->valuestring);
                    user_info.refresh_token_update_time = xy_api_get_sys_time();
                }
                else
                {
                    goto exit_status_parse;
                }

                pSub = xm_cJSON_GetObjectItem(sub_object, "uid");
                if(pSub && pSub->valueint)
                {
                    user_info.user_id = pSub->valueint;
                    XY_LOG_I("user_info.user_id : %d", user_info.user_id);
                }
                else
                {
                    goto exit_status_parse;
                }

                pSub = xm_cJSON_GetObjectItem(sub_object, "nick_name");
                if(pSub && pSub->valuestring && strlen(pSub->valuestring))
                {
                    snprintf(user_info.nick_name, sizeof(user_info.nick_name), "%s", pSub->valuestring);
                }

                #ifdef XYSDK_SUPPORT_IMAGE_DOWNLOAD
                pSub = xm_cJSON_GetObjectItem(sub_object, "logo_pic");
                if(pSub && pSub->valuestring && strlen(pSub->valuestring))
                {
                    snprintf(user_info.avatar_url, sizeof(user_info.avatar_url), "%s", pSub->valuestring);
                }
                #endif

                xy_common_set_user_info(&user_info);
                resp_data->login_status = 1;
                ret_info_type |= 0x02; //拿到登录信息
            }
        }
    }
    
    if(get_info_type == ret_info_type)
    {
        ret = XY_HTTP_OK;
    }
    else
    {
        ret = XY_HTTP_LOG_PAY_INFO_ERR;
    }
	
	
exit_status_parse:
    
    if (sub_object) 
	{
        xm_cJSON_Delete(sub_object);
        sub_object = NULL;
    }

	if (root) 
	{
        xm_cJSON_Delete(root);
        root = NULL;
    }

	return ret;
}

int xy_api_http_get_login_pay_status(char *qrcode_id, xy_qrcode_status_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;

    if(!resp_data || !qrcode_id)
    {
        return XY_HTTP_PARAM_NULL;
    }

    xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        return XY_HTTP_MALLOC_ERR;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
    xy_http_request_setPath(getRequest, XY_HTTP_QRCODE_CHECK_INTERFACE);
    xy_http_request_addParam(getRequest, "sid", qrcode_id);
    XY_LOG_I("qrcode_id: %s", qrcode_id);
    xy_http_request_addParam(getRequest, "ts", xy_api_get_timestamp_ms_str());
    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_qrcode_status_parse(getRequest->response_buff->data, getRequest->response_buff->length, resp_data);
    }
    xy_http_request_destroy(getRequest);
    
    return ret;
}

static int xy_logout_parse(char *response_data, int len)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *pSub = NULL;

	if(!response_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}
	
    ret = XY_HTTP_DATA_PRASE_ERR;
    pSub = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(pSub)
    {
        if(pSub->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else 
        {
            ret = pSub->valueint;
        }
    }
    else
    {
        pSub = xm_cJSON_GetObjectItem(new_obj, "code");
        if(pSub)
        {
            if(pSub->valueint == 0)
            {
                ret = XY_HTTP_OK;
                xy_common_clear_user_info();
            }
        }
    }

	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}
int xy_api_http_user_logout(uint8_t type)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    char logout_type_str[4]={0};
    xy_http_request_model *getRequest = NULL;
    xy_user_info_model *user_info = xy_common_get_user_info();
    if(!user_info)
    {
        return XY_HTTP_PARAM_NULL;
    }

    char *user_info_str = (char *)xy_common_malloc(XY_MAX_STR_LEN_256);
    if(!user_info_str)
    {
        return XY_HTTP_MALLOC_ERR;
    }
    snprintf(user_info_str, XY_MAX_STR_LEN_256, USER_INFO_JSON_STR,user_info->user_id, user_info->access_token, user_info->refresh_token);
    
    getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        ret =  XY_HTTP_MALLOC_ERR;
        goto exit_logout;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_POST);
    xy_http_request_setPath(getRequest, XY_HTTP_USER_LOGOUT_INTERFACE);
    snprintf(logout_type_str, sizeof(logout_type_str), "%d", type);
    xy_http_request_addParam(getRequest, "logout_type", logout_type_str);
    xy_http_request_addParam(getRequest, "main_device_id", xy_api_get_device_id());
    xy_http_request_addParam(getRequest, "user_info", user_info_str);
    xy_http_request_addParam(getRequest, "is_main", "true");
    
    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_logout_parse(getRequest->response_buff->data, getRequest->response_buff->length);
    }


exit_logout:

    xy_http_request_destroy(getRequest);

    xy_common_free((void **)&user_info_str); 
 
    return ret;
}

static int xy_refresh_access_token_parse(char *response_data, int len)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *pSub = NULL;

	if(!response_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}
	
    pSub = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(pSub)
    {
        if(pSub->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else 
        {
            ret = pSub->valueint;
        }
        goto exit_refresh_token_parse;
    }

    ret = XY_HTTP_DATA_PRASE_ERR;
    xy_user_info_model *user_info=xy_common_get_user_info();
    pSub = xm_cJSON_GetObjectItem(new_obj, "access_token");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(user_info->access_token, sizeof(user_info->access_token), "%s", pSub->valuestring);
        user_info->access_token_update_time = xy_api_get_sys_time();
    }
    else
    {
        goto exit_refresh_token_parse;
    }

    pSub = xm_cJSON_GetObjectItem(new_obj, "refresh_token");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(user_info->refresh_token, sizeof(user_info->refresh_token), "%s", pSub->valuestring);
        user_info->refresh_token_update_time = xy_api_get_sys_time();
    }
   

    pSub = xm_cJSON_GetObjectItem(new_obj, "expires_in");
    if(pSub)
    {
        user_info->expires_in = pSub->valueint;
    }
    xy_common_set_user_info(user_info);
        
	ret = XY_HTTP_OK;
	
exit_refresh_token_parse:
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}

int xy_api_http_refresh_access_token(void)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();


    if(!is_login)
    {
        return XY_HTTP_PARAM_NULL;
    }

    char *refresh_token = xy_common_get_refresh_token();
    if(!refresh_token)
    {
        xy_api_http_user_logout(2);
        xy_common_clear_user_info();
        return XY_HTTP_LOGIN_INVALID;
    }

    xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        return XY_HTTP_MALLOC_ERR;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
    xy_http_request_setPath(getRequest, XY_HTTP_REFRESH_TOKEN_INTERFACE);
    xy_http_request_addParam(getRequest, "refresh_token", refresh_token);

    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_refresh_access_token_parse(getRequest->response_buff->data, getRequest->response_buff->length);
    }

    xy_http_request_destroy(getRequest);
    return ret;
}

static int xy_user_vip_info_parse(char *response_data, int len)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *pSub = NULL;

	if(!response_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}
	
	ret = XY_HTTP_DATA_PRASE_ERR;
    pSub = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(pSub)
    {
        if(pSub->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else if(pSub->valueint== XY_HTTP_REFRESH_TOKEN_ERR_CODE|| pSub->valueint == XY_HTTP_ACCESS_TOKEN_ERR_CODE)
        {
            ret = XY_HTTP_TOKEN_INVALID;
        }
        else 
        {
            ret = pSub->valueint;
        }
    }
    else
    {
        xy_user_vip_info vip_info={0};
        pSub = xm_cJSON_GetObjectItem(new_obj, "vip_expired_at");
        if(pSub)
        {
            vip_info.vip_expires_in =  (uint32_t)(pSub->valuedouble/1000);
            xy_api_timestamp_to_date(vip_info.vip_expires_in_str, vip_info.vip_expires_in);
        }
    
        pSub = xm_cJSON_GetObjectItem(new_obj, "is_vip");
        if(pSub)
        {
            vip_info.is_vip = pSub->valueint;
            xy_common_set_user_vip_info(&vip_info);
            ret = XY_HTTP_OK;
        }
    }
    
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}

int xy_api_http_user_vip_info(void)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();
    uint8_t cnt = 0;

    if(!is_login)
    {
        return XY_HTTP_LOGIN_INVALID;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        if(!xy_common_user_is_login())
        {
            return XY_HTTP_LOGIN_INVALID;
        }
        return XY_HTTP_PARAM_NULL;
    }

    do
    {
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            return XY_HTTP_MALLOC_ERR;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
        xy_http_request_setPath(getRequest, XY_HTTP_USER_VIP_INFO_INTERFACE);
        xy_http_request_addParam(getRequest, "access_token", access_token);

        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_user_vip_info_parse(getRequest->response_buff->data, getRequest->response_buff->length);
        }

        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);

		h_post_ui_msg(6);
    return ret;
}

//album_type  0:收藏列表， 1：收听历史， 2：已购列表
static int xy_user_albumlist_resp(char *response_data, int len, int page, int album_type, xy_get_source_list_resp_model *resp_data)
{
	xm_cJSON *new_obj = NULL;
	xm_cJSON *object = NULL;
	xm_cJSON *pSub = NULL;
	xm_cJSON *arrayItem = NULL;
    xm_cJSON *activity_obj = NULL;
    bool is_activity = false;
	int i = 0;
    int is_paid;
    int is_vipfree;
    int is_vip_exclusive;
	int ret = XY_HTTP_NET_ERR;
	int offset =0;
	int j = 0;

	if(!response_data || !resp_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}
	
	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}

    arrayItem = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(arrayItem)
    {
        if(arrayItem->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else if(arrayItem->valueint== XY_HTTP_REFRESH_TOKEN_ERR_CODE|| arrayItem->valueint == XY_HTTP_ACCESS_TOKEN_ERR_CODE)
        {
            ret = XY_HTTP_TOKEN_INVALID;
        }
        else 
        {
            ret = arrayItem->valueint;
        }
        goto exit_get_user_albumlist;
    }

    ret = XY_HTTP_DATA_PRASE_ERR;
	arrayItem = xm_cJSON_GetObjectItem(new_obj, "total_count");
	if(arrayItem)
	{
		resp_data->source_total = arrayItem->valueint;
	}
	else
	{
		resp_data->source_total = 0;
	}
	
	if(resp_data->source_total==0)
	{
        ret = XY_HTTP_SERVER_DATA_ENPTY;
		goto exit_get_user_albumlist;
	}
    if(resp_data->source_total > XY_ALBUMLIST_MAX_NUM)
    {
        resp_data->source_total = XY_ALBUMLIST_MAX_NUM;
    }
    resp_data->data_type = 0;
    resp_data->cur_page = page;

    if(album_type == 0)
    {
        arrayItem = xm_cJSON_GetObjectItem(new_obj, "subscribe_albums");
    }
    else if(album_type == 1)
    {
        arrayItem = xm_cJSON_GetObjectItem(new_obj, "play_histories");
    }
    else if(album_type == 2)
    {
        arrayItem = xm_cJSON_GetObjectItem(new_obj, "values");
    }
	if(arrayItem)
	{
		resp_data->source_num = xm_cJSON_GetArraySize(arrayItem);
	}
	else
	{
		resp_data->source_num = 0;
        ret = XY_HTTP_SERVER_DATA_ENPTY;
		goto exit_get_user_albumlist;
	}

	for(i = 0; i<resp_data->source_num; i++)
	{
		object=xm_cJSON_GetArrayItem(arrayItem,i);
		if(!object)
		{
			offset++;
			continue;
		}
		j = i - offset;

		is_paid = 0;
		is_vipfree = 0;
		is_vip_exclusive = 0;
		pSub = xm_cJSON_GetObjectItem(object, "is_paid");
		if(pSub)
		{
			is_paid = pSub->valueint;
		}

		pSub = xm_cJSON_GetObjectItem(object, "is_vipfree");
		if(pSub)
		{
			is_vipfree = pSub->valueint;
		}

		pSub = xm_cJSON_GetObjectItem(object, "is_vip_exclusive");
		if(pSub)
		{
			is_vip_exclusive = pSub->valueint;
		}

		if(is_paid)
		{
			if((!is_vipfree)&&(!is_vip_exclusive))
			{
				resp_data->source_array[j].pay_type = 2;
			}
			else
			{
				resp_data->source_array[j].pay_type = 1;
			}
		}
		else
		{
			resp_data->source_array[j].pay_type = 0;
		}

		pSub = xm_cJSON_GetObjectItem(object, "is_activity");
		if(pSub)
		{
			is_activity = pSub->valueint;
		}

		if(is_activity)
		{
			pSub = xm_cJSON_GetObjectItem(object, "AlbumActivity");
			if(pSub)
			{
				activity_obj = xm_cJSON_GetObjectItem(pSub, "activity_type");
				if(activity_obj && activity_obj->valueint==2)
				{
					resp_data->source_array[j].pay_type = 3;
				}
			}    
		}

		XY_LOG_I("[%d]pay_type: %d", j, resp_data->source_array[j].pay_type);

		if(album_type == 1)
		{
			pSub = xm_cJSON_GetObjectItem(object, "album_id");
		}
		else
		{
			pSub = xm_cJSON_GetObjectItem(object, "id");
		}
		if(pSub)
		{
			snprintf(resp_data->source_array[j].album_id, sizeof(resp_data->source_array[j].album_id), "%.0lf", pSub->valuedouble);
			XY_LOG_I("[%d]album_id: %s", j, resp_data->source_array[j].album_id);
		}
		else
		{
			offset++;
			continue;
		}

		pSub = xm_cJSON_GetObjectItem(object, "album_title");
		if(pSub && pSub->valuestring)
		{
			snprintf(resp_data->source_array[j].title, sizeof(resp_data->source_array[j].title), "%s", pSub->valuestring);
		}
		else
		{
			snprintf(resp_data->source_array[j].title, sizeof(resp_data->source_array[j].title), "%s", "未知");
		}
		if(album_type==0)
		{
			extern void himalaya_favorite_list_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * album_id,u8 *track_id);
			himalaya_favorite_list_info_cpy(resp_data->source_total,resp_data->source_array[j].pay_type,j,resp_data->source_array[j].title,resp_data->source_array[j].album_id,NULL);
		}
		if(album_type==1)
		{
			extern void himalaya_history_list_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * album_id,u8 *track_id);
			himalaya_history_list_info_cpy(resp_data->source_total,resp_data->source_array[j].pay_type,j,resp_data->source_array[j].title,resp_data->source_array[j].album_id,NULL);
		}

#ifdef XYSDK_SUPPORT_IMAGE_DOWNLOAD
		pSub = xm_cJSON_GetObjectItem(object, "cover_url_small");
		if(pSub && pSub->valuestring && strlen(pSub->valuestring))
		{
			snprintf(resp_data->source_array[j].image, sizeof(resp_data->source_array[j].image), "%s", pSub->valuestring);
		}
#endif

		if(album_type == 0) //收藏列表解析timeline
		{
			pSub = xm_cJSON_GetObjectItem(object, "timeline");
			if(pSub)
			{
				snprintf(resp_data->source_array[j].timeline, sizeof(resp_data->source_array[j].timeline), "%.0lf", pSub->valuedouble);
			}
		}
		else if(album_type == 1) //收听历史获取断点
		{
			pSub = xm_cJSON_GetObjectItem(object, "track_title");// 直接拿到track_title
			if(pSub && pSub->valuestring)
			{
				snprintf(resp_data->source_array[j].title, sizeof(resp_data->source_array[j].title), "%s", pSub->valuestring);
			}
			else
			{
				snprintf(resp_data->source_array[j].title, sizeof(resp_data->source_array[j].title), "%s", "未知");
			}
			pSub = xm_cJSON_GetObjectItem(object, "track_id");
			if(pSub)
			{
				snprintf(resp_data->source_array[j].history_track_id, sizeof(resp_data->source_array[j].history_track_id), "%.0lf", pSub->valuedouble);
			}

			pSub = xm_cJSON_GetObjectItem(object, "break_second");
			if(pSub)
			{
				resp_data->source_array[j].break_point = pSub->valueint;
			}
		}

		resp_data->source_array[j].index = (resp_data->cur_page-1)*XY_ALBUM_NUM_PER_PAGE + j;


	}
	
	resp_data->source_num -= offset;
	ret = XY_HTTP_OK;
	
exit_get_user_albumlist:
	if(album_type==1)
	{
		h_post_ui_msg(6);
	}
	else if(album_type==0)
	{
		h_post_ui_msg(7);
	}
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }
	return ret;

}
int xy_api_http_get_user_subscribe_list(xy_get_subscribe_req_model* req_param, xy_get_source_list_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();
    char offset_str[4] = {0};
    uint8_t cnt = 0;

    if(!req_param || !resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }

    if(!is_login)
    {
        return XY_HTTP_LOGIN_INVALID;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        if(!xy_common_user_is_login())
        {
            return XY_HTTP_LOGIN_INVALID;
        }
        return XY_HTTP_PARAM_NULL;
    }

    do
    {
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            return XY_HTTP_MALLOC_ERR;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
        xy_http_request_setPath(getRequest, XY_HTTP_USER_SUBSCRIBE_LIST_INTERFACE);
        xy_http_request_addParam(getRequest, "timeline", req_param->timeline);
        snprintf(offset_str, sizeof(offset_str), "%d", req_param->limit);    
        xy_http_request_addParam(getRequest, "offset", offset_str);
        xy_http_request_addParam(getRequest, "access_token", access_token);
        if(req_param->contains_paid)
        {
            xy_http_request_addParam(getRequest, "contains_paid", "true");
        }

        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_user_albumlist_resp(getRequest->response_buff->data, getRequest->response_buff->length, req_param->page, 0, resp_data);
        }

        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);
    return ret;
}

static int xy_subcribe_ctrl_parse(char *response_data, int len)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *pSub = NULL;

	if(!response_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}
	
	ret = XY_HTTP_DATA_PRASE_ERR;
    pSub = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(pSub)
    {
        if(pSub->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else if(pSub->valueint== XY_HTTP_REFRESH_TOKEN_ERR_CODE|| pSub->valueint == XY_HTTP_ACCESS_TOKEN_ERR_CODE)
        {
            ret = XY_HTTP_TOKEN_INVALID;
        }
        else 
        {
            ret = pSub->valueint;
        }
    }
    else
    {
        pSub = xm_cJSON_GetObjectItem(new_obj, "code");
        if(pSub)
        {
            if(pSub->valueint == 0)
            {
                ret = XY_HTTP_OK;
            }
        }
    }
    
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}

int xy_api_http_get_subcribe_ctrl(char *album_id, uint8_t operation_type)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();
    char operation_type_str[4] = {0};
    uint8_t cnt = 0;
    if(!album_id)
    {
        return XY_HTTP_PARAM_NULL;
    }

    if(!is_login)
    {
        return XY_HTTP_LOGIN_INVALID;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        if(!xy_common_user_is_login())
        {
            return XY_HTTP_LOGIN_INVALID;
        }
        return XY_HTTP_PARAM_NULL;
    }

    do
    {
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            return XY_HTTP_MALLOC_ERR;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_POST);
        xy_http_request_setPath(getRequest, XY_HTTP_SUBSCRIBE_CTRL_INTERFACE);
        xy_http_request_addParam(getRequest, "album_id", album_id);
        snprintf(operation_type_str, sizeof(operation_type_str), "%d", operation_type);    
        xy_http_request_addParam(getRequest, "operation_type", operation_type_str);
        xy_http_request_addParam(getRequest, "access_token", access_token);

        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_subcribe_ctrl_parse(getRequest->response_buff->data, getRequest->response_buff->length);
        }

        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);
    return ret;
}

static int xy_subcribe_status_parse(char *response_data, int len, char *album_id)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *pSub = NULL;

	if(!response_data || len==0 || !album_id)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}
	
    ret = XY_HTTP_DATA_PRASE_ERR;
    pSub = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(pSub)
    {
        if(pSub->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else if(pSub->valueint== XY_HTTP_REFRESH_TOKEN_ERR_CODE|| pSub->valueint == XY_HTTP_ACCESS_TOKEN_ERR_CODE)
        {
            ret = XY_HTTP_TOKEN_INVALID;
        }
        else 
        {
            ret = pSub->valueint;
        }
    }
    else
    {
        pSub = xm_cJSON_GetObjectItem(new_obj, album_id);
        if(pSub)
        {
            if(pSub->valueint == 1)
            {
                ret = XY_HTTP_OK;
            }
        }
    }
    
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}

bool xy_api_http_get_album_is_subcribe(char *album_id)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();
    uint8_t cnt=0;

    if(!album_id)
    {
        return false;
    }

    if(!is_login)
    {
        return false;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        if(!xy_common_user_is_login())
        {
            return false;
        }
        return false;
    }

    do
    {
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            return false;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_POST);
        xy_http_request_setPath(getRequest, XY_HTTP_ALBUM_IS_SUBCRIBE_INTERFACE);
        xy_http_request_addParam(getRequest, "ids", album_id);
        xy_http_request_addParam(getRequest, "access_token", access_token);

        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_subcribe_status_parse(getRequest->response_buff->data, getRequest->response_buff->length, album_id);
        }

        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);
    return ret==0?true:false;
}

int xy_api_http_get_user_history_list(xy_get_history_req_model* req_param, xy_get_source_list_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();
    char page_str[6] = {0};
    char count_str[4] = {0};
    uint8_t cnt = 0;

    if(!req_param || !resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }

    if(!is_login)
    {
        return XY_HTTP_LOGIN_INVALID;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        if(!xy_common_user_is_login())
        {
            return XY_HTTP_LOGIN_INVALID;
        }
        return XY_HTTP_PARAM_NULL;
    }

    do
    {
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            return XY_HTTP_MALLOC_ERR;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
        xy_http_request_setPath(getRequest, XY_HTTP_USER_HISTORY_LIST_INTERFACE);
        snprintf(page_str, sizeof(page_str), "%d", req_param->page);    
        xy_http_request_addParam(getRequest, "page", page_str);
        snprintf(count_str, sizeof(count_str), "%d", req_param->limit);    
        xy_http_request_addParam(getRequest, "count", count_str);
        xy_http_request_addParam(getRequest, "access_token", access_token);
        if(req_param->contains_paid)
        {
            xy_http_request_addParam(getRequest, "contains_paid", "true");
        }

        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_user_albumlist_resp(getRequest->response_buff->data, getRequest->response_buff->length, req_param->page, 1, resp_data);
        }

        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);

    return ret;
}


static int xy_bought_status_parse(char *response_data, int len, char *id)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *object = NULL;
    xm_cJSON *pSub = NULL;
    int bought_status = 0;
    char id_tmp[XY_MAX_ID_LEN] = {0};
    int size = 0;
    int i = 0;

	if(!response_data || len==0 || !id)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}
	
    pSub = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(pSub && pSub->valueint!=0)
    {
        if(pSub->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else if(pSub->valueint== XY_HTTP_REFRESH_TOKEN_ERR_CODE|| pSub->valueint == XY_HTTP_ACCESS_TOKEN_ERR_CODE)
        {
            ret = XY_HTTP_TOKEN_INVALID;
        }
        else 
        {
            ret = pSub->valueint;
        }
        goto exit_get_pay_status;
    }
    
    ret = XY_HTTP_DATA_PRASE_ERR;
	size = xm_cJSON_GetArraySize(new_obj);
	for(i = 0; i < size; i++)
	{
		pSub = xm_cJSON_GetArrayItem(new_obj,i);
		if(!pSub)
		{
			goto exit_get_pay_status;
		}
	
		object = xm_cJSON_GetObjectItem(pSub, "has_bought");
		if(object)
		{
            bought_status = object->valueint;
        }
        XY_LOG_I("bought_status: %d", bought_status);
        object = xm_cJSON_GetObjectItem(pSub, "id");
		if(object)
		{
            snprintf(id_tmp, sizeof(id_tmp), "%.0lf", object->valuedouble);
            if(!strcmp(id, id_tmp) && bought_status==1)
            {
                ret = XY_HTTP_OK;
            }
        }
	}
    
exit_get_pay_status:
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}

bool xy_api_http_get_album_is_bought(char *album_id)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();
    uint8_t cnt = 0;

    if(!album_id)
    {
        return false;
    }

    if(!is_login)
    {
        return false;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        if(!xy_common_user_is_login())
        {
            return false;
        }
        return false;
    }

    do
    {
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            return false;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
        xy_http_request_setPath(getRequest, XY_HTTP_ALBUM_IS_BOUGHT_INTERFACE);
        xy_http_request_addParam(getRequest, "ids", album_id);
        xy_http_request_addParam(getRequest, "access_token", access_token);

        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_bought_status_parse(getRequest->response_buff->data, getRequest->response_buff->length, album_id);
        }

        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);

    return ret==0?true:false;
}


bool xy_api_http_get_track_is_bought(char *track_id)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();
    uint8_t cnt = 0;

    if(!track_id)
    {
        return false;
    }

    if(!is_login)
    {
        return false;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        if(!xy_common_user_is_login())
        {
            return false;
        }
        return false;
    }

    do
    {
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            return false;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
        xy_http_request_setPath(getRequest, XY_HTTP_TRACK_IS_BOUGHT_INTERFACE);
        xy_http_request_addParam(getRequest, "ids", track_id);
        xy_http_request_addParam(getRequest, "access_token", access_token);

        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_bought_status_parse(getRequest->response_buff->data, getRequest->response_buff->length, track_id);
        }

        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);

    return ret==0?true:false;
}

int xy_api_http_get_user_bought_list(xy_get_bought_req_model* req_param, xy_get_source_list_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();
    char page_str[6] = {0};
    char count_str[4] = {0};
    uint8_t cnt=0;

    if(!req_param || !resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }

    if(!is_login)
    {
        return XY_HTTP_LOGIN_INVALID;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        if(!xy_common_user_is_login())
        {
            return XY_HTTP_LOGIN_INVALID;
        }
        return XY_HTTP_PARAM_NULL;
    }

    do
    {    
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            return XY_HTTP_MALLOC_ERR;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
        xy_http_request_setPath(getRequest, XY_HTTP_USER_BOUGHT_LIST_INTERFACE);
        snprintf(page_str, sizeof(page_str), "%d", req_param->page);    
        xy_http_request_addParam(getRequest, "page", page_str);
        snprintf(count_str, sizeof(count_str), "%d", req_param->limit);    
        xy_http_request_addParam(getRequest, "count", count_str);
        xy_http_request_addParam(getRequest, "access_token", access_token);
        if(req_param->exclude_not_permission)
        {
            xy_http_request_addParam(getRequest, "exclude_not_permission", "true");
        }

        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_user_albumlist_resp(getRequest->response_buff->data, getRequest->response_buff->length, req_param->page, 2, resp_data);
        }

        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);
    return ret;
}

static int xy_data_upload_parse(char *response_data, int len)
{
	int ret = XY_HTTP_NET_ERR;
	xm_cJSON *new_obj = NULL;
	xm_cJSON *pSub = NULL;

	if(!response_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}

    ret = XY_HTTP_DATA_PRASE_ERR;
    pSub = xm_cJSON_GetObjectItem(new_obj, "error_no");
    if(pSub)
    {
        if(pSub->valueint==XY_HTTP_SIG_ERR_CODE)
        {
            ret = XY_HTTP_SIG_ERR;
        }
        else if(pSub->valueint== XY_HTTP_REFRESH_TOKEN_ERR_CODE|| pSub->valueint == XY_HTTP_ACCESS_TOKEN_ERR_CODE)
        {
            ret = XY_HTTP_TOKEN_INVALID;
        }
        else 
        {
            ret = pSub->valueint;
        }
    }
	else
    {
        pSub = xm_cJSON_GetObjectItem(new_obj, "code");
        if(pSub)
        {
            if(pSub->valueint == 0)
            {
                ret = XY_HTTP_OK;
            }
        }
    }
    
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }

	return ret;
}

static char *xysdk_post_track_history(xy_play_info_upload_model *tracks)
{
	int i;
	xm_cJSON *object = NULL;
	char *trackInfo = NULL;
	xm_cJSON *track_object = NULL;
    if(!tracks)
    {
        return NULL;
    }

    track_object=xm_cJSON_CreateArray();
    for(i=0; i<tracks->count; i++)
    {
        object=xm_cJSON_CreateObject();
        
        xm_cJSON_AddNumberToObject(object, "content_type", 1);
        xm_cJSON_AddStringToObject(object, "album_id", tracks->play_array[i].album_id);
        xm_cJSON_AddStringToObject(object, "track_id", tracks->play_array[i].track_id);
        xm_cJSON_AddStringToObject(object, "play_begin_at", tracks->play_array[i].started_at);
        xm_cJSON_AddStringToObject(object, "play_end_at", tracks->play_array[i].end_at);
        xm_cJSON_AddNumberToObject(object, "break_second", tracks->play_array[i].break_secs);

        xm_cJSON_AddItemToArray(track_object,object);
        
    }
    trackInfo = (char *)xm_cJSON_Print(track_object);

    xm_cJSON_Delete(track_object);

	return trackInfo;
}

int xy_api_http_upload_play_history(xy_play_info_upload_model* req_param)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    bool is_login = xy_common_user_is_login();
    uint8_t cnt=0;
    
    if(!req_param)
    {
        return XY_HTTP_PARAM_NULL;
    }

    if(!is_login)
    {
        return XY_HTTP_LOGIN_INVALID;
    }

    char *access_token = xy_common_get_access_token();
    if(!access_token)
    {
        if(!xy_common_user_is_login())
        {
            return XY_HTTP_LOGIN_INVALID;
        }
        return XY_HTTP_PARAM_NULL;
    }
    char *history_json = xysdk_post_track_history(req_param);
    if(!history_json)
    {
        return XY_HTTP_PARAM_NULL;
    }

    do
    {
        xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
        if(getRequest == NULL) {
            xy_common_free((void **)&history_json);
            return XY_HTTP_MALLOC_ERR;
        }
        xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_POST);
        xy_http_request_setPath(getRequest, XY_HTTP_USER_HISTORY_UPLOAD_INTERFACE);
        xy_http_request_addParam(getRequest, "access_token", access_token);
        xy_http_request_addParam(getRequest, "play_history_records", history_json);
        //xy_http_request_setJsonBody(getRequest, history_json);
        ret = xy_http_request_request(getRequest);
        if(ret == 0 && getRequest->response_buff)
        {
            ret = xy_data_upload_parse(getRequest->response_buff->data, getRequest->response_buff->length);
        }

        xy_http_request_destroy(getRequest);
        if(ret == XY_HTTP_TOKEN_INVALID && cnt == 0)
        {
            access_token = xy_common_get_access_token();
            if(!access_token)
            {
                break;
            }
            cnt++;
        }
        else
        {
            break;
        }
    }while(1);
    xy_common_free((void **)&history_json);
    
    return ret;
}

static char *xysdk_post_track_records(xy_play_info_upload_model *tracks)
{
	int i;
	xm_cJSON *object = NULL;
	char *trackInfo = NULL;
	xm_cJSON *track_object = NULL;
    if(!tracks)
    {
        return NULL;
    }

    track_object=xm_cJSON_CreateArray();
    for(i=0; i<tracks->count; i++)
    {
        object=xm_cJSON_CreateObject();
        
        xm_cJSON_AddNumberToObject(object, "play_type", 0);
        xm_cJSON_AddStringToObject(object, "album_id", tracks->play_array[i].album_id);
        xm_cJSON_AddStringToObject(object, "track_id", tracks->play_array[i].track_id);
        xm_cJSON_AddStringToObject(object, "started_at", tracks->play_array[i].started_at);
        xm_cJSON_AddNumberToObject(object, "played_secs", tracks->play_array[i].break_secs);
        xm_cJSON_AddNumberToObject(object, "duration", tracks->play_array[i].played_duration);
        if(tracks->play_array[i].business_type == 1 || tracks->play_array[i].business_type == 2)
        {
            xm_cJSON_AddNumberToObject(object, "business_type", tracks->play_array[i].business_type);
            if(tracks->play_array[i].business_type == 2)
            {
                xm_cJSON_AddNumberToObject(object, "channel_id", tracks->play_array[i].channel_id);
                xm_cJSON_AddNumberToObject(object, "scene_id", tracks->play_array[i].scene_id);
            }
            else
            {
                xm_cJSON_AddNumberToObject(object, "topic_id", tracks->play_array[i].topic_id);
            }
        }

        xm_cJSON_AddItemToArray(track_object,object);
    }
    trackInfo = (char *)xm_cJSON_Print(track_object);
    xm_cJSON_Delete(track_object);
	return trackInfo;
}
int xy_api_http_upload_play_records(xy_play_info_upload_model* req_param)
{
    int ret = XY_HTTP_UNKNOW_ERR;

    if(!req_param)
    {
        return XY_HTTP_PARAM_NULL;
    }
    char *record_json = xysdk_post_track_records(req_param);
    if(!record_json)
    {
        return XY_HTTP_PARAM_NULL;
    }

    xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        xy_common_free((void **)&record_json);
        return XY_HTTP_MALLOC_ERR;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_POST);
    xy_http_request_setPath(getRequest, XY_HTTP_PLAY_RECORD_UPLOAD_INTERFACE);
    xy_http_request_addParam(getRequest, "track_records", record_json);
    //xy_http_request_setJsonBody(getRequest, record_json);
    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_data_upload_parse(getRequest->response_buff->data, getRequest->response_buff->length);
    }

    xy_http_request_destroy(getRequest);
    xy_common_free((void **)&record_json);

    return ret;
}


static int xy_listen_info_resp(char *response_data, int len, xy_get_source_list_resp_model *resp_data, char *listen_id, uint16_t page)
{
	xm_cJSON *new_obj = NULL;
	xm_cJSON *data_obj = NULL;
	xm_cJSON *listen_obj = NULL;
	xm_cJSON *object = NULL;
	xm_cJSON *pSub = NULL;
	xm_cJSON *arrayItem = NULL;
	int i = 0;
	int ret = XY_HTTP_NET_ERR;
	int offset =0;
	int j = 0;


	if(!response_data || !resp_data || len==0 || !listen_id)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}

	data_obj = xm_cJSON_GetObjectItem(new_obj, "data");
	if(!data_obj)
	{
		goto exit_get_listen_list;
	}

	listen_obj = xm_cJSON_GetObjectItem(data_obj, listen_id);
	if(!listen_obj)
	{
		goto exit_get_listen_list;
	}

	ret = XY_HTTP_DATA_PRASE_ERR;
	arrayItem = xm_cJSON_GetObjectItem(listen_obj, "count");
	if(arrayItem)
	{
		resp_data->source_total = arrayItem->valueint;
	}
	else
	{
		resp_data->source_total = 0;
	}

	arrayItem = xm_cJSON_GetObjectItem(listen_obj, "biz_type");
	if(arrayItem && arrayItem->valuestring && strlen(arrayItem->valuestring))
	{
		if(!strcmp(arrayItem->valuestring, "album"))
		{
			resp_data->data_type = 0;   
		}
		else
		{
			resp_data->data_type = 1;   
		}
	}

	if(resp_data->source_total==0)
	{
		ret = XY_HTTP_SERVER_DATA_ENPTY;
		goto exit_get_listen_list;
	}

	if(resp_data->source_total > XY_ALBUMLIST_MAX_NUM)
	{
		resp_data->source_total = XY_ALBUMLIST_MAX_NUM;
	}

	arrayItem = xm_cJSON_GetObjectItem(listen_obj, "listens");
	if(arrayItem)
	{
		resp_data->source_num = xm_cJSON_GetArraySize(arrayItem);
	}
	else
	{
		resp_data->source_num = 0;
		ret = XY_HTTP_SERVER_DATA_ENPTY;
		goto exit_get_listen_list;
	}

	for(i = 0; i<resp_data->source_num; i++)
	{
		object=xm_cJSON_GetArrayItem(arrayItem,i);
		if(!object)
		{
			offset++;
			continue;
		}
		j = i - offset;

		pSub = xm_cJSON_GetObjectItem(object, "source_type");
		if(pSub)
		{
			resp_data->source_array[j].source_type = pSub->valueint;
		}

		pSub = xm_cJSON_GetObjectItem(object, "pay_type");
		if(pSub)
		{
			resp_data->source_array[j].pay_type  = pSub->valueint;
		}

		pSub = xm_cJSON_GetObjectItem(object, "duration");
		if(pSub)
		{
			resp_data->source_array[j].play_duration = pSub->valueint;
		}

		XY_LOG_I("[%d]pay_type: %d", j, resp_data->source_array[j].pay_type);

		pSub = xm_cJSON_GetObjectItem(object, "album_id");
		if(pSub)
		{
			snprintf(resp_data->source_array[j].album_id, sizeof(resp_data->source_array[j].album_id), "%.0lf", pSub->valuedouble);
			XY_LOG_I("[%d]album_id: %s", j, resp_data->source_array[j].album_id);
		}

		pSub = xm_cJSON_GetObjectItem(object, "source_id");
		if(pSub)
		{
			snprintf(resp_data->source_array[j].track_id, sizeof(resp_data->source_array[j].track_id), "%.0lf", pSub->valuedouble);
			XY_LOG_I("[%d]track_id: %s", j, resp_data->source_array[j].track_id);
		}
		else
		{
			offset++;
			continue;
		}

		resp_data->source_array[j].index = (page-1)*XY_ALBUM_NUM_PER_PAGE + j;
		pSub = xm_cJSON_GetObjectItem(object, "title");
		if(pSub && pSub->valuestring)
		{
			snprintf(resp_data->source_array[j].title, sizeof(resp_data->source_array[j].title), "%s", pSub->valuestring);
			//snprintf(resp_data->source_array[j].title, sizeof(resp_data->source_array[j].title), "%d+%s", resp_data->source_array[j].index, pSub->valuestring);
		}
		else
		{
			snprintf(resp_data->source_array[j].title, sizeof(resp_data->source_array[j].title), "%s", "未知");
		}

#ifdef XYSDK_SUPPORT_IMAGE_DOWNLOAD
		pSub = xm_cJSON_GetObjectItem(object, "img");
		if(pSub && pSub->valuestring && strlen(pSub->valuestring))
		{
			snprintf(resp_data->source_array[j].image, sizeof(resp_data->source_array[j].image), "%s", pSub->valuestring);
		}
#endif
		extern void himalaya_homepage_list_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * album_id,u8 *track_id);
		himalaya_homepage_list_info_cpy(j,resp_data->source_array[j].pay_type,resp_data->source_array[j].index,resp_data->source_array[j].title,resp_data->source_array[j].album_id,resp_data->source_array[j].track_id);
	}
	resp_data->source_num -= offset;
	ret = XY_HTTP_OK;

exit_get_listen_list:
	h_post_ui_msg(1);
	if (new_obj) 
	{
		xm_cJSON_Delete(new_obj);
		new_obj = NULL;
	}

	return ret;
}

int xy_api_http_get_listen_info(xy_get_listen_list_req_model* req_param, xy_get_source_list_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    char page_str[6] = {0};
    char count_str[4] = {0};

    if(!req_param || !resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }
 
    xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        return XY_HTTP_MALLOC_ERR;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
    xy_http_request_setPath(getRequest, XY_HTTP_LISTEN_LIST_INTERFACE);
    snprintf(page_str, sizeof(page_str), "%d", req_param->page);    
    xy_http_request_addParam(getRequest, "page", page_str);
    snprintf(count_str, sizeof(count_str), "%d", req_param->limit);    
    xy_http_request_addParam(getRequest, "limit", count_str);
    xy_http_request_addParam(getRequest, "ids", req_param->id);
    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_listen_info_resp(getRequest->response_buff->data, getRequest->response_buff->length, resp_data, req_param->id, req_param->page);
    }
    xy_http_request_destroy(getRequest);
    return ret;
}



static int xy_track_list_resp(char *response_data, int len, xy_get_source_list_resp_model *resp_data)
{
	xm_cJSON *new_obj = NULL;
    xm_cJSON *data_obj = NULL;
	xm_cJSON *object = NULL;
	xm_cJSON *pSub = NULL;
	xm_cJSON *arrayItem = NULL;
	int i = 0;
	int ret = XY_HTTP_NET_ERR;
	int offset =0;
	int j = 0;

  
	if(!response_data || !resp_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}

	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}

    data_obj = xm_cJSON_GetObjectItem(new_obj, "data");
	if(!data_obj)
	{
		goto exit_get_track_list;
	}

    ret = XY_HTTP_DATA_PRASE_ERR;
	arrayItem = xm_cJSON_GetObjectItem(data_obj, "count");
	if(arrayItem)
	{
		resp_data->source_total = arrayItem->valueint;
	}
	else
	{
		resp_data->source_total = 0;
	}
	
	if(resp_data->source_total==0)
	{
        ret = XY_HTTP_ALBUM_NOT_EXIST;
		goto exit_get_track_list;
	}
  
    arrayItem = xm_cJSON_GetObjectItem(data_obj, "current_page");
	if(arrayItem)
	{
		resp_data->cur_page = arrayItem->valueint;
	}
	
	arrayItem = xm_cJSON_GetObjectItem(data_obj, "tracks");
	if(arrayItem)
	{
		resp_data->source_num = xm_cJSON_GetArraySize(arrayItem);
	}
	else
	{
		resp_data->source_num = 0;
        ret = XY_HTTP_ALBUM_NOT_EXIST;
		goto exit_get_track_list;
	}
	resp_data->data_type = 1;
    for(i = 0; i<resp_data->source_num; i++)
	{
		object=xm_cJSON_GetArrayItem(arrayItem,i);
		if(!object)
		{
			offset++;
			continue;
		}
		j = i - offset;

		pSub = xm_cJSON_GetObjectItem(object, "source_type");
		if(pSub)
		{
			resp_data->source_array[j].source_type = pSub->valueint;
		}

		pSub = xm_cJSON_GetObjectItem(object, "pay_type");
		if(pSub)
		{
			resp_data->source_array[j].pay_type  = pSub->valueint;
		}

		pSub = xm_cJSON_GetObjectItem(object, "duration");
		if(pSub)
		{
			resp_data->source_array[j].play_duration = pSub->valueint;
		}

		//XY_LOG_I("[%d]pay_type: %d", j, resp_data->source_array[j].pay_type);
		
		pSub = xm_cJSON_GetObjectItem(object, "album_id");
		if(pSub)
		{
			snprintf(resp_data->source_array[j].album_id, sizeof(resp_data->source_array[j].album_id), "%.0lf", pSub->valuedouble);
			//XY_LOG_I("[%d]album_id: %s", j, resp_data->source_array[j].album_id);
		}
		
        pSub = xm_cJSON_GetObjectItem(object, "track_id");
		if(pSub)
		{
			snprintf(resp_data->source_array[j].track_id, sizeof(resp_data->source_array[j].track_id), "%.0lf", pSub->valuedouble);
			//XY_LOG_I("[%d]track_id: %s", j, resp_data->source_array[j].track_id);
		}
		else
		{
			offset++;
			continue;
		}

		pSub = xm_cJSON_GetObjectItem(object, "title");
		if(pSub && pSub->valuestring)
		{
			snprintf(resp_data->source_array[j].title, sizeof(resp_data->source_array[j].title), "%s", pSub->valuestring);
		}
		else
		{
			snprintf(resp_data->source_array[j].title, sizeof(resp_data->source_array[j].title), "%s", "未知");
		}

		#ifdef XYSDK_SUPPORT_IMAGE_DOWNLOAD
		pSub = xm_cJSON_GetObjectItem(object, "img");
		if(pSub && pSub->valuestring && strlen(pSub->valuestring))
		{
			snprintf(resp_data->source_array[j].image, sizeof(resp_data->source_array[j].image), "%s", pSub->valuestring);
		}
        #endif
        resp_data->source_array[j].index = (resp_data->cur_page-1)*XY_ALBUM_NUM_PER_PAGE + j;
		extern void himalaya_track_list_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * album_id,u8 *track_id);
		himalaya_track_list_info_cpy(j,resp_data->source_array[j].pay_type,resp_data->source_array[j].index,resp_data->source_array[j].title,resp_data->source_array[j].album_id,resp_data->source_array[j].track_id);
	}
	
	resp_data->source_num -= offset;
	ret = XY_HTTP_OK;
	
exit_get_track_list:
	h_post_ui_msg(9);
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }
	return ret;
}

int xy_api_http_get_track_list_by_albumId(xy_get_track_list_req_model* req_param, xy_get_source_list_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    char page_str[6] = {0};
    char count_str[4] = {0};
    char source_type_str[4]={0};

    if(!req_param || !resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }
 
    xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        return XY_HTTP_MALLOC_ERR;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
    xy_http_request_setPath(getRequest, XY_HTTP_TRACK_LIST_BY_ALBUMID_INTERFACE);
    snprintf(page_str, sizeof(page_str), "%d", req_param->page);    
    xy_http_request_addParam(getRequest, "page", page_str);
    snprintf(count_str, sizeof(count_str), "%d", req_param->limit);    
    xy_http_request_addParam(getRequest, "limit", count_str);
    if(req_param->source_type != 1 && req_param->source_type != 2)
    {
        req_param->source_type = 1;
    }
    snprintf(source_type_str, sizeof(source_type_str), "%d", req_param->source_type);
    xy_http_request_addParam(getRequest, "source_type", source_type_str);
    xy_http_request_addParam(getRequest, "album_id", req_param->album_id);
    if(strlen(req_param->record_id)>0)
    {
        xy_http_request_addParam(getRequest, "record_id", req_param->record_id);
    }

    if(strlen(req_param->sort)>0)
    {
        xy_http_request_addParam(getRequest, "sort", req_param->sort);
    }
    else
    {
        xy_http_request_addParam(getRequest, "sort", "asc");
    }

    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_track_list_resp(getRequest->response_buff->data, getRequest->response_buff->length, resp_data);
    }

    xy_http_request_destroy(getRequest);
    return ret;
}


int xy_api_http_get_track_list_by_trackId(xy_get_track_req_model* req_param, xy_get_source_list_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;
    char page_str[6] = {0};
    char count_str[4] = {0};

    if(!req_param || !resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }
 
    xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        return XY_HTTP_MALLOC_ERR;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
    xy_http_request_setPath(getRequest, XY_HTTP_TRACK_LIST_BY_TRACKID_INTERFACE);
    snprintf(page_str, sizeof(page_str), "%d", req_param->page);    
    xy_http_request_addParam(getRequest, "page", page_str);
    snprintf(count_str, sizeof(count_str), "%d", req_param->limit);    
    xy_http_request_addParam(getRequest, "limit", count_str);
    xy_http_request_addParam(getRequest, "track_id", req_param->track_id);
    if(strlen(req_param->sort)>0)
    {
        xy_http_request_addParam(getRequest, "sort", req_param->sort);
    }
    else
    {
        xy_http_request_addParam(getRequest, "sort", "asc");
    }

    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_track_list_resp(getRequest->response_buff->data, getRequest->response_buff->length, resp_data);
    }

    xy_http_request_destroy(getRequest);
    return ret;
}


static int xy_get_homepage_info_resp(char *response_data, int len, xy_get_homepage_info_resp_model *resp_data)
{
	xm_cJSON *new_obj = NULL;
    xm_cJSON *data_obj = NULL;
    xm_cJSON *data_sub = NULL;
    xm_cJSON *card_obj = NULL;
    xm_cJSON *card_sub = NULL;
	xm_cJSON *object = NULL;
	xm_cJSON *pSub = NULL;
	xm_cJSON *banner_obj = NULL;
    xm_cJSON *meta_obj = NULL;
	int i = 0, j = 0, k=0;
	int ret = XY_HTTP_NET_ERR;
    int data_size;
    int card_size;
    int banner_size;
    int banner_num, listen_num=0;
    uint8_t card_type=0;  //1：banner  2:listen
    bool got_banner_flag = false;

  
	if(!response_data || !resp_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}
	
	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}

    ret = XY_HTTP_DATA_PRASE_ERR;
    data_obj = xm_cJSON_GetObjectItem(new_obj, "data");
	if(!data_obj)
	{
		goto exit_get_homepage_info;
	}

    data_size = xm_cJSON_GetArraySize(data_obj);
    XY_LOG_I("xy_get_homepage_info_resp data_size: %d",data_size);
    for (i = 0; i < data_size; i++) 
    {
        data_sub = xm_cJSON_GetArrayItem(data_obj, i);
        if(!data_sub)
        {
            continue;
        }
        
        card_obj = xm_cJSON_GetObjectItem(data_sub, "cards");
        if(!card_obj)
        {
            continue;
        }
        listen_num = 0;
        banner_num= 0;
        card_size = xm_cJSON_GetArraySize(card_obj);
        XY_LOG_I("xy_get_homepage_info_resp card_size: %d",card_size);
        for (j = 0; j < card_size; j++) 
        {
            card_sub = xm_cJSON_GetArrayItem(card_obj, j);
            if(!card_sub)
            {
                continue;
            }
            card_type = 0;
            object = xm_cJSON_GetObjectItem(card_sub, "style");
            if(object && object->valuestring && strlen(object->valuestring))
            {
                if(!strcmp(object->valuestring, "w_banner"))
                {
                    card_type = 1;   
                }
            }

            if(card_type != 1)
            {
                object = xm_cJSON_GetObjectItem(card_sub, "jump_type");
                if(object && object->valuestring && strlen(object->valuestring))
                {
                    if(!strcmp(object->valuestring, "listen"))
                    {
                        card_type = 2;   
                    }
                }
            }
            
            XY_LOG_I("[%d]card_type: %d, got_banner_flag: %d", j, card_type, got_banner_flag);
            if(card_type == 0 || (card_type==1&&got_banner_flag)) //非banner和listen || 已获取到第一组banner
            {
                continue;
            }


            if(card_type == 1)   //banner
            {
                #ifdef XYSDK_SUPPORT_BANNER
                object = xm_cJSON_GetObjectItem(card_sub, "card_blocks");
                banner_size = xm_cJSON_GetArraySize(object);
                XY_LOG_I("xy_get_homepage_info_resp banner_size: %d",banner_size);
                for (k = 0; k < banner_size; k++) 
                {
                    banner_obj = xm_cJSON_GetArrayItem(object, k);
                    if(!banner_obj)
                    {
                        continue;
                    }

                    pSub = xm_cJSON_GetObjectItem(banner_obj, "jump_type");
                    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
                    {
                        if(!strcmp(pSub->valuestring, "album"))
                        {
                            resp_data->banner_array[banner_num].jump_type = 1;
                        }
                        else if(!strcmp(pSub->valuestring, "track"))
                        {
                            resp_data->banner_array[banner_num].jump_type = 2;
                        }
                        else if(!strcmp(pSub->valuestring, "qrcode"))
                        {
                            resp_data->banner_array[banner_num].jump_type = 3;   
                        }
                        else if(!strcmp(pSub->valuestring, "listen"))
                        {
                            resp_data->banner_array[banner_num].jump_type = 4;
                        }
                    }

                    if(resp_data->banner_array[banner_num].jump_type == 3 || resp_data->banner_array[banner_num].jump_type == 4)
                    {
                        pSub = xm_cJSON_GetObjectItem(banner_obj, "jump_value");
                        if(pSub && pSub->valuestring && strlen(pSub->valuestring))
                        {
                            snprintf(resp_data->banner_array[banner_num].jump_value, sizeof(resp_data->banner_array[banner_num].jump_value), "%s", pSub->valuestring);
                            XY_LOG_I("[%d]jump_value: %s", banner_num, resp_data->banner_array[banner_num].jump_value);
                        }
                        else
                        {
                            continue;
                        }

                        pSub = xm_cJSON_GetObjectItem(banner_obj, "title");
                        if(pSub && pSub->valuestring && strlen(pSub->valuestring))
                        {
                            snprintf(resp_data->banner_array[banner_num].jump_title, sizeof(resp_data->banner_array[banner_num].jump_title), "%s", pSub->valuestring);
                        }
                        else
                        {
                            snprintf(resp_data->banner_array[banner_num].jump_title, sizeof(resp_data->banner_array[banner_num].jump_title), "%s", "未知");
                        }
                        #ifdef XYSDK_SUPPORT_IMAGE_DOWNLOAD
                        pSub = xm_cJSON_GetObjectItem(banner_obj, "img");
                        if(pSub && pSub->valuestring && strlen(pSub->valuestring))
                        {
                            snprintf(resp_data->banner_array[banner_num].image, sizeof(resp_data->banner_array[banner_num].image), "%s", pSub->valuestring);
                        }
                        #endif
                        resp_data->banner_array[banner_num].index = banner_num;
                        banner_num++;
                    }
                    else if(resp_data->banner_array[banner_num].jump_type == 1 || resp_data->banner_array[banner_num].jump_type == 2)
                    {
                        pSub = xm_cJSON_GetObjectItem(banner_obj, "metadata");
                        if(pSub)
                        {
                            meta_obj = xm_cJSON_GetObjectItem(pSub, "source_id");
                            if(meta_obj)
                            {
                                snprintf(resp_data->banner_array[banner_num].jump_value, sizeof(resp_data->banner_array[banner_num].jump_value), "%.0lf", meta_obj->valuedouble);
                                XY_LOG_I("[%d]jump_value: %s", banner_num, resp_data->banner_array[banner_num].jump_value);
                            }
                            else
                            {
                                continue;
                            }

                            meta_obj = xm_cJSON_GetObjectItem(pSub, "source_title");
                            if(meta_obj && meta_obj->valuestring && strlen(meta_obj->valuestring))
                            {
                                snprintf(resp_data->banner_array[banner_num].jump_title, sizeof(resp_data->banner_array[banner_num].jump_title), "%s", meta_obj->valuestring);
                            }
                            else
                            {
                                snprintf(resp_data->banner_array[banner_num].jump_title, sizeof(resp_data->banner_array[banner_num].jump_title), "%s", "未知");
                            }

                            meta_obj = xm_cJSON_GetObjectItem(pSub, "source_type");
                            if(meta_obj)
                            {
                                resp_data->banner_array[banner_num].source_type = meta_obj->valueint;
                                XY_LOG_I("[%d]source_type: %d", banner_num, resp_data->banner_array[banner_num].source_type);
                            }

                            meta_obj = xm_cJSON_GetObjectItem(pSub, "pay_type");
                            if(meta_obj)
                            {
                                resp_data->banner_array[banner_num].pay_type = meta_obj->valueint;
                                XY_LOG_I("[%d]pay_type: %d", banner_num, resp_data->banner_array[banner_num].pay_type);
                            }
                        }

                        #ifdef XYSDK_SUPPORT_IMAGE_DOWNLOAD
                        pSub = xm_cJSON_GetObjectItem(banner_obj, "img");
                        if(pSub && pSub->valuestring && strlen(pSub->valuestring))
                        {
                            snprintf(resp_data->banner_array[banner_num].image, sizeof(resp_data->banner_array[banner_num].image), "%s", pSub->valuestring);
                        }   
                        #endif

                        resp_data->banner_array[banner_num].index = banner_num;
                        banner_num++;
                    }
                    else
                    {
                        continue;
                    }
                    got_banner_flag = true;
                }
                #else 
                got_banner_flag = true;
                #endif
            }

            if(card_type == 2) //listen
            {
                object = xm_cJSON_GetObjectItem(card_sub, "jump_value");
                if(object && object->valuestring && strlen(object->valuestring))
                {
                    snprintf(resp_data->listen_array[listen_num].listen_id, sizeof(resp_data->listen_array[listen_num].listen_id), "%s", object->valuestring);
                    XY_LOG_I("[%d]listen_id: %s", listen_num, resp_data->listen_array[listen_num].listen_id);
                }
                else
                {
                    continue;
                }

                object = xm_cJSON_GetObjectItem(card_sub, "title");
				if(object && object->valuestring && strlen(object->valuestring))
				{
					snprintf(resp_data->listen_array[listen_num].listen_name, sizeof(resp_data->listen_array[listen_num].listen_name), "%s", object->valuestring);
				}
                else
                {
                    snprintf(resp_data->listen_array[listen_num].listen_name, sizeof(resp_data->listen_array[listen_num].listen_name), "%s", "未知");
                }
                
                #ifdef XYSDK_SUPPORT_IMAGE_DOWNLOAD
                object = xm_cJSON_GetObjectItem(card_sub, "img");
                if(object && object->valuestring && strlen(object->valuestring))
                {
                    snprintf(resp_data->listen_array[listen_num].image, sizeof(resp_data->listen_array[listen_num].image), "%s", object->valuestring);
                }
                #endif

                resp_data->listen_array[listen_num].index = listen_num;
				extern void himalaya_homepage_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * id);
				himalaya_homepage_info_cpy(listen_num+1,resp_data->listen_array[listen_num].pay_type,resp_data->listen_array[listen_num].index,resp_data->listen_array[listen_num].listen_name,resp_data->listen_array[listen_num].listen_id);
                listen_num++;
            }

        }
        resp_data->banner_total = banner_num;
        resp_data->listen_total = listen_num;

        break;
    }

	ret = XY_HTTP_OK;
	
exit_get_homepage_info:
	h_post_ui_msg(0);//post消息到UI层跳转到播单UI
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }
	return ret;
}


int xy_api_http_get_homepage_info(xy_get_homepage_info_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;

    if(!resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }
 
    xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        return XY_HTTP_MALLOC_ERR;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
    xy_http_request_setPath(getRequest, XY_HTTP_HOMEPAGE_INFO_INTERFACE);
    xy_http_request_addParam(getRequest, "tag", "home");
    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_get_homepage_info_resp(getRequest->response_buff->data, getRequest->response_buff->length, resp_data);
    }

    xy_http_request_destroy(getRequest);
    return ret;
}


static int xy_popup_screen_info_resp(char *response_data, int len, popup_screen_info_resp_model *resp_data)
{
	xm_cJSON *new_obj = NULL;
	xm_cJSON *object = NULL;
	xm_cJSON *pSub = NULL;

	int ret = XY_HTTP_NET_ERR;

  
	if(!response_data || !resp_data || len==0)
	{
		return XY_HTTP_PARAM_NULL;
	}
	
	new_obj = xm_cJSON_Parse(response_data);
	if(!new_obj)
	{
		return XY_HTTP_NET_ERR;
	}

    object = xm_cJSON_GetObjectItem(new_obj, "data");
	if(!object)
	{
		goto exit_get_popup_screen;
	}

    pSub = xm_cJSON_GetObjectItem(object, "id");
    if(pSub)
    {
        resp_data->popup_id = pSub->valueint;
    }
    else
    {
        goto exit_get_popup_screen;
    }
    
    ret = XY_HTTP_DATA_PRASE_ERR;
    pSub = xm_cJSON_GetObjectItem(object, "interval_duration");
    if(pSub)
    {
        resp_data->interval_duration  = pSub->valueint;
    }

    pSub = xm_cJSON_GetObjectItem(object, "daily_max_count");
    if(pSub)
    {
        resp_data->daily_max_count = pSub->valueint;
    }

    pSub = xm_cJSON_GetObjectItem(object, "show_content_type");
    if(pSub)
    {
        resp_data->content_type  = pSub->valueint;
    }

    pSub = xm_cJSON_GetObjectItem(object, "event_type");
    if(pSub)
    {
        resp_data->event_type = pSub->valueint;
    }

	XY_LOG_I("xy_popup_screen_info_resp event_type: %d", resp_data->event_type);
    pSub = xm_cJSON_GetObjectItem(object, "show_content_title");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->content_title, sizeof(resp_data->content_title), "%s", pSub->valuestring);
    }

    pSub = xm_cJSON_GetObjectItem(object, "show_content_text");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->content_text, sizeof(resp_data->content_text), "%s", pSub->valuestring);
    }

    pSub = xm_cJSON_GetObjectItem(object, "show_content_value");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->content_value, sizeof(resp_data->content_value), "%s", pSub->valuestring);
    }
		
    pSub = xm_cJSON_GetObjectItem(object, "event_value");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->event_value, sizeof(resp_data->event_value), "%s", pSub->valuestring);
    }    
		
    pSub = xm_cJSON_GetObjectItem(object, "qr_img");
    if(pSub && pSub->valuestring && strlen(pSub->valuestring))
    {
        snprintf(resp_data->qr_img, sizeof(resp_data->qr_img), "%s", pSub->valuestring);
    }   

	ret = XY_HTTP_OK;
	
exit_get_popup_screen:
	if (new_obj) 
	{
        xm_cJSON_Delete(new_obj);
        new_obj = NULL;
    }
	return ret;
}

int xy_api_http_get_popup_screen_info(uint8_t need_qr_img, popup_screen_info_resp_model *resp_data)
{
    int ret = XY_HTTP_UNKNOW_ERR;

    if(!resp_data)
    {
        return XY_HTTP_PARAM_NULL;
    }
 
    xy_http_request_model *getRequest = xy_http_request_create(NULL, NULL, NULL);
    if(getRequest == NULL) {
        return XY_HTTP_MALLOC_ERR;
    }
    xy_http_request_setMethod(getRequest, XY_HTTP_REQUEST_METHOD_GET);
    xy_http_request_setPath(getRequest, XY_HTTP_POPUP_SCREEN_INFO_INTERFACE);
    if(need_qr_img == 1)
    {
        xy_http_request_addParam(getRequest, "need_qr_img", "1"); 
    }
    else
    {
        xy_http_request_addParam(getRequest, "need_qr_img", "0"); 
    }

    ret = xy_http_request_request(getRequest);
    if(ret == 0 && getRequest->response_buff)
    {
        ret = xy_popup_screen_info_resp(getRequest->response_buff->data, getRequest->response_buff->length, resp_data);
    }

    xy_http_request_destroy(getRequest);
    return ret;
}