Skip to content

Commit

Permalink
企业微信:根据code, user_ticket获取成员信息; 下载媒体文件;获取jsapi_ticket;部门的修改、删除、查询;成员的…
Browse files Browse the repository at this point in the history
…获取、新建、删除、修改;二次验证成功;
  • Loading branch information
yfdai committed Jan 29, 2018
1 parent 4973500 commit c6bd969
Show file tree
Hide file tree
Showing 23 changed files with 977 additions and 27 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
.idea
*.iml
*.versionsBackup
pom.xml
.svn
9 changes: 5 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>cn.buk.api.wechat</groupId>
<artifactId>weniu</artifactId>
<version>1.3.8-SNAPSHOT</version>
<version>1.3.23-SNAPSHOT</version>
<packaging>pom</packaging>

<name>weniu</name>
Expand Down Expand Up @@ -35,8 +35,9 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.version>4.3.9.RELEASE</springframework.version>
<hibernate.version>5.1.7.Final</hibernate.version>
<springframework.version>4.3.12.RELEASE</springframework.version>
<hibernate.version>5.2.12.Final</hibernate.version>
<!--<hibernate.version>5.1.7.Final</hibernate.version>-->
<!--<hibernate.version>5.0.12.Final</hibernate.version>-->
<!--<hibernate.version>4.3.11.Final</hibernate.version>-->
<httpcore.version>4.4.6</httpcore.version>
Expand Down Expand Up @@ -157,7 +158,7 @@
<dependency>
<groupId>cn.buk.common</groupId>
<artifactId>buk-common-util</artifactId>
<version>1.0.17</version>
<version>1.0.21</version>
</dependency>

<!--spring -->
Expand Down
2 changes: 1 addition & 1 deletion weniu-dao/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>cn.buk.api.wechat</groupId>
<artifactId>weniu</artifactId>
<version>1.3.8-SNAPSHOT</version>
<version>1.3.23-SNAPSHOT</version>
</parent>

<artifactId>weniu-dao</artifactId>
Expand Down
10 changes: 9 additions & 1 deletion weniu-dao/src/main/java/cn/buk/api/wechat/dao/WeixinDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface WeixinDao {
*
* @return
*/
Token retrieveWeixinToken(int weixinId, int weixinType);
Token retrieveWeixinToken(int weixinId, int weixinType, int msgType);

/**
* 保存Token
Expand Down Expand Up @@ -61,4 +61,12 @@ public interface WeixinDao {
int createWxNews(WeixinNews o);

int deleteWxNews(int enterpriseId, int id);

/**
* 获取企业微信的设置参数
* @param enterpriseId 企业ID
* @param msgType 消息类型
* @return
*/
WeixinEntConfig getWeixinEntConfig(int enterpriseId, int msgType);
}
36 changes: 28 additions & 8 deletions weniu-dao/src/main/java/cn/buk/api/wechat/dao/WeixinDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public int createWeixinAccessTime(String weixinOpenId, int weixinId) {
EntityManager em = createEntityManager();
try {
List<WeixinAccessTime> accessTimes = em.createQuery("select o from WeixinAccessTime o " +
" where o.weixinOpenId = :weixinOpenId and o.weixinId = :weixinId ")
" where o.weixinOpenId = :weixinOpenId and o.weixinId = :weixinId ", WeixinAccessTime.class)
.setParameter("weixinOpenId", weixinOpenId)
.setParameter("weixinId", weixinId)
.getResultList();
Expand Down Expand Up @@ -67,14 +67,15 @@ public int createWeixinAccessTime(String weixinOpenId, int weixinId) {
}

@Override
public Token retrieveWeixinToken(final int weixinId, final int weixinType) {
public Token retrieveWeixinToken(final int weixinId, final int weixinType, final int msgType) {
EntityManager em = createEntityManager();
try {
List<Token> tokens = em.createQuery("select o from Token o " +
"where o.weixinId = :weixinId and o.weixinType = :weixinType " +
"order by o.id desc")
"where o.weixinId = :weixinId and o.weixinType = :weixinType and o.msgType = :msgType " +
"order by o.id desc", Token.class)
.setParameter("weixinId", weixinId)
.setParameter("weixinType", weixinType)
.setParameter("msgType", msgType)
.setMaxResults(1)
.getResultList();
if (tokens == null || tokens.size() == 0)
Expand Down Expand Up @@ -160,7 +161,7 @@ public WeixinUser searchWeixinUser(int enterpriseId, String openId) {
EntityManager em = createEntityManager();
try {
List<WeixinUser> users = em.createQuery("select o from WeixinUser o " +
"where o.ownerId = :enterpriseId and o.weixinOpenId = :openId")
"where o.ownerId = :enterpriseId and o.weixinOpenId = :openId", WeixinUser.class)
.setParameter("enterpriseId",enterpriseId)
.setParameter("openId", openId)
.getResultList();
Expand All @@ -183,7 +184,7 @@ public WeixinTemplate searchWeixinTemplate(int ownerId, String id) {
EntityManager em = createEntityManager();
try {
List<WeixinTemplate> list = em.createQuery("select o from WeixinTemplate o " +
"where o.ownerId = :ownerId and o.businessId = :id")
"where o.ownerId = :ownerId and o.businessId = :id", WeixinTemplate.class)
.setParameter("ownerId",ownerId)
.setParameter("id", id)
.getResultList();
Expand All @@ -200,7 +201,7 @@ public List<WeixinTemplate> searchWeixinTemplates(int ownerId) {
EntityManager em = createEntityManager();
try {
List<WeixinTemplate> list = em.createQuery("select o from WeixinTemplate o " +
"where o.ownerId = :ownerId ")
"where o.ownerId = :ownerId ", WeixinTemplate.class)
.setParameter("ownerId",ownerId)
.getResultList();

Expand All @@ -216,7 +217,7 @@ public List<WeixinCustomMenu> searchCustomMenus(int ownerId) {
EntityManager em = createEntityManager();
try {
return em.createQuery("select o from WeixinCustomMenu o " +
"where o.enterpriseId = :enterpriseId order by o.level, o.parentId, o.id")
"where o.enterpriseId = :enterpriseId order by o.level, o.parentId, o.id", WeixinCustomMenu.class)
.setParameter("enterpriseId", ownerId)
.getResultList();

Expand Down Expand Up @@ -408,4 +409,23 @@ public int deleteWxNews(int enterpriseId, int id) {
return retCode;
}

@Override
public WeixinEntConfig getWeixinEntConfig(int enterpriseId, int msgType) {
EntityManager em = createEntityManager();
try {
List<WeixinEntConfig> list = em.createQuery("select o from WeixinEntConfig o " +
" where o.enterpriseId = :enterpriseId and o.msgType = :msgType", WeixinEntConfig.class)
.setParameter("enterpriseId", enterpriseId)
.setParameter("msgType", msgType)
.getResultList();

if (list == null || list.size() == 0)
return null;
else
return list.get(0);
} finally {
em.close();
}
}

}
14 changes: 13 additions & 1 deletion weniu-dao/src/main/java/cn/buk/api/wechat/entity/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
public class Token {

public static final int WEIXIN_SERVICE_TOKEN = 0; // 微信服务号 token
public static final int WEIXIN_ENT_TOKEN = 8; // 微信企业号 token
public static final int WORK_WEIXIN_TOKEN = 8; // 企业微信 token

public static final int WEIXIN_JS_SDK_TICKET = 100; // 微信 js-sdk ticket
public static final int WORK_WEIXIN_JSAPI_TICKET = 108; // 企业微信 js-sdk ticket

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -30,6 +31,9 @@ public class Token {
@Column(name = "weixin_type")
private int weixinType;

@Column(name = "msg_type")
private Integer msgType = 0;

@Column(length=512)
private String access_token;

Expand Down Expand Up @@ -86,4 +90,12 @@ public int getWeixinType() {
public void setWeixinType(int weixinType) {
this.weixinType = weixinType;
}

public Integer getMsgType() {
return msgType == null ? 0 : msgType;
}

public void setMsgType(Integer msgType) {
this.msgType = msgType;
}
}
119 changes: 119 additions & 0 deletions weniu-dao/src/main/java/cn/buk/api/wechat/entity/WeixinEntConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package cn.buk.api.wechat.entity;

import javax.persistence.*;
import java.util.Date;

/**
* Created by yfdai on 16/7/9.
*
* 微信企业号设置
*/
@Entity
@Table(name = "weixin_ent_config", uniqueConstraints = @UniqueConstraint(name = "uk_weixin_ent_config", columnNames = {"enterprise_id", "msg_type"}))
public class WeixinEntConfig {

/**
* 企业微信的默认自建应用
*/
public static final int WORK_WX_DEFAULT = 0;
/**
* 企业微信的通讯录API
*/
public static final int WORK_WX_CONTACTS = 1;


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "enterprise_id")
private int enterpriseId;

/**
* 消息类型
* 0 - 默认的企业微信自定义应用
* 1 - 企业微信的通讯录API
*/
@Column(name = "msg_type")
private int msgType;

@Column(name = "corp_id")
private String corpId;

private String secret;

@Column(name = "agent_id")
private int agentId;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "create_time", insertable = false, updatable = false)
private Date createTime;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_update", insertable = false)
private Date lastUpdate;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getEnterpriseId() {
return enterpriseId;
}

public void setEnterpriseId(int enterpriseId) {
this.enterpriseId = enterpriseId;
}

public int getMsgType() {
return msgType;
}

public void setMsgType(int msgType) {
this.msgType = msgType;
}

public String getCorpId() {
return corpId;
}

public void setCorpId(String corpId) {
this.corpId = corpId;
}

public String getSecret() {
return secret;
}

public void setSecret(String secret) {
this.secret = secret;
}

public int getAgentId() {
return agentId;
}

public void setAgentId(int agentId) {
this.agentId = agentId;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public Date getLastUpdate() {
return lastUpdate;
}

public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class WeixinNews {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "enterprise_id")
Expand Down
2 changes: 1 addition & 1 deletion weniu-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>cn.buk.api.wechat</groupId>
<artifactId>weniu</artifactId>
<version>1.3.8-SNAPSHOT</version>
<version>1.3.23-SNAPSHOT</version>
</parent>

<name>weniu-service</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
public interface WeixinService {

// void testImgUrl();

int getWeixinId();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -42,7 +41,6 @@
/**
* Created by yfdai on 2017/2/6.
*/
@Component
public class WeixinServiceImpl implements WeixinService {

private static Logger logger = Logger.getLogger(WeixinServiceImpl.class);
Expand Down Expand Up @@ -445,7 +443,7 @@ public String createCustomMenu() {


private synchronized Token getToken() {
Token token = weixinDao.retrieveWeixinToken(this.weixinId, Token.WEIXIN_SERVICE_TOKEN);
Token token = weixinDao.retrieveWeixinToken(this.weixinId, Token.WEIXIN_SERVICE_TOKEN, 0);

if (token == null || DateUtil.getPastSeconds(token.getCreateTime()) >= token.getExpires_in()) {
token = refreshWeixinToken();
Expand All @@ -458,7 +456,7 @@ private synchronized Token getToken() {
* 获取js-sdk ticket, 可刷新
*/
private synchronized Token getJsSdkTicket() {
Token token = weixinDao.retrieveWeixinToken(this.weixinId, Token.WEIXIN_JS_SDK_TICKET);
Token token = weixinDao.retrieveWeixinToken(this.weixinId, Token.WEIXIN_JS_SDK_TICKET, 0);

if (token == null || DateUtil.getPastSeconds(token.getCreateTime()) >= token.getExpires_in()) {
token = refreshWeixinJsSdkTicket();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static Map<String, String> sign(String jsapi_ticket, String url) {
"&noncestr=" + nonce_str +
"&timestamp=" + timestamp +
"&url=" + url;
System.out.println(string1);
// System.out.println(string1);

try
{
Expand Down
Loading

0 comments on commit c6bd969

Please sign in to comment.