|
@ -4,6 +4,9 @@ import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.doctor.dao.BaseDoctorDao;
|
|
|
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
|
|
|
import com.yihu.jw.entity.base.wx.EnterpriseJsApiTicket;
|
|
|
import com.yihu.jw.entity.base.wx.JsApiTicket;
|
|
|
import com.yihu.jw.entity.base.wx.WxWechatDO;
|
|
|
import com.yihu.jw.entity.hospital.enterprise.WxEnterpriseDO;
|
|
|
import com.yihu.jw.entity.hospital.enterprise.WxEnterpriseTokenDO;
|
|
|
import com.yihu.jw.entity.hospital.enterprise.WxEnterpriseUserDO;
|
|
@ -14,17 +17,27 @@ import com.yihu.jw.util.wechat.wxhttp.HttpUtil;
|
|
|
import com.yihu.jw.wechat.enterprise.dao.WxEnterpriseDao;
|
|
|
import com.yihu.jw.wechat.enterprise.dao.WxEnterpriseTokenDao;
|
|
|
import com.yihu.jw.wechat.enterprise.dao.WxEnterpriseUserDao;
|
|
|
import com.yihu.jw.wechat.service.WxAccessTokenService;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.checkerframework.checker.units.qual.A;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Random;
|
|
|
|
|
|
/**
|
|
|
* Created by Trick on 2020/2/8.
|
|
@ -42,6 +55,9 @@ public class EnterpriseService {
|
|
|
|
|
|
private String agentId = "1000003";
|
|
|
|
|
|
@Value("${FileTempPath.upload_temp_path}")
|
|
|
private String upload_temp_path;
|
|
|
|
|
|
@Autowired
|
|
|
private WxEnterpriseTokenDao wxEnterpriseTokenDao;
|
|
|
@Autowired
|
|
@ -52,6 +68,10 @@ public class EnterpriseService {
|
|
|
private BaseDoctorDao baseDoctorDao;
|
|
|
@Autowired
|
|
|
private WlyyHttpLogDao wlyyHttpLogDao;
|
|
|
@Autowired
|
|
|
private WxAccessTokenService wxAccessTokenService;
|
|
|
@Autowired
|
|
|
protected HttpServletRequest request;
|
|
|
|
|
|
|
|
|
public String getToken(String enterpriseId)throws Exception{
|
|
@ -90,6 +110,12 @@ public class EnterpriseService {
|
|
|
return token.getAccessToken();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 小程序token
|
|
|
* @param enterpriseId
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public String getXCXToken(String enterpriseId)throws Exception{
|
|
|
|
|
|
logger.info("getXCXToken");
|
|
@ -481,7 +507,245 @@ public class EnterpriseService {
|
|
|
return "ok";
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取企业微信的jsapi_ticket
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public String getEnterpriseJsapi_ticketByToken(String wxId) throws Exception {
|
|
|
try {
|
|
|
System.out.println("getEnterpriseJsapi_ticketByToken="+wxId);
|
|
|
Iterable<EnterpriseJsApiTicket> jsapiTickets = null;
|
|
|
WxWechatDO wxWechat = null;
|
|
|
|
|
|
//中山医院互联网医院
|
|
|
if("xm_zsyy_wx".equals(wxId) || "xm_xzzx_wx".equals(wxId) || "xm_ykyy_wx".equals(wxId)||"xm_hcyy_wx".equals(wxId)){
|
|
|
jsapiTickets = wxAccessTokenService.findEnterpriseJsapiTicket(wxId);
|
|
|
}else{
|
|
|
wxWechat = wxAccessTokenService.getWxInfo(wxId);
|
|
|
jsapiTickets = wxAccessTokenService.findEnterpriseJsapiTicket(wxWechat.getAppOriginId());
|
|
|
}
|
|
|
|
|
|
if (jsapiTickets != null) {
|
|
|
for (EnterpriseJsApiTicket jsApiTicket : jsapiTickets) {
|
|
|
if ((System.currentTimeMillis() - jsApiTicket.getAdd_timestamp()) < (jsApiTicket.getExpires_in() * 1000)) {
|
|
|
return jsApiTicket.getJsapi_ticket();
|
|
|
} else {
|
|
|
wxAccessTokenService.delEnterpriseJsapiTicket(jsApiTicket);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
String token = "";
|
|
|
|
|
|
//中山医院互联网医院
|
|
|
|
|
|
token = getToken(wxId);
|
|
|
System.out.println("getticket_token:"+token);
|
|
|
if (token != null) {
|
|
|
String token_url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?";
|
|
|
String params = "access_token=" + token ;
|
|
|
String result = HttpUtil.sendGet(token_url, params);
|
|
|
System.out.println("result:"+result);
|
|
|
org.json.JSONObject json = new org.json.JSONObject(result);
|
|
|
System.out.println("getticket:"+result);
|
|
|
if (json.has("ticket")) {
|
|
|
String ticket = json.get("ticket").toString();
|
|
|
String expires_in = json.get("expires_in").toString();
|
|
|
EnterpriseJsApiTicket newJsApiTicket = new EnterpriseJsApiTicket();
|
|
|
newJsApiTicket.setJsapi_ticket(ticket);
|
|
|
newJsApiTicket.setExpires_in(Long.parseLong(expires_in));
|
|
|
if("xm_zsyy_wx".equals(wxId) || "xm_xzzx_wx".equals(wxId) || "xm_ykyy_wx".equals(wxId)||"xm_hcyy_wx".equals(wxId)){
|
|
|
newJsApiTicket.setAcc_id(wxId);
|
|
|
}else{
|
|
|
newJsApiTicket.setAcc_id(wxWechat.getAppOriginId());
|
|
|
}
|
|
|
|
|
|
wxAccessTokenService.addEnterpriseJsapiTicket(newJsApiTicket);
|
|
|
return ticket;
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
System.out.println(e.getMessage());
|
|
|
throw new Exception(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 下载多媒体文件(请注意,视频文件不支持下载,调用该接口需http协议)
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public InputStream getInputStream(String mediaId, String wechat_appid) throws Exception {
|
|
|
String accessToken = getToken(wechat_appid);
|
|
|
InputStream is = null;
|
|
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=" + accessToken + "&media_id=" + mediaId;
|
|
|
// String url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=30_OBJbMMPW9TnyGHKf32bcjkXtxWnzBrtqWlSMBnNk8MWpJ6qis2TnsecRYNZImgahdx30WrsI1bGco6K67-j4sT1QkDxYKBrVvjaYF6QgbY8dgBMzuoARKoaxtX3ehiYvdCLSUHvogrpF3n0GANIfCHAHUP&media_id=R_060hnyXAFyHD9HaOLl15xhDIzDvcA61-Wz34GkXmCakdrsFJqxrPEBHewmAEK9";
|
|
|
logger.info("media/get" + url);
|
|
|
try {
|
|
|
URL urlGet = new URL(url);
|
|
|
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
|
|
|
http.setRequestMethod("GET"); // 必须是get方式请求
|
|
|
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
http.setDoOutput(true);
|
|
|
http.setDoInput(true);
|
|
|
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
|
|
|
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
|
|
|
http.connect();
|
|
|
|
|
|
// 获取文件转化为byte流
|
|
|
is = http.getInputStream();
|
|
|
if (is.available() <= 200) {
|
|
|
logger.info("下载多媒体文件 accessToken 过期");
|
|
|
accessToken=getToken(wechat_appid);
|
|
|
is = getInputStreamReLoad(mediaId, wechat_appid, accessToken);
|
|
|
return is;
|
|
|
}
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
logger.info(e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
throw new Exception("下载失败," + e.getMessage());
|
|
|
}
|
|
|
return is;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* accessToken 过期时重新下载多媒体文件
|
|
|
* 下载多媒体文件(请注意,视频文件不支持下载,调用该接口需http协议)
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public InputStream getInputStreamReLoad(String mediaId, String wechat_appid, String accessToken) throws Exception {
|
|
|
logger.info("accessToken 过期时重新下载多媒体文件:accessToken:" + accessToken);
|
|
|
logger.info("accessToken 过期时重新下载多媒体文件:wechat_appid:" + wechat_appid);
|
|
|
logger.info("accessToken 过期时重新下载多媒体文件:mediaId:" + mediaId);
|
|
|
InputStream is = null;
|
|
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=" + accessToken + "&media_id=" + mediaId;
|
|
|
logger.info("media/get" + url);
|
|
|
try {
|
|
|
URL urlGet = new URL(url);
|
|
|
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
|
|
|
http.setRequestMethod("GET"); // 必须是get方式请求
|
|
|
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
http.setDoOutput(true);
|
|
|
http.setDoInput(true);
|
|
|
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
|
|
|
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
|
|
|
http.connect();
|
|
|
// 获取文件转化为byte流
|
|
|
is = http.getInputStream();
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
logger.info(e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
throw new Exception("下载失败," + e.getMessage());
|
|
|
}
|
|
|
return is;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取下载语音信息(jpg)
|
|
|
*
|
|
|
* @param mediaId 文件的id
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public String saveVoiceToDisk(String mediaId, String wechat_appid) throws Exception {
|
|
|
// 文件保存的临时路径
|
|
|
String tempPath = upload_temp_path + File.separator;
|
|
|
System.out.println("tempPath:" + tempPath);
|
|
|
// 拼接年月日路径
|
|
|
String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
|
|
|
System.out.println("datePath:" + datePath);
|
|
|
// 重命名文件
|
|
|
String fileBase = DateUtil.dateToStr(new Date(), DateUtil.YYYYMMDDHHMMSS) + "_" + new Random().nextInt(1000);
|
|
|
System.out.println("fileBase:" + fileBase);
|
|
|
String newFileName = fileBase + ".amr";
|
|
|
String mp3FileName = fileBase + ".mp3";
|
|
|
System.out.println("newFileName:" + newFileName);
|
|
|
System.out.println("mp3FileName:" + mp3FileName);
|
|
|
// 保存路径
|
|
|
File uploadFile = new File(tempPath + datePath + newFileName);
|
|
|
|
|
|
InputStream inputStream = null;
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
try {
|
|
|
if (!uploadFile.getParentFile().exists()) {
|
|
|
uploadFile.getParentFile().mkdirs();
|
|
|
}
|
|
|
inputStream = getInputStream(mediaId, wechat_appid);
|
|
|
byte[] data = new byte[1024];
|
|
|
int len = 0;
|
|
|
fileOutputStream = new FileOutputStream(uploadFile);
|
|
|
while ((len = inputStream.read(data)) != -1) {
|
|
|
fileOutputStream.write(data, 0, len);
|
|
|
}
|
|
|
String amrFilePath = tempPath + datePath + newFileName;
|
|
|
String Mp3FilePath = tempPath + datePath + mp3FileName;
|
|
|
System.out.println("amrFilePath:" + amrFilePath);
|
|
|
System.out.println("Mp3FilePath:" + Mp3FilePath);
|
|
|
// fileUtil.changeToMp3(amrFilePath,Mp3FilePath);
|
|
|
// 返回保存路径
|
|
|
return amrFilePath;
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
if (inputStream != null) {
|
|
|
try {
|
|
|
inputStream.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
if (fileOutputStream != null) {
|
|
|
try {
|
|
|
fileOutputStream.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取微信服务器语音
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public String fetchWxVoices(String wecha_appid) {
|
|
|
String voiceIds = "";
|
|
|
try {
|
|
|
String voices = request.getParameter("voices");
|
|
|
System.out.println("voices" + voices);
|
|
|
if (org.apache.commons.lang3.StringUtils.isEmpty(voices)) {
|
|
|
return voices;
|
|
|
}
|
|
|
String[] mediaIds = voices.split(",");
|
|
|
for (String mediaId : mediaIds) {
|
|
|
if (org.apache.commons.lang3.StringUtils.isEmpty(mediaId)) {
|
|
|
continue;
|
|
|
}
|
|
|
String temp = saveVoiceToDisk(mediaId, wecha_appid);
|
|
|
if (org.apache.commons.lang3.StringUtils.isNotEmpty(temp)) {
|
|
|
if (voiceIds.length() == 0) {
|
|
|
voiceIds = temp;
|
|
|
} else {
|
|
|
voiceIds += "," + temp;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
// error(e);
|
|
|
}
|
|
|
return voiceIds;
|
|
|
}
|
|
|
// public String send
|
|
|
|
|
|
// public static void main(String args[])throws Exception{
|
|
@ -608,4 +872,5 @@ public class EnterpriseService {
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
}
|