ShopExchangeView.lua
5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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