|
@@ -1,29 +1,39 @@
|
|
package com.management.platform.service.impl;
|
|
package com.management.platform.service.impl;
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.management.platform.entity.User;
|
|
|
|
+import com.management.platform.entity.WechatAccount;
|
|
|
|
+import com.management.platform.service.CompanyService;
|
|
|
|
+import com.management.platform.service.UserService;
|
|
|
|
+import com.management.platform.service.WechatAccountService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
@Slf4j
|
|
@Slf4j
|
|
public class WechatApiService {
|
|
public class WechatApiService {
|
|
|
|
|
|
- @Value("${weixin.APP_ID}")
|
|
|
|
- private String appId;
|
|
|
|
-
|
|
|
|
- @Value("${weixin.APP_SECRET}")
|
|
|
|
- private String appSecret;
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private UserService userService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private CompanyService companyService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private WechatAccountService wechatAccountService;
|
|
|
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
- public Map<String, Object> getUserInfo(String openId) {
|
|
|
|
- String accessToken = getAccessToken();
|
|
|
|
|
|
+ public Map<String, Object> getUserInfo(String openId,String userId) {
|
|
|
|
+ String accessToken = getAccessToken(userId);
|
|
if (accessToken == null) {
|
|
if (accessToken == null) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
@@ -44,21 +54,18 @@ public class WechatApiService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private String getAccessToken() {
|
|
|
|
- String url = String.format(
|
|
|
|
- "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
|
|
|
|
- appId, appSecret);
|
|
|
|
-
|
|
|
|
- try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
|
- HttpGet request = new HttpGet(url);
|
|
|
|
- String response = EntityUtils.toString(client.execute(request).getEntity());
|
|
|
|
- Map<String, Object> result = objectMapper.readValue(response, Map.class);
|
|
|
|
- log.info("获取企业的token==>"+result);
|
|
|
|
- return (String) result.get("access_token");
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- log.info("获取企业对应的token失败==>"+e.getMessage());
|
|
|
|
|
|
+ private String getAccessToken(String userId) {
|
|
|
|
+ User user = userService.getById(userId);
|
|
|
|
+ if (user==null){
|
|
|
|
+ log.info("获取企业的微信Token时,未获取该用户");
|
|
return null;
|
|
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());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|