新增公众号相关类

master
星期八 6 years ago
parent 2a643b85ae
commit e8b9d148b7
  1. 22
      src/main/java/com/lq/cms/service/WechatUserService.java
  2. 51
      src/main/java/com/lq/cms/service/impl/WechatUserServiceImpl.java
  3. 162
      src/main/java/com/lq/cms/vo/WechatUserVo.java
  4. 40
      src/main/java/com/lq/cms/web/wechat/WechatUserController.java
  5. 108
      src/main/java/com/lq/code/util/redis/RedisUtil.java
  6. 16
      src/main/java/com/lq/dao/WechatUserDao.java
  7. 99
      src/main/java/com/lq/entity/WechatRule.java
  8. 68
      src/main/java/com/lq/wechat/mode/AccessToken.java
  9. 18
      src/main/java/com/lq/wechat/util/WechatUserUtil.java
  10. 14
      src/main/java/com/lq/wechat/util/accessToken/AccessTokenAdapter.java
  11. 64
      src/main/java/com/lq/wechat/util/accessToken/AccessTokenUtil.java
  12. 31
      src/main/java/com/lq/wechat/util/accessToken/DefaultAccessTokenAdapter.java

@ -0,0 +1,22 @@
package com.lq.cms.service;
import com.lq.cms.vo.WechatUserVo;
import com.lq.code.service.BaseService;
import com.lq.entity.WechatInfo;
import com.lq.entity.WechatUser;
import java.util.Map;
/**
* @Author: qi
* @Description: 微信公众号用户事务类
* @Date: Create in 9:23 AM 2019/3/15
*/
public interface WechatUserService extends BaseService<WechatUser> {
/**
*
* @param openId
* @return
*/
WechatUser saveWechatUser(String openId, WechatInfo wechatInfo);
}

@ -0,0 +1,51 @@
package com.lq.cms.service.impl;
import com.lq.cms.dao.AdminBaseDao;
import com.lq.cms.emun.WechatAccessTokenTypeEnum;
import com.lq.cms.service.WechatUserService;
import com.lq.cms.vo.AdminBaseVo;
import com.lq.cms.vo.WechatUserVo;
import com.lq.code.dao.BaseDao;
import com.lq.code.service.impl.BaseServiceImpl;
import com.lq.dao.WechatAccesstokenDao;
import com.lq.dao.WechatUserDao;
import com.lq.entity.WechatAccessToken;
import com.lq.entity.WechatInfo;
import com.lq.entity.WechatUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
/**
* @Author: qi
* @Description:
* @Date: Create in 9:02 PM 2019/3/16
*/
@Service
public class WechatUserServiceImpl extends BaseServiceImpl<WechatUser> implements WechatUserService {
@Autowired
private WechatUserDao wechatUserDao;
@Autowired
private WechatAccesstokenDao wechatAccesstokenDao;
@Override
public WechatUser saveWechatUser(String openId, WechatInfo wechatInfo) {
WechatUser wechatUser = wechatUserDao.getByOpenId(openId);
if (wechatUser==null){
wechatUser = new WechatUser();
WechatAccessToken wechatAccessToken = wechatAccesstokenDao.getByWechatInfoIdAndTokenType(wechatInfo.getId(), WechatAccessTokenTypeEnum.CURRENCY.getValue());
}
return wechatUser;
}
@Override
public BaseDao<WechatUser> getBaseDao() {
return wechatUserDao;
}
}

@ -0,0 +1,162 @@
package com.lq.cms.vo;
import java.util.Date;
/**
* @Author: qi
* @Description:
* @Date: Create in 9:34 AM 2019/3/15
*/
public class WechatUserVo extends AdminBaseVo{
//用户openId
private String openId;
//昵称
private String nickName;
//性别
private Integer sex;
//语言
private String language;
//城市
private String city;
//省份
private String province;
//国家
private String country;
//头像地址
private String headImgUrl;
//用户关注时间(多次关注,取最后一次时间)
private Date subscribeTime;
//用户对应微信开放平台账号(只有绑定了开放平台账号,才出现该字段)
private String unionid;
//运营者对用户备注
private String remark;
//用户对应分组
private String groupid;
//用户对应的标签
private String tagidId;
//用户关注渠道
private String sybscribeScene;
//关联的微信ID
private Long wechatInfoId;
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getHeadImgUrl() {
return headImgUrl;
}
public void setHeadImgUrl(String headImgUrl) {
this.headImgUrl = headImgUrl;
}
public Date getSubscribeTime() {
return subscribeTime;
}
public void setSubscribeTime(Date subscribeTime) {
this.subscribeTime = subscribeTime;
}
public String getUnionid() {
return unionid;
}
public void setUnionid(String unionid) {
this.unionid = unionid;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getGroupid() {
return groupid;
}
public void setGroupid(String groupid) {
this.groupid = groupid;
}
public String getTagidId() {
return tagidId;
}
public void setTagidId(String tagidId) {
this.tagidId = tagidId;
}
public String getSybscribeScene() {
return sybscribeScene;
}
public void setSybscribeScene(String sybscribeScene) {
this.sybscribeScene = sybscribeScene;
}
public Long getWechatInfoId() {
return wechatInfoId;
}
public void setWechatInfoId(Long wechatInfoId) {
this.wechatInfoId = wechatInfoId;
}
}

@ -0,0 +1,40 @@
package com.lq.cms.web.wechat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author: qi
* @Description: 微信公众号用户管理页面
* @Date: Create in 9:36 AM 2019/3/15
*
*/
@Controller
@RequestMapping("/cms/wechat/user")
public class WechatUserController {
public String index(){
return "";
}
public Object list(){
return null;
}
public Object save(){
return null;
}
public Object update(){
return null;
}
public Object delete(){
return null;
}
}

@ -0,0 +1,108 @@
package com.lq.code.util.redis;
import com.lq.code.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* Created by qi_liang on 2018/8/31.
*/
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 指定缓存失效时间
* @param key
* @param time 时间
* @return
*/
public boolean expirce(String key,Long time){
boolean result = false;
try {
if (time>0){
redisTemplate.expire(key,time, TimeUnit.SECONDS);
result = true;
}
}catch (Exception e){
e.printStackTrace();
result = false;
}
return result;
}
/**
* 根据key 获取过期时间
* @param key 不能为null
* @return 时间() 返回0代码永久有效
*/
public long getExpire(String key){
return redisTemplate.getExpire(key,TimeUnit.SECONDS);
}
/**
* 判断key是否在缓存中存在
* @param key
* @return
*/
public boolean hasKey(String key){
return redisTemplate.hasKey(key);
}
/**
* 通过key删除缓存
* @param key
*/
public void del(String ...key){
if (key!=null&&key.length>0){
for (String keyStr:key){
redisTemplate.delete(keyStr);
}
}
}
/**
* 通过key获取缓存
* @param key
* @return
*/
public Object get(String key){
return StringUtil.isNotNull(key)?redisTemplate.opsForValue().get(key):null;
}
/**
* 设置缓存
* @param key
* @param value 缓存内容
* @return
*/
public void set(String key,Object value){
redisTemplate.opsForValue().set(key,value);
}
/**
* 设置缓存
* @param key
* @param value 缓存内容
* @param time 失效时间单位
*/
public void set(String key,Object value,long time){
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
}
}

@ -0,0 +1,16 @@
package com.lq.dao;
import com.lq.cms.dao.AdminBaseDao;
import com.lq.cms.vo.AdminBaseVo;
import com.lq.code.dao.BaseDao;
import com.lq.entity.WechatUser;
/**
* @Author: qi
* @Description:
* @Date: Create in 5:11 PM 2019/3/14
*/
public interface WechatUserDao extends BaseDao<WechatUser>{
WechatUser getByOpenId(String openId);
}

@ -0,0 +1,99 @@
package com.lq.entity;
import com.lq.code.entity.IdEntity;
import java.util.Date;
/**
* @Author: qi
* @Description: 微信公众号-消息回复规则
* @Date: Create in 9:47 PM 2019/3/16
*
*/
public class WechatRule extends IdEntity {
/**
* 规则名称
*/
private String ruleName;
/**
* 创建时间
*/
private Date createTime;
/**
* 创建用户
*/
private Long createUserId;
/**
* 更新时间
*/
private Date updateTime;
/**
* 更新用户
*/
private Long updateUserId;
/**
* 关联微信公众号ID
*/
private Long wechatInfoId;
/**
* 回复类型(全回复随机回复)
*/
private Integer replyType ;
public String getRuleName() {
return ruleName;
}
public void setRuleName(String ruleName) {
this.ruleName = ruleName;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Long getCreateUserId() {
return createUserId;
}
public void setCreateUserId(Long createUserId) {
this.createUserId = createUserId;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Long getUpdateUserId() {
return updateUserId;
}
public void setUpdateUserId(Long updateUserId) {
this.updateUserId = updateUserId;
}
public Long getWechatInfoId() {
return wechatInfoId;
}
public void setWechatInfoId(Long wechatInfoId) {
this.wechatInfoId = wechatInfoId;
}
public Integer getReplyType() {
return replyType;
}
public void setReplyType(Integer replyType) {
this.replyType = replyType;
}
}

@ -0,0 +1,68 @@
package com.lq.wechat.mode;
/**
* @Author: qi
* @Description:
* @Date: Create in 9:28 AM 2019/3/15
*/
public class AccessToken {
/**
* 微信公众号令牌
*/
private String access_token;
/**
* 令牌过期时间
*/
private Long expires_in;
/**
* 错误码
*/
private Integer errcode;
/**
* 错误提示
*/
private String errmsg;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public Long getExpires_in() {
return expires_in;
}
public void setExpires_in(Long expires_in) {
this.expires_in = expires_in;
}
public Integer getErrcode() {
return errcode;
}
public void setErrcode(Integer errcode) {
this.errcode = errcode;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
@Override
public String toString() {
return "AccessToken{" +
"access_token='" + access_token + '\'' +
", expires_in=" + expires_in +
", errcode=" + errcode +
", errmsg='" + errmsg + '\'' +
'}';
}
}

@ -0,0 +1,18 @@
package com.lq.wechat.util;
/**
* @Author: qi
* @Description: 微信公众号-用户相关的工具类
* @Date: Create in 4:56 PM 2019/3/17
*/
public class WechatUserUtil {
/**
* 获取用户基本信息接口 (get请求)
*/
public static final String GET_USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
/**
* 批量获取用户基本信息 (post请求)
*/
public static final String BATCHGET_USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";
}

@ -0,0 +1,14 @@
package com.lq.wechat.util.accessToken;
import com.lq.entity.WechatAccessToken;
import com.lq.wechat.mode.AccessToken;
/**
* @Author: qi
* @Description: 数据结构适配器
* @Date: Create in 11:13 PM 2019/3/16
*/
public interface AccessTokenAdapter {
WechatAccessToken adapter(AccessToken accessToken);
}

@ -0,0 +1,64 @@
package com.lq.wechat.util.accessToken;
import com.alibaba.fastjson.JSONObject;
import com.lq.code.util.HttpKit;
import com.lq.dao.WechatAccesstokenDao;
import com.lq.entity.WechatAccessToken;
import com.lq.entity.WechatInfo;
import com.lq.wechat.mode.AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.UUID;
/**
* Created by qi_liang on 2018/5/31.
*/
@Component
public class AccessTokenUtil {
/**
* 微信公众号获取access_token接口
*/
public final static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
public final static String APPID ="wx76ca7130852c4baa";
public final static String APPSECRET = "d87125562b8e60618bc7b3120dfe3583";
@Autowired
private RedisTemplate<String,Object> redisTemplate;
public static String getAccessToken(String appid,String appsecret){
String url = ACCESS_TOKEN_URL.replace("APPID",appid).replace("APPSECRET",appsecret);
String resultStr = HttpKit.get(url);
return resultStr;
}
public static AccessToken getAccessToken(WechatInfo wechatInfo){
AccessToken accessToken = null;
if (wechatInfo!=null){
String result = getAccessToken(wechatInfo.getAppId(),wechatInfo.getAppSecpet());
accessToken = JSONObject.parseObject(result,AccessToken.class);
}
return accessToken;
}
public static WechatAccessToken accessTokenToWechatAccessToken(WechatInfo wechatInfo,AccessTokenAdapter accessTokenAdapter){
AccessToken accessToken = getAccessToken(wechatInfo);
return accessTokenAdapter.adapter(accessToken);
}
public static void main(String[] args) {
String result = getAccessToken(APPID,APPSECRET);
System.out.println(result);
AccessToken accessToken = JSONObject.parseObject(result,AccessToken.class);
System.out.println(accessToken);
}
}

@ -0,0 +1,31 @@
package com.lq.wechat.util.accessToken;
import com.lq.cms.emun.WechatAccessTokenTypeEnum;
import com.lq.code.util.StringUtil;
import com.lq.entity.WechatAccessToken;
import com.lq.wechat.mode.AccessToken;
import java.util.Date;
/**
* @Author: qi
* @Description:
* @Date: Create in 11:16 PM 2019/3/16
*/
public class DefaultAccessTokenAdapter implements AccessTokenAdapter{
@Override
public WechatAccessToken adapter(AccessToken accessToken) {
String accessTokenStr = accessToken.getAccess_token();
WechatAccessToken wechatAccessToken = new WechatAccessToken();
if (StringUtil.isNotNull(accessTokenStr)){
wechatAccessToken.setAccessToken(accessToken.getAccess_token());
wechatAccessToken.setCreateTime(new Date());
wechatAccessToken.setExpiresTime(accessToken.getExpires_in());
wechatAccessToken.setTokenType(WechatAccessTokenTypeEnum.CURRENCY.getValue());
}
return wechatAccessToken;
}
}
Loading…
Cancel
Save