|  | @ -0,0 +1,125 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.security.service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.wx.WxAppletsAccessTokenDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.wx.WxWechatDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.exception.ApiException;
 | 
	
		
			
				|  |  | import com.yihu.jw.exception.code.ExceptionCode;
 | 
	
		
			
				|  |  | import com.yihu.jw.security.dao.patient.WechatDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.security.dao.patient.WxAppletsAccessTokenDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.wechat.wxhttp.HttpUtil;
 | 
	
		
			
				|  |  | import com.yihu.utils.network.HttpUtils;
 | 
	
		
			
				|  |  | import org.json.JSONObject;
 | 
	
		
			
				|  |  | 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.util.StringUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by yeshijie on 2022/11/2.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class WechatService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static Logger logger = LoggerFactory.getLogger(WechatService.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Value("${wechat.id}")
 | 
	
		
			
				|  |  |     private String wxId;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private WechatDao wechatDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private WxAppletsAccessTokenDao wxAppletsAccessTokenDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据小程序获取用户手机号
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param code
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String getUserPhoneByApplets(String code) throws Exception {
 | 
	
		
			
				|  |  |         String token = getWxAppletsAccessTokenById(wxId).getAccessToken();
 | 
	
		
			
				|  |  |         if (org.apache.commons.lang3.StringUtils.isNoneBlank(code)){
 | 
	
		
			
				|  |  |             String token_url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + token;
 | 
	
		
			
				|  |  |             String params = "{\"code\": \""+code+"\"}";
 | 
	
		
			
				|  |  |             logger.info("参数:"+params);
 | 
	
		
			
				|  |  |             String result = HttpUtil.sendPost(token_url, params);
 | 
	
		
			
				|  |  |             logger.info("步骤1:"+result);
 | 
	
		
			
				|  |  |             if (org.apache.commons.lang3.StringUtils.isNoneBlank(result)){
 | 
	
		
			
				|  |  |                 com.alibaba.fastjson.JSONObject object = com.alibaba.fastjson.JSONObject.parseObject(result);
 | 
	
		
			
				|  |  |                 if (object.getString("errcode").equalsIgnoreCase("0")){
 | 
	
		
			
				|  |  |                     com.alibaba.fastjson.JSONObject jsonObject= object.getJSONObject("phone_info");
 | 
	
		
			
				|  |  |                     return jsonObject.getString("phoneNumber");
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 生成小程序token
 | 
	
		
			
				|  |  |      * @param wechatId
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public WxAppletsAccessTokenDO getWxAppletsAccessTokenById(String wechatId) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             //根据wechatCode查找出appid和appSecret
 | 
	
		
			
				|  |  |             WxWechatDO wxWechat = wechatDao.findById(wechatId);
 | 
	
		
			
				|  |  |             List<WxAppletsAccessTokenDO> wxAccessTokens =  wxAppletsAccessTokenDao.getWxAccessTokenById(wechatId);
 | 
	
		
			
				|  |  |             if(wxWechat==null){
 | 
	
		
			
				|  |  |                 throw new ApiException("wxWechat is not exist", ExceptionCode.common_error_params_code);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if(wxAccessTokens!=null&&wxAccessTokens.size()>0){
 | 
	
		
			
				|  |  |                 for (WxAppletsAccessTokenDO accessToken : wxAccessTokens) {
 | 
	
		
			
				|  |  |                     if ((System.currentTimeMillis() - accessToken.getAddTimestamp()) < (accessToken.getExpiresIn() * 500)) {
 | 
	
		
			
				|  |  |                         return accessToken;
 | 
	
		
			
				|  |  |                     } else {
 | 
	
		
			
				|  |  |                         wxAppletsAccessTokenDao.delete(accessToken);
 | 
	
		
			
				|  |  |                         break;
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             String token_url = "https://api.weixin.qq.com/cgi-bin/token";
 | 
	
		
			
				|  |  |             String appId="";
 | 
	
		
			
				|  |  |             String appSecret="";
 | 
	
		
			
				|  |  |             appId = wxWechat.getApplets();
 | 
	
		
			
				|  |  |             appSecret = wxWechat.getAppletsSecret();
 | 
	
		
			
				|  |  |             if (StringUtils.isEmpty(appId)){
 | 
	
		
			
				|  |  |                 throw new ApiException("appId is null", ExceptionCode.common_error_params_code);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (StringUtils.isEmpty(appSecret)){
 | 
	
		
			
				|  |  |                 throw new ApiException("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();
 | 
	
		
			
				|  |  |                 WxAppletsAccessTokenDO newaccessToken = new WxAppletsAccessTokenDO();
 | 
	
		
			
				|  |  |                 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);
 | 
	
		
			
				|  |  |                 wxAppletsAccessTokenDao.save(newaccessToken);
 | 
	
		
			
				|  |  |                 return newaccessToken;
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 return null;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |