package com.yeshi.buwan.controller.parser;
|
|
import com.google.gson.GsonBuilder;
|
import com.yeshi.buwan.domain.*;
|
import com.yeshi.buwan.domain.system.DetailSystem;
|
import com.yeshi.buwan.domain.user.LoginUser;
|
import com.yeshi.buwan.dto.user.LoginInfoDto;
|
import com.yeshi.buwan.dto.user.QQUserInfo;
|
import com.yeshi.buwan.exception.user.LoginUserException;
|
import com.yeshi.buwan.exception.SMSException;
|
import com.yeshi.buwan.exception.user.RegisterUserException;
|
import com.yeshi.buwan.service.imp.CommentService;
|
import com.yeshi.buwan.service.imp.MaskKeyService;
|
import com.yeshi.buwan.service.imp.SystemService;
|
import com.yeshi.buwan.service.imp.UserService;
|
import com.yeshi.buwan.service.imp.push.PushService;
|
import com.yeshi.buwan.service.inter.LoginUserService;
|
import com.yeshi.buwan.service.inter.SMSService;
|
import com.yeshi.buwan.util.*;
|
import com.yeshi.buwan.util.annotation.RequireUid;
|
import com.yeshi.buwan.util.wx.MyWXLoginUtil;
|
import com.yeshi.buwan.vo.AcceptData;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.stereotype.Controller;
|
import org.yeshi.utils.entity.wx.WeiXinUser;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.PrintWriter;
|
import java.io.Serializable;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.List;
|
|
@Controller
|
public class CommentParser {
|
|
Logger logger = LoggerFactory.getLogger(CommentParser.class);
|
|
@Resource
|
private SystemService systemService;
|
@Resource
|
private CommentService commentService;
|
@Resource
|
private UserService userService;
|
@Resource
|
private PushService pushService;
|
@Resource
|
private MaskKeyService maskKeyService;
|
|
@Resource
|
private LoginUserService loginUserService;
|
|
@Resource
|
private SMSService smsService;
|
|
@Resource
|
private RedisManager redisManager;
|
|
@RequireUid
|
public void getReadState(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
|
boolean have = commentService.isHaveNewMessage(acceptData.getUid());
|
JSONObject object = new JSONObject();
|
object.put("Read", have);
|
out.print(JsonUtil.loadTrueJson(object.toString()));
|
|
}
|
|
public void login(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
|
String name = request.getParameter("Name");
|
String portrait = request.getParameter("Portrait");
|
String openid = request.getParameter("OpenId");
|
String sex = request.getParameter("Sex");// 1-男 2-女
|
String loginType = request.getParameter("LoginType");// 登录类型
|
|
|
if (StringUtil.isNullOrEmpty(name)) {
|
out.print(JsonUtil.loadFalseJson("请上传Platform"));
|
return;
|
}
|
|
if (StringUtil.isNullOrEmpty(portrait)) {
|
out.print(JsonUtil.loadFalseJson("请上传Platform"));
|
return;
|
}
|
|
if (StringUtil.isNullOrEmpty(openid)) {
|
out.print(JsonUtil.loadFalseJson("请上传Platform"));
|
return;
|
}
|
|
LoginInfoDto loginInfoDto = new LoginInfoDto();
|
loginInfoDto.setIpInfo(IPUtil.getRemotIP(request) + ":" + request.getRemotePort());
|
loginInfoDto.setSystemId(acceptData.getDetailSystem().getSystem().getId());
|
|
int loginTypeInt = Integer.parseInt(loginType);
|
loginInfoDto.setLoginType(loginTypeInt);
|
|
switch (loginTypeInt) {
|
case LoginUser.LOGIN_TYPE_EMAIL:
|
loginInfoDto.setEmail(openid);
|
break;
|
case LoginUser.LOGIN_TYPE_QQ:
|
QQUserInfo qqUserInfo = new QQUserInfo();
|
qqUserInfo.setName(name);
|
qqUserInfo.setOpenId(openid);
|
qqUserInfo.setPortrait(portrait);
|
qqUserInfo.setSex(sex);
|
loginInfoDto.setQqUserInfo(qqUserInfo);
|
break;
|
}
|
|
|
LoginUser lu = null;
|
try {
|
lu = userService.login(loginInfoDto);
|
if (lu == null) {
|
out.print(JsonUtil.loadFalseJson("登录失败"));
|
return;
|
}
|
} catch (LoginUserException e) {
|
//用户不存在
|
if (e.getCode() == LoginUserException.CODE_NO_USER && loginTypeInt != LoginUser.LOGIN_TYPE_EMAIL) {
|
//注册
|
try {
|
lu = userService.register(loginInfoDto);
|
} catch (RegisterUserException e1) {
|
out.print(JsonUtil.loadFalseJson("登录失败"));
|
return;
|
}
|
|
} else {
|
out.print(JsonUtil.loadFalseJson(e.getMessage()));
|
return;
|
}
|
}
|
if (lu != null) {
|
JSONObject object = new JSONObject();
|
object.put("LoginUid", lu.getId());
|
object.put("Portrait", lu.getPortrait());
|
object.put("NickName", lu.getName());
|
out.print(JsonUtil.loadTrueJson(object.toString()));
|
} else {
|
out.print(JsonUtil.loadFalseJson("登录失败"));
|
}
|
}
|
|
|
public void wxLogin(AcceptData acceptData, String code, HttpServletRequest request, PrintWriter out) {
|
|
if (StringUtil.isNullOrEmpty(code)) {
|
out.print(JsonUtil.loadFalseJson("code为空"));
|
return;
|
}
|
//通过code换取个人信息
|
WeiXinUser weiXinUser = MyWXLoginUtil.getUserInfo(code, MyWXLoginUtil.WEIXIN_APPID, MyWXLoginUtil.WEIXIN_SECRET);
|
try {
|
logger.error("微信登录:" + new GsonBuilder().create().toJson(weiXinUser));
|
} catch (Exception e) {
|
|
}
|
if (weiXinUser == null) {
|
out.print(JsonUtil.loadFalseJson("获取授权信息失败,请稍后再试"));
|
return;
|
}
|
|
LoginInfoDto loginInfoDto = new LoginInfoDto();
|
loginInfoDto.setSystemId(acceptData.getDetailSystem().getSystem().getId());
|
loginInfoDto.setLoginType(LoginUser.LOGIN_TYPE_WX);
|
loginInfoDto.setWeiXinUser(weiXinUser);
|
loginInfoDto.setIpInfo(IPUtil.getRemotIP(request) + ":" + request.getRemotePort());
|
|
|
try {
|
LoginUser lu = userService.login(loginInfoDto);
|
if (lu != null) {
|
JSONObject object = new JSONObject();
|
object.put("user", new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(lu));
|
out.print(JsonUtil.loadTrueJson(object.toString()));
|
} else {
|
out.print(JsonUtil.loadFalseJson("登录失败"));
|
}
|
} catch (LoginUserException e) {
|
//用户不存在
|
if (e.getCode() == LoginUserException.CODE_NO_USER) {
|
//注册
|
try {
|
LoginUser lu = userService.register(loginInfoDto);
|
if (lu != null) {
|
JSONObject object = new JSONObject();
|
object.put("user", new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(lu));
|
out.print(JsonUtil.loadTrueJson(object.toString()));
|
} else {
|
out.print(JsonUtil.loadFalseJson("登录失败"));
|
}
|
} catch (RegisterUserException e1) {
|
out.print(JsonUtil.loadFalseJson("登录失败"));
|
}
|
} else {
|
out.print(JsonUtil.loadFalseJson(e.getMessage()));
|
}
|
}
|
}
|
|
/**
|
* 允许一键登录
|
*
|
* @param acceptData
|
* @param request
|
* @param out
|
*/
|
public void allowOneKeyLogin(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
Long loginUid = StringUtil.isNullOrEmpty(request.getParameter("loginUid")) ? null : Long.parseLong(request.getParameter("loginUid"));
|
if (loginUid == null || loginUid == 0L) {
|
out.print(JsonUtil.loadFalseJson("尚未登录"));
|
return;
|
}
|
String device = request.getParameter("Device");
|
long time = System.currentTimeMillis();
|
|
String key = "onkeylogin-" + StringUtil.Md5(device + "#" + TimeUtil.getGernalTime(time, "yyyyMMdd"));
|
|
//一个设备一天之内只能允许5次
|
String value = redisManager.getCommonString(key);
|
if (!StringUtil.isNullOrEmpty(value) && Integer.parseInt(value) > 10) {
|
out.print(JsonUtil.loadFalseJson("一键登录次数超限,请明天再试"));
|
return;
|
}
|
out.print(JsonUtil.loadTrueJson(""));
|
}
|
|
|
|
|
|
/**
|
* 电话号码绑定
|
*
|
* @param acceptData
|
* @param request
|
* @param out
|
*/
|
public void bindPhone(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
Long loginUid = StringUtil.isNullOrEmpty(request.getParameter("loginUid")) ? null : Long.parseLong(request.getParameter("loginUid"));
|
String phone = request.getParameter("phone");
|
String code = request.getParameter("code");
|
String accessToken = request.getParameter("accessToken");
|
String systemId = request.getParameter("system");
|
String device = request.getParameter("Device");
|
|
long time = System.currentTimeMillis();
|
|
|
if (loginUid == null || loginUid == 0L) {
|
out.print(JsonUtil.loadFalseJson("尚未登录"));
|
return;
|
}
|
|
String mobile = null;
|
if (!StringUtil.isNullOrEmpty(accessToken)) {
|
//一键登录
|
mobile = AliyunOneKeyLoginUtil.getMobile(accessToken, "");
|
|
//增加一键登录的次数
|
String key = "onkeylogin-" + StringUtil.Md5(device + "#" + TimeUtil.getGernalTime(time, "yyyyMMdd"));
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(time);
|
calendar.add(Calendar.DAY_OF_WEEK, 1);
|
int s = (int) ((TimeUtil.convertGernalTime(TimeUtil.getGernalTime(calendar.getTimeInMillis(), "yyyyMMdd"), "yyyyMMdd") - time) / 1000);
|
redisManager.increase(key, s);
|
} else {
|
//通过验证码登录
|
//判断验证码是否正确
|
|
if (StringUtil.isNullOrEmpty(phone)) {
|
out.print(JsonUtil.loadFalseJson("电话号码不能为空"));
|
return;
|
}
|
|
if (StringUtil.isNullOrEmpty(code)) {
|
out.print(JsonUtil.loadFalseJson("验证码不能为空"));
|
return;
|
}
|
|
if (!smsService.verifyBindVCode(phone, code)) {
|
out.print(JsonUtil.loadFalseJson("验证码错误"));
|
return;
|
}
|
mobile = phone;
|
}
|
|
try {
|
loginUserService.bindPhone(loginUid + "", mobile);
|
out.print(JsonUtil.loadTrueJson(""));
|
} catch (LoginUserException e) {
|
out.print(JsonUtil.loadFalseJson(e.getMessage()));
|
}
|
|
}
|
|
/**
|
* 绑定QQ
|
*
|
* @param acceptData
|
* @param request
|
* @param out
|
*/
|
public void bindQQ(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
Long loginUid = StringUtil.isNullOrEmpty(request.getParameter("loginUid")) ? null : Long.parseLong(request.getParameter("loginUid"));
|
String name = request.getParameter("Name");
|
String portrait = request.getParameter("Portrait");
|
String openId = request.getParameter("OpenId");
|
String systemId = request.getParameter("system");
|
if (loginUid == null || loginUid == 0L) {
|
out.print(JsonUtil.loadFalseJson("尚未登录"));
|
return;
|
}
|
if (StringUtil.isNullOrEmpty(openId)) {
|
out.print(JsonUtil.loadFalseJson("授权信息为空"));
|
return;
|
}
|
|
try {
|
loginUserService.bindQQ(loginUid + "", openId, name, portrait);
|
out.print(JsonUtil.loadTrueJson(""));
|
} catch (LoginUserException e) {
|
out.print(JsonUtil.loadFalseJson(e.getMessage()));
|
}
|
|
}
|
|
|
/**
|
* 绑定微信
|
*
|
* @param acceptData
|
* @param request
|
* @param out
|
*/
|
public void bindWX(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
Long loginUid = StringUtil.isNullOrEmpty(request.getParameter("loginUid")) ? null : Long.parseLong(request.getParameter("loginUid"));
|
String code = request.getParameter("code");
|
if (loginUid == null || loginUid == 0L) {
|
out.print(JsonUtil.loadFalseJson("尚未登录"));
|
return;
|
}
|
if (StringUtil.isNullOrEmpty(code)) {
|
out.print(JsonUtil.loadFalseJson("code为空"));
|
return;
|
}
|
|
//通过code换取个人信息
|
WeiXinUser weiXinUser = MyWXLoginUtil.getUserInfo(code, MyWXLoginUtil.WEIXIN_APPID, MyWXLoginUtil.WEIXIN_SECRET);
|
|
if (weiXinUser == null) {
|
out.print(JsonUtil.loadFalseJson("获取授权信息失败"));
|
return;
|
}
|
|
try {
|
loginUserService.bindWX(loginUid + "", weiXinUser);
|
out.print(JsonUtil.loadTrueJson(""));
|
} catch (LoginUserException e) {
|
out.print(JsonUtil.loadFalseJson(e.getMessage()));
|
}
|
|
}
|
|
|
/**
|
* 发送电话号码绑定验证码
|
*
|
* @param acceptData
|
* @param request
|
* @param out
|
*/
|
public void sendBindVerifyCode(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
String loginUid = request.getParameter("loginUid");
|
String phone = request.getParameter("phone");
|
|
|
if (StringUtil.isNullOrEmpty(phone)) {
|
out.print(JsonUtil.loadFalseJson("电话号码不能为空"));
|
return;
|
}
|
|
if (StringUtil.isNullOrEmpty(loginUid)) {
|
out.print(JsonUtil.loadFalseJson("用户尚未登录"));
|
return;
|
}
|
|
LoginUser loginUser = userService.getLoginUser(loginUid + "");
|
if (loginUser == null) {
|
out.print(JsonUtil.loadFalseJson("用户不存在"));
|
return;
|
}
|
|
try {
|
smsService.sendBindVCode(loginUid, phone, 6);
|
} catch (SMSException e) {
|
e.printStackTrace();
|
out.print(JsonUtil.loadFalseJson(e.getMessage()));
|
logger.error("发送验证码出错:phone-{} msg-{}",phone,e.getMessage(),e);
|
return;
|
} catch (Exception e) {
|
out.print(JsonUtil.loadFalseJson("短信发送出错,请稍后再试"));
|
logger.error("发送验证码出错:phone-{} msg-{}",phone,e.getMessage(),e);
|
return;
|
}
|
out.print(JsonUtil.loadTrueJson(""));
|
}
|
|
|
|
public void getVideoCommentList(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
|
String videoId = request.getParameter("VideoId");
|
String thirdType = request.getParameter("ThirdType");
|
String page = request.getParameter("Page");
|
|
if (StringUtil.isNullOrEmpty(videoId)) {
|
out.print(JsonUtil.loadFalseJson("请上传videoId"));
|
return;
|
}
|
|
if (StringUtil.isNullOrEmpty(page)) {
|
out.print(JsonUtil.loadFalseJson("请上传page"));
|
return;
|
}
|
|
if (StringUtil.isNullOrEmpty(thirdType)) {
|
out.print(JsonUtil.loadFalseJson("请上传thirdType"));
|
return;
|
}
|
int pageIndex = Integer.parseInt(page);
|
|
java.util.List<Comment2> list1 = commentService.getComment2List(videoId, thirdType, pageIndex);
|
List<Comment2> list = new ArrayList<Comment2>();
|
if (list1 != null)
|
for (Comment2 cm : list1)
|
list.add(cm);
|
long count = commentService.getComment2ListCount(videoId, thirdType);
|
|
JSONObject object = new JSONObject();
|
JSONArray array = new JSONArray();
|
for (int j = 0; j < list.size(); j++) {
|
JSONObject obj = JSONObject.fromObject(StringUtil.outPutResultJson(list.get(j)));
|
JSONArray ar = new JSONArray();
|
if (list.get(j).getReplyList() != null)
|
for (CommentReply cr : list.get(j).getReplyList())
|
ar.add(StringUtil.outPutResultJson(cr));
|
obj.put("ReplyList", ar);
|
array.add(obj);
|
}
|
object.put("data", array);
|
object.put("count", count);
|
out.print(JsonUtil.loadTrueJson(object.toString()));
|
}
|
|
@RequireUid
|
public void getMyCommentReply(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
String page = request.getParameter("Page");
|
int pageIndex = StringUtil.getPage(page);
|
if (pageIndex == 0) {
|
out.print(JsonUtil.loadFalseJson("请上传Page"));
|
return;
|
}
|
|
List<CommentReply> list = commentService.getCommentReplyList(acceptData.getUid(), pageIndex);
|
long count = commentService.getCommentReplyListCount(acceptData.getUid());
|
|
JSONObject object = new JSONObject();
|
|
JSONArray array = new JSONArray();
|
for (int i = 0; i < list.size(); i++)
|
array.add(StringUtil.outPutResultJson(list.get(i)));
|
commentService.setCommentReplyRead(acceptData.getUid());
|
object.put("data", array);
|
object.put("count", count);
|
out.print(JsonUtil.loadTrueJson(object.toString()));
|
}
|
|
@RequireUid
|
public void replayComment(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
|
String commentReplyId = request.getParameter("CommentReplyId");
|
String commentId = request.getParameter("CommentId");
|
String content = request.getParameter("Content");
|
|
if (StringUtil.isNullOrEmpty(commentId)) {
|
out.print(JsonUtil.loadFalseJson("请上传commentId"));
|
return;
|
}
|
|
if (StringUtil.match("[\\d]{6,}", content)) {
|
out.print(JsonUtil.loadFalseJson("信息格式不正确"));
|
return;
|
}
|
|
Serializable repid = commentService.replyComment(commentId, commentReplyId, acceptData.getUid(), content);
|
if (repid != null) {
|
pushService.pushCommentReplay(commentId, commentReplyId, repid + "");
|
}
|
out.print(JsonUtil.loadTrueJson(""));
|
}
|
|
@RequireUid
|
public void comment(AcceptData acceptData, HttpServletRequest request, PrintWriter out) {
|
String videoId = request.getParameter("VideoId");
|
String thirdType = request.getParameter("ThirdType");
|
String content = request.getParameter("Content");
|
|
if (StringUtil.isNullOrEmpty(videoId)) {
|
out.print(JsonUtil.loadFalseJson("请上传VideoId"));
|
return;
|
}
|
|
if (StringUtil.isNullOrEmpty(acceptData.getPlatform())) {
|
out.print(JsonUtil.loadFalseJson("请上传Platform"));
|
return;
|
}
|
|
if (StringUtil.match("[\\d]{6,}", content)) {
|
out.print(JsonUtil.loadFalseJson("信息格式不正确"));
|
return;
|
}
|
|
DetailSystem detailSystem = systemService.getDetailSystemByPackage(acceptData.getPackageName());
|
|
Comment2 comment = new Comment2();
|
content = maskKeyService.maskAction(content);
|
comment.setContent(content);
|
comment.setCreatetime(System.currentTimeMillis() + "");
|
comment.setShow(true);
|
comment.setThirdType(thirdType);
|
comment.setUser(new LoginUser(acceptData.getUid()));
|
comment.setVideo(new VideoInfo(videoId));
|
comment.setDetailsystem(detailSystem.getId());
|
commentService.addComment2(comment);
|
|
out.print(JsonUtil.loadTrueJson(""));
|
|
}
|
}
|