這篇文章主要講解了“微信小程序中如何實現(xiàn)一個簽到功能”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“微信小程序中如何實現(xiàn)一個簽到功能”吧!
(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去除重復(fù)項,然后判斷一下當(dāng)天的日期是否在其中,如果不在其中,signed=False表示今日未簽到。簽到總數(shù)就是all_date的長度
使用了datetime庫來獲取日期信息。from datetime import date
(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文件
點(diǎn)擊此處簽到 今日已簽到 已簽到{{total_sign}}天
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) } }, }) },
感謝各位的閱讀,以上就是“微信小程序中如何實現(xiàn)一個簽到功能”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對微信小程序中如何實現(xiàn)一個簽到功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!