微信接口开发

This commit is contained in:
huangdeliang
2021-05-13 20:01:15 +08:00
parent 2e8e6d7b8d
commit 735296b0f9
6 changed files with 125 additions and 1 deletions

View File

@ -20,7 +20,6 @@ public class SysWxUserInfo {
*/
private String openid;
@JsonIgnore
private Long cusId;
private String customerId;

View File

@ -0,0 +1,17 @@
package com.stdiet.custom.domain.wechat;
import lombok.Data;
import java.util.Date;
@Data
public class WxPostRecipesMessage {
String name;
Date startDate;
Date endDate;
String remark;
}

View File

@ -0,0 +1,8 @@
package com.stdiet.custom.service;
public interface IWechatAppletService {
public String getAccessToken(String appId);
public void postRecipesMessage(String appId, String openId, String name, String startDate, String endDate, String remark);
}

View File

@ -0,0 +1,84 @@
package com.stdiet.custom.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.stdiet.common.core.redis.RedisCache;
import com.stdiet.common.utils.StringUtils;
import com.stdiet.custom.service.IWechatAppletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class WeChartAppletServiceImp implements IWechatAppletService {
@Autowired
private RedisCache redisCache;
@Autowired
private RestTemplate restTemplate;
@Override
public String getAccessToken(String appId) {
String accessToken = redisCache.getCacheObject(appId);
if (StringUtils.isNull(accessToken)) {
String appSecret = "";
if (appId.equals("wx26be9b2aa525fc1e")) {
appSecret = "de436c17e42e6fc0637bd0de169ea0c1";
}
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appId}&secret={appSecret}";
Map<String, String> param = new HashMap<>();
param.put("appId", appId);
param.put("appSecret", appSecret);
ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class, param);
JSONObject resultObj = JSONObject.parseObject(entity.getBody());
if (resultObj.getInteger("errcode") == 0) {
accessToken = resultObj.getString("access_token");
Integer expiresIn = resultObj.getInteger("expires_in");
redisCache.setCacheObject(appId, accessToken, expiresIn, TimeUnit.SECONDS);
}
}
return accessToken;
}
@Override
public void postRecipesMessage(String appId, String openId, String name, String startDate, String endDate, String remark) {
String accessToken = getAccessToken(appId);
if (StringUtils.isNull(accessToken)) {
return;
}
String tmpId = "";
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
JsonObject param = new JsonObject();
param.addProperty("access_token", accessToken);
param.addProperty("touser", openId);
param.addProperty("template_id", tmpId);
param.addProperty("page", "pages/recipes/index");
JsonObject dataParam = new JsonObject();
dataParam.addProperty("key1", name);
dataParam.addProperty("key2", startDate);
dataParam.addProperty("key3", endDate);
dataParam.addProperty("key4", remark);
param.add("data", dataParam);
ResponseEntity<String> entity = restTemplate.postForEntity(url, param, String.class);
JSONObject resultObj = JSONObject.parseObject(entity.getBody());
System.out.println(resultObj.toJSONString());
// Integer errcode = resultObj.getInteger("errcode");
}
}