微信接口开发
This commit is contained in:
parent
2e8e6d7b8d
commit
735296b0f9
@ -66,6 +66,8 @@ public class WechatAppletController extends BaseController {
|
||||
private ISysOrderPauseService sysOrderPauseService;
|
||||
@Autowired
|
||||
private ISysDictTypeService iSysDictTypeService;
|
||||
@Autowired
|
||||
private IWechatAppletService iWechatAppletService;
|
||||
|
||||
/**
|
||||
* 查询微信小程序中展示的客户案例
|
||||
@ -472,6 +474,7 @@ public class WechatAppletController extends BaseController {
|
||||
}
|
||||
|
||||
curWxUserInfo.setCustomerId(AesUtils.encrypt(curWxUserInfo.getCusId().toString()));
|
||||
curWxUserInfo.setCusId(null);
|
||||
|
||||
// 并返回一系列登录后的数据
|
||||
return AjaxResult.success(curWxUserInfo);
|
||||
@ -530,6 +533,11 @@ public class WechatAppletController extends BaseController {
|
||||
return request.getParameter("echostr");
|
||||
}
|
||||
|
||||
@GetMapping("/getToken")
|
||||
public String getToken(@RequestParam String appId) {
|
||||
return iWechatAppletService.getAccessToken(appId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,6 @@ public class SysWxUserInfo {
|
||||
*/
|
||||
private String openid;
|
||||
|
||||
@JsonIgnore
|
||||
private Long cusId;
|
||||
|
||||
private String customerId;
|
||||
|
@ -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;
|
||||
}
|
@ -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);
|
||||
}
|
@ -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");
|
||||
|
||||
}
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
package com.stdiet.framework.config;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import com.stdiet.common.utils.ServletUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* 服务相关配置
|
||||
@ -29,4 +32,9 @@ public class ServerConfig
|
||||
String contextPath = request.getServletContext().getContextPath();
|
||||
return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user