版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sky_fate/article/details/85160409
代码记录:
package com.yunxi.access.action;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.access.constant.config.RedisCache;
import com.yunxi.util.common.RandomCreateCode;
import com.yunxi.util.http.HttpPostUtil;
import com.yunxi.util.security.Sha1;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.net.URLDecoder;
import static com.yunxi.util.common.RandomCreateCode.MODE_CHARACTER;
/**
* 微信js分享
* 获取 access_token,ticket,signature
* @Author QZJ
* @CreateTime 2018/12/20
*/
@RestController
public class WxAction {
@Value("${wx.appId}")
String appId;
@Value("${wx.appSecret}")
String appSecret;
@Autowired
RedisCache redisCache;
private static final String ACCESS_TOKEN = "ACCESS_TOKEN";
private static final String JS_TICKET = "JS_TICKET";
private static final int EXPIRE_TIME = 120 * 60;
@ApiOperation(value = "获取微信分享信息", notes = "JSON")
@PostMapping("/getWxInfo")
public @ResponseBody
JSONObject getWxInfo(@RequestParam(value = "shareLink") String shareLink) throws Exception {
// 获取token
String accessToken = getValue("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret, "access_token", ACCESS_TOKEN);
// 获取 ticket 注意链接前端需要encode,因为包含连接符
String ticket = getValue("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi", "ticket", JS_TICKET);
// 生成签名
return getSignature(ticket, shareLink);
}
/**
* 生成签名信息
* @param ticket
* @param shareLink
* @return
* @throws Exception
*/
private JSONObject getSignature(String ticket, String shareLink) throws Exception {
JSONObject wxObject = new JSONObject();
Long timestamp = System.currentTimeMillis() / 1000;
String nonceStr = RandomCreateCode.randomNumber(10, MODE_CHARACTER);
String param = "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + URLDecoder.decode(shareLink, "utf-8");
wxObject.put("timestamp", timestamp);
wxObject.put("nonceStr", nonceStr);
wxObject.put("signature", Sha1.encode(param));
return wxObject;
}
/**
* 取微信值 并 存储 redis
*
* @param url
* @param key
* @param redisKey
* @return
* @throws Exception
*/
private String getValue(String url, String key, String redisKey) throws Exception {
// 从redis中取值
String value = redisCache.getValue(redisKey);
if (!StringUtils.isEmpty(value)) {
return value;
}
// 微信取值
String result = HttpPostUtil.sendPost(url, "", HttpPostUtil.FORM);
value = JSONObject.parseObject(result).getString(key);
// 存储
redisCache.setValue(redisKey, value, EXPIRE_TIME);
return value;
}
}