|
@@ -1,17 +1,26 @@
|
|
|
package com.management.platform.controller;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.management.platform.entity.Custom;
|
|
|
+import com.management.platform.entity.User;
|
|
|
import com.management.platform.entity.WechatQrcodeScan;
|
|
|
import com.management.platform.entity.WechatUserFollow;
|
|
|
import com.management.platform.mapper.WechatQrcodeScanMapper;
|
|
|
import com.management.platform.mapper.WechatUserFollowMapper;
|
|
|
+import com.management.platform.service.CustomService;
|
|
|
+import com.management.platform.service.UserService;
|
|
|
import com.management.platform.service.impl.WechatApiService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.dom4j.Document;
|
|
|
import org.dom4j.DocumentHelper;
|
|
|
import org.dom4j.Element;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
@@ -36,6 +45,12 @@ public class WechatCallbackController {
|
|
|
this.wechatApiService = wechatApiService;
|
|
|
}
|
|
|
|
|
|
+ @Resource
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private CustomService customService;
|
|
|
+
|
|
|
// 微信配置验证接口(GET请求)
|
|
|
@GetMapping("/callback")
|
|
|
public String validate(@RequestParam("signature") String signature,
|
|
@@ -76,7 +91,9 @@ public class WechatCallbackController {
|
|
|
String event = root.elementText("Event");
|
|
|
String openId = root.elementText("FromUserName");
|
|
|
String eventKey = root.elementText("EventKey");
|
|
|
+ log.info("场景值ID==>"+(StringUtils.isEmpty(eventKey)?"空":eventKey));
|
|
|
String ticket = root.elementText("Ticket");
|
|
|
+ log.info("ticket==>"+(StringUtils.isEmpty(ticket)?"空":ticket));
|
|
|
String createTime = root.elementText("CreateTime");
|
|
|
|
|
|
// 处理关注/扫码事件
|
|
@@ -143,6 +160,23 @@ public class WechatCallbackController {
|
|
|
followRecord.setFollowTime(LocalDateTime.now());
|
|
|
followRecord.setSalesmanId(salesmanId);
|
|
|
followMapper.insert(followRecord);
|
|
|
+
|
|
|
+ Custom serviceOne = customService.getOne(new QueryWrapper<Custom>().eq("plate4", openId));
|
|
|
+ if(serviceOne==null) {
|
|
|
+ Custom custom = new Custom();
|
|
|
+ String cityByIp = getCityByIp(ipAddress);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(cityByIp);
|
|
|
+ custom.setPlate2(jsonObject.getString("city"));
|
|
|
+ custom.setPlate3(jsonObject.getString("regionName"));
|
|
|
+ custom.setPlate4(openId);//用户的openId
|
|
|
+ custom.setIsDelete(0);
|
|
|
+ custom.setInchargerId(salesmanId);
|
|
|
+ if (StringUtils.isNotEmpty(salesmanId)) {
|
|
|
+ User user = userService.getById(salesmanId);
|
|
|
+ custom.setCompanyId(user != null ? user.getCompanyId() : null);
|
|
|
+ }
|
|
|
+ customService.save(custom);
|
|
|
+ }
|
|
|
log.info("用户新关注成功");
|
|
|
} else {
|
|
|
followRecord.setIsFollow(true);
|
|
@@ -160,6 +194,8 @@ public class WechatCallbackController {
|
|
|
followRecord.setIsFollow(false);
|
|
|
followRecord.setUnfollowTime(LocalDateTime.now());
|
|
|
followMapper.update(followRecord);
|
|
|
+
|
|
|
+ customService.remove(new QueryWrapper<Custom>().eq("plate4", openId));
|
|
|
log.info("用户取消关注成功");
|
|
|
}
|
|
|
}
|
|
@@ -181,17 +217,15 @@ public class WechatCallbackController {
|
|
|
}
|
|
|
|
|
|
private String getClientIp(HttpServletRequest request) {
|
|
|
- String ip = request.getHeader("X-Forwarded-For");
|
|
|
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
- ip = request.getHeader("Proxy-Client-IP");
|
|
|
+ String ipAddress = request.getHeader("X-Forwarded-For");
|
|
|
+ if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
+ ipAddress = request.getRemoteAddr();
|
|
|
}
|
|
|
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
- ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ // 如果有多个 IP,取第一个
|
|
|
+ if (ipAddress != null && ipAddress.contains(",")) {
|
|
|
+ ipAddress = ipAddress.split(",")[0];
|
|
|
}
|
|
|
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
- ip = request.getRemoteAddr();
|
|
|
- }
|
|
|
- return ip;
|
|
|
+ return ipAddress;
|
|
|
}
|
|
|
|
|
|
// Unix时间戳(秒) -> LocalDateTime
|
|
@@ -201,4 +235,14 @@ public class WechatCallbackController {
|
|
|
ZoneId.systemDefault()
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
+ //根据IP获取用户的省份,城市信息
|
|
|
+ private String getCityByIp(String ip) {
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ String url = "http://ip-api.com/json/" + ip;
|
|
|
+ String response = restTemplate.getForObject(url, String.class);
|
|
|
+ // 解析 JSON 响应并提取城市信息
|
|
|
+ // 这里可以使用 Jackson 或 Gson 进行解析
|
|
|
+ return response; // 返回城市信息
|
|
|
+ }
|
|
|
}
|