Bootstrap

【企业微信对接】常用接口

【注】基本参数

参考地址(官方文档)

  • 获取token:https://developer.work.weixin.qq.com/document/path/91039
  • 手机号获取userid:https://developer.work.weixin.qq.com/document/path/95402
  • 短信通知:https://elinkuat.spic.com.cn/api/doc#10167/%E6%96%87%E6%9C%AC%E6%B6%88%E6%81%AF

企业ID

在这里插入图片描述

应用ID和应用Secret

在这里插入图片描述

一、获取Token

1、准备

@Data
public class VxAccessTokenResult {
    private String errcode;
    private String errmsg;
    private String access_token;
    private Integer expires_in;
}

2、正式代码

    // http请求调用的是Hutool
    import cn.hutool.http.HttpUtil;
    import com.alibaba.fastjson.JSONObject;

    public VxAccessTokenResult getAccessToken() {
        VxAccessTokenResult accessToken = null;
        String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CorpID&corpsecret=SECRET"
            // 企业ID,上面【基本参数】
            .replace("CorpID", corpId)
            // 应用Secret,上面【基本参数】
            .replace("SECRET", clientSecret);
        String res = HttpUtil.get(requestUrl);
        if (StringUtils.isNotBlank(res)) {
            accessToken = JSONObject.parseObject(res, VxAccessTokenResult.class);
        }
        return accessToken;
    }

二、根据手机号码查企业微信用户ID

1、准备

@Data
public class VxUserIdResult extends VxResult {
    private String errcode;
    private String errmsg;
    private String userid;
}

2、正式代码

    // http请求调用的是Hutool
    import cn.hutool.http.HttpUtil;
    import com.alibaba.fastjson.JSONObject;

    public String getUserIdByMobile(String mobile){
        // 上面【获取Token】接口,可以另外存起来,不用每次都调新的
        String token = this.getAccessToken();
        String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=ACCESS_TOKEN"
            .replace("ACCESS_TOKEN", token);
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        String res = HttpUtil.post(requestUrl, JSONObject.toJSONString(map));
        if (StringUtils.isNotBlank(res)) {
            VxUserIdResult vxUserIdResult = JSONObject.parseObject(res, VxUserIdResult.class);
            if (vxUserIdResult != null && "0".equals(vxUserIdResult.getErrcode())) {
                return vxUserIdResult.getUserid();
            }
        }
        return null;
    }

三、信息推送(文本Text推送为例)

1、准备

@Data
public class VxResult {
    private String errcode;
    private String errmsg;
}

@Data
public class VxMessageBase {
    private String touser;
    private String toparty;
    private String totag;
    private String msgtype;
    private int agentid;
    private String safe;
}

@Data
public class TextEntity {
    private String content;
}

@Data
public class Text extends VxMessageBase {
    private TextEntity text;
}

@Getter
public enum VxMsgType {
    TEXT("text"),
    TEXT_CARD("textcard");
    private final String type;
    VxMsgType(String type) {
        this.type = type;
    }
}

2、正式代码

    // http请求调用的是Hutool
    import cn.hutool.http.HttpUtil;
    import com.alibaba.fastjson.JSONObject;

    public VxResult sendText(String mobile, String content){
        // 上面【根据手机号码查企业微信用户ID】接口
        String thirdUserId = getUserIdByMobile(mobile);
        return this.sendText(thirdUserId, content);
    }
    
    public VxResult sendText(String thirdUserId, String content){
        JSONObject body = this.notifyBody(thirdUserId, content);
        return this.send(body);
    }
    /**
     * http请求的body内容
     */
    private JSONObject notifyBody(String thirdUserId, String content){
        VxMessageBase baseMessage = this.baseMessage(thirdUserId, VxMsgType.TEXT);
        Text text = new Text();
        BeanUtil.copyProperties(baseMessage, text);
        TextEntity entity = new TextEntity();
        entity.setContent(content);
        text.setText(entity);
        return new JSONObject(BeanUtil.beanToMap(text));
    }
    /**
     * 基础消息信息,主要为了后续添加企业微信另外的推送方式,如卡片textcard
     */
    public VxMessageBase baseMessage(String thirdUserId, VxMsgType type){
        VxMessageBase result = new VxMessageBase();
        result.setMsgtype(type.getType());
        result.setTouser(thirdUserId);
        // 应用ID,上面【基本参数】
        result.setAgentid(Integer.parseInt(agentId));
        return result;
    }
    /**
     * 【发送】
     */
    public VxResult send(JSONObject body){
        // 上面【获取Token】接口,可以另外存起来,不用每次都调新的
        String token = this.getAccessToken();
        String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN"
            .replace("ACCESS_TOKEN", token);
        String postResult = HttpUtil.post(requestUrl, body.toJSONString());
        if (StringUtils.isBlank(postResult)) {
            return null;
        }
        return JSONObject.parseObject(postResult, VxResult.class);
    }

3、另外方式:文本卡片Textcard

【注】配合官方文档,举一反三

@Data
public class TextCard extends VxMessageBase {
    private TextCardEntity textcard;
}

@Data
public class TextCardEntity {
    private String title;
    private String description;
    private String url;
}
    // 参照【文本Text推送】,替换掉notifyBody
    private JSONObject notifyBody(String mobile, String title, String description, String url){
        VxMessageBase baseMessage = this.baseMessage(mobile, VxMsgType.TEXT_CARD);
        TextCard textCard = new TextCard();
        BeanUtil.copyProperties(baseMessage, textCard);
        TextCardEntity textCardEntity = new TextCardEntity();
        textCardEntity.setTitle(title);
        textCardEntity.setDescription(description);
        textCardEntity.setUrl(appUrl + url);
        textCard.setTextcard(textCardEntity);
        return new JSONObject(BeanUtil.beanToMap(textCard));
    }

注:企业微信通知建议

建议1:冗余记录企业微信用户id、企业微信电话字段,别和系统用户表电话混用
原因:一个人可能存在多个电话
建议2:通知时填企业用户ID时,优先级:企业微信用户ID>企业微信电话>系统用户表电话;既直接填写优先企业微信电话查询用户ID,企业微信电话查询用户ID优先系统用户表电话查询用户ID

;