sring boot项目集成企业微信第三方应用

集成企业微信第三方应用前,开发者应当浏览企业微信开发者官方文档。开发者文档
首先企业微信服务商需要身份认证,才能创建第三方应用

1.创建第三方应用

1)点击服务商管理后台扫码登录
2)选择 应用管理–网页应用–配置基础信息,这些可以先随便填写,创建完成应用后还可以修改sring boot项目集成企业微信第三方应用_第1张图片
sring boot项目集成企业微信第三方应用_第2张图片
sring boot项目集成企业微信第三方应用_第3张图片

2.项目接口开发

1)回调接口

@Api(tags = "企业微信回调")
@RestController
@RequestMapping("/callback/weCom")
public class WeComCallbackController {
   

    @Autowired
    IWeComService weComService;

    @Value("${domain}")
    private String domain;

    @ApiOperation("企业微信服务器配置地址,用于验证启用 应用管理-回调配置-数据回调URL")
    @GetMapping(value = "/msg")
    @ResponseBody
    public String weComMsgVerify(HttpServletRequest request) {
   
        String signature = request.getParameter("msg_signature");// 微信加密签名
        String timestamp = request.getParameter("timestamp");// 时间戳
        String nonce = request.getParameter("nonce");// 随机数
        String echostr = request.getParameter("echostr");// 随机字符串

        // 确认此次GET请求来自企业微信服务器,原样返回echostr参数内容
        return weComService.weComVerify(signature, timestamp, nonce, echostr);
    }

    @ApiOperation("企业微信服务器配置地址,用于验证启用 应用管理-回调配置-指令回调URL")
    @GetMapping(value = "/command")
    @ResponseBody
    public String weComCommandVerify(HttpServletRequest request) {
   
        String signature = request.getParameter("msg_signature");// 微信加密签名
        String timestamp = request.getParameter("timestamp");// 时间戳
        String nonce = request.getParameter("nonce");// 随机数
        String echostr = request.getParameter("echostr");// 随机字符串

        // 确认此次GET请求来自企业微信服务器,原样返回echostr参数内容
        return weComService.weComVerify(signature, timestamp, nonce, echostr);
    }

    @ApiOperation("企业微信,指令回调(刷新ticket)")
    @PostMapping(value = "/command")
    @ResponseBody
    public String weComCommand(HttpServletRequest request) throws Exception {
   
        String signature = request.getParameter("msg_signature");// 微信加密签名
        String timestamp = request.getParameter("timestamp");// 时间戳
        String nonce = request.getParameter("nonce");// 随机数
        ServletInputStream in = request.getInputStream();
        String success = weComService.weComCommand(signature, timestamp, nonce, in);
        return success;
    }

    @ApiOperation("企业微信授权")
    @GetMapping("/oauth")
    @ResponseBody
    public void oauth(HttpServletRequest request, HttpServletResponse response) throws Exception {
   
        String code = request.getParameter("code");
        String state = request.getParameter("state"); //state是自己添加的参数,可以根据场景自定义
        OauthDto oauthDto = weComService.oauth(code, state);
        String authCorpId = oauthDto.getAuthCorpId();
        String openUserId = oauthDto.getOpenUserId();
        String timestamp = System.currentTimeMillis() + "";
        String signature = SignatureUtil.getSignature(authCorpId + "_" + openUserId + "_" + timestamp, timestamp);
        //实际登录地址,或者根据场景做跳转
        String loginUrl;
        if (state.equals("weCom-web")){
   
            loginUrl = domain + "/wecom/web/weCom/autoLogin?authCorpId=";
        }else {
   
            loginUrl = domain + "/wecom/app/weCom/autoLogin?authCorpId=";
        }
        response.sendRedirect(loginUrl + authCorpId + "&openUserId=" + openUserId + "×tamp=" + timestamp + "&signature=" + signature);
    }
}

2)service方法

public interface IWeComService {
   

    /**
     * 获取第三方应用凭证
     */
    AccessTokenDto getSuiteAccessToken(String suiteTicket);

    String weComVerify(String msgSignature, String timeStamp, String nonce, String echoStr);

    String weComCommand(String msgSignature, String timeStamp, String nonce, InputStream in);

    String weComDecryptMsg(String msgSignature, String timeStamp, String nonce, String postData, ReceiveDto receive);

    OauthDto oauth(String code, String state);

    /**
     * 获取授权用户基本信息
     */
    String getWeComUserInfo(String code, String suiteAccessToken);

    /**
     * 获取授权凭证
     */
    String getAccessToken(String authCorpId);
}

3)service实现

@Slf4j
@Service
public class WeComServiceImpl implements IWeComService {
   

    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;

    @Value("${weCom.corpId}")
    private String corpId;
    @Value("${weCom.suite.suiteId}")
    private String suiteId;
    @Value("${weCom.suite.secret}")
    private String su

你可能感兴趣的:(企业微信,java,微信)