ShopExchangeView.lua 5.99 KB
local ShopExchangeView = dialog.uinode("ui/ShopExchangeLayer.csb",import(".BaseView"))

function ShopExchangeView:ctor()
    ShopExchangeView.super.ctor(self);
end

--params   {shopID=107,onSuccess=onSuccess}
function ShopExchangeView:show(params)
    local scene = cc.Director:getInstance():getRunningScene()
    local inst = ShopExchangeView:create();
    scene:addChild(inst, dialog.ZORDER_APP);

    inst:setContentSize(cc.size(display.width,display.height));
    ccui.Helper:doLayout(inst);

    local panel = inst:getChildByName("panel");
    panel:setScale(0.5);
    local scaleTo = cc.ScaleTo:create(0.2,1);
    panel:runAction(scaleTo);

    inst.shopID = params.shopID;
    inst.onSuccess = params.onSuccess;
    inst.onCancel = params.onCancel;

    inst:init();

    return inst;
end

function ShopExchangeView:init()
    self.panel = self:getChildByName("panel");
    
    self.item = self.panel:getChildByName("item");
    self.txt_cnt = self.panel:getChildByName("txt_cnt");
    self.txt_cost = self.panel:getChildByName("txt_cost");

    local txt_name = self.panel:getChildByName("item"):getChildByName("txt_name");
    local txt_des = self.panel:getChildByName("item"):getChildByName("txt_des");
    local icon = self.panel:getChildByName("item"):getChildByName("icon");
--    local txt_price = self.panel:getChildByName("item"):getChildByName("txt_price");

    local shopInfo = ShopInfo.getShopInfo(self.shopID);
    local itemID = shopInfo.itemList[1].id;
    local itemInfo = ItemInfo.getItemInfo(itemID);
    if shopInfo.itemList[1].num > 1 then
        txt_name:setString(itemInfo.name.."*"..shopInfo.itemList[1].num);
    else
        txt_name:setString(itemInfo.name);
    end
    txt_des:setString(itemInfo.describe);
    local costNum = shopInfo.costList[1].num;
--    txt_price:setString(costNum);
    icon:loadTexture("res/iconsmall/"..itemInfo.id..".png");

    if shopInfo.costList[1].id == 100001 then
        self.panel:getChildByName("img_ticket"):setSpriteFrame(cc.Sprite:create("res/iconsmall/small_"..shopInfo.costList[1].id..".png"):getSpriteFrame());
    end

    local myNum = UserModel.getItemNum(shopInfo.costList[1].id);
    self.txt_cnt:setString(1);

    local shopInfo = ShopInfo.getShopInfo(self.shopID);
    local costNum = shopInfo.costList[1].num;
    self.txt_cost:setString(costNum);


    self.btn_close = self.panel:getChildByName("btn_close");
    self.btn_reduce = self.panel:getChildByName("btn_reduce");
    self.btn_add = self.panel:getChildByName("btn_add");
    self.btn_ok = self.panel:getChildByName("btn_ok");
    self.nodes[1] = {self.btn_close};
    self.nodes[2] = {self.btn_reduce,self.btn_add};
    self.nodes[3] = {self.btn_ok};

    self.btn_close.fn = handler(self,self.onCloseFn);
    self.btn_close:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type,self.btn_close.fn)end);
    self.btn_reduce.fn = handler(self,self.onReduceFn);
    self.btn_reduce:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type,self.btn_reduce.fn)end);
    self.btn_add.fn = handler(self,self.onAddFn);
    self.btn_add:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type,self.btn_add.fn)end);
    self.btn_ok.fn = handler(self,self.onOkFn);
    self.btn_ok:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type,self.btn_ok.fn)end);
    
    self.nodeIndex = 2;
    self.selectedIndex = 2;
    self:updateSelectedState();
end

function ShopExchangeView:onCloseFn(ref)
    if self.onCancel then
        self.onCancel();
    end
    self:close();
end

function ShopExchangeView:onReduceFn(ref)
    local cnt = tonumber(self.txt_cnt:getString());
    if cnt > 1 then
        cnt = cnt - 1;
        self.txt_cnt:setString(cnt);

        local shopInfo = ShopInfo.getShopInfo(self.shopID);
        local costNum = shopInfo.costList[1].num;
        self.txt_cost:setString(costNum*cnt);
    end
end

function ShopExchangeView:onAddFn(ref)
    local cnt = tonumber(self.txt_cnt:getString());
    if cnt < 99 then
        local shopInfo = ShopInfo.getShopInfo(self.shopID);
        local costNum = shopInfo.costList[1].num;
        local myNum = UserModel.getItemNum(shopInfo.costList[1].id);
        if myNum >= costNum*(cnt+1) then
            cnt = cnt + 1;
            self.txt_cnt:setString(cnt);

            self.txt_cost:setString(costNum*cnt);
        else
            if shopInfo.costList[1].id == 100001 then
                Alert.showOne("金币不足");
            else
                Alert.showOne("点券不足");
            end
        end
    end
end

function ShopExchangeView:onOkFn(ref)
    local totalCostNum = tonumber(self.txt_cost:getString());
    local shopInfo = ShopInfo.getShopInfo(self.shopID);
    local costID = shopInfo.costList[1].id;
    local myNum = UserModel.getItemNum(costID);

    if myNum >= totalCostNum then
        LoadingManager.showDataLoading();
        local cnt = tonumber(self.txt_cnt:getString());
        ShopModel.buy(self.shopID,cnt,handler(self,self.onBuyCallBack),handler(self,self.onTimeoutFn));
    else
        local function okFun()
            local scene = cc.Director:getInstance():getRunningScene();
            local shopView = scene:getChildByName("ShopView");
            if shopView then
                shopView:setTab(2);
                self:close();
            else
                AppManager.show("ShopView",2);
            end
        end
        Alert.showTwo(strings.msg_1003,okFun);
    end
end

function ShopExchangeView:onBuyCallBack(params)
    LoadingManager.removeDataLoading();
    if params.errcode == EnumErrorCode.EC_SUCCESS then
        if self.onSuccess then
            self.onSuccess();
        end
        self:close();
    else
        EffectManager.showFntPop(strings.msg_2017);
    end
end

function ShopExchangeView:onTimeoutFn(params)
    if self.onCancel then
        self.onCancel();
    end
    LoadingManager.removeDataLoading();
    self:close();
end

--返回键处理函数
function ShopExchangeView:onKeypadBack()
    if self.onCancel then
        self.onCancel();
    end
    self:close();
end

return ShopExchangeView