ShopInfo.lua 9.16 KB
cc.exports.ShopInfo = {}

local xml = require("core.utils.pl.xml")

local shopCouponsList = {}; --点券
local shopCoinList = {}; --金币
local shopItemsList = {};   --道具
local shopCoinItemsList = {};    --头像框列表
local vipList = {}; --月卡
local firstPayList = {};    --首充礼包
local dailyPayList = {};    --每日充值
local cyclePayList = {};    --周期活动

local shop_real_digital = {};    --数码产品
local shop_real_other = {};    --其他产品

--道具配置表信息
local function init()
    local configStr = cc.FileUtils:getInstance():getStringFromFile("res/config/shop.txt");

    local function readLineFun(lineStr)
        local info;
        local contents = StringUtil.split(lineStr,'\t');
        if #contents > 1 then
            info = {};
            info.id = tonumber(contents[1]);
            info.itemList = {};
            local itemArr = StringUtil.split(contents[2],';');
            for i,v in ipairs(itemArr) do
                local itemItemArr = StringUtil.split(v,',');
                local itemInfo = {id=tonumber(itemItemArr[1]),num=tonumber(itemItemArr[2])};
                info.itemList[i] = itemInfo;
            end
            info.costList = {};
            local costArr = StringUtil.split(contents[3],';');
            for i,v in ipairs(costArr) do
                local costItemArr = StringUtil.split(v,',');
                local costInfo = {id=tonumber(costItemArr[1]),num=tonumber(costItemArr[2])};
                info.costList[i] = costInfo;
            end
            info.des = contents[4];
            info.extra = tonumber(contents[5]);
        end
        return info;
    end

    local index = 1;
    local function readLine(str)
        local a,b = string.find(str,"\r\n");
        if a then
            local lineStr = string.sub(str,1,a-1);
            if index > 2 then
                local info = readLineFun(lineStr);
                if info then
                    if info.id > 100 and info.id < 200 then
                        shopItemsList[#shopItemsList+1] = info;
                    elseif info.id > 3000 and info.id < 4000 then
                        shopCoinItemsList[#shopCoinItemsList+1] = info;
                    elseif info.id > 4000 and info.id < 5000 then
                        shop_real_digital[#shop_real_digital+1] = info;
                    elseif info.id > 5000 and info.id < 6000 then
                        shop_real_other[#shop_real_other+1] = info;
                    else
                        shopCoinList[#shopCoinList+1] = info;
                    end
                end
            end

            index = index + 1;
            str = string.sub(str,b+1);
            readLine(str);
        else
            local info = readLineFun(str);
            if info then
                if info.id > 100 and info.id < 200 then
                    shopItemsList[#shopItemsList+1] = info;
                elseif info.id > 3000 and info.id < 4000 then
                    shopCoinItemsList[#shopCoinItemsList+1] = info;
                else
                    shopCoinList[#shopCoinList+1] = info;
                end
            end
        end
    end
    readLine(configStr);


    local configStr = cc.FileUtils:getInstance():getStringFromFile("res/config/pay.xml");
    local data = xml.parse(configStr, false, true);

    for k,v in pairs(data) do
        if k ~= "attr" and k ~= "tag" then
            local info = {};
            info.id = tonumber(v.attr.id);
            info.name = v.attr.name;
            info.rmb = tonumber(v.attr.rmb);
            info.type = tonumber(v.attr.type);
            info.itemList = {};
            local itemArr = StringUtil.split(v.attr.item,";");
            for ii,vv in ipairs(itemArr) do
                local arr = StringUtil.split(vv,",");
                local itemInfo = {id=tonumber(arr[1]),num=tonumber(arr[2])};
                info.itemList[ii] = itemInfo;
            end
            
            local costInfo = {id=0,num=v.attr.rmb};
            info.costList = {costInfo};
            if CHANNEL == "putao" then
                info.product = v.attr.productPuTao;
            else
                info.product = v.attr.product;
            end
            if info.product and info.product ~= "" then 
                if info.type == 1 then
                    info.extra = tonumber(v.attr.extra);
                    shopCouponsList[#shopCouponsList+1] = info;
                elseif info.type == 2 then
                    info.des = v.attr.des;
                    info.day = v.attr.day;
                    local arr = StringUtil.split(v.attr.payitem,",");
                    info.payItemInfo = {id=tonumber(arr[1]),num=tonumber(arr[2])};
                    vipList[#vipList+1] = info;
                elseif info.type == 3 then
                    info.worth = v.attr.worth;
                    firstPayList[#firstPayList+1] = info;
                elseif info.type == 4 then
                    info.worth = v.attr.worth;
                    dailyPayList[#dailyPayList+1] = info;
                elseif info.type == 5 then
                    info.startTime = tonumber(v.attr.startTime);
                    info.endTime = tonumber(v.attr.endTime);
                    info.cuccFlag = tonumber(v.attr.cuccFlag);
                    cyclePayList[#cyclePayList+1] = info;
                end
            end
        end
    end
    table.sort(firstPayList,function (aa,bb)
        return aa.id<bb.id;
    end);
end
init();

--通过ID获取商品信息
function ShopInfo.getShopInfo(id)
    local info = nil;
    for i,v in ipairs(shopCoinList) do
        if v.id == id then
            info = v;
            break;
        end
    end
    if not info then
        for i,v in ipairs(shopItemsList) do
            if v.id == id then
                info = v;
                break;
            end
        end
    end
    if not info then
        for i,v in ipairs(shopCoinItemsList) do
            if v.id == id then
                info = v;
                break;
            end
        end
    end
    if not info then
        for i,v in ipairs(shop_real_digital) do
            if v.id == id then
                info = v;
                break;
            end
        end
    end
    if not info then
        for i,v in ipairs(shop_real_other) do
            if v.id == id then
                info = v;
                break;
            end
        end
    end
    
    return info;
end

function ShopInfo.getPayInfo(id)
    local info = nil;
    for i,v in ipairs(shopCouponsList) do
        if v.id == id then
            info = v;
            break;
        end
    end
    if not info then
        for i,v in ipairs(firstPayList) do
            if v.id == id then
                info = v;
                break;
            end
        end
    end
    if not info then
        for i,v in ipairs(dailyPayList) do
            if v.id == id then
                info = v;
                break;
            end
        end
    end
    if not info then
        for i,v in ipairs(cyclePayList) do
            if v.id == id then
                info = v;
                break;
            end
        end
    end
    if not info then
        for i,v in ipairs(vipList) do
            if v.id == id then
                info = v;
                break;
            end
        end
    end
    return info;
end

function ShopInfo.getShopCouponsList()
    return shopCouponsList;
end

function ShopInfo.getShopItemsList()
    return shopItemsList;
end

function ShopInfo.getListByType(itemType)
    local _list = {};
    if itemType == 1 then
        for i,v in ipairs(shopCoinList) do
            if v.id > 1000 and v.id < 2000 then
                _list[#_list + 1] = v;
            end
        end
    elseif itemType == 2 then
        _list = shopCouponsList;
    elseif itemType == 3 then
        _list = shopCoinItemsList;
    elseif itemType == 4 then
        for i,v in ipairs(shopItemsList) do
            _list[#_list + 1] = v;
        end
    elseif itemType == 5 then
        _list = shop_real_digital;
    elseif itemType == 6 then
        _list = shop_real_other;
    end
    return _list;
end

function ShopInfo.getFirstPayList()
    return firstPayList;
end

function ShopInfo.getDailyPayList()
    return dailyPayList;
end

function ShopInfo.getCyclePayList()
    return cyclePayList;
end

function ShopInfo.getVipList()
    return vipList;
end

function ShopInfo.getPayInfoByProduct(product)
    local info = nil;
    for i,v in ipairs(shopCouponsList) do
        if v.product == product then
            info = v;
        end
    end
    for i,v in ipairs(vipList) do
        if v.product == product then
            info = v;
        end
    end
    if not info then
        for i,v in ipairs(firstPayList) do
            if v.product == product then
                info = v;
                break;
            end
        end
    end
    if not info then
        for i,v in ipairs(dailyPayList) do
            if v.product == product then
                info = v;
                break;
            end
        end
    end
    if not info then
        for i,v in ipairs(cyclePayList) do
            if v.product == product then
                info = v;
                break;
            end
        end
    end
    return info;
end

return ShopInfo