微信小程序簽到功能
本文實(shí)例為大家分享了簡(jiǎn)易微信小程序簽到功能的具體代碼,供大家參考,具體內(nèi)容如下
一、效果圖

點(diǎn)擊簽到后

二、數(shù)據(jù)庫(kù)
用一張數(shù)據(jù)表存用戶簽到的信息,每次用戶簽到都會(huì)往表中添加一條記錄了用戶id和簽到日期的數(shù)據(jù),如下圖

三、后端
后端寫兩個(gè)接口,一個(gè)用于查詢用戶今日是否簽到和簽到記錄總數(shù),一個(gè)用于添加用戶簽到信息到數(shù)據(jù)庫(kù)。這里用的是python的flask框架。
(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去除重復(fù)項(xiàng),然后判斷一下當(dāng)天的日期是否在其中,如果不在其中,signed=False表示今日未簽到。簽到總數(shù)就是all_date的長(zhǎng)度
使用了datetime庫(kù)來獲取日期信息。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'>點(diǎn)擊此處簽到</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://服務(wù)器公網(wǎng)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://服務(wù)器公網(wǎng)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)
}
},
})
},
用戶登錄后,會(huì)立即觸發(fā)get_sign函數(shù),從數(shù)據(jù)庫(kù)獲取用戶簽到信息存到page的data中,頁(yè)面也會(huì)顯示用戶今日是否簽到和簽到總數(shù)。
用戶點(diǎn)擊簽到后,會(huì)保存簽到信息,并更新data。用showToast彈窗提示簽到成功。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序?qū)崿F(xiàn)打卡簽到頁(yè)面
- 微信小程序?qū)崿F(xiàn)簽到彈窗動(dòng)畫
- 微信小程序?qū)崿F(xiàn)日歷簽到
- 微信小程序以7天為周期連續(xù)簽到7天功能效果的示例代碼
- 微信小程序連續(xù)簽到7天積分獲得功能的示例代碼
- 使用python實(shí)現(xiàn)微信小程序自動(dòng)簽到功能
- 微信小程序?qū)崿F(xiàn)二維碼簽到考勤系統(tǒng)
- 微信小程序本地存儲(chǔ)實(shí)現(xiàn)每日簽到、連續(xù)簽到功能
- 微信小程序?qū)崿F(xiàn)簽到功能
- 微信小程序和公眾號(hào)實(shí)現(xiàn)簽到頁(yè)面
相關(guān)文章
淺談JS for循環(huán)中使用break和continue的區(qū)別
這篇文章主要介紹了淺談for循環(huán)中使用break和continue的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
JavaScript實(shí)現(xiàn)矩形塊大小任意縮放
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)矩形塊大小任意縮放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
基于JavaScript+HTML5 實(shí)現(xiàn)打地鼠小游戲邏輯流程圖文詳解(附完整代碼)
打地鼠小游戲大家都喜歡玩,本文是以html編寫的,并且使用HBulider軟件進(jìn)行編寫的,下面通過本文給大家分享基于JavaScript+HTML5 實(shí)現(xiàn)打地鼠小游戲邏輯流程圖文詳解,需要的朋友參考下吧2017-11-11

