|
|
@@ -3,6 +3,9 @@ package vip.xiaonuo.weixin.gongzhong.controller;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
@@ -13,10 +16,14 @@ import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
|
|
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import vip.xiaonuo.coldchain.modular.alarmuser.entity.AlarmUser;
|
|
|
+import vip.xiaonuo.coldchain.modular.alarmuser.enums.AlarmUserEnum;
|
|
|
import vip.xiaonuo.coldchain.modular.alarmuser.service.AlarmUserService;
|
|
|
import vip.xiaonuo.coldchain.modular.push.entity.WeChatUser;
|
|
|
import vip.xiaonuo.coldchain.modular.push.utils.PushUtil;
|
|
|
+import vip.xiaonuo.common.pojo.CommonResult;
|
|
|
import vip.xiaonuo.dev.modular.config.service.DevConfigService;
|
|
|
+import vip.xiaonuo.sys.modular.user.entity.SysUser;
|
|
|
+import vip.xiaonuo.sys.modular.user.service.SysUserService;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
@@ -33,6 +40,8 @@ public class WxMpController {
|
|
|
private final AlarmUserService alarmUserService;
|
|
|
private final DevConfigService devConfigService;
|
|
|
|
|
|
+ private final SysUserService sysUserService;
|
|
|
+
|
|
|
@GetMapping("/auth")
|
|
|
public String check(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce, @RequestParam String echostr) {
|
|
|
log.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
|
|
|
@@ -85,9 +94,9 @@ public class WxMpController {
|
|
|
@SneakyThrows
|
|
|
@PostMapping("/auth")
|
|
|
public String wxEventListener(@RequestBody String param,
|
|
|
- @RequestParam(required = false) String signature,
|
|
|
- @RequestParam(required = false) String timestamp,
|
|
|
- @RequestParam(required = false) String nonce) {
|
|
|
+ @RequestParam(required = false) String signature,
|
|
|
+ @RequestParam(required = false) String timestamp,
|
|
|
+ @RequestParam(required = false) String nonce) {
|
|
|
log.info("\n接收微信事件:[signature=[{}], timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
|
|
|
signature, timestamp, nonce, param);
|
|
|
|
|
|
@@ -97,4 +106,69 @@ public class WxMpController {
|
|
|
// 将响应消息转换为xml格式返回
|
|
|
return outMessage == null ? "" : outMessage.toXml();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信公众号实名认证接口
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ @GetMapping("/auth/check")
|
|
|
+ public CommonResult<AlarmUser> checkAndRegister(@RequestParam(required = false) String code,@RequestParam String name, @RequestParam String phone, @RequestParam String createOrg) {
|
|
|
+ AlarmUser alarmUser=
|
|
|
+ alarmUserService.getOne(new LambdaQueryWrapper<AlarmUser>()
|
|
|
+ .eq(AlarmUser::getPhone, phone), false);
|
|
|
+ //用户通过微信公众号实名认证
|
|
|
+ if(StrUtil.isNotBlank(code)){
|
|
|
+ log.info("\n接收到来自微信服务器的授权消息:code={}", code);
|
|
|
+ try {
|
|
|
+ WeChatUser weChatUser = PushUtil.getUserInfo(code);
|
|
|
+ log.info("获取微信用户信息:{}", JSONUtil.toJsonStr(weChatUser));
|
|
|
+ List<AlarmUser> userList = alarmUserService.getByOpenId(weChatUser.getOpenid());
|
|
|
+ if (CollUtil.isNotEmpty(userList)) {
|
|
|
+ //检查用户的组织机构
|
|
|
+ alarmUser = userList.get(0);
|
|
|
+ alarmUser.setNickName(weChatUser.getNickname());
|
|
|
+ } else {
|
|
|
+ //没有用户信息则创建用户
|
|
|
+ alarmUser = alarmUserService.subscribe(weChatUser,createOrg,name,phone);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("获取微信用户失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("\n接收实名认证消息:name={},phone={},createOrg={}", name, phone,createOrg);
|
|
|
+ log.info("获取旧用户信息:{}", JSONUtil.toJsonStr(alarmUser));
|
|
|
+ if (alarmUser == null) {
|
|
|
+ //用户没有通过微信公众号认证 创建用户
|
|
|
+ alarmUser = new AlarmUser();
|
|
|
+ alarmUser.setNickName(name);
|
|
|
+ alarmUser.setCreateOrg(createOrg);
|
|
|
+ alarmUser.setPhone(phone);
|
|
|
+ alarmUser.setSubscribed(AlarmUserEnum.UNSUBSCRIBE.getValue());
|
|
|
+ SysUser sysUser = alarmUserService.createSysUser(alarmUser, createOrg, name);
|
|
|
+ alarmUser.setUserId(sysUser.getId());
|
|
|
+ alarmUser.setId(IdWorker.getIdStr());
|
|
|
+ alarmUserService.save(alarmUser);
|
|
|
+ } else {
|
|
|
+ //修改用户 只能修改真实姓名
|
|
|
+ if(StrUtil.isNotBlank(alarmUser.getOpenId())){
|
|
|
+ alarmUserService.update(new LambdaUpdateWrapper<AlarmUser>()
|
|
|
+ .set(AlarmUser::getNickName, alarmUser.getNickName())
|
|
|
+ .set(AlarmUser::getPhone, phone)
|
|
|
+ .eq(AlarmUser::getId, alarmUser.getId()));
|
|
|
+ sysUserService.update(new LambdaUpdateWrapper<SysUser>()
|
|
|
+ .set(SysUser::getName, name)
|
|
|
+ .set(SysUser::getPhone, phone)
|
|
|
+ .eq(SysUser::getId, alarmUser.getUserId()));
|
|
|
+ }else{
|
|
|
+ sysUserService.update(new LambdaUpdateWrapper<SysUser>()
|
|
|
+ .set(SysUser::getName, name)
|
|
|
+ .eq(SysUser::getId, alarmUser.getUserId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return CommonResult.result(true, alarmUser);
|
|
|
+
|
|
|
+ }
|
|
|
}
|