|  | @ -0,0 +1,140 @@
 | 
												
													
														
															|  | 
 |  | package com.yihu.jw.patient.endpoint.wechat;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.entity.base.wx.JsApiTicket;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.entity.base.wx.WxWechatDO;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.restmodel.web.Envelop;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.restmodel.web.ObjEnvelop;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.rm.patient.PatientRequestMapping;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.util.wechat.wxhttp.HttpUtil;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.wechat.service.WxAccessTokenService;
 | 
												
													
														
															|  | 
 |  | import io.swagger.annotations.Api;
 | 
												
													
														
															|  | 
 |  | import org.json.JSONObject;
 | 
												
													
														
															|  | 
 |  | import org.slf4j.Logger;
 | 
												
													
														
															|  | 
 |  | import org.slf4j.LoggerFactory;
 | 
												
													
														
															|  | 
 |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
												
													
														
															|  | 
 |  | import org.springframework.stereotype.Controller;
 | 
												
													
														
															|  | 
 |  | import org.springframework.web.bind.annotation.RequestMapping;
 | 
												
													
														
															|  | 
 |  | import org.springframework.web.bind.annotation.RequestMethod;
 | 
												
													
														
															|  | 
 |  | import org.springframework.web.bind.annotation.ResponseBody;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import java.security.MessageDigest;
 | 
												
													
														
															|  | 
 |  | import java.util.HashMap;
 | 
												
													
														
															|  | 
 |  | import java.util.Map;
 | 
												
													
														
															|  | 
 |  | import java.util.UUID;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * Created by Trick on 2018/12/3.
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | @Controller
 | 
												
													
														
															|  | 
 |  | @RequestMapping(value = PatientRequestMapping.Wechat.wxBase)
 | 
												
													
														
															|  | 
 |  | @Api(value = "微信相关操作", description = "微信相关操作", tags = {"居民端 - 微信相关操作"})
 | 
												
													
														
															|  | 
 |  | public class WechatEndPoint extends EnvelopRestEndpoint {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     private static Logger logger = LoggerFactory.getLogger(WechatEndPoint.class);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     @Autowired
 | 
												
													
														
															|  | 
 |  |     private WxAccessTokenService wxAccessTokenService;
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      * 获取签名signature
 | 
												
													
														
															|  | 
 |  |      *
 | 
												
													
														
															|  | 
 |  |      * @param pageUrl 需要签名的页面全地址(?后的也需要除了#后的不需要)
 | 
												
													
														
															|  | 
 |  |      * @return
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  | 
 |  |     @RequestMapping(value = "getSign", method = RequestMethod.POST)
 | 
												
													
														
															|  | 
 |  |     @ResponseBody
 | 
												
													
														
															|  | 
 |  |     public Envelop getSign(String pageUrl,String wxId) {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         String ticket = getJsapi_ticketByToken(wxId);
 | 
												
													
														
															|  | 
 |  |             Map<Object, Object> map = new HashMap<Object, Object>();
 | 
												
													
														
															|  | 
 |  |             if (ticket != null) {
 | 
												
													
														
															|  | 
 |  |                 String noncestr = UUID.randomUUID().toString();
 | 
												
													
														
															|  | 
 |  |                 long timestamp = System.currentTimeMillis() / 1000;
 | 
												
													
														
															|  | 
 |  |                 String url = pageUrl;
 | 
												
													
														
															|  | 
 |  |                 String str = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url;
 | 
												
													
														
															|  | 
 |  |                 // sha1加密
 | 
												
													
														
															|  | 
 |  |                 String signature = SHA1(str);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 map.put("noncestr", noncestr);
 | 
												
													
														
															|  | 
 |  |                 map.put("timestamp", timestamp);
 | 
												
													
														
															|  | 
 |  |                 map.put("signature", signature);
 | 
												
													
														
															|  | 
 |  |                 return success(PatientRequestMapping.Wechat.api_success,map);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         return failed(PatientRequestMapping.Wechat.api_error);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      * 获取微信的jsapi_ticket
 | 
												
													
														
															|  | 
 |  |      *
 | 
												
													
														
															|  | 
 |  |      * @return
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  | 
 |  |     public String getJsapi_ticketByToken(String wxId) {
 | 
												
													
														
															|  | 
 |  |         try {
 | 
												
													
														
															|  | 
 |  |             WxWechatDO wxWechat = wxAccessTokenService.getWxInfo(wxId);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             Iterable<JsApiTicket> jsapiTickets = wxAccessTokenService.findJsapiTicket(wxWechat.getAppOriginId());
 | 
												
													
														
															|  | 
 |  |             if (jsapiTickets != null) {
 | 
												
													
														
															|  | 
 |  |                 for (JsApiTicket jsApiTicket : jsapiTickets) {
 | 
												
													
														
															|  | 
 |  |                     if ((System.currentTimeMillis() - jsApiTicket.getAdd_timestamp()) < (jsApiTicket.getExpires_in() * 1000)) {
 | 
												
													
														
															|  | 
 |  |                         return jsApiTicket.getJsapi_ticket();
 | 
												
													
														
															|  | 
 |  |                     } else {
 | 
												
													
														
															|  | 
 |  |                         wxAccessTokenService.delJsapiTicket(jsApiTicket);
 | 
												
													
														
															|  | 
 |  |                         break;
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |             String token = wxAccessTokenService.getWxAccessTokenById(wxId).getAccessToken();
 | 
												
													
														
															|  | 
 |  |             if (token != null) {
 | 
												
													
														
															|  | 
 |  |                 String token_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
 | 
												
													
														
															|  | 
 |  |                 String params = "access_token=" + token + "&type=jsapi";
 | 
												
													
														
															|  | 
 |  |                 String result = HttpUtil.sendGet(token_url, params);
 | 
												
													
														
															|  | 
 |  |                 JSONObject json = new JSONObject(result);
 | 
												
													
														
															|  | 
 |  |                 if (json.has("ticket")) {
 | 
												
													
														
															|  | 
 |  |                     String ticket = json.get("ticket").toString();
 | 
												
													
														
															|  | 
 |  |                     String expires_in = json.get("expires_in").toString();
 | 
												
													
														
															|  | 
 |  |                     JsApiTicket newJsApiTicket = new JsApiTicket();
 | 
												
													
														
															|  | 
 |  |                     newJsApiTicket.setJsapi_ticket(ticket);
 | 
												
													
														
															|  | 
 |  |                     newJsApiTicket.setExpires_in(Long.parseLong(expires_in));
 | 
												
													
														
															|  | 
 |  |                     newJsApiTicket.setAcc_id(wxWechat.getAppOriginId());
 | 
												
													
														
															|  | 
 |  |                     newJsApiTicket.setAcc_id(wxWechat.getAppOriginId());
 | 
												
													
														
															|  | 
 |  |                     wxAccessTokenService.addJsapiTicket(newJsApiTicket);
 | 
												
													
														
															|  | 
 |  |                     return ticket;
 | 
												
													
														
															|  | 
 |  |                 } else {
 | 
												
													
														
															|  | 
 |  |                     return null;
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |             } else {
 | 
												
													
														
															|  | 
 |  |                 return null;
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         } catch (Exception e) {
 | 
												
													
														
															|  | 
 |  |             logger.error(e.getMessage());
 | 
												
													
														
															|  | 
 |  |             return null;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      * @description: SHA、SHA1加密 @parameter: str:待加密字符串 @return: 加密串
 | 
												
													
														
															|  | 
 |  |      **/
 | 
												
													
														
															|  | 
 |  |     public String SHA1(String str) {
 | 
												
													
														
															|  | 
 |  |         try {
 | 
												
													
														
															|  | 
 |  |             MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); // 如果是SHA加密只需要将"SHA-1"改成"SHA"即可
 | 
												
													
														
															|  | 
 |  |             digest.update(str.getBytes());
 | 
												
													
														
															|  | 
 |  |             byte messageDigest[] = digest.digest();
 | 
												
													
														
															|  | 
 |  |             // Create Hex String
 | 
												
													
														
															|  | 
 |  |             StringBuffer hexStr = new StringBuffer();
 | 
												
													
														
															|  | 
 |  |             // 字节数组转换为 十六进制 数
 | 
												
													
														
															|  | 
 |  |             for (int i = 0; i < messageDigest.length; i++) {
 | 
												
													
														
															|  | 
 |  |                 String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
 | 
												
													
														
															|  | 
 |  |                 if (shaHex.length() < 2) {
 | 
												
													
														
															|  | 
 |  |                     hexStr.append(0);
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |                 hexStr.append(shaHex);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |             return hexStr.toString();
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         } catch (Exception e) {
 | 
												
													
														
															|  | 
 |  |             logger.error(e.getMessage());
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         return null;
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | }
 |