微信云开发小游戏分享给好友点击获得复活卡

if (window.wx) {

    wx.cloud.init()

    wx.getSetting({

        success(res) {

            if (!res.authSetting["scope.userInfo"]) {

                let button = wx.createUserInfoButton({

                    type: 'text',

                    text: '获取用户信息',

                    style: {

                        left: 220,

                        top: 76,

                        width: 200,

                        height: 40,

                        lineHeight: 40,

                        backgroundColor: '#ffffff',

                        color: '#ffffff',

                        textAlign: 'center',

                        fontSize: 16,

                        borderRadius: 4

                    }

                })

                button.onTap((res) = > {

                    var userInfo = res.userInfo

                    var nickName = userInfo.nickName

                    var avatarUrl = userInfo.avatarUrl

                    var gender = userInfo.gender //性别 0:未知、1:男、2:女

                    var province = userInfo.province

                    var city = userInfo.city

                    var country = userInfo.country

                    const db = wx.cloud.database()

                    db.collection('user').add({

                    data: {

                        nickName: nickName,

                        avatarUrl: avatarUrl,

                        regtime: Date.now()

                    },

                    success: function (res) {

                        console.log(res)

                        button.hide();

                    }

                })

            })

            }

        }

    })

}



 /*wx.cloud.callFunction({

   // 需调用的云函数名

   name: 'add', 

   // 传给云函数的参数

   data: {       

 a: 12,

 b: 19,

   },

   // 成功回调

   complete: console.log

 })*/

        /*

                // 1. 获取数据库引用

                const db = wx.cloud.database()

                for(var i=0;i<10;i++){

                    var score=cc.vv.util.getRandomInt(50,100)

                    db.collection('score').add({

                        // data 字段表示需新增的 JSON 数据

                        data: {

                            nick:'粉丝'+i,

                            imgurl:'http://sssssss'+i,

                            score:score

                        },

                        success: function(res) {

                            console.log(res)

                        }

                    })

                }

                db.collection('score').orderBy('score', 'desc').limit(5)

                .get()

                .then(console.log)

                .catch(console.error)*/

WxCloudMgr.js
cc.Class({

 //道具Id:1为金币2为红包3为复活卡4为提示卡

    properties: {

 openId:'',

 tbUser:'user',//用户表

 tbConfig:'config',//系统动态参数表

 tbScore:'score',//分数表

 tbClick:'click',//邀请点击表

 tbOutfit:'outfit',//道具表

 configInfo:null,//配置信息

 userInfo:null,//玩家个人信息

 scoreInfo:null//玩家个人分数信息

    },

 init(){

 if (window.wx) {

            wx.cloud.init()

 this.db=wx.cloud.database();

 this.initConfig();

 this.initOpenId();

 this.onShow();

        } 

 },

 //后台回到前台触发

 onShow(){

 var self=this

 wx.onShow(function(res){

 //获取回前台时去检测是否有人帮点击

 console.log('onShow',res)

 })

 },



 //初始分享参数

 initShare(){

 var query='openId='+this.openId

 var imageUrl=this.userInfo.avatarUrl

 var title='快来帮帮我'

 var data={title:title,query:query,imageUrl:imageUrl}

 wx.onShareAppMessage(function(){

 return data;

 })

 },

 //获取配置值

 getConfigVal(key){

 if(this.configInfo&&this.configInfo[key]){

 return this.configInfo[key];

 }

 return null;

 },

 //从数据库加载配置

 initConfig(){

 var self=this

 this.db.collection(this.tbConfig).doc('sys_config').get({

 data: {},

 success: function (res) {

 console.log('查询配置信息',res)

 self.configInfo=res.data;

 }

 })

 },

 //从云函数获取openId

 initOpenId(){

 var self=this;

 wx.cloud.callFunction({

 name: 'getOpenId', 

 data: {},

 success: function(res) {

 console.log('getOpenId success',res)

 if(res&&res.result&&res.result!=null&&'null'!=res.result){

 self.openId=res.result

 self.initUser()

 }

 },

 fail: function(res) {

 console.log('getOpenId fail',res)

 }

 })

 },

 //检测用户是否存在,不存在则用openId创建一个账号

 initUser(){

 var self=this

 this.db.collection(this.tbUser).doc(this.openId).get({

 data: {},

 success: function (res) {

 console.log('玩家信息存在',res)

 self.userInfo=res.data;

 self.checkAuthState()

 },

 fail: function (res) {

   console.log('玩家信息不存在')

   self.db.collection(self.tbUser).add({

 data: {

 _id: self.openId,

 nickName: '',

 avatarUrl: '',

 regTime: Date.now()

 },

 complete: function (res) {

 console.log('建立用户信息',res)

 self.checkAuthState()

 }

   })

 }

 })

 },

 //检测是否已授权,没授权则弹出授权界面

 checkAuthState(){

 var self=this

 wx.getSetting({

 success(res) {

 if (!res.authSetting["scope.userInfo"]) {

 self.showLoginForm();

 }else{

 self.loginSuccess();

 }

 }

 })

 },

 //显示授权界面

 showLoginForm(){

 var self=this

 let button = wx.createUserInfoButton({

 type: 'text',

 text: ' ',

 style: {

 left: 0,

 top: 0,

 width: 1100,

 height: 1100

 }

 })

 button.onTap((res) => {

 console.log(res)

 var userInfo = res.userInfo

 if(userInfo){

 console.log('用户授权成功')

 self.updateUserInfo(userInfo.nickName,userInfo.avatarUrl)

 self.loginSuccess();

 button.hide();

 }

 })

 },

 //更新授权信息到数据库

 updateUserInfo(nickName,avatarUrl){

 console.log('更新玩家昵称头像')

 this.db.collection(this.tbUser).doc(this.openId).update({

 data: {

   nickName: nickName,

   avatarUrl: avatarUrl

 },

 complete: function (res) {

   console.log('updateUserInfo',nickName,avatarUrl)

 }

 })

 },

 //登录成功调用

 loginSuccess(){

 console.log('登录成功',this.userInfo)

 this.initShare();

 this.addClickReward()

 },

 //如果是别人发出的邀请则给邀请人增加奖励

 addClickReward(){

 var self=this

 var launchData=wx.getLaunchOptionsSync()

 console.log('启动参数',launchData)

 if(launchData){

 //启动时给转发者增加奖励

 console.log('启动时给转发者增加奖励',launchData.query)

 var query=launchData.query;

 if(query){

 if(query.openId){

 //帮点击获得奖励

 self.addClick(query.openId)

 } 

 } 

 }

 },

 //为转发者增加点击记录

 addClick(openId){

 var self=this

 self.db.collection(self.tbClick)

 .where({_openid:self.userInfo.openId,toOpenId:openId}).get()

 .then(function(res){

 if(res.data&&res.data.length>0){

 var obj=res.data[0]

 if(obj){

 //if(Date.now()-obj.clickTime>6*60*60*1000){

 if(Date.now()-obj.clickTime>6*1000){

 console.log('过期添加点击')

 self.db.collection(self.tbClick).doc(obj._id).update({

 data: {

   count:1,

   clickTime: Date.now()

 }

 }) 

 }else{

 console.log('6小时只能点一次',res.data)

 }

 }

 }else{

 console.log('添加点击')

 self.db.collection(self.tbClick).add({

 data: {

   toOpenId: openId,

   count:1,

   nickName: self.userInfo.nickName,

   clickTime: Date.now()

 }

 }) 

 }

 });

 },

 //检测获取奖励

 getClickReward(){

 var self=this;

 wx.cloud.callFunction({

 name: 'getClickReward', 

 data: {},

 success: function(res) {

 console.log('getClickReward success',res)

 if(res&&res.result&&res.result!=null&&'null'!=res.result){

 var count=res.result.stats.updated||0;

 if(count>0){

 self.addOutfit(3,count)

 console.log('你的好友送你'+count+'张复活卡')

 }

 }

 }

 })

 },

 //更新分数,不存在则增加,存在则更新

 addScore(score){

 console.log('增加积分'+score)

 score = score?parseInt(score):0

 var self=this

 this.db.collection(this.tbScore).doc(this.openId).get({

 data: {},

 success: function (res) {

 console.log('玩家积分存在',res)

 self.scoreInfo=res.data;

 self.db.collection(self.tbScore).doc(self.openId).update({

 data: {

   score: self.db.command.inc(score)

 },

 complete: function (res) {

   console.log('updateScoreInfo',res)

 }

 })

 },

 fail: function (res) {

 console.log('玩家积分不存在')

 self.db.collection(self.tbScore).add({

 data: {

   _id: self.openId,

   score: score,

   nickName: self.userInfo.nickName,

   avatarUrl: self.userInfo.avatarUrl

 },

 complete: function (res) {

   console.log('建立用户积分信息',res)

 }

 })

 }

 })

 },

 //获取排行榜

 getRankList(data,callBack){

 var limit=5

 var skip=0

 if(data&&data.limit){

 limit=data.limit

 }

 if(data&&data.skip){

 skip=data.skip

 }

 this.db.collection(this.tbScore).orderBy('score', 'desc').limit(limit).skip(skip).get().then(callBack).catch(console.error)

 },

 //获取超过的指定分数的一条记录

 getMorethen(score,callBack){

 this.db.collection(this.tbScore).where({score:this.db.command.lt(score)}).orderBy('score', 'desc').limit(1).get().then(callBack).catch(console.error)

 },

 //添加道具

 addOutfit(outfitId,num){

 console.log('增加道具',outfitId,num)

 num = num?parseInt(num):0

 var self=this

 this.db.collection(this.tbOutfit).where({_openid:this.openId,outfitId:outfitId}).get({

 data: {},

 success: function (res) {

 if(res&&res.data.length>0){

 console.log('玩家道具存在',res)

 self.db.collection(self.tbOutfit).doc(res.data._id).update({

 data: {

   num: self.db.command.inc(num)

 },

 complete: function (res) {

   console.log('增加道具成功',res)

 }

 }) 

 }else{

 console.log('玩家道具不存在')

 self.db.collection(self.tbOutfit).add({

 data: {

 outfitId:outfitId,

 num:num   

 },

 complete: function (res) {

   console.log('建立道具信息',res)

 }

 }) 

 }



 },

 fail: function (res) {

 console.log('查询玩家道具失败',res)

 }

 })

 },

 //使用道具

 desOutfit(outfitId,num){

 num = num?parseInt(num):0

 var self=this

 this.db.collection(this.tbOutfit).where({_openid:this.openId,outfitId:outfitId}).get({

 success: function (res) {

 if(res.data&&res.data.length>0){

 console.log('扣减玩家道具',res)

 var outfit=res.data[0]

 if(outfit.num>=num){

 self.db.collection(self.tbOutfit).doc(outfit._id).update({

 data: {

   num: self.db.command.inc(-num)

 },

 complete: function (res) {

   console.log('desOutfit',res)

 }

 }) 

 }else{

 console.log('物品数量不足')

 }

 }

 }

 })

 }

});



getClickReward
// 云函数入口文件

const cloud = require('wx-server-sdk')



cloud.init()



// 云函数入口函数

exports.main = async (event, context) => {

  let { OPENID, APPID } = cloud.getWXContext()

  const db=cloud.database();

  return await db.collection('click').where({

    toOpenId: OPENID,

    count:1

  }).update({

    data:{

      count:0

    }

  })

}

getOpenId
// 云函数入口文件

const cloud = require('wx-server-sdk')



cloud.init()



// 云函数入口函数

exports.main =  (event, context) => {

  let { OPENID, APPID } = cloud.getWXContext()

  return OPENID;

}

Author:

发表评论

电子邮件地址不会被公开。 必填项已用*标注