您当前的位置: 首页 > 解决方案

简易微信小程序签到功能

  • 作者: admin
  • 发布于 2018-08-25 13:37:32
  • 来源:  
  • 栏目:解决方案

导语: 一、效果图   点击签到后 二、数据库

 

一、效果图

05.jpg

  点击签到后

06.jpg

 

二、数据库

  用一张数据表存用户签到的信息,每次用户签到都会往表中添加一条记录了用户id和签到日期的数据,如下图

07.jpg

 

三、后端

  后端写两个接口,一个用于查询用户今日是否签到和签到记录总数,一个用于添加用户签到信息到数据库。这里用的是python的flask框架。

  (1)查询用户签到信息接口:

 
  1. @app.route('/get_sign/<user_id>')
  2. def get_sign(user_id):
  3. try:
  4. data=get_sign_info(user_id)
  5. except Exception as e:
  6. return jsonify({'status':0,'Exception':str(e)})
  7. return jsonify({'status':1,'data':data})
  8.  
  9. def get_sign_info(user_id):
  10. conn = sqlite3.connect('test.sqlite')
  11. cursor = conn.cursor()
  12. cursor.execute('select date from sign where user_id=?',(user_id,))
  13. all_date=set([x[0] for x in cursor.fetchall()])
  14. now_date=date.today().strftime('%Y-%m-%d')//将日期字符串化
  15. if now_date in all_date:
  16. signed=True
  17. else:
  18. signed=False
  19. total=len(all_date)
  20. conn.close()
  21. return {'total':total,'signed':signed}

  查询到所有签到日期后用set去除重复项,然后判断一下当天的日期是否在其中,如果不在其中,signed=False表示今日未签到。签到总数就是all_date的长度

  使用了datetime库来获取日期信息。from datetime import date

(2)添加用户签到信息接口:

 
  1. @app.route('/sign/<user_id>')
  2. def sign(user_id):
  3. try:
  4. update_sign(user_id)
  5. except Exception as e:
  6. return jsonify({'status':0,'Exception':str(e)})
  7. return jsonify({'status':1})
  8.  
  9. def update_sign(user_id):
  10. now_date=date.today().strftime('%Y-%m-%d')
  11. conn = sqlite3.connect('test.sqlite')
  12. cursor = conn.cursor()
  13. cursor.execute('insert into sign (user_id,date) values(?,?)',\
  14. (user_id,now_date))
  15. conn.commit()
  16. conn.close()
 

wxml文件

 
  1. <view class="sign" wx:if="{{isLogin == true}}">
  2. <image class="image" src='../../dist/images/sign.png'></image>
  3. <view class="sign_info">
  4. <view wx:if="{{signed==false}}" bindtap='sign'>点击此处签到</view>
  5. <view wx:if="{{signed==true}}">今日已签到</view>
  6. <view>已签到{{total_sign}}天</view>
  7. </view>
  8. </view>

wxss文件

 
  1. .image{
  2. float:left;
  3. width: 140rpx;
  4. height: 140rpx;
  5. margin-right: 7%;
  6. margin-left:20%;
  7. }
  8.  
  9.  
  10. .sign{
  11. margin-top: 10%;
  12. }
  13.  
  14. .sign_info{
  15. width: 100%;
  16. color: #666;
  17. font-size: 43rpx;
  18. }

js文件

 
  1. get_sign: function(){
  2. var that = this;
  3. var userId = wx.getStorageSync("userId");
  4. wx.request({
  5. url: 'http://服务器公网ip:80/get_sign/'+userId,
  6. method: "GET",
  7. success: function (res) {
  8. if (res.data.status == 1) {
  9. that.setData({
  10. total_sign: res.data.data.total,
  11. signed: res.data.data.signed,
  12. })
  13. }
  14. else{
  15. console.log("status error: " + res.data.Exception)
  16. }
  17. },
  18. })
  19. },
  20.  
  21. sign:function(){
  22. var that = this;
  23. var userId = wx.getStorageSync("userId");
  24. wx.request({
  25. url: 'http://服务器公网ip:80/sign/' + userId,
  26. method: "GET",
  27. success: function (res) {
  28. if (res.data.status == 1) {
  29. that.setData({
  30. total_sign: that.data.total_sign+1,
  31. signed: true,
  32. })
  33. wx.showToast({
  34. title: '成功',
  35. icon: 'success',
  36. duration: 2000
  37. })
  38. }
  39. else {
  40. console.log("status error: " + res.data.Exception)
  41. }
  42. },
  43. })
  44. },

  用户登录后,会立即触发get_sign函数,从数据库获取用户签到信息存到page的data中,页面也会显示用户今日是否签到和签到总数。

  用户点击签到后,会保存签到信息,并更新data。用showToast弹窗提示签到成功。



温馨提示:这篇文章没有解决您的问题?欢迎添加微信:18948083295,有微信小程序专业人员,保证有问必答。转载本站文章请注明转自http://www.okeydown.com/(微信小程序网)。

  • 微信扫描二维码关注官方微信
  • ▲长按图片识别二维码
关注我们

微信小程序官方微信

栏目最新
栏目推荐
返回顶部