一、效果图
点击登录后
二、数据库
使用数据表存储用户的登录信息。每次用户登录时微信小程序云开发签到插件,都会将记录用户ID和登录日期的一条数据添加到表中,如下所示
三、后端
在后端编写了两个接口,一个用于查询用户今天是否已登录以及登录记录的总数,另一个用于将用户登录信息添加到数据库。这里使用的框架。
([1)查询用户登录信息界面:
@app.route('/get_sign/') 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删除重复项,然后确定日期是否在其中。如果不是,则=表示今天不办理登机手续。入住总数为长度

使用库获取日期信息。从日期起
([2)添加用户登录信息界面:
@app.route('/sign/') 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) } }, }) },
用户登录后,将立即触发该功能。用户登录信息是从数据库中获取的,并存储在页面数据中。该页面还将显示用户今天是否已登录以及登录总数。
用户单击登录后,登录信息将被保存并更新数据。使用弹出窗口指示登录成功。