package com.ks.tool.bkz.controller;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.google.gson.Gson;
|
import com.ks.tool.bkz.entity.user.SDLJShareOpenHistory;
|
import com.ks.tool.bkz.entity.user.UserInfo;
|
import com.ks.tool.bkz.exception.CardPwdException;
|
import com.ks.tool.bkz.exception.SDLJShareOpenHistoryException;
|
import com.ks.tool.bkz.exception.UserException;
|
import com.ks.tool.bkz.service.manager.RedisManager;
|
import com.ks.tool.bkz.service.user.SDLJShareOpenHistoryService;
|
import com.ks.tool.bkz.service.user.UserService;
|
import com.ks.tool.bkz.service.user.UserUpgradeService;
|
import com.ks.tool.bkz.util.*;
|
import com.ks.tool.bkz.util.email.MailSenderUtil;
|
import com.ks.tool.bkz.vo.user.UserInfoVO;
|
import io.netty.util.Constant;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpSession;
|
import java.io.IOException;
|
|
@Controller
|
@RequestMapping("user")
|
public class UserController {
|
|
private final static String RANDKEY = "LoginImageCode";
|
|
@Resource
|
private RedisManager redisManager;
|
|
@Resource
|
private UserService userService;
|
|
@Resource
|
private SDLJShareOpenHistoryService sdljShareOpenHistoryService;
|
|
@Resource
|
private UserUpgradeService userUpgradeService;
|
|
|
private String getVCodeKey(String account) {
|
|
return "login-vcode-" + StringUtil.Md5(account);
|
}
|
|
/**
|
* 登录
|
*
|
* @param account
|
* @param vcode
|
* @return
|
*/
|
@RequestMapping("login")
|
@ResponseBody
|
public String login(String account, String vcode, HttpServletRequest request) {
|
if (StringUtil.isNullOrEmpty(account))
|
return JsonUtil.loadFalseResult(1, "账号不能为空");
|
if (StringUtil.isNullOrEmpty(vcode))
|
return JsonUtil.loadFalseResult(1, "验证码不能为空");
|
String key = getVCodeKey(account);
|
String oldVcode = redisManager.get(key);
|
// if (oldVcode == null || !oldVcode.equalsIgnoreCase(vcode))
|
// return JsonUtil.loadFalseResult(1, "验证码错误");
|
redisManager.delete(key);
|
account = account.trim();
|
UserInfo user = userService.selectByAccount(account);
|
if (user == null) {
|
try {
|
user = new UserInfo();
|
user.setAccount(account);
|
user.setState(UserInfo.STATE_NORMAL);
|
userService.addUser(user);
|
userService.login(account, request.getRemoteHost() + ":" + request.getRemotePort());
|
|
} catch (UserException e) {
|
return JsonUtil.loadFalseResult(e.getCode(), e.getMsg());
|
} catch (Exception e) {
|
return JsonUtil.loadFalseResult(1000, "服务器内部出错,请稍后再试!");
|
}
|
} else {
|
userService.login(account, request.getRemoteHost() + ":" + request.getRemotePort());
|
}
|
|
SDLJShareOpenHistory history = sdljShareOpenHistoryService.selectLatestHistory(user.getId());
|
|
String token = UserUtil.getToken(account, System.currentTimeMillis());
|
return JsonUtil.loadTrueResult(JsonUtil.getSimpleGson().toJson(new UserInfoVO(user.getAccount(), token, history == null ? null : history.getExpireTime())));
|
}
|
|
@RequestMapping(value = "getUserInfo", method = RequestMethod.POST)
|
@ResponseBody
|
public String getUserInfo(HttpServletRequest request) {
|
String token = request.getHeader("token");
|
String account = UserUtil.getAccountFromToken(token);
|
UserInfo user = userService.selectByAccount(account);
|
if (user != null) {
|
SDLJShareOpenHistory history = sdljShareOpenHistoryService.selectLatestHistory(user.getId());
|
return JsonUtil.loadTrueResult(JsonUtil.getSimpleGson().toJson(new UserInfoVO(user.getAccount(), "", history == null ? null : history.getExpireTime())));
|
}
|
return JsonUtil.loadFalseResult(1, "用户不存在");
|
}
|
|
|
/**
|
* 分享版续费
|
*
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "renewShare", method = RequestMethod.POST)
|
@ResponseBody
|
public String renewShare(String card, String pwd, HttpServletRequest request) {
|
if (StringUtil.isNullOrEmpty(card) || StringUtil.isNullOrEmpty(pwd))
|
return JsonUtil.loadFalseResult(1, "请输入卡号和密码");
|
String token = request.getHeader("token");
|
String account = UserUtil.getAccountFromToken(token);
|
UserInfo user = userService.selectByAccount(account);
|
if (user != null) {
|
try {
|
userUpgradeService.upgradeSDLJShare(user.getId(), card, pwd);
|
SDLJShareOpenHistory history = sdljShareOpenHistoryService.selectLatestHistory(user.getId());
|
if (history == null) {
|
try {
|
Thread.sleep(100);
|
} catch (Exception e) {
|
}
|
history = sdljShareOpenHistoryService.selectLatestHistory(user.getId());
|
}
|
return JsonUtil.loadTrueResult(JsonUtil.getSimpleGson().toJson(new UserInfoVO(user.getAccount(), "", history.getExpireTime())));
|
} catch (UserException e) {
|
return JsonUtil.loadFalseResult(2, e.getMsg());
|
} catch (CardPwdException e) {
|
return JsonUtil.loadFalseResult(3, e.getMsg());
|
} catch (SDLJShareOpenHistoryException e) {
|
return JsonUtil.loadFalseResult(4, e.getMsg());
|
}
|
|
} else {
|
return JsonUtil.loadFalseResult(1, "用户不存在");
|
}
|
}
|
|
|
/**
|
* 获取邮箱验证码
|
*
|
* @param email
|
* @param request
|
* @return
|
*/
|
@RequestMapping("getVerifyCode")
|
@ResponseBody
|
public String getVerifyCode(String email, String vcode, HttpServletRequest request) {
|
if (!StringUtil.isEmail(email)) {
|
return JsonUtil.loadFalseResult(1, "邮箱格式不正确");
|
}
|
|
HttpSession session = request.getSession(true);
|
if (session.getAttribute(RANDKEY) == null || !session.getAttribute(RANDKEY).toString().equalsIgnoreCase(vcode))
|
return JsonUtil.loadFalseResult(1, "图片验证码错误");
|
session.removeAttribute(RANDKEY);
|
String source = "0123456789";
|
String code = "";
|
for (int i = 0; i < 6; i++) {
|
code += source.charAt((int) (Math.random() * source.length()));
|
}
|
MailSenderUtil.sendEmail(email, "爆款猪登录验证", "登录验证码为:" + code);
|
redisManager.save(getVCodeKey(email), code, 5 * 60);
|
return JsonUtil.loadTrueResult("");
|
}
|
|
/**
|
* 图片验证码
|
*
|
* @param request
|
* @param response
|
*/
|
@RequestMapping(value = "/imgcode", method = RequestMethod.GET)
|
public void imgCode(HttpServletRequest request, HttpServletResponse response) {
|
response.setHeader("Pragma", "No-cache");
|
response.setHeader("Cache-Control", "no-cache");
|
response.setDateHeader("Expires", 0);
|
response.setContentType("image/jpeg");
|
|
// 生成随机字串
|
String verifyCode = VerifyCodeUtil.generateVerifyCode(4);
|
// 存入会话session
|
HttpSession session = request.getSession(true);
|
session.setAttribute(RANDKEY, verifyCode.toLowerCase());
|
// 生成图片
|
int w = 200, h = 80;
|
try {
|
VerifyCodeUtil.outputImage(w, h, response.getOutputStream(), verifyCode);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|