微信签到...4.2

CoreService.java

 1 package org.weixin.service;
 2
 3 import java.text.DateFormat;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Calendar;
 6 import java.util.Date;
 7 import java.util.HashMap;
 8
 9 import javax.servlet.http.HttpServletRequest;
10
11 import org.weixin.message.TextMessage;
12 import org.weixin.util.MessageUtil;
13 import org.weixin.util.MySQLUtil;
14
15 public class CoreService {
16     public static String processRequest(HttpServletRequest request){
17         String respXML = null;
18         TextMessage tm = new TextMessage();
19         //解析微信服务器发送的请求
20         try {
21             HashMap<String,String>requestMap = MessageUtil.parseXML(request);
22             //用户的OpenID
23             String fromUserName = requestMap.get("FromUserName");
24             //公众号的原始ID
25             String toUserName = requestMap.get("ToUserName");
26             //请求消息类型
27             String msgType = requestMap.get("MsgType");
28
29             //回复
30             tm.setFromUserName(toUserName);
31             tm.setToUserName(fromUserName);
32             tm.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
33             tm.setCreateTime(new Date().getTime());
34
35             //将文本消息保存到数据库
36             if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)){
37                 String content = requestMap.get("Content");
38                 boolean result1 = MySQLUtil.isTodaySigned(fromUserName);
39                 boolean result2 = MySQLUtil.judgeSign(fromUserName);
40                 if(content.equals("签到")){
41                     if(!result1){
42                         MySQLUtil.saveDate(fromUserName);
43                     }
44                     if(result2){
45                         MySQLUtil.signIn(fromUserName);
46                         tm.setContent("签到成功[机智]\n签到时间为:\n"+MySQLUtil.showNow());
47                     }else{
48                         tm.setContent("上次还没签退呢[皱眉]无法签到!");
49                     }
50                 }
51                 if(content.equals("签退")){
52                     if(!result2){
53                         tm.setContent("签退成功[嘿哈]\n签退时间为:\n"+MySQLUtil.showNow()+"\n本次学习了 "+MySQLUtil.signOut(fromUserName));
54                     }else{
55                         tm.setContent("还没签到就想签退[奸笑]签到去吧孩子!");
56                     }
57                 }
58                 if(content.equals("查今天")){
59                     tm.setContent("今天已经学习了 "+MySQLUtil.selectDayTime(fromUserName, "today"));
60                 }
61                 if(content.equals("查昨天")){
62                     tm.setContent("你昨天学习了 "+MySQLUtil.selectDayTime(fromUserName, "yesterday"));
63                 }
64                 if(content.equals("查本周")){
65                     tm.setContent("你本周学习了 "+MySQLUtil.selectWeekTime(fromUserName));
66                 }
67                 if(content.contains("bd")){
68                     tm.setContent("您的微信已绑定学号"+MySQLUtil.linkNumber(fromUserName, content));
69                 }
70                 respXML=MessageUtil.messageToXML(tm);
71             }
72             else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)){
73                 tm.setContent("您发送的是图片消息!");
74                 respXML=MessageUtil.messageToXML(tm);
75             }
76             else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)){
77                 //事件类型
78                 String eventType = requestMap.get("Event");
79                 //关注事件
80                 if(eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)){
81                     tm.setContent("欢迎关注!");
82                     //MySQLUtil.saveWeixinUser(fromUserName);
83                 }
84                 else if(eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)){
85                     //更新weixin_user表的关注状态
86                 }
87             }
88             //respXML = MessageUtil.messageToXML(tm);
89         } catch (Exception e) {
90             e.printStackTrace();
91         }
92         return respXML;
93     }
94 }

MySQLUtil.java

package org.weixin.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MySQLUtil {
    public Connection getConnection() {
        Connection conn = null;
        String url = "jdbc:mysql://127.0.0.1:3306/student?useSSL=true";
        String username = "root";
        String password = "123456";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void releaseResource(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (null != rs) {
                rs.close();
            }
            if (null != ps) {
                ps.close();
            }
            if (null != conn) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 绑定学号
    public static String linkNumber(String openId, String s) {
        String number = s.substring(s.length()-10,s.length());
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        String sql = "UPDATE user SET open_id = ? WHERE number = ?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, openId);
            ps.setString(2, number);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            mysql.releaseResource(conn, ps, null);
        }
        return number;
    }

    // 签到
    public static void signIn(String openId) {
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        String sql = "insert into sign_in_out (open_id,date,sign_in) values (?,curdate(),curtime())";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, openId);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            mysql.releaseResource(conn, ps, null);
        }
    }

    // 存储用户名及日期到day_time
    public static void saveDate(String openId) {
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        String sql = "insert into day_time (open_id,date,time_count,weekday) values (?,curdate(),0,WEEKDAY(NOW())+1)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, openId);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            mysql.releaseResource(conn, ps, null);
        }
    }

    // 签退
    public static String signOut(String openId) {
        int maxId = 0;
        String result;
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        ResultSet rs = null;
        PreparedStatement ps = null;
        String sql1 = "SELECT MAX(id) as max FROM sign_in_out WHERE open_id = ? AND date = CURDATE()";
        try {
            ps = conn.prepareStatement(sql1);
            ps.setString(1, openId);
            rs = ps.executeQuery();
            if(rs.next()){
                maxId = rs.getInt("max");
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        String sql2 = "UPDATE sign_in_out SET sign_out = curtime() WHERE id = ?";
        try {
            ps = conn.prepareStatement(sql2);
            ps.setInt(1, maxId);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();

        } finally {
            mysql.releaseResource(conn, ps, rs);
        }
        result = MySQLUtil.countDayTime(openId, maxId);
        return result;
    }

    // 计算当天总时间
    public static String countDayTime(String openId, int maxId) {
        int sec = 0;
        String result = null;
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        ResultSet rs = null;
        PreparedStatement ps = null;
        String sql1 = "select (TIME_TO_SEC(sign_out) - TIME_TO_SEC(sign_in)) sec from sign_in_out WHERE id = ? ";
        try {
            ps = conn.prepareStatement(sql1);
            ps.setInt(1, maxId);
            rs = ps.executeQuery();
            if(rs.next()){
                sec = rs.getInt("sec");
                result = sec/3600 + "小时" + sec%3600/60 + "分" + sec%3600%60 +"秒";
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        String sql2 = "UPDATE day_time SET time_count = SEC_TO_TIME(TIME_TO_SEC(time_count)+"+sec+") WHERE open_id = ? AND date = CURDATE()";
        try {
            ps = conn.prepareStatement(sql2);
            ps.setString(1, openId);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            mysql.releaseResource(conn, ps, rs);
        }
        return result;
    }    

    // 判断用户今天是否签到过
    public static boolean isTodaySigned(String openId) {
        boolean result = false;
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        ResultSet rs = null;
        PreparedStatement ps = null;
        String sql = "SELECT count(*) as i FROM sign_in_out WHERE open_id= ? AND date = curdate()";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, openId);
            rs = ps.executeQuery();
            int signCounts = 0;
            if(rs.next()){
                signCounts = rs.getInt("i");
            }
            if(signCounts>0){
                result = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            mysql.releaseResource(conn, ps, rs);
        }
        return result;
    }

    //判断是否签退  没签退不能签到 没签到不能签退
    public static boolean judgeSign(String openId) {
        boolean result = false;
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        ResultSet rs = null;
        PreparedStatement ps = null;
        String sql1 = "SELECT sign_out FROM sign_in_out WHERE open_id = ? ORDER BY id DESC";
        try {
            ps = conn.prepareStatement(sql1);
            ps.setString(1, openId);
            rs = ps.executeQuery();
            if(rs.next()){
                if(rs.getString(1)!=null){
                    result = true;
                }
            }else{
                result = true;
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        finally {
            mysql.releaseResource(conn, ps, rs);
        }
        return result;
    }

    //查询每日时间   今天 昨天
    public static String selectDayTime(String openId, String day) {
        String dayTime = "\n查什么吖[捂脸][捂脸][捂脸]你都没来过";
        int time = 0;
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        ResultSet rs = null;
        PreparedStatement ps = null;
        String sql = "";
        if(day.equals("today")){
            sql = "SELECT TIME_TO_SEC(time_count) FROM day_time WHERE open_id = ? AND date = CURDATE()";
        }else if(day.equals("yesterday")){
            sql = "SELECT TIME_TO_SEC(time_count) FROM day_time WHERE open_id = ? AND date = DATE_SUB(curdate(),INTERVAL 1 DAY)";
        }
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, openId);
            rs = ps.executeQuery();
            if(rs.next()){
                time = rs.getInt(1);
                dayTime = time/3600 + "小时" + time%3600/60 + "分" + time%3600%60 + "秒[惊讶]";
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        finally {
            mysql.releaseResource(conn, ps, rs);
        }
        return dayTime;
    }

    //查询本周时间
    public static String selectWeekTime(String openId) {
        String weekTime = "\n查什么吖[捂脸][捂脸][捂脸]你都没来过";
        int time = 0;
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        ResultSet rs = null;
        PreparedStatement ps = null;
        String sql = "SELECT TIME_TO_SEC(time_count) FROM day_time WHERE open_id = ? AND date BETWEEN (SELECT DATE_SUB(CURDATE(),INTERVAL (SELECT WEEKDAY(NOW())) DAY)) AND CURDATE()";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, openId);
            rs = ps.executeQuery();
            while(rs.next()){
                time = time + rs.getInt(1);
            }
            weekTime = time/3600 + "小时" + time%3600/60 + "分" + time%3600%60 + "秒[惊讶]";
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        finally {
            mysql.releaseResource(conn, ps, rs);
        }
        return weekTime;
    }

    //显示时间
    public static String showNow() {
        String dateTime = null;
        MySQLUtil mysql = new MySQLUtil();
        Connection conn = mysql.getConnection();
        ResultSet rs = null;
        PreparedStatement ps = null;
        String sql = "SELECT NOW()";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                dateTime = rs.getString(1);
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        finally {
            mysql.releaseResource(conn, ps, rs);
        }
        return dateTime;
    }

}

用户表      签到签退表      每日时间表

  

时间: 2024-11-09 01:48:00

微信签到...4.2的相关文章

微信签到积分换卷小程序(完整前后台)

最近给学校旁边火锅店写了一个微信的签到领积分,积分兑换优惠卷.可乐.雪碧等商品的小程序. 思路:后台用的node express框架 数据库用的mongodb mongoose连接服务器和数据库,数据库这边是一个用户的集合,每个用户有用户名.所拥有的积分.签到天数.拥有的券等字段 签到页面:点击签到后变成今日已签到,函数用if,else判断 如果为点击签到,则发起请求在数据库查找该用户的天数并加1,积分+5,onshow函数请求签到的天数并显示,在点击签到后再触发一下 onshow函数,loca

java做的一个简易的微信签到系统

1.发送签到会提示你签到成功,并获得2积分 2.一天只能签到一次 3.连续一星期签到会额外送12个积分 4.可以查询你当前积分总数 效果图:本订阅好还在完善中,可能还不能用,不久后将会一步步完善功能,用于生活,到时欢迎大家关注"z植物空间z"订阅号, 部分代码: // 保存用户信息 public static void saveWeixinUser(String openId) { MySQLUtil mysql = new MySQLUtil(); Connection conn =

微信公众号开发系列-网页授权获取用户基本信息

OAuth2.0网页授权这个也是在做微信公众平台用到最多的,可以利用授权接口对自己平台内用户进行绑定,实现用户扫描码和微信分享.微信签到.微信商城购物等: 1.高级接口OAuth2.0网页授权设置 a.进入高级接口列表-OAuth2.0-点击修改设置授权域名,域名可以上一级域名和二级域名 b.填写要设置授权域名,我这里用的的是二级域名 体授权Url规则见下面详细描述,主要摘自微信公众平台开发文档,点击进入 2.网页授权获取用户基本信息说明 如果用户在微信中(Web微信除外)访问公众号的第三方网页

微信考勤玩法曝光!

上班签到.下班签退,还有时不时加个班.请个假.出个差--考勤管理作为企业最基础也最琐碎的一项事务,随着现在外勤业务越来越多,恐怕传统的考勤机已经奈何不了了!不过,时间总会给我们更好的馈赠--去年9月25日微信企业号推出以来,已经有不少企业尝试微信移动化考勤,有效地解决了企业考勤的几大棘手问题: 1.一对一对一,杜绝"友情"代签 众所周知,一个手机号绑定一个微信,考勤(工作)账号也是通过手机号码与微信账号进行匹配,这样,一个手机号一个微信一个工作账户形成了"一对一对一"

AEAI WX微信扩展框架技术手册

1 概述 数通畅联微信公众号申请之后,由于要满足提供网站推广.功能演示.以及公司内部移动办公三方面的需求,所以把最初的订阅号更改为服务号,同时做了实名认证,这样就可以获取微信公众平台绝大部分接口,在完成数通畅联公众号相关功能过程中参考网上大量资料,期间封装AEAI WX微信扩展框架托管于开源中国社区http://git.oschina.net/agileai/aeaiwx. 在这里感谢特别柳峰对微信公众号知识的普及和推广,这是他博客链接http://blog.csdn.net/lyq8479,在

微会动微信互动3D摇一摇助力沃奥思航海教育产品推介会

2018年6月11日,由秦皇岛国际旅游港泰盛鸿洲国际游艇俱乐部与沃奥思营地联合主办,枫宝文化(北京)有限公司承办"'星'教育,新文旅",沃奥思航海教育产品推介会在京北京京瑞温泉国际酒店隆重举行,200余位各界人士出席参加本次活动. 在本次推介会上青少年冠军帆船夏令营横空出世,颠覆传统教育运动.程式化的夏令营模式,将更健康.更新颖.更有意义的营地教育带给青少年,开创青少年教育的新范式.新领域. 本届推介会现场引入了时下火爆的微信3D大屏互动摇一摇,助力现场火热氛围,技术由"微信

python3之51cto自动定时签到

前言 之前写过一遍,pyhton结合图灵机器人自动回复,之前51cto是使用网页签到,但是已经失效了,微信签到的原理和之前的机器人差不多的,不过是使用了python的itchat模块,结合定时功能,自动签到. 安装模块 这里使用的是python3 pip install itchat pip install apscheduler 代码 #!/usr/bin/env python # -*- coding:utf-8 -*- """ @author:Aiker Zhao @f

腾讯实习生的0-&gt;1

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 11.0px "PingFang SC"; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Helvetica; color: #000000; min-height: 13.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px;

Stay Hungry Stay Foolish——网络学习平台分享

从1月24号回家也有一阵子了,今天已经是31号,这一个周的中心思想就是一个字,玩. 学生一但远离学校,就会碰到许多学习的阻力,有来自外界的,家里有活要干,有亲戚要访,有同学邀约:也有来自内在的,自由的作息时间和内心可怕的惰性.我还记得之前几个学期,每个学期都背好多本书回来,但却没有一次看完哪怕一本.这一次,无论如何,这一个多月的寒假,不应该被忽略掉,挥霍掉,为了以后的寒假可以轻松愉快,学生时代的寒假需要我们充分利用. 最近几年,学习技能的传统媒介--书本受到了来自网络学习平台的强有力的冲击,全面