ArrowStore.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. scoreLabel:cc.Label,
  29. content:cc.Node,
  30. products:null,
  31. selectedPosition:0,
  32. },
  33. // LIFE-CYCLE CALLBACKS:
  34. onEnable () {
  35. this.getArrows(Global.user);
  36. this.scoreLabel.string = Global.user.score;
  37. },
  38. getArrows(userInfo){
  39. wx.request({
  40. url:Global.storeUrl,
  41. data:{
  42. token:userInfo.token,
  43. uid:userInfo.uid,
  44. channel:Global.channel,
  45. os:Global.os,
  46. ver:Global.ver,
  47. type:2
  48. },
  49. success:(response)=>{
  50. if(response.data.code==-5){
  51. wx.showToast({
  52. title:'登录过期'
  53. })
  54. Global.showLoginPage = true;
  55. cc.director.loadScene('MainMenu');
  56. }
  57. this.products = response.data.data.products;
  58. let user = response.data.data.user;
  59. Global.user.score = user.score;
  60. this.scoreLabel.string = user.score;
  61. if(this.products!=undefined){
  62. for(let i =0;i<this.products.length;i++){
  63. if(i<this.content.childrenCount){
  64. let skinItem = this.content.children[i].getComponent('ArrowSkinItem');
  65. let selected = false;
  66. let product = this.products[i];
  67. if(Global.arrowSkinId == -1&&i==0){
  68. selected = true;
  69. }else if(Global.arrowSkinId == product.id){
  70. selected = true;
  71. }
  72. skinItem.init(product,selected,this,i);
  73. }
  74. }
  75. }else{
  76. wx.showToast({
  77. title:'数据出错,请重试!',
  78. icon:none,
  79. })
  80. }
  81. },
  82. });
  83. },
  84. onItemClick(position){
  85. let product = this.products[position];
  86. let skinItem = this.content.children[position].getComponent('ArrowSkinItem');
  87. if(!skinItem.lockedSprite.active){
  88. this.selectItem(position);
  89. }else{
  90. if(product.price <= Global.user.score){
  91. wx.showModal({
  92. title:'',
  93. content:'是否解锁这个皮肤?',
  94. cancelButton:true,
  95. cancelText:'取消',
  96. confirmText:'确定',
  97. success:(res)=>{
  98. if(res.confirm){
  99. wx.showLoading();
  100. this.buySkin(product,position);
  101. }
  102. }
  103. })
  104. }else{
  105. wx.showToast({
  106. title:'分数不够,继续努力吧!',
  107. icon:null
  108. })
  109. }
  110. }
  111. },
  112. selectItem(position){
  113. let product = this.products[position];
  114. Global.arrowSkinId = product.id;
  115. Global.arrowSkinIndex = position;
  116. wx.setStorage({
  117. key:"skinId",
  118. data:{
  119. bottleSkinId:Global.bottleSkinId,
  120. arrowSkinId:Global.arrowSkinId,
  121. bottleSkinIndex:Global.bottleSkinIndex,
  122. arrowSkinIndex:Global.arrowSkinIndex
  123. }
  124. })
  125. for(let i =0;i<this.products.length;i++){
  126. if(i<this.content.childrenCount){
  127. let skinItem = this.content.children[i].getComponent('ArrowSkinItem');
  128. skinItem.setSelected(i==position);
  129. }
  130. }
  131. },
  132. buySkin(product,position){
  133. let skinItem = this.content.children[position].getComponent('ArrowSkinItem');
  134. let productJson =JSON.stringify({
  135. id:product.id,
  136. price:product.price,
  137. type:2
  138. });
  139. console.log(productJson);
  140. wx.request({
  141. url:Global.skinBuyUrl,
  142. data:{
  143. token:Global.user.token,
  144. uid:Global.user.uid,
  145. channel:Global.channel,
  146. os:Global.os,
  147. ver:100,
  148. productJson:productJson
  149. },
  150. header: {
  151. "Content-Type": "application/x-www-form-urlencoded"
  152. },
  153. method:"POST",
  154. success:(response)=>{
  155. if(response.data.code==-5){
  156. wx.showToast({
  157. title:'登录过期'
  158. })
  159. Global.showLoginPage = true;
  160. cc.director.loadScene('MainMenu');
  161. }
  162. if(response.data.code==0||response.data.code==-5101){
  163. let data = response.data.data;
  164. skinItem.setUnLock();
  165. this.selectItem(position);
  166. wx.showToast({
  167. title:'解锁成功',
  168. icon:null
  169. })
  170. }else{
  171. console.log(response.data.msg);
  172. }
  173. },
  174. complete:()=>{
  175. wx.hideLoading();
  176. }
  177. })
  178. }
  179. // update (dt) {},
  180. });