You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.8 KiB
Java

package com.ibeetl.jlw.service;
import cn.hutool.core.collection.CollectionUtil;
import cn.jlw.token.TokenService;
import cn.jlw.webSocket.WebSocketServer;
import com.ibeetl.admin.core.conf.CacheConfig;
import com.ibeetl.admin.core.conf.WxMpConfig;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.CoreUserService;
import com.ibeetl.jlw.entity.CoreUserWx;
import com.ibeetl.jlw.entity.Student;
import com.ibeetl.jlw.web.query.CoreUserWxQuery;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.builder.kefu.TextBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* @author Binary Wang
*/
@Component
public class MsgHandler extends AbstractHandler {
@Autowired
private CoreUserService coreUserService;
@Autowired
private CoreUserWxService coreUserWxService;
@Autowired
private StudentService studentService;
@Autowired
private TokenService tokenService;
@Autowired
private WxMpConfig wxConfig;
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
Map<String, Object> context, WxMpService wxMpService,
WxSessionManager sessionManager) {
WeixinService weixinService = (WeixinService) wxMpService;
String fromUser = wxMessage.getFromUser();
String userId = wxMessage.getEventKey();
if (StringUtils.isEmpty(userId)) {
return WxMpXmlOutMessage.TEXT().content("欢迎关注天泽教育").toUser(fromUser)
.fromUser(wxMessage.getToUser())
.build();
}
userId = userId.replace("qrscene_", "");
//登录
if (userId.contains("login")) {
//登录所包含userId为雪花算法ID
userId = userId.replace("login", "");
CoreUserWxQuery coreUserWxQuery = new CoreUserWxQuery();
coreUserWxQuery.setOpenid(fromUser);
List<CoreUserWx> info = coreUserWxService.getValuesByQueryNotWithPermission(coreUserWxQuery);
if (CollectionUtil.isNotEmpty(info)) {
Student student = studentService.getByUserId(info.get(0).getUserId());
String token = tokenService.getToken(student);
WebSocketServer.sendToUser("token="+token, userId);
return WxMpXmlOutMessage.TEXT().content("登录成功").toUser(fromUser)
.fromUser(wxMessage.getToUser())
.build();
}
return WxMpXmlOutMessage.TEXT().content("登录失败,未绑定账号").toUser(fromUser)
.fromUser(wxMessage.getToUser())
.build();
}
CoreUserWxQuery coreUserWxQuery1 = new CoreUserWxQuery();
coreUserWxQuery1.setOpenid(fromUser);
CoreUserWx info1 = coreUserWxService.getInfo(coreUserWxQuery1);
CoreUserWxQuery coreUserWxQuery2 = new CoreUserWxQuery();
coreUserWxQuery2.setUserId(Long.parseLong(userId));
CoreUserWx info2 = coreUserWxService.getInfo(coreUserWxQuery2);
if (info2 != null || info1 != null) {
return WxMpXmlOutMessage.TEXT().content("绑定失败,已经绑定其他账号").toUser(fromUser)
.fromUser(wxMessage.getToUser())
.build();
}
coreUserWxQuery2.setOpenid(fromUser);
coreUserWxService.add(coreUserWxQuery2);
// String redirectURI = wxConfig.getCodeBackUrl();
// String s = weixinService.oauth2buildAuthorizationUrl(redirectURI, "snsapi_userinfo", "STATE");
return WxMpXmlOutMessage.TEXT().content("绑定成功").toUser(fromUser)
.fromUser(wxMessage.getToUser())
.build();
}
}