|
@@ -1,9 +1,11 @@
|
|
|
package com.management.platform.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
import com.management.platform.controller.WeiXinCorpController;
|
|
|
import com.management.platform.entity.*;
|
|
|
import com.management.platform.mapper.SysConfigMapper;
|
|
@@ -13,23 +15,28 @@ import com.management.platform.mapper.WxCorpInfoMapper;
|
|
|
import com.management.platform.service.WxCorpInfoService;
|
|
|
import com.management.platform.util.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.client.utils.HttpClientUtils;
|
|
|
+import org.json.HTTP;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.io.FileSystemResource;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
|
+import sun.net.www.http.HttpClient;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
+import java.io.*;
|
|
|
import java.lang.reflect.Array;
|
|
|
+import java.net.URI;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.*;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -55,6 +62,11 @@ public class WxCorpInfoServiceImpl extends ServiceImpl<WxCorpInfoMapper, WxCorpI
|
|
|
@Value("${suitSecret}")
|
|
|
private String suitSecret;
|
|
|
|
|
|
+ @Value("${corpId}")
|
|
|
+ private String corpId;
|
|
|
+ @Value("${providerSecret}")
|
|
|
+ private String providerSecret;
|
|
|
+
|
|
|
@Value(value = "${upload.path}")
|
|
|
private String path;
|
|
|
|
|
@@ -75,6 +87,113 @@ public class WxCorpInfoServiceImpl extends ServiceImpl<WxCorpInfoMapper, WxCorpI
|
|
|
@Resource
|
|
|
UserCorpwxTimeMapper userCorpwxTimeMapper;
|
|
|
|
|
|
+
|
|
|
+ //获取服务商provider_access_token
|
|
|
+ @Override
|
|
|
+ public String getProviderAccessToken() throws Exception {
|
|
|
+ String access_token="";
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_token";
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ JSONObject jsonObject=new JSONObject();
|
|
|
+ jsonObject.put("corpid",corpId);
|
|
|
+ jsonObject.put("provider_secret",providerSecret);
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<String>(jsonObject.toJSONString(), headers);
|
|
|
+ ResponseEntity<String> responseEntity = this.restTemplate.exchange(url,
|
|
|
+ HttpMethod.POST, requestEntity, String.class);
|
|
|
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
+ String resp = responseEntity.getBody();
|
|
|
+ JSONObject json = JSONObject.parseObject(resp);
|
|
|
+ if(json.getIntValue("errcode")>0){
|
|
|
+ throw new Exception(json.toJSONString());
|
|
|
+ }
|
|
|
+ access_token= json.getString("provider_access_token");
|
|
|
+ }
|
|
|
+ return access_token;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取上传转移文件 获得的media_ia
|
|
|
+ @Override
|
|
|
+ public String getTranslationMediaId(String fileName) throws Exception {
|
|
|
+ String media_id="";
|
|
|
+ URI url = UriComponentsBuilder.fromHttpUrl("https://qyapi.weixin.qq.com/cgi-bin/service/media/upload")
|
|
|
+ .queryParam("provider_access_token", getProviderAccessToken())
|
|
|
+ .queryParam("type", "file")
|
|
|
+ .build().toUri();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
+ //然后处理文件 通讯录id转译
|
|
|
+ FileSystemResource fileSystemResource = new FileSystemResource(path+fileName);
|
|
|
+ ContentDisposition build = ContentDisposition.builder("form-data").filename(fileSystemResource.getFilename()).build();
|
|
|
+ headers.setContentDisposition(build);
|
|
|
+ MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
|
|
|
+ params.add("media", fileSystemResource);
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(params, headers);
|
|
|
+ MultiValueMap<String, Object> body = requestEntity.getBody();
|
|
|
+ ResponseEntity<String> responseEntity = this.restTemplate.exchange(url,
|
|
|
+ HttpMethod.POST, requestEntity, String.class);
|
|
|
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
+ String resp = responseEntity.getBody();
|
|
|
+ JSONObject json = JSONObject.parseObject(resp);
|
|
|
+ if(json.getIntValue("errcode")==0){
|
|
|
+ media_id= json.getString("media_id");
|
|
|
+ }else {
|
|
|
+ throw new Exception(json.toJSONString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return media_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ //异步通讯录id转译
|
|
|
+ @Override
|
|
|
+ public String syncTranslation(String mediaId,String outPutFileName,String outputFileFormat) throws Exception {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/service/contact/id_translate?provider_access_token=ACCESS_TOKEN".replaceAll("ACCESS_TOKEN",getProviderAccessToken());
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ List<String> list=new ArrayList<>();
|
|
|
+ list.add(mediaId);
|
|
|
+ String jobid="";
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ JSONObject jsonObject=new JSONObject();
|
|
|
+ jsonObject.put("auth_corpid",corpId);
|
|
|
+ jsonObject.put("media_id_list",list);
|
|
|
+ jsonObject.put("output_file_name",outPutFileName);
|
|
|
+ jsonObject.put("output_file_format",outputFileFormat);
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<String>(jsonObject.toJSONString(), headers);
|
|
|
+ ResponseEntity<String> responseEntity = this.restTemplate.exchange(url,
|
|
|
+ HttpMethod.POST, requestEntity, String.class);
|
|
|
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
+ String resp = responseEntity.getBody();
|
|
|
+ JSONObject json = JSONObject.parseObject(resp);
|
|
|
+ if(json.getIntValue("errcode")==0){
|
|
|
+ jobid= json.getString("jobid");
|
|
|
+ }else {
|
|
|
+ throw new Exception(json.toJSONString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return jobid;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取异步任务结果
|
|
|
+ @Override
|
|
|
+ public String getSyncTranslationResult(String jobId) throws Exception {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/service/batch/getresult?provider_access_token=ACCESS_TOKEN&jobid=JOBID"
|
|
|
+ .replaceAll("ACCESS_TOKEN",getProviderAccessToken()).replaceAll("JOBID",jobId);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ String resultUrl="";
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<String>(null, headers);
|
|
|
+ ResponseEntity<String> responseEntity = this.restTemplate.exchange(url,
|
|
|
+ HttpMethod.GET, requestEntity, String.class);
|
|
|
+ String resp = responseEntity.getBody();
|
|
|
+ JSONObject json = JSONObject.parseObject(resp);
|
|
|
+ if(json.getIntValue("errcode")==0){
|
|
|
+ JSONObject result = (JSONObject) json.get("result");
|
|
|
+ JSONObject contact_id_translate = (JSONObject) result.get("contact_id_translate");
|
|
|
+ resultUrl = contact_id_translate.getString("url");
|
|
|
+ }else {
|
|
|
+ throw new Exception(json.toJSONString());
|
|
|
+ }
|
|
|
+ return resultUrl;
|
|
|
+ }
|
|
|
@Override
|
|
|
public void sendWXCorpMsg(WxCorpInfo corpInfo, String corpUserid, String msg) {
|
|
|
try {
|