BottleStore.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. // foo: {
  14. // // ATTRIBUTES:
  15. // default: null, // The default value will be used only when the component attaching
  16. // // to a node for the first time
  17. // type: cc.SpriteFrame, // optional, default is typeof default
  18. // serializable: true, // optional, default is true
  19. // },
  20. // bar: {
  21. // get () {
  22. // return this._bar;
  23. // },
  24. // set (value) {
  25. // this._bar = value;
  26. // }
  27. // },
  28. goldLabel:cc.Label,
  29. content:cc.Node,
  30. products:null,
  31. userInfo:null,
  32. },
  33. // LIFE-CYCLE CALLBACKS:
  34. onEnable () {
  35. this.getBottles(Global.user);
  36. this.goldLabel.string = Global.user.gold;
  37. // if(window.wx!=undefined){
  38. // wx.getStorage({
  39. // key:"userInfo",
  40. // success:(response)=>{
  41. // this.userInfo = response.data;
  42. // this.goldLabel.string = this.userInfo.gold;
  43. // this.getBottles(this.userInfo);
  44. // },
  45. // fail:()=>{
  46. // console.log('获取用户数据失败');
  47. // }
  48. // })
  49. // }
  50. },
  51. getBottles(userInfo){
  52. wx.request({
  53. url:Global.storeUrl,
  54. data:{
  55. token:userInfo.token,
  56. uid:userInfo.uid,
  57. channel:Global.channel,
  58. os:Global.os,
  59. ver:Global.ver,
  60. type:1
  61. },
  62. success:(response)=>{
  63. if(response.data.code==-5){
  64. wx.showToast({
  65. title:'登录过期'
  66. })
  67. Global.showLoginPage = true;
  68. cc.director.loadScene('MainMenu');
  69. }
  70. this.products = response.data.data.products;
  71. let user = response.data.data.user;
  72. Global.user.gold = user.coin;
  73. this.goldLabel.string = Global.user.gold;
  74. if(this.products!=undefined){
  75. for(let i =0;i<this.products.length;i++){
  76. if(i<this.content.childrenCount){
  77. let selected = false;
  78. if(Global.bottleSkinId == -1 && i == 0){
  79. selected = true;
  80. }else if(Global.bottleSkinId == this.products[i].id){
  81. selected = true;
  82. }
  83. let skinItem = this.content.children[i].getComponent('BottleSkinItem');
  84. skinItem.init(this.products[i],selected,this,i);
  85. }
  86. }
  87. }else{
  88. wx.showToast({
  89. title:'数据出错,请重试!'
  90. })
  91. }
  92. },
  93. });
  94. },
  95. onItemClick(position){
  96. let product = this.products[position];
  97. let skinItem = this.content.children[position].getComponent('BottleSkinItem');
  98. if(!skinItem.lockedSprite.active){
  99. this.selectItem(position);
  100. }else{
  101. if(product.price <= Global.user.gold){
  102. wx.showModal({
  103. title:'',
  104. content:'是否解锁这个皮肤?',
  105. cancelButton:true,
  106. cancelText:'取消',
  107. confirmText:'确定',
  108. success:(res)=>{
  109. if(res.confirm) {
  110. wx.showLoading();
  111. this.buySkin(product, position);
  112. }
  113. }
  114. })
  115. }else{
  116. wx.showToast({
  117. title:'金币不够',
  118. icon:null,
  119. })
  120. }
  121. }
  122. },
  123. selectItem(position){
  124. let product = this.products[position];
  125. Global.bottleSkinId = product.id;
  126. switch (position){
  127. case 0:
  128. Global.bottleSkinIndex = 0;
  129. break;
  130. case 1:
  131. Global.bottleSkinIndex = 4;
  132. break;
  133. case 2:
  134. Global.bottleSkinIndex = 5;
  135. break;
  136. case 3:
  137. Global.bottleSkinIndex = 7;
  138. break;
  139. case 4:
  140. Global.bottleSkinIndex = 3;
  141. break;
  142. case 5:
  143. Global.bottleSkinIndex = 2;
  144. break;
  145. case 6:
  146. Global.bottleSkinIndex = 1;
  147. break;
  148. case 7:
  149. Global.bottleSkinIndex = 8;
  150. break;
  151. case 8:
  152. Global.bottleSkinIndex = 6;
  153. break;
  154. case 9:
  155. Global.bottleSkinIndex = 10;
  156. break;
  157. }
  158. wx.setStorage({
  159. key:"skinId",
  160. data:{
  161. bottleSkinId:Global.bottleSkinId,
  162. arrowSkinId:Global.arrowSkinId,
  163. bottleSkinIndex:Global.bottleSkinIndex,
  164. arrowSkinIndex:Global.arrowSkinIndex
  165. }
  166. })
  167. for(let i =0;i<this.products.length;i++){
  168. if(i<this.content.childrenCount){
  169. let skinItem = this.content.children[i].getComponent('BottleSkinItem');
  170. skinItem.setSelected(i==position);
  171. }
  172. }
  173. },
  174. buySkin(product,position){
  175. let skinItem = this.content.children[position].getComponent('BottleSkinItem');
  176. let productJson =JSON.stringify({
  177. id:product.id,
  178. price:product.price,
  179. type:1
  180. });
  181. wx.request({
  182. url:Global.skinBuyUrl,
  183. data:{
  184. token:Global.user.token,
  185. uid:Global.user.uid,
  186. channel:Global.channel,
  187. os:Global.os,
  188. ver:Global.ver,
  189. productJson:productJson
  190. },
  191. header: {
  192. "Content-Type": "application/x-www-form-urlencoded"
  193. },
  194. method:"POST",
  195. success:(response)=>{
  196. if(response.data.code==-5){
  197. wx.showToast({
  198. title:'登录过期'
  199. })
  200. Global.showLoginPage = true;
  201. cc.director.loadScene('MainMenu');
  202. }
  203. if(response.data.code==0||response.data.code==-5101){
  204. let data = response.data.data;
  205. skinItem.setUnLock();
  206. this.selectItem(position);
  207. Global.user.gold = Global.user.gold - product.price;
  208. this.goldLabel.string = Global.user.gold;
  209. wx.hideLoading();
  210. wx.showToast({
  211. title:'解锁成功',
  212. icon:null,
  213. })
  214. }else{
  215. console.log(response.data);
  216. }
  217. },
  218. fail(){
  219. wx.hideLoading();
  220. },
  221. })
  222. },
  223. saveUserInfo(){
  224. }
  225. // update (dt) {},
  226. });