这篇文章主要讲解了“微信小程序中如何实现一个签到功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“微信小程序中如何实现一个签到功能”吧!
(1)查询用户签到信息接口:
@app.route('/get_sign/<user_id>')
def get_sign(user_id):
try:
data=get_sign_info(user_id)
except Exception as e:
return jsonify({'status':0,'Exception':str(e)})
return jsonify({'status':1,'data':data})
def get_sign_info(user_id):
conn = sqlite3.connect('test.sqlite')
cursor = conn.cursor()
cursor.execute('select date from sign where user_id=?',(user_id,))
all_date=set([x[0] for x in cursor.fetchall()])
now_date=date.today().strftime('%Y-%m-%d')//将日期字符串化
if now_date in all_date:
signed=True
else:
signed=False
total=len(all_date)
conn.close()
return {'total':total,'signed':signed}
查询到所有签到日期后用set去除重复项,然后判断一下当天的日期是否在其中,如果不在其中,signed=False表示今日未签到。签到总数就是all_date的长度
使用了datetime库来获取日期信息。from datetime import date
(2)添加用户签到信息接口:
@app.route('/sign/<user_id>')
def sign(user_id):
try:
update_sign(user_id)
except Exception as e:
return jsonify({'status':0,'Exception':str(e)})
return jsonify({'status':1})
def update_sign(user_id):
now_date=date.today().strftime('%Y-%m-%d')
conn = sqlite3.connect('test.sqlite')
cursor = conn.cursor()
cursor.execute('insert into sign (user_id,date) values(?,?)',\
(user_id,now_date))
conn.commit()
conn.close()
四、小程序前端
wxml文件
<view class="sign" wx:if="{{isLogin == true}}">
<image class="image" src='../../dist/images/sign.png'></image>
<view class="sign_info">
<view wx:if="{{signed==false}}" bindtap='sign'>点击此处签到</view>
<view wx:if="{{signed==true}}">今日已签到</view>
<view>已签到{{total_sign}}天</view>
</view>
</view>
wxss文件
.image{
float:left;
width: 140rpx;
height: 140rpx;
margin-right: 7%;
margin-left:20%;
}
.sign{
margin-top: 10%;
}
.sign_info{
width: 100%;
color: #666;
font-size: 43rpx;
}
js文件
get_sign: function(){
var that = this;
var userId = wx.getStorageSync("userId");
wx.request({
url: 'http://服务器公网ip:80/get_sign/'+userId,
method: "GET",
success: function (res) {
if (res.data.status == 1) {
that.setData({
total_sign: res.data.data.total,
signed: res.data.data.signed,
})
}
else{
console.log("status error: " + res.data.Exception)
}
},
})
},
sign:function(){
var that = this;
var userId = wx.getStorageSync("userId");
wx.request({
url: 'http://服务器公网ip:80/sign/' + userId,
method: "GET",
success: function (res) {
if (res.data.status == 1) {
that.setData({
total_sign: that.data.total_sign+1,
signed: true,
})
wx.showToast({
title: '成功',
icon: 'success',
duration: 2000
})
}
else {
console.log("status error: " + res.data.Exception)
}
},
})
},
感谢各位的阅读,以上就是“微信小程序中如何实现一个签到功能”的内容了,经过本文的学习后,相信大家对微信小程序中如何实现一个签到功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.xuebuyuan.com/3285016.html