Merge branch 'feature/profilemodify' into develop

This commit is contained in:
Sola
2015-12-15 21:30:07 +08:00
7 changed files with 225 additions and 67 deletions

View File

@@ -1,7 +1,26 @@
package love.sola.netsupport.api; package love.sola.netsupport.api;
import com.google.gson.Gson;
import love.sola.netsupport.enums.Attribute;
import love.sola.netsupport.enums.ISP;
import love.sola.netsupport.pojo.User;
import love.sola.netsupport.sql.SQLCore;
import love.sola.netsupport.sql.TableUser;
import love.sola.netsupport.util.Checker;
import love.sola.netsupport.util.ParseUtil;
import love.sola.netsupport.wechat.Command;
import me.chanjar.weixin.common.session.WxSession;
import org.hibernate.exception.ConstraintViolationException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import static love.sola.netsupport.util.Checker.*;
/** /**
* *********************************************** * ***********************************************
@@ -9,9 +28,59 @@ import javax.servlet.http.HttpServlet;
* Don't modify this source without my agreement * Don't modify this source without my agreement
* *********************************************** * ***********************************************
*/ */
@WebServlet(name = "Register", urlPatterns = "/api/register", loadOnStartup = 22) @WebServlet(name = "ProfileModify", urlPatterns = "/api/profilemodify", loadOnStartup = 22)
public class ProfileModify extends HttpServlet { public class ProfileModify extends HttpServlet {
private Gson gson = SQLCore.gson;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.addHeader("Content-type", "text/json;charset=utf-8");
PrintWriter out = response.getWriter();
String json = gson.toJson(process(request));
out.println(ParseUtil.parseJsonP(request, json));
out.close();
}
private Response process(HttpServletRequest request) {
WxSession session = Checker.isAuthorized(request, Command.PROFILE);
if (session == null) {
return new Response(Response.ResponseCode.UNAUTHORIZED);
}
User u = (User) session.getAttribute(Attribute.USER);
if (u == null) return new Response(Response.ResponseCode.UNAUTHORIZED);
ISP isp = checkISP(request.getParameter("isp"));
String netAccount = checkNetAccount(request.getParameter("username"), isp);
int block = checkBlock(request.getParameter("block"));
int room = checkRoom(request.getParameter("room"), block);
long phone = checkPhoneNumber(request.getParameter("phone"));
if (netAccount != null) {
u.setIsp(isp);
u.setNetAccount(netAccount);
}
if (room != -1) {
u.setBlock(block);
u.setRoom(room);
}
if (phone != -1) {
u.setPhone(phone);
}
try {
TableUser.update(u);
} catch (ConstraintViolationException e) {
String dupKey = e.getConstraintName();
return new Response(Response.ResponseCode.REQUEST_FAILED, "Duplicated_" + dupKey.toUpperCase());
} catch (Exception e) {
e.printStackTrace();
return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage());
}
session.invalidate();
return new Response(Response.ResponseCode.OK);
}
} }

View File

@@ -20,6 +20,8 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import static love.sola.netsupport.util.Checker.*;
/** /**
* *********************************************** * ***********************************************
* Created by Sola on 2015/11/29. * Created by Sola on 2015/11/29.
@@ -29,9 +31,6 @@ import java.io.PrintWriter;
@WebServlet(name = "Register", urlPatterns = "/api/register", loadOnStartup = 21) @WebServlet(name = "Register", urlPatterns = "/api/register", loadOnStartup = 21)
public class Register extends HttpServlet { public class Register extends HttpServlet {
public static final String STUDENT_ID_REGEX = "^(2010|2012|2013|2014|2015)[0-9]{9}$";
public static final String PHONE_NUMBER_REGEX = "^1[34578][0-9]{9}$";
private Gson gson = SQLCore.gson; private Gson gson = SQLCore.gson;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
@@ -73,7 +72,6 @@ public class Register extends HttpServlet {
out.close(); out.close();
} }
@SuppressWarnings("Duplicates")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response); doPost(request, response);
} }
@@ -106,66 +104,6 @@ public class Register extends HttpServlet {
return "Register_Success"; return "Register_Success";
} }
private long checkStudentId(String studentId) {
if (studentId == null) return -1;
if (studentId.matches(STUDENT_ID_REGEX)) {
try {
return Long.parseLong(studentId);
} catch (NumberFormatException ignored) {
}
}
return -1;
}
private long checkPhoneNumber(String phone) {
if (phone == null) return -1;
if (phone.matches(PHONE_NUMBER_REGEX)) {
try {
return Long.parseLong(phone);
} catch (NumberFormatException ignored) {
}
}
return -1;
}
private ISP checkISP(String isp) {
if (isp == null) return null;
try {
return ISP.fromId(Integer.parseInt(isp));
} catch (NumberFormatException ignored) {
}
return null;
}
private String checkNetAccount(String account, ISP isp) {
if (isp == null) return null;
if (account == null) return null;
if (!account.matches(isp.accountRegex)) return null;
return account;
}
private int checkBlock(String block) {
if (block == null) return -1;
try {
return Integer.parseInt(block);
} catch (NumberFormatException ignored) {
}
return -1;
}
private int checkRoom(String room, int block) {
if (block == -1) return -1;
if (room == null) return -1;
try {
Integer i = Integer.parseInt(room);
if (i <= 100 || i >= 1300) return -1;
return i;
} catch (NumberFormatException ignored) {
}
return -1;
}
private void printAuthorizeFailed(HttpServletRequest request, PrintWriter out) { private void printAuthorizeFailed(HttpServletRequest request, PrintWriter out) {
out.println(ParseUtil.parseJsonP(request, gson.toJson(new Response(Response.ResponseCode.AUTHORIZE_FAILED)))); out.println(ParseUtil.parseJsonP(request, gson.toJson(new Response(Response.ResponseCode.AUTHORIZE_FAILED))));
out.close(); out.close();

View File

@@ -53,4 +53,45 @@ public class Block {
} }
} }
public static final int[][] AVAILABLE = new int[62][0];
static {
// -------------------------------------------- //
// THANKS DATA PROVIDED BY Lai Juncheng
// -------------------------------------------- //
AVAILABLE[FX_1] = new int[]{108, 208, 308, 408, 508};
AVAILABLE[FX_2] = new int[]{110, 210, 310, 410, 510, 610};
AVAILABLE[FX_3] = new int[]{110, 210, 310, 410, 510, 610};
AVAILABLE[FX_4] = new int[]{110, 210, 310, 410, 510, 610, 710};
AVAILABLE[FX_5] = new int[]{108, 208, 308, 408, 508, 608, 708};
AVAILABLE[BM_7] = new int[]{100, 216, 317, 417, 517, 617, 717};
AVAILABLE[BM_8] = new int[]{100, 221, 321, 421, 521, 621, 721};
AVAILABLE[BM_9] = new int[]{100, 221, 321, 421, 521, 621};
AVAILABLE[BM_10] = new int[]{111, 239, 339, 439, 558, 658, 758, 858};
AVAILABLE[BM_11] = new int[]{100, 220, 320, 420, 520, 620, 720, 820};
AVAILABLE[DM_12] = new int[]{119, 221, 321, 421, 521, 621, 720};
AVAILABLE[DM_13] = new int[]{120, 222, 322, 422, 522, 622, 722};
AVAILABLE[DM_14] = new int[]{100, 230, 330, 430, 530, 630, 730};
AVAILABLE[DM_15] = new int[]{119, 219, 319, 419, 519, 619};
AVAILABLE[QT_16] = new int[]{154, 257, 357, 457, 557, 657, 757};
AVAILABLE[QT_17] = new int[]{154, 257, 357, 457, 557, 657, 757};
AVAILABLE[QT_18] = new int[]{139, 239, 339, 439, 539, 639, 739};
AVAILABLE[QT_19] = new int[]{100, 200, 332, 432, 532, 632, 732};
AVAILABLE[DM_20] = new int[]{109, 209, 309, 409, 509, 609, 709};
AVAILABLE[DM_21] = new int[]{109, 209, 309, 409, 509, 609, 709};
AVAILABLE[XH_A] = new int[]{129, 231, 331, 431, 531, 631, 731, 831, 931, 1031, 1131, 1231};
AVAILABLE[XH_B] = new int[]{129, 229, 329, 429, 529, 629, 729, 829, 929, 1029, 1129, 1229};
AVAILABLE[XH_C] = new int[]{126, 226, 326, 426, 526, 626, 726, 826, 926, 1026, 1126, 1226};
AVAILABLE[XH_D] = new int[]{128, 228, 328, 428, 528, 628, 728, 828, 928, 1028, 1128, 1228};
AVAILABLE[FX_6] = new int[0];
}
public static boolean checkRoom(int block, int room) {
int floor = room / 100;
if (floor == 0 || room % 100 == 0) return false;
if (block < 0 || block >= AVAILABLE.length) return false;
if (AVAILABLE[block].length < floor) return false;
return room <= AVAILABLE[block][floor - 1];
}
} }

View File

@@ -1,6 +1,8 @@
package love.sola.netsupport.util; package love.sola.netsupport.util;
import love.sola.netsupport.enums.Attribute; import love.sola.netsupport.enums.Attribute;
import love.sola.netsupport.enums.Block;
import love.sola.netsupport.enums.ISP;
import love.sola.netsupport.wechat.Command; import love.sola.netsupport.wechat.Command;
import love.sola.netsupport.wechat.WechatSession; import love.sola.netsupport.wechat.WechatSession;
import me.chanjar.weixin.common.session.WxSession; import me.chanjar.weixin.common.session.WxSession;
@@ -15,6 +17,9 @@ import javax.servlet.http.HttpServletRequest;
*/ */
public class Checker { public class Checker {
public static final String STUDENT_ID_REGEX = "^(2010|2012|2013|2014|2015)[0-9]{9}$";
public static final String PHONE_NUMBER_REGEX = "^1[34578][0-9]{9}$";
public static boolean hasNull(Object... v) { public static boolean hasNull(Object... v) {
for (Object o : v) if (o == null) return true; for (Object o : v) if (o == null) return true;
return false; return false;
@@ -27,4 +32,64 @@ public class Checker {
return s == null ? null : s.getAttribute(Attribute.AUTHORIZED) == c ? s : null; return s == null ? null : s.getAttribute(Attribute.AUTHORIZED) == c ? s : null;
} }
public static long checkStudentId(String studentId) {
if (studentId == null) return -1;
if (studentId.matches(STUDENT_ID_REGEX)) {
try {
return Long.parseLong(studentId);
} catch (NumberFormatException ignored) {
}
}
return -1;
}
public static long checkPhoneNumber(String phone) {
if (phone == null) return -1;
if (!phone.matches(PHONE_NUMBER_REGEX)) return -1;
try {
return Long.parseLong(phone);
} catch (NumberFormatException ignored) { }
return -1;
}
public static ISP checkISP(String isp) {
if (isp == null) return null;
try {
return ISP.fromId(Integer.parseInt(isp));
} catch (NumberFormatException ignored) { }
return null;
}
public static String checkNetAccount(String account, ISP isp) {
if (isp == null) return null;
if (account == null) return null;
if (!account.matches(isp.accountRegex)) return null;
return account;
}
public static int checkBlock(String block) {
if (block == null) return -1;
try {
int b = Integer.parseInt(block);
if (Block.inverseMap.containsKey(b))
return b;
else
return -1;
} catch (NumberFormatException ignored) { }
return -1;
}
public static int checkRoom(String room, int block) {
if (block == -1) return -1;
if (room == null) return -1;
try {
Integer i = Integer.parseInt(room);
if (Block.checkRoom(block, i))
return i;
else
return -1;
} catch (NumberFormatException ignored) { }
return -1;
}
} }

View File

@@ -20,6 +20,7 @@ public enum Command {
QUERY(1, QueryHandler.class), QUERY(1, QueryHandler.class),
SUBMIT(2, SubmitHandler.class), SUBMIT(2, SubmitHandler.class),
CANCEL(3, CancelHandler.class), CANCEL(3, CancelHandler.class),
PROFILE(4, ProfileHandler.class),
LOGIN(10, LoginHandler.class), LOGIN(10, LoginHandler.class),
; ;

View File

@@ -0,0 +1,42 @@
package love.sola.netsupport.wechat.handler;
import love.sola.netsupport.enums.Attribute;
import love.sola.netsupport.pojo.User;
import love.sola.netsupport.sql.TableUser;
import love.sola.netsupport.wechat.Command;
import love.sola.netsupport.wechat.WechatSession;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSession;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.outxmlbuilder.TextBuilder;
import java.util.Map;
import static love.sola.netsupport.config.Lang.format;
/**
* ***********************************************
* Created by Sola on 2015/12/15.
* Don't modify this source without my agreement
* ***********************************************
*/
public class ProfileHandler implements WxMpMessageHandler {
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
User u = TableUser.getByWechat(wxMessage.getFromUserName());
String id = WechatSession.genId();
WxSession session = WechatSession.get(id, true);
session.setAttribute(Attribute.AUTHORIZED, Command.PROFILE);
session.setAttribute(Attribute.WECHAT, wxMessage.getFromUserName());
session.setAttribute(Attribute.USER, u);
TextBuilder out = WxMpXmlOutMessage.TEXT().fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName());
out.content(format("User_Profile_Link", id, u.getName(), u.getIsp().id, u.getNetAccount(), u.getBlock(), u.getRoom(), u.getPhone()));
return out.build();
}
}

View File

@@ -5,10 +5,11 @@ Unknown_Encrypt_Type: 'Unknown encrypt-type.'
#Command Regex #Command Regex
REGEX_QUERY: '^(?i)(Query)|(查询)|(cx)$' REGEX_QUERY: '^(?i)(Query)|(查询)|(cx)$'
REGEX_REGISTER: '^(?i)(Reg(ister)?)|(注册)|(绑定)|(zc)$' REGEX_REGISTER: '^(?i)(Reg(ister)?)|(注册)|(绑定)|(zc)|(bd)$'
REGEX_SUBMIT: '^(?i)(Submit)|(报修)|(bx)$' REGEX_SUBMIT: '^(?i)(Submit)|(报修)|(bx)$'
REGEX_CANCEL: '^(?i)(Cancel)|(取消)|(撤销)|(qx)$' REGEX_CANCEL: '^(?i)(Cancel)|(取消)|(撤销)|(qx)|(cx)$'
REGEX_LOGIN: '^(?i)Authme$' REGEX_LOGIN: '^(?i)Authme$'
REGEX_PROFILE: '^(?i)(EditProfile)|(修改资料)|(xgzl)$'
#Event #Event
Event_Subscribe: "欢迎使用电子科技大学中山学院网络维护科微信自助报修平台。\n如您在使用中遇到任何问题请将投诉或建议邮件至loli@sola.love.\n\n请发送'绑定'进行微信绑定" Event_Subscribe: "欢迎使用电子科技大学中山学院网络维护科微信自助报修平台。\n如您在使用中遇到任何问题请将投诉或建议邮件至loli@sola.love.\n\n请发送'绑定'进行微信绑定"
@@ -44,6 +45,7 @@ Internal_Error: '啊哦,登录失败了哦。'
User_Register_Link: '您尚未进行微信绑定, 请<a href="http://topaz.sinaapp.com/nm/v1/reg.html?token={0}">点击这里</a>进行微信绑定操作。' User_Register_Link: '您尚未进行微信绑定, 请<a href="http://topaz.sinaapp.com/nm/v1/reg.html?token={0}">点击这里</a>进行微信绑定操作。'
User_Query_Link: 'http://topaz.sinaapp.com/nm/v1/list.html?token={0}' User_Query_Link: 'http://topaz.sinaapp.com/nm/v1/list.html?token={0}'
User_Submit_Link: 'http://topaz.sinaapp.com/nm/v1/rrepair.html?token={0}&name={1}&isp={2}&room={3}&block={4}&phone={5,number,#}' User_Submit_Link: 'http://topaz.sinaapp.com/nm/v1/rrepair.html?token={0}&name={1}&isp={2}&room={3}&block={4}&phone={5,number,#}'
User_Profile_Link: 'http://topaz.sinaapp.com/nm/v1/profile.html?token={0}&name={1}&isp={2}&account={3}&block={4}&room={5}&phone={6,number,#}'
Result_Page: 'http://topaz.sinaapp.com/nm/v1/result.html' Result_Page: 'http://topaz.sinaapp.com/nm/v1/result.html'
Operator_Home_Page: '<a href="http://topaz.sinaapp.com/nm/v1/man/home.html?token={0}">CLICK HERE</a>' Operator_Home_Page: '<a href="http://topaz.sinaapp.com/nm/v1/man/home.html?token={0}">CLICK HERE</a>'
Operator_Login_Page: '' Operator_Login_Page: ''