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

时间:2017-03-25 22:23:44   收藏:0   阅读:954

1.发送签到会提示你签到成功,并获得2积分

2.一天只能签到一次

3.连续一星期签到会额外送12个积分

4.可以查询你当前积分总数

效果图:本订阅好还在完善中,可能还不能用,不久后将会一步步完善功能,用于生活,到时欢迎大家关注"z植物空间z"订阅号,

技术分享

部分代码:

// 保存用户信息
public static void saveWeixinUser(String openId) {
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
String sql = "insert into weixin_user (open_id,subscribe_time,subscribe_status) values (?,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 void updateUserPoints(String openId, int signPoints) {
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
String sql = "update weixin_user set points=points+? where open_id = ?";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, signPoints);
ps.setString(2, openId);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
mysql.releaseResource(conn, ps, null);
}
}

// 保存签到信息
public static void saveWeixinSign(String openId, int signPoints) {
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
String sql = "insert into weixin_sign (open_id,sign_time,sign_points) values (?,now(),?)";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, openId);
ps.setInt(2, signPoints);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
mysql.releaseResource(conn, ps, null);
}
}

//用户查积分
public static String searchjifen(String openId){
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
String signPoints=null;
String sql = "select sign_Points from weixin_sign where open_id=?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, openId);
rs = ps.executeQuery();
if(rs.next()){
signPoints=rs.getString("sign_Points");
}

} catch (SQLException e) {
e.printStackTrace();
} finally {
mysql.releaseResource(conn, ps, rs);
}
return signPoints;
}

// 判断用户今天是否签到过
public static boolean isTodaySigned(String openId) {
boolean result = false;
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
ResultSet rs = null;
String sql = "SELECT count(*) as signCounts FROM weixin_sign WHERE open_id=? AND DATE_FORMAT(sign_time,‘%Y-%m-%d‘) = DATE_FORMAT(now(),‘%Y-%m-%d‘)";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, openId);
rs = ps.executeQuery();

int signCounts = 0;
if(rs.next()){
signCounts = rs.getInt("signCounts");
}
if(1 == signCounts){
result = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
mysql.releaseResource(conn, ps, rs);
}
return result;
}

// 判断用户本周是否第七次签到
public static boolean isSevenSign(String openId, String monday) {
boolean result = false;
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
ResultSet rs = null;
String sql = "SELECT count(*) FROM weixin_sign WHERE open_id=? AND sign_time between str_to_date(‘?‘,‘%Y-%m-%d %H:%i:%s‘) and now()";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, openId);
ps.setString(2, monday);
rs = ps.executeQuery();

int signCounts = 0;
if(rs.next()){
signCounts = rs.getInt("signCounts");
}
if(6 == signCounts){
result = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
mysql.releaseResource(conn, ps, rs);
}
return result;
}

----------------------------------------------------------------------------------------------------------------

//将文本消息保存到数据库
if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)){
String content = requestMap.get("Content");
if(content.equals("签到")){
boolean result = MySQLUtil.isTodaySigned(fromUserName);
if(!result){
boolean flag = MySQLUtil.isSevenSign(fromUserName, getMondayOfThisWeek()+" 00:00:00");
//连续7天签到,送12积分
if(flag){
MySQLUtil.saveWeixinSign(fromUserName, 12);
MySQLUtil.updateUserPoints(fromUserName, 12);
tm.setContent("签到成功!获得2个积分!"+"\n签到时间为:"+str+"\n本周连续7次签到,额外赠送10个积分!");
}
//每天签到送2积分
else{
MySQLUtil.saveWeixinSign(fromUserName, 2);
MySQLUtil.updateUserPoints(fromUserName, 2);
tm.setContent("签到成功!获得2积分"+"\n签到时间为:"+str);
}
}else{
tm.setContent("您今日已签到,明日再来吧!");
}
}else if(content.equals("查积分")){
String signPoints=MySQLUtil.searchjifen(fromUserName);
tm.setContent("您的当前积分为:"+signPoints+"积分");
}else{tm.setContent("暂时无此服务");}
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!