微信小程序簽到功能
本文實(shí)例為大家分享了簡易微信小程序簽到功能的具體代碼,供大家參考,具體內(nèi)容如下
一、效果圖
點(diǎn)擊簽到后
二、數(shù)據(jù)庫
用一張數(shù)據(jù)表存用戶簽到的信息,每次用戶簽到都會往表中添加一條記錄了用戶id和簽到日期的數(shù)據(jù),如下圖
三、后端
后端寫兩個接口,一個用于查詢用戶今日是否簽到和簽到記錄總數(shù),一個用于添加用戶簽到信息到數(shù)據(jù)庫。這里用的是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的長度
使用了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'>點(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) } }, }) },
用戶登錄后,會立即觸發(fā)get_sign函數(shù),從數(shù)據(jù)庫獲取用戶簽到信息存到page的data中,頁面也會顯示用戶今日是否簽到和簽到總數(shù)。
用戶點(diǎn)擊簽到后,會保存簽到信息,并更新data。用showToast彈窗提示簽到成功。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談JS for循環(huán)中使用break和continue的區(qū)別
這篇文章主要介紹了淺談for循環(huán)中使用break和continue的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07JavaScript實(shí)現(xiàn)矩形塊大小任意縮放
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)矩形塊大小任意縮放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08基于JavaScript+HTML5 實(shí)現(xiàn)打地鼠小游戲邏輯流程圖文詳解(附完整代碼)
打地鼠小游戲大家都喜歡玩,本文是以html編寫的,并且使用HBulider軟件進(jìn)行編寫的,下面通過本文給大家分享基于JavaScript+HTML5 實(shí)現(xiàn)打地鼠小游戲邏輯流程圖文詳解,需要的朋友參考下吧2017-11-11