Преглед на файлове

居民发送语音失败

huangwenjie преди 5 години
родител
ревизия
46835e55ce

+ 5 - 0
business/base-service/src/main/java/com/yihu/jw/wechat/dao/WxAccessTokenDao.java

@ -2,6 +2,7 @@ package com.yihu.jw.wechat.dao;
import com.yihu.jw.entity.base.wx.WxAccessTokenDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
@ -14,4 +15,8 @@ public interface WxAccessTokenDao extends PagingAndSortingRepository<WxAccessTok
    @Query("from WxAccessTokenDO w where w.wechatId =?1 order by w.addTimestamp desc")
    List<WxAccessTokenDO> getWxAccessTokenById(String wechatId);
    
    @Modifying
    @Query("delete from WxAccessTokenDO p where p.wechatId=?1 ")
    void deleteByWechatId(String wechatId);
}

+ 49 - 1
business/base-service/src/main/java/com/yihu/jw/wechat/service/WxAccessTokenService.java

@ -105,7 +105,55 @@ public class WxAccessTokenService extends BaseJpaService<WxAccessTokenDO, WxAcce
            return null;
        }
    }
    
    /**
     * 过期后删掉accesstoken,  重新获取
     * @param wechatId
     * @return
     */
    public String updateAccessToken(String wechatId){
        try {
            wxAccessTokenDao.deleteByWechatId(wechatId);
            //根据wechatCode查找出appid和appSecret
            WxWechatDO wxWechat = wechatDao.findById(wechatId);
            String token_url = "https://api.weixin.qq.com/cgi-bin/token";
            String appId="";
            String appSecret="";
            appId = wxWechat.getAppId();
            appSecret = wxWechat.getAppSecret();
            if (StringUtils.isEmpty(appId)){
                throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appId_is_null, ExceptionCode.common_error_params_code);
            }
            if (StringUtils.isEmpty(appSecret)){
                throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appSecret_is_null, ExceptionCode.common_error_params_code);
            }
            Map<String, Object> params = new HashMap<>();
            params.put("grant_type", "client_credential");
            params.put("appid", appId);
            params.put("secret", appSecret);
            String result = HttpUtils.doGet(token_url, params).getContent();
            logger.info("--------------wechat token return:"+result+"---------------");
            JSONObject json = new JSONObject(result);
            if (json.has("access_token")) {
                String token = json.get("access_token").toString();
                String expires_in = json.get("expires_in").toString();
                WxAccessTokenDO newaccessToken = new WxAccessTokenDO();
                newaccessToken.setAccessToken(token);
                newaccessToken.setExpiresIn(Long.parseLong(expires_in));
                newaccessToken.setAddTimestamp(System.currentTimeMillis());
                newaccessToken.setCzrq(new Date());
                newaccessToken.setCode(UUID.randomUUID().toString().replace("-",""));
                newaccessToken.setWechatId(wechatId);
                wxAccessTokenDao.save(newaccessToken);
                return token;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public WxAccessTokenDO getWxAccessTokenById(String wechatId) {
        try {

+ 61 - 6
business/im-service/src/main/java/com/yihu/jw/im/service/ImService.java

@ -663,14 +663,63 @@ public class ImService {
	 *
	 * @return
	 */
	public InputStream getInputStream(String mediaId,String wechat_appid) {
	public InputStream getInputStream(String mediaId,String wechat_appid) throws Exception {
		String accessToken = "";
		//中山医院互联网医院
		if("xm_zsyy_wx".equals(wechat_appid)){
			accessToken = entranceService.getAccessToken("100033");
		}else{
			accessToken = wxAccessTokenService.getWxAccessTokenById(wechat_appid).getAccessToken();
//		//中山医院互联网医院
//		if("xm_zsyy_wx".equals(wechat_appid)){
//			accessToken = entranceService.getAccessToken("100033");
//		}else{
//			accessToken = wxAccessTokenService.getWxAccessTokenById(wechat_appid).getAccessToken();
//		}
		InputStream is = null;
//		String url = "http://file.api.weixin.qq.com/cgi-bin/media/get?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 过期");
				//中山医院互联网医院
				if("xm_zsyy_wx".equals(wechat_appid)){
					accessToken = entranceService.getAccessToken("100033");
				}else{
					accessToken = wxAccessTokenService.updateAccessToken(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 = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + accessToken + "&media_id=" + mediaId;
		logger.info("media/get"+url);
@ -686,13 +735,19 @@ public class ImService {
			http.connect();
			// 获取文件转化为byte流
			is = http.getInputStream();
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			e.printStackTrace();
			throw new Exception("下载失败,"+e.getMessage());
		}
		return is;
	}
	
	
	
	
	
	/**
	 * 获取微信服务器图片
	 * @param wechat_appid 微信公众号appid

+ 3 - 0
svr/svr-internet-hospital/src/main/java/com/yihu/jw/hospital/endpoint/consult/PatientConsultEndpoint.java

@ -234,6 +234,9 @@ public class PatientConsultEndpoint extends EnvelopRestEndpoint {
		
		//处理语音文件
		if(!org.springframework.util.StringUtils.isEmpty(wechat_appid)){
			
			imService.getInputStream("1",wechat_appid);
			
			String path = imService.fetchWxVoices(wechat_appid);
			logger.info("voice_path:"+path);
			JSONObject obj = new JSONObject();