就像拼夕夕一样,有些活动需要朋友助力。
最近有个需求是这样的:
核心:
- 与微信小程序的激励广告联系一起,点击按钮助力,弹出微信激励广告,观看广告后,给予奖励,助力人数减1
- 分享给朋友,请朋友助力,助力人数减1
- 数据全部通过云函数进行处理
- 剩余时间
逻辑:
- 用户开始拼团添加拼团表->点击助力广告->更新拼团人数和广告->当助力人数满了,更新拼团状态
- 朋友助力->更新拼团人数,添加助力人表->助力成功后跳转成用户逻辑
- 用户或朋友进入拼团,查询拼团表和助力人表,显示拼团信息,设置定时器
- 开始拼团时设置开始时间和结束时间,定时器中进行判断是否超时
1.在微信公众平台里的流量主中进行创建。
2.复制代码,就可以运行了。
let rewardedVideoAd = null
Page({
onLoad() {
let that = this
if(wx.createRewardedVideoAd){
rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: 'xxxx' })
rewardedVideoAd.onLoad(() => {
console.log('onLoad event emit')
})
rewardedVideoAd.onError((err) => {
console.log('onError event emit', err)
})
rewardedVideoAd.onClose(res => {
// 用户点击了【关闭广告】按钮
if (res && res.isEnded) {
// 正常播放结束,可以下发游戏奖励
//这里是奖励
that.sendGift()
} else {
// 播放中途退出,不下发游戏奖励
}
})
}
},
//按钮助力
videoShow: function () {
let that = this
that.setData({
isUser: false
})
// 用户触发广告后,显示激励视频广告
if (that.createRewardedVideoAd) {
that.createRewardedVideoAd.show().catch(() => {
// 失败重试
that.createRewardedVideoAd.load()
.then(() => that.createRewardedVideoAd.show())
.catch(err => {
wx.showToast({
title: '广告显示失败,请刷新页面',
duration: 2000
})
console.log('激励视频 广告显示失败')
})
})
}
},
})
/**
* 用户点击右上角分享
* groupId:订单id,imgurl团主头像
*/
onShareAppMessage: function () {
let that = this;
console.log(that.data.memberInfo.headimgurl)
let imgUrl = that.data.memberInfo.headimgurl || that.data.memberInfo.avatar
return {
title: '戳我领取,帮朋友助力',
path: 'pages/***/***?groupId=' + that.data.groupId + "&imgurl=" + that.data.imgurl,
success: (res) => {
console.log("转发成功", res);
if (res.shareTickets != undefined) {
wx.getShareInfo({
shareTicket: res.shareTickets,
success: function (res) { }
})
}
}
}
},
<button open-type="share">邀请好友助力</button>
涉及到云函数添加,更新,查找,删除数据库,云函数调用发送订阅消息
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
switch (event.action) {
//添加拼团
case 'addGroup': {
return addGroup(event)
}
//更新人数和观看广告的次数
case 'updateGroup': {
return updateGroup(event)
}
//通过openid和课程id查询信息
case 'queryByOpenid': {
return queryGroup(event)
}
//通过订单id查询信息
case 'queryByGroupId': {
return queryByGroupId(event)
}
//更新订单状态
case 'updateStauts': {
return updateStauts(event)
}
//更新订单状态和完成时间
case 'updateStautsAndTime': {
return updateStautsAndTime(event)
}
//添加助力人信息表
case 'addGroupHelp': {
return addGroupHelp(event)
}
//通过订单id查询助力人消息
case 'queryGroupHelpByopenId': {
return queryGroupHelpByopenId(event)
}
//发送完成订阅消息
case 'sendSubscribeMessageFinish': {
return sendSubscribeMessageFinish(event)
}
//发送过期订阅消息
case 'sendSubscribeMessageGroup': {
return sendSubscribeMessageGroup(event)
}
//查询订单完成条数
case 'querySucdess': {
return querySucdess()
}
//删除订单
case 'removeID':{
return removeID(event)
}
default: {
return
}
}
async function addGroup(event) {
const res = await db.collection('***').add({
data: {
}
})
return res;
}
async function updateGroup(event) {
const res = await db.collection('***').doc(event.id).update({
data: {
}
})
return res;
}
//通过课程id和openid查询信息
async function queryGroup(event) {
const res = await db.collection('***').where({
atta_id: event.atta_id,
openid: event.openid,
}).get()
return res;
}
/**
* 发送订阅消息--成功
*/
async function sendSubscribeMessageGroup(event) {
const { OPENID } = cloud.getWXContext()
const sendResult = await cloud.openapi.subscribeMessage.send({
touser: OPENID,
templateId: event.templateId,
miniprogram_state: 'developer',
page: 'pages/***/****?dataId='+event.dataId,
// 此处字段应修改为所申请模板所要求的字段
data: {
thing14: {
value : '****',
},
date8: {
value : event.time,
},
phrase2: {
value : '***',
}
}
})
return sendResult
}
async function querySucdess() {
const res = db.collection('****').where({
}).count()
return res;
}
async function removeID(event) {
const res = await db.collection('***').where({
}).remove()
return res;
}
}
/**
* 设置砍价倒计时时间
* @method setAnswerTime
* @param {number} data 结束时间
* @return {} 返回查询对象数组
*/
setAnswerTime(data) {
let that = this;
clearInterval(that.data.timer)
//如果将定时器设置在外面,那么用户就看不到countDownNum的数值动态变化,所以要把定时器存进data里面
let timer = setInterval(function () {
that.formatTime(data);
}, 1e3);
that.setData({
timer: timer
});
},
/**
* 剩余时间格式
*/
formatTime: function (date) {
let that = this
let nowTime = new Date(),
_nowTime = nowTime.getTime(),
resTime = date - _nowTime
if (resTime <= 0) {
clearInterval(that.data.timer)
if (that.data.groupDate.stauts != 2) {
that.setData({
status: 3
})
that.updateStauts(3)
that.sendSubscribeMessageFinish()
}
return;
}
var hour = parseInt((resTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minute = parseInt((resTime % (1000 * 60 * 60)) / (1000 * 60));
var second = parseInt(resTime % (1000 * 60) / 1000);
let time = [hour, minute, second].map(this.formatNumber).join(':') + '后结束'
this.setData({
time: time
})
},
/**
* 剩余时间格式01
*/
formatNumber: function (n) {
n = n.toString()
return n[1] ? n : '0' + n
},