123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- // Learn cc.Class:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
- // Learn Attribute:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
- cc.Class({
- extends: cc.Component,
- properties: {
- // foo: {
- // // ATTRIBUTES:
- // default: null, // The default value will be used only when the component attaching
- // // to a node for the first time
- // type: cc.SpriteFrame, // optional, default is typeof default
- // serializable: true, // optional, default is true
- // },
- // bar: {
- // get () {
- // return this._bar;
- // },
- // set (value) {
- // this._bar = value;
- // }
- // },
- scoreLabel:cc.Label,
- content:cc.Node,
- products:null,
- selectedPosition:0,
- },
- // LIFE-CYCLE CALLBACKS:
- onEnable () {
- this.getArrows(Global.user);
- this.scoreLabel.string = Global.user.score;
- },
- getArrows(userInfo){
- wx.request({
- url:Global.storeUrl,
- data:{
- token:userInfo.token,
- uid:userInfo.uid,
- channel:Global.channel,
- os:Global.os,
- ver:Global.ver,
- type:2
- },
- success:(response)=>{
- if(response.data.code==-5){
- wx.showToast({
- title:'登录过期'
- })
- Global.showLoginPage = true;
- cc.director.loadScene('MainMenu');
- }
- this.products = response.data.data.products;
- let user = response.data.data.user;
- Global.user.score = user.score;
- this.scoreLabel.string = user.score;
- if(this.products!=undefined){
- for(let i =0;i<this.products.length;i++){
- if(i<this.content.childrenCount){
- let skinItem = this.content.children[i].getComponent('ArrowSkinItem');
- let selected = false;
- let product = this.products[i];
- if(Global.arrowSkinId == -1&&i==0){
- selected = true;
- }else if(Global.arrowSkinId == product.id){
- selected = true;
- }
- skinItem.init(product,selected,this,i);
- }
- }
- }else{
- wx.showToast({
- title:'数据出错,请重试!',
- icon:none,
- })
- }
- },
- });
- },
- onItemClick(position){
- let product = this.products[position];
- let skinItem = this.content.children[position].getComponent('ArrowSkinItem');
- if(!skinItem.lockedSprite.active){
- this.selectItem(position);
- }else{
- if(product.price <= Global.user.score){
- wx.showModal({
- title:'',
- content:'是否解锁这个皮肤?',
- cancelButton:true,
- cancelText:'取消',
- confirmText:'确定',
- success:(res)=>{
- if(res.confirm){
- wx.showLoading();
- this.buySkin(product,position);
- }
- }
- })
- }else{
- wx.showToast({
- title:'分数不够,继续努力吧!',
- icon:null
- })
- }
- }
- },
- selectItem(position){
- let product = this.products[position];
- Global.arrowSkinId = product.id;
- Global.arrowSkinIndex = position;
- wx.setStorage({
- key:"skinId",
- data:{
- bottleSkinId:Global.bottleSkinId,
- arrowSkinId:Global.arrowSkinId,
- bottleSkinIndex:Global.bottleSkinIndex,
- arrowSkinIndex:Global.arrowSkinIndex
- }
- })
- for(let i =0;i<this.products.length;i++){
- if(i<this.content.childrenCount){
- let skinItem = this.content.children[i].getComponent('ArrowSkinItem');
- skinItem.setSelected(i==position);
- }
- }
- },
- buySkin(product,position){
- let skinItem = this.content.children[position].getComponent('ArrowSkinItem');
- let productJson =JSON.stringify({
- id:product.id,
- price:product.price,
- type:2
- });
- console.log(productJson);
- wx.request({
- url:Global.skinBuyUrl,
- data:{
- token:Global.user.token,
- uid:Global.user.uid,
- channel:Global.channel,
- os:Global.os,
- ver:100,
- productJson:productJson
- },
- header: {
- "Content-Type": "application/x-www-form-urlencoded"
- },
- method:"POST",
- success:(response)=>{
- if(response.data.code==-5){
- wx.showToast({
- title:'登录过期'
- })
- Global.showLoginPage = true;
- cc.director.loadScene('MainMenu');
- }
- if(response.data.code==0||response.data.code==-5101){
- let data = response.data.data;
- skinItem.setUnLock();
- this.selectItem(position);
- wx.showToast({
- title:'解锁成功',
- icon:null
- })
- }else{
- console.log(response.data.msg);
- }
- },
- complete:()=>{
- wx.hideLoading();
- }
- })
- }
- // update (dt) {},
- });
|