参考资料:
官网配置:分享接口官方文档
按照步骤来即可,图如下:
第一步:绑定域名
- 这里是测试公众号地址:测试号
- 我这里使用的ngrok工具配置的域名,你也可以去下载,如果你实在弄不好私信我我发给你,网上教程挺多的耐心搜一下
第三步:看wx.config所需要的配置
- ps:(第二步 前端引入我就直接过了)
- 这里需要后端给前端返回去签名,时间戳,签名随机串,jsApiList接口列表(这个反不反都行).
准备工作
- 在这里先做一下准备工作,引入maven
<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-mp -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.1.0</version>
</dependency>
<!-- 增加hutool工具包 5.6.3 版本 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
配置yaml
- 配置类先贴出来
wechat:
ip: ca.ngrok.io
path: http://ca.ngrok.io
mpappid: appid
mpappsecret: secret上边在测试公众号去拿就可以了
template:
follow:
push: 模板消息的id
配置类
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author hanfeng
* @date 2021/12/4
*/
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
private String mpAppId;
private String mpAppSecret;
@Value(value = "${wechat.template.follow.push}")
private String followPush;
}
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* @author hanfeng
* @date 2021/12/4
*/
@Component
public class WeChatMpConfig {
@Autowired
private WechatAccountConfig wechatAccountConfig;
@Bean
public WxMpService wxMpService(){
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage(){
WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
wxMpDefaultConfig.setAppId(wechatAccountConfig.getMpAppId());
wxMpDefaultConfig.setSecret(wechatAccountConfig.getMpAppSecret());
return wxMpDefaultConfig;
}
}
首先获取token
-
然后先把获取token的方法写上(WxMpApiUrl,WxMpService 这个两个是maven带的)
-
这里是controller层的代码
@Autowired
private WxMpService wxMpService;
@Autowired
WxMpConfigStorage wxMpConfigStorage;
@Autowired
private WxMpMessageService mpMessageService;
@Autowired
private WechatAccountConfig accountConfig;
@ApiOperation("获取token")
@GetMapping("/accessToken")
public String accessToken() {
String accessToken = "";
try {
WxMpApiUrl.Other getAccessTokenUrl = WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL;
String url = String.format(
getAccessTokenUrl.getPrefix() + getAccessTokenUrl.getPath(),
wxMpConfigStorage.getAppId(),
wxMpConfigStorage.getSecret());
accessToken = HttpUtil.get(url);
JSONObject jsonObject = JSONUtil.parseObj(accessToken);
if (jsonObject.getStr("access_token") != null) {
accessToken = jsonObject.getStr("access_token");
}
log.info("获取token:" + accessToken);
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
获取签名
@ApiOperation("获取签名")
@ApiImplicitParams({
@ApiImplicitParam(name = "url", value = "当前页面url", required = true)
})
@GetMapping("/signature")
public Result signature(String url) {
Map<String, Object> map = new HashMap<>();
try {
WxMpApiUrl.Other getAccessTokenUrl = WxMpApiUrl.Other.GET_TICKET_URL;
WxMpConfigStorage wxMpConfigStorage = wxMpService.getWxMpConfigStorage();
String requestUrl = String.format(
getAccessTokenUrl.getPrefix() + getAccessTokenUrl.getPath() + "%s&access_token=%s",
"jsapi",
accessToken());
String signature = HttpUtil.get(requestUrl);
JSONObject jsonObject = JSONUtil.parseObj(signature);
String errcode = jsonObject.getStr("errcode");
if (StrUtil.equals("0", errcode)) {
map.put("appId", wxMpConfigStorage.getAppId());
map.put("jsApiList", CollUtil.newArrayList("updateAppMessageShareData", "updateTimelineShareData"));
//获取到签名
String jsapiTicket = jsonObject.getStr("ticket");
//生成签名
String nonceStr = RandomUtil.randomString(10);
map.put("nonceStr", nonceStr);
//当前时间戳
String current = DateUtil.current() + "";
map.put("timestamp", current);
//我这里比较省事是直接拼起来的
String str = "jsapi_ticket=" + jsapiTicket
+ "&noncestr=" + nonceStr
+ "×tamp=" + current
+ "&url" + url;
String sha1 = SecureUtil.sha1(str);
map.put("signature", sha1);
}
log.info("获取签名:" + signature);
} catch (Exception e) {
e.printStackTrace();
}
return Result.success(map);
}
第四步: 前端配置即可,后端工作准备到此