微信服务号和小程序的access_token存储至Redis

master
3y 2 years ago
parent 2187e6064b
commit 364bcfcfbd

@ -18,5 +18,15 @@ public class SendAccountConstant {
*/
public static final String GE_TUI_ACCESS_TOKEN_PREFIX = "ge_tui_access_token_";
/**
*
*/
public static final String OFFICIAL_ACCOUNT_ACCESS_TOKEN_PREFIX = "official_account";
/**
*
*/
public static final String MINI_PROGRAM_TOKEN_PREFIX = "mini_program";
}

@ -1,6 +1,6 @@
package com.java3y.austin.handler.handler.impl;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
@ -10,7 +10,7 @@ import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.handler.handler.BaseHandler;
import com.java3y.austin.handler.handler.Handler;
import com.java3y.austin.support.domain.MessageTemplate;
import com.java3y.austin.support.utils.WxServiceUtils;
import com.java3y.austin.support.utils.AccountUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -28,7 +28,7 @@ import java.util.Set;
@Slf4j
public class MiniProgramAccountHandler extends BaseHandler implements Handler {
@Autowired
private WxServiceUtils wxServiceUtils;
private AccountUtils accountUtils;
public MiniProgramAccountHandler() {
channelCode = ChannelType.MINI_PROGRAM.getCode();
@ -37,11 +37,11 @@ public class MiniProgramAccountHandler extends BaseHandler implements Handler {
@Override
public boolean handler(TaskInfo taskInfo) {
MiniProgramContentModel contentModel = (MiniProgramContentModel) taskInfo.getContentModel();
WxMaSubscribeService wxMaSubscribeService = wxServiceUtils.getMiniProgramServiceMap().get(taskInfo.getSendAccount().longValue());
WxMaService wxMaService = accountUtils.getAccountById(taskInfo.getSendAccount(), WxMaService.class);
List<WxMaSubscribeMessage> wxMaSubscribeMessages = assembleReq(taskInfo.getReceiver(), contentModel);
for (WxMaSubscribeMessage message : wxMaSubscribeMessages) {
try {
wxMaSubscribeService.sendSubscribeMsg(message);
wxMaService.getSubscribeService().sendSubscribeMsg(message);
} catch (Exception e) {
log.info("MiniProgramAccountHandler#handler fail! param:{},e:{}", JSON.toJSONString(taskInfo), Throwables.getStackTraceAsString(e));
}

@ -8,7 +8,7 @@ import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.handler.handler.BaseHandler;
import com.java3y.austin.handler.handler.Handler;
import com.java3y.austin.support.domain.MessageTemplate;
import com.java3y.austin.support.utils.WxServiceUtils;
import com.java3y.austin.support.utils.AccountUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
@ -30,7 +30,7 @@ import java.util.Set;
public class OfficialAccountHandler extends BaseHandler implements Handler {
@Autowired
private WxServiceUtils wxServiceUtils;
private AccountUtils accountUtils;
public OfficialAccountHandler() {
channelCode = ChannelType.OFFICIAL_ACCOUNT.getCode();
@ -40,7 +40,7 @@ public class OfficialAccountHandler extends BaseHandler implements Handler {
public boolean handler(TaskInfo taskInfo) {
try {
OfficialAccountsContentModel contentModel = (OfficialAccountsContentModel) taskInfo.getContentModel();
WxMpService wxMpService = wxServiceUtils.getOfficialAccountServiceMap().get(taskInfo.getSendAccount().longValue());
WxMpService wxMpService = accountUtils.getAccountById(taskInfo.getSendAccount(), WxMpService.class);
List<WxMpTemplateMessage> messages = assembleReq(taskInfo.getReceiver(), contentModel);
for (WxMpTemplateMessage message : messages) {
try {

@ -1,9 +1,6 @@
package com.java3y.austin.support.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@ -19,6 +16,7 @@ import javax.persistence.Id;
@NoArgsConstructor
@AllArgsConstructor
@Entity
@ToString
public class ChannelAccount {
@Id

@ -30,9 +30,9 @@ public class ProcessException extends RuntimeException {
public String getMessage() {
if (Objects.nonNull(this.processContext)) {
return this.processContext.getResponse().getMsg();
} else {
return RespStatusEnum.CONTEXT_IS_NULL.getMsg();
}
return RespStatusEnum.CONTEXT_IS_NULL.getMsg();
}
public ProcessContext getProcessContext() {

@ -1,38 +1,80 @@
package com.java3y.austin.support.utils;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.WeChatMiniProgramAccount;
import com.java3y.austin.common.dto.account.WeChatOfficialAccount;
import com.java3y.austin.common.dto.account.sms.SmsAccount;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.domain.ChannelAccount;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
/**
*
*
* @author 3y
*/
@Component
@Slf4j
@Configuration
public class AccountUtils {
@Autowired
private ChannelAccountDao channelAccountDao;
@Autowired
private StringRedisTemplate redisTemplate;
/**
* /
*/
private Map<ChannelAccount, WxMpService> officialAccountServiceMap = new ConcurrentHashMap<>();
private Map<ChannelAccount, WxMaService> miniProgramServiceMap = new ConcurrentHashMap<>();
@Bean
public RedisTemplateWxRedisOps redisTemplateWxRedisOps() {
return new RedisTemplateWxRedisOps(redisTemplate);
}
/**
* WxMaService
* WxMpService
* XXXAccount
*
* @param sendAccountId
* @param clazz
* @param <T>
* @return
*/
public <T> T getAccountById(Integer sendAccountId, Class<T> clazz) {
try {
Optional<ChannelAccount> optionalChannelAccount = channelAccountDao.findById(Long.valueOf(sendAccountId));
if (optionalChannelAccount.isPresent()) {
ChannelAccount channelAccount = optionalChannelAccount.get();
return JSON.parseObject(channelAccount.getAccountConfig(), clazz);
if (clazz.equals(WxMaService.class)) {
return (T) miniProgramServiceMap.computeIfAbsent(channelAccount, account -> initMiniProgramService(JSON.parseObject(account.getAccountConfig(), WeChatMiniProgramAccount.class)));
} else if (clazz.equals(WxMpService.class)) {
return (T) officialAccountServiceMap.computeIfAbsent(channelAccount, account -> initOfficialAccountService(JSON.parseObject(account.getAccountConfig(), WeChatOfficialAccount.class)));
} else {
return JSON.parseObject(channelAccount.getAccountConfig(), clazz);
}
}
} catch (Exception e) {
log.error("AccountUtils#getAccount fail! e:{}", Throwables.getStackTraceAsString(e));
@ -68,4 +110,37 @@ public class AccountUtils {
return null;
}
/**
*
* access_token redis
*
* @return
*/
public WxMpService initOfficialAccountService(WeChatOfficialAccount officialAccount) {
WxMpService wxMpService = new WxMpServiceImpl();
WxMpRedisConfigImpl config = new WxMpRedisConfigImpl(redisTemplateWxRedisOps(), SendAccountConstant.OFFICIAL_ACCOUNT_ACCESS_TOKEN_PREFIX);
config.setAppId(officialAccount.getAppId());
config.setSecret(officialAccount.getSecret());
config.setToken(officialAccount.getToken());
wxMpService.setWxMpConfigStorage(config);
return wxMpService;
}
/**
*
* access_token redis
*
* @return
*/
private WxMaService initMiniProgramService(WeChatMiniProgramAccount miniProgramAccount) {
WxMaService wxMaService = new WxMaServiceImpl();
WxMaRedisBetterConfigImpl config = new WxMaRedisBetterConfigImpl(redisTemplateWxRedisOps(), SendAccountConstant.MINI_PROGRAM_TOKEN_PREFIX);
config.setAppid(miniProgramAccount.getAppId());
config.setSecret(miniProgramAccount.getAppSecret());
wxMaService.setWxMaConfig(config);
return wxMaService;
}
}

@ -1,113 +0,0 @@
package com.java3y.austin.support.utils;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaSubscribeServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.alibaba.fastjson.JSON;
import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.common.dto.account.WeChatMiniProgramAccount;
import com.java3y.austin.common.dto.account.WeChatOfficialAccount;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.domain.ChannelAccount;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* /
*
* @author 3y
*/
@Component
@Slf4j
@Data
public class WxServiceUtils {
/**
* /
*/
private Map<Long, WxMpService> officialAccountServiceMap = new ConcurrentHashMap<>();
private Map<Long, WxMaSubscribeService> miniProgramServiceMap = new ConcurrentHashMap<>();
@Autowired
private ChannelAccountDao channelAccountDao;
@PostConstruct
public void init() {
initOfficialAccount();
initMiniProgram();
}
/**
* /Map
*/
public void fresh() {
init();
}
/**
*
*/
private void initMiniProgram() {
List<ChannelAccount> miniProgram = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.MINI_PROGRAM.getCode());
for (ChannelAccount channelAccount : miniProgram) {
WeChatMiniProgramAccount weChatMiniProgramAccount = JSON.parseObject(channelAccount.getAccountConfig(), WeChatMiniProgramAccount.class);
miniProgramServiceMap.put(channelAccount.getId(), initMiniProgramService(weChatMiniProgramAccount));
}
}
/**
*
*/
private void initOfficialAccount() {
List<ChannelAccount> officialAccountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.OFFICIAL_ACCOUNT.getCode());
for (ChannelAccount channelAccount : officialAccountList) {
WeChatOfficialAccount weChatOfficialAccount = JSON.parseObject(channelAccount.getAccountConfig(), WeChatOfficialAccount.class);
officialAccountServiceMap.put(channelAccount.getId(), initOfficialAccountService(weChatOfficialAccount));
}
}
/**
*
*
* @return
*/
public WxMpService initOfficialAccountService(WeChatOfficialAccount officialAccount) {
WxMpService wxMpService = new WxMpServiceImpl();
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
config.setAppId(officialAccount.getAppId());
config.setSecret(officialAccount.getSecret());
config.setToken(officialAccount.getToken());
wxMpService.setWxMpConfigStorage(config);
return wxMpService;
}
/**
*
*
* @return
*/
private WxMaSubscribeServiceImpl initMiniProgramService(WeChatMiniProgramAccount miniProgramAccount) {
WxMaService wxMaService = new WxMaServiceImpl();
WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
wxMaConfig.setAppid(miniProgramAccount.getAppId());
wxMaConfig.setSecret(miniProgramAccount.getAppSecret());
wxMaService.setWxMaConfig(wxMaConfig);
return new WxMaSubscribeServiceImpl(wxMaService);
}
}

@ -2,7 +2,7 @@ package com.java3y.austin.web.config;
import com.java3y.austin.common.constant.OfficialAccountParamConstant;
import com.java3y.austin.common.dto.account.WeChatOfficialAccount;
import com.java3y.austin.support.utils.WxServiceUtils;
import com.java3y.austin.support.utils.AccountUtils;
import lombok.Data;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
@ -39,7 +39,7 @@ public class WeChatLoginConfig {
private String token;
@Autowired
private WxServiceUtils wxServiceUtils;
private AccountUtils accountUtils;
/**
*
@ -55,7 +55,7 @@ public class WeChatLoginConfig {
@PostConstruct
private void init() {
WeChatOfficialAccount account = WeChatOfficialAccount.builder().appId(appId).secret(secret).token(token).build();
officialAccountLoginService = wxServiceUtils.initOfficialAccountService(account);
officialAccountLoginService = accountUtils.initOfficialAccountService(account);
initConfig();
initRouter();
}

@ -1,12 +1,12 @@
package com.java3y.austin.web.controller;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.http.HttpUtil;
import com.google.common.base.Throwables;
import com.java3y.austin.common.enums.RespStatusEnum;
import com.java3y.austin.common.vo.BasicResultVO;
import com.java3y.austin.support.utils.WxServiceUtils;
import com.java3y.austin.support.utils.AccountUtils;
import com.java3y.austin.web.utils.Convert4Amis;
import com.java3y.austin.web.vo.amis.CommonAmisVo;
import io.swagger.annotations.Api;
@ -35,15 +35,15 @@ import java.util.Objects;
public class MiniProgramController {
@Autowired
private WxServiceUtils wxServiceUtils;
private AccountUtils accountUtils;
@GetMapping("/template/list")
@ApiOperation("/根据账号Id获取模板列表")
public BasicResultVO queryList(Long id) {
public BasicResultVO queryList(Integer id) {
try {
List<CommonAmisVo> result = new ArrayList<>();
WxMaSubscribeService wxMaSubscribeService = wxServiceUtils.getMiniProgramServiceMap().get(id);
List<TemplateInfo> templateList = wxMaSubscribeService.getTemplateList();
WxMaService wxMaService = accountUtils.getAccountById(id, WxMaService.class);
List<TemplateInfo> templateList = wxMaService.getSubscribeService().getTemplateList();
for (TemplateInfo templateInfo : templateList) {
CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(templateInfo.getTitle()).value(templateInfo.getPriTmplId()).build();
result.add(commonAmisVo);
@ -63,13 +63,13 @@ public class MiniProgramController {
*/
@PostMapping("/detailTemplate")
@ApiOperation("/根据账号Id和模板ID获取模板列表")
public BasicResultVO queryDetailList(Long id, String wxTemplateId) {
public BasicResultVO queryDetailList(Integer id, String wxTemplateId) {
if (Objects.isNull(id) || Objects.isNull(wxTemplateId)) {
return BasicResultVO.success(RespStatusEnum.CLIENT_BAD_PARAMETERS);
}
try {
WxMaSubscribeService wxMaSubscribeService = wxServiceUtils.getMiniProgramServiceMap().get(id);
List<TemplateInfo> templateList = wxMaSubscribeService.getTemplateList();
WxMaService wxMaService = accountUtils.getAccountById(id, WxMaService.class);
List<TemplateInfo> templateList = wxMaService.getSubscribeService().getTemplateList();
CommonAmisVo wxMpTemplateParam = Convert4Amis.getWxMaTemplateParam(wxTemplateId, templateList);
return BasicResultVO.success(wxMpTemplateParam);
} catch (Exception e) {

@ -10,7 +10,7 @@ import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.common.constant.OfficialAccountParamConstant;
import com.java3y.austin.common.enums.RespStatusEnum;
import com.java3y.austin.common.vo.BasicResultVO;
import com.java3y.austin.support.utils.WxServiceUtils;
import com.java3y.austin.support.utils.AccountUtils;
import com.java3y.austin.web.config.WeChatLoginConfig;
import com.java3y.austin.web.utils.Convert4Amis;
import com.java3y.austin.web.utils.LoginUtils;
@ -46,7 +46,7 @@ import java.util.*;
public class OfficialAccountController {
@Autowired
private WxServiceUtils wxServiceUtils;
private AccountUtils accountUtils;
@Autowired
private LoginUtils loginUtils;
@ -60,10 +60,10 @@ public class OfficialAccountController {
*/
@GetMapping("/template/list")
@ApiOperation("/根据账号Id获取模板列表")
public BasicResultVO queryList(Long id) {
public BasicResultVO queryList(Integer id) {
try {
List<CommonAmisVo> result = new ArrayList<>();
WxMpService wxMpService = wxServiceUtils.getOfficialAccountServiceMap().get(id);
WxMpService wxMpService = accountUtils.getAccountById(id, WxMpService.class);
List<WxMpTemplate> allPrivateTemplate = wxMpService.getTemplateMsgService().getAllPrivateTemplate();
for (WxMpTemplate wxMpTemplate : allPrivateTemplate) {
@ -85,12 +85,12 @@ public class OfficialAccountController {
*/
@PostMapping("/detailTemplate")
@ApiOperation("/根据账号Id和模板ID获取模板列表")
public BasicResultVO queryDetailList(Long id, String wxTemplateId) {
public BasicResultVO queryDetailList(Integer id, String wxTemplateId) {
if (Objects.isNull(id) || Objects.isNull(wxTemplateId)) {
return BasicResultVO.success(RespStatusEnum.CLIENT_BAD_PARAMETERS);
}
try {
WxMpService wxMpService = wxServiceUtils.getOfficialAccountServiceMap().get(id);
WxMpService wxMpService = accountUtils.getAccountById(id, WxMpService.class);
List<WxMpTemplate> allPrivateTemplate = wxMpService.getTemplateMsgService().getAllPrivateTemplate();
CommonAmisVo wxMpTemplateParam = Convert4Amis.getWxMpTemplateParam(wxTemplateId, allPrivateTemplate);
return BasicResultVO.success(wxMpTemplateParam);

@ -6,7 +6,6 @@ import com.java3y.austin.common.constant.AustinConstant;
import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.domain.ChannelAccount;
import com.java3y.austin.support.utils.WxServiceUtils;
import com.java3y.austin.web.service.ChannelAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -22,8 +21,6 @@ public class ChannelAccountServiceImpl implements ChannelAccountService {
@Autowired
private ChannelAccountDao channelAccountDao;
@Autowired
private WxServiceUtils wxServiceUtils;
@Override
public ChannelAccount save(ChannelAccount channelAccount) {
@ -33,9 +30,7 @@ public class ChannelAccountServiceImpl implements ChannelAccountService {
}
channelAccount.setCreator(StrUtil.isBlank(channelAccount.getCreator()) ? AustinConstant.DEFAULT_CREATOR : channelAccount.getCreator());
channelAccount.setUpdated(Math.toIntExact(DateUtil.currentSeconds()));
ChannelAccount result = channelAccountDao.save(channelAccount);
wxServiceUtils.fresh();
return result;
return channelAccountDao.save(channelAccount);
}
@Override

Loading…
Cancel
Save