WechatServiceImpl.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.yingying.tourist.service.impl;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.concurrent.TimeUnit;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.data.redis.core.StringRedisTemplate;
  8. import org.springframework.data.redis.core.ValueOperations;
  9. import org.springframework.stereotype.Service;
  10. import com.alibaba.fastjson.JSON;
  11. import com.alibaba.fastjson.JSONObject;
  12. import com.yingying.tourist.common.HttpRequestSimple;
  13. import com.yingying.tourist.domain.User;
  14. import com.yingying.tourist.service.WechatService;
  15. @Service
  16. public class WechatServiceImpl implements WechatService {
  17. @Autowired
  18. private StringRedisTemplate redisTemplate;
  19. @Value("${weixin.appid}")
  20. private String appId;
  21. @Value("${weixin.appSecret}")
  22. private String appSecret;
  23. /**
  24. * 微信测试号
  25. */
  26. // private static String appId = "wx8a98f5f7df1e89b5";
  27. // private static String appSecret = "f622f3300cb3d12d01f8e82ff1cce1ee";
  28. /**
  29. * 获取微信服务端token
  30. *
  31. * @return
  32. */
  33. @Override
  34. public String getToken() {
  35. ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
  36. String tokenResult = opsForValue.get("token");
  37. if (StringUtils.isNoneBlank(tokenResult)) {
  38. return tokenResult;
  39. }
  40. String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret="
  41. + appSecret;
  42. String result = HttpRequestSimple.doGet(url);
  43. JSONObject jsonObject = JSONObject.parseObject(result);
  44. String token = jsonObject.getString("access_token");
  45. opsForValue.set("token", token, 7200, TimeUnit.SECONDS);
  46. return token;
  47. }
  48. /**
  49. * JS-SDK使用权限签名算法 获取ticket
  50. *
  51. * @return
  52. */
  53. @Override
  54. public String getTicket() {
  55. String token = getToken();
  56. String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token + "&type=jsapi";
  57. String result = HttpRequestSimple.doGet(url);
  58. JSONObject jsonObject = JSONObject.parseObject(result);
  59. String ticket = jsonObject.getString("ticket");
  60. return ticket;
  61. }
  62. /**
  63. * 通过code换取网页授权access_token
  64. *
  65. * @param code
  66. * @return
  67. */
  68. @Override
  69. public String getAuthToken(String code) {
  70. String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret
  71. + "&code=" + code + "&grant_type=authorization_code";
  72. String result = HttpRequestSimple.doGet(url);
  73. return result;
  74. }
  75. @Override
  76. public User getWechatUser(String token, String openId) throws UnsupportedEncodingException {
  77. String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token + "&openid=" + openId
  78. + "&lang=zh_CN";
  79. String result = HttpRequestSimple.doGet(url);
  80. String decodeResult = new String(result.getBytes("ISO-8859-1"), "UTF-8");
  81. User user = JSON.parseObject(decodeResult, User.class);
  82. return user;
  83. }
  84. }