How to realize a check-in function in WeChat Mini Programs
This article mainly explains "how to achieve a check-in function in WeChat Mini Programs". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn "how to achieve a check-in function in WeChat Mini Programs".
(1) query user check-in information API:
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 ('% Ymuri% mmi% d') / / serialize the date if now_date in all_date: signed=True else: signed=False total=len (all_date) conn.close () return {'total':total,'signed':signed}
After querying all check-in dates, use set to remove duplicates, and then determine whether the date of the day is among them. If not, signed=False indicates that you did not check in today. The total number of check-ins is the length of all_date.
The datetime library is used to get date information. From datetime import date
(2) add user check-in information API:
@ 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 ('% Ymuri% mmi% 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 ()
IV. The front end of Mini Program
Wxml file
Click here to sign in today has signed in {{total_sign}} days
Wxss file
.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 file
Get_sign: function () {var that = this; var userId = wx.getStorageSync ("userId") Wx.request ({url: 'http:// server public network 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:// server public network 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:' success', icon: 'success' Duration: 2000})} else {console.log ("status error:" + res.data.Exception)}},})}, thank you for reading The above is the content of "how to achieve a check-in function in WeChat Mini Programs". After the study of this article, I believe you have a deeper understanding of how to achieve a check-in function in WeChat Mini Programs. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!