|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|