|
@@ -1,24 +1,33 @@
|
|
package com.management.platform.controller;
|
|
package com.management.platform.controller;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.management.platform.entity.Custom;
|
|
import com.management.platform.entity.Custom;
|
|
import com.management.platform.entity.MiniBindUser;
|
|
import com.management.platform.entity.MiniBindUser;
|
|
import com.management.platform.entity.User;
|
|
import com.management.platform.entity.User;
|
|
|
|
+import com.management.platform.entity.WechatAccount;
|
|
import com.management.platform.service.CustomService;
|
|
import com.management.platform.service.CustomService;
|
|
import com.management.platform.service.MiniBindUserService;
|
|
import com.management.platform.service.MiniBindUserService;
|
|
import com.management.platform.service.UserService;
|
|
import com.management.platform.service.UserService;
|
|
|
|
+import com.management.platform.service.WechatAccountService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
import org.dom4j.Document;
|
|
import org.dom4j.Document;
|
|
import org.dom4j.DocumentHelper;
|
|
import org.dom4j.DocumentHelper;
|
|
import org.dom4j.Element;
|
|
import org.dom4j.Element;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
@RequestMapping("/wechat")
|
|
@RequestMapping("/wechat")
|
|
@@ -35,6 +44,14 @@ public class WechatCallbackController {
|
|
@Resource
|
|
@Resource
|
|
private CustomService customService;
|
|
private CustomService customService;
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private RestTemplate restTemplate;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private WechatAccountService wechatAccountService;
|
|
|
|
+
|
|
|
|
+ private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
+
|
|
// 微信配置验证接口(GET请求)
|
|
// 微信配置验证接口(GET请求)
|
|
@GetMapping("/callback")
|
|
@GetMapping("/callback")
|
|
public String validate(@RequestParam("signature") String signature,
|
|
public String validate(@RequestParam("signature") String signature,
|
|
@@ -107,27 +124,35 @@ public class WechatCallbackController {
|
|
private void handleSubscribe(String openId) {
|
|
private void handleSubscribe(String openId) {
|
|
// 实现用户关注的业务逻辑
|
|
// 实现用户关注的业务逻辑
|
|
log.info("处理用户关注逻辑: {}", openId);
|
|
log.info("处理用户关注逻辑: {}", openId);
|
|
- List<MiniBindUser> miniBindUserList = miniBindUserService.list(new QueryWrapper<MiniBindUser>().eq("openid", openId));
|
|
|
|
- if (!miniBindUserList.isEmpty()) {
|
|
|
|
- if (customService.count(new QueryWrapper<Custom>().eq("custom_name", openId))==0){
|
|
|
|
- MiniBindUser miniBindUser = miniBindUserList.get(0);
|
|
|
|
- Custom custom = new Custom();
|
|
|
|
- custom.setCustomName(openId);//用户的openId
|
|
|
|
- custom.setIsDelete(0);
|
|
|
|
- custom.setCreateTime(new Date());
|
|
|
|
- if (StringUtils.isNotEmpty(miniBindUser.getUserId())) {
|
|
|
|
- custom.setInchargerId(miniBindUser.getUserId());
|
|
|
|
- User user = userService.getById(miniBindUser.getUserId());
|
|
|
|
- custom.setCompanyId(user != null ? user.getCompanyId() : null);
|
|
|
|
- customService.save(custom);
|
|
|
|
- log.info("新增客户成功");
|
|
|
|
|
|
+ Map<String, Object> userInfo = getUserInfo(openId, null);
|
|
|
|
+ log.info("返回的userInfo信息==>"+userInfo.toString());
|
|
|
|
+ if (userInfo.get("unionid") != null) {
|
|
|
|
+ String unionid = (String) userInfo.get("unionid");
|
|
|
|
+ List<MiniBindUser> miniBindUserList = miniBindUserService.list(new QueryWrapper<MiniBindUser>().eq("unionid",unionid ));
|
|
|
|
+ if (!miniBindUserList.isEmpty()) {
|
|
|
|
+ if (customService.count(new QueryWrapper<Custom>().eq("custom_name", unionid))==0){
|
|
|
|
+ MiniBindUser miniBindUser = miniBindUserList.get(0);
|
|
|
|
+ Custom custom = new Custom();
|
|
|
|
+ custom.setCustomName(unionid);//用户的openId
|
|
|
|
+ custom.setIsDelete(0);
|
|
|
|
+ custom.setCreateTime(new Date());
|
|
|
|
+ if (StringUtils.isNotEmpty(miniBindUser.getUserId())) {
|
|
|
|
+ custom.setInchargerId(miniBindUser.getUserId());
|
|
|
|
+ User user = userService.getById(miniBindUser.getUserId());
|
|
|
|
+ custom.setCompanyId(user != null ? user.getCompanyId() : null);
|
|
|
|
+ customService.save(custom);
|
|
|
|
+ log.info("新增客户成功");
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ log.info(unionid+":系统中已存在对应的客户");
|
|
}
|
|
}
|
|
}else {
|
|
}else {
|
|
- log.info(openId+":系统中已存在对应的客户");
|
|
|
|
|
|
+ log.info(unionid+":对应的客户,暂未绑定相关的销售人员");
|
|
}
|
|
}
|
|
}else {
|
|
}else {
|
|
- log.info(openId+":对应的客户,暂未绑定相关的销售人员");
|
|
|
|
|
|
+ log.info("unionid为空");
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
private void handleUnsubscribe(String openId) {
|
|
private void handleUnsubscribe(String openId) {
|
|
@@ -140,8 +165,8 @@ public class WechatCallbackController {
|
|
log.info("处理用户扫码逻辑: {}", openId);
|
|
log.info("处理用户扫码逻辑: {}", openId);
|
|
}
|
|
}
|
|
|
|
|
|
- // ========== 响应消息构建方法 ==========
|
|
|
|
|
|
|
|
|
|
+ // ========== 响应消息构建方法 ==========
|
|
private String successResponse(Element root) {
|
|
private String successResponse(Element root) {
|
|
return String.format(
|
|
return String.format(
|
|
"<xml>" +
|
|
"<xml>" +
|
|
@@ -195,4 +220,55 @@ public class WechatCallbackController {
|
|
System.currentTimeMillis() / 1000);
|
|
System.currentTimeMillis() / 1000);
|
|
}*/
|
|
}*/
|
|
|
|
|
|
|
|
+ public Map<String, Object> getUserInfo(String openId, String userId) {
|
|
|
|
+ String accessToken=null;
|
|
|
|
+ if (StringUtils.isNotEmpty(userId)){
|
|
|
|
+ accessToken= getAccessToken(userId);
|
|
|
|
+ }else {
|
|
|
|
+ accessToken= getAccessToken();
|
|
|
|
+ }
|
|
|
|
+ if (accessToken == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String url = String.format(
|
|
|
|
+ "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN",
|
|
|
|
+ accessToken, openId);
|
|
|
|
+
|
|
|
|
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
|
+ HttpGet request = new HttpGet(url);
|
|
|
|
+ String response = EntityUtils.toString(client.execute(request).getEntity());
|
|
|
|
+ log.info("获取用户信息===>"+response);
|
|
|
|
+ return objectMapper.readValue(response, Map.class);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ log.info("获取用户信息失败==>"+e.getMessage());
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getAccessToken(String userId) {
|
|
|
|
+ User user = userService.getById(userId);
|
|
|
|
+ if (user==null){
|
|
|
|
+ log.info("获取企业的微信Token时,未获取该用户");
|
|
|
|
+ return null;
|
|
|
|
+ }else {
|
|
|
|
+ WechatAccount wechatAccount = wechatAccountService.getOne(new QueryWrapper<WechatAccount>().eq("company_id", user.getCompanyId()));
|
|
|
|
+ if (wechatAccount==null){
|
|
|
|
+ log.info("该公司没有配置公众号相关的参数");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return wechatAccountService.getAccessToken(user.getCompanyId(), wechatAccount.getAppId());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getAccessToken() {
|
|
|
|
+ WechatAccount wechatAccount = wechatAccountService.list().get(0);
|
|
|
|
+ if (wechatAccount==null){
|
|
|
|
+ log.info("该公司没有配置公众号相关的参数");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return wechatAccountService.getAccessToken(wechatAccount.getCompanyId(), wechatAccount.getAppId());
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|