Search in sources :

Example 6 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException in project fw-cloud-framework by liuweijw.

the class WxJsdkController method wechatParam.

@RequestMapping(value = "/wechatParam")
@ResponseBody
public HttpResult wechatParam(@RequestParam("url") String url) {
    long start = System.currentTimeMillis();
    if (StringHelper.isBlank(url))
        return new HttpResult().failure("url 参数验证失败!");
    String jsdkUrl = WebUtils.buildURLDecoder(url);
    try {
        WxJsapiSignature jsapi = wxService.createJsapiSignature(WebUtils.buildURLDecoder(jsdkUrl));
        log.info("[微信jsdk],请求{},获取耗时{}", jsdkUrl, (System.currentTimeMillis() - start));
        return new HttpResult().data(jsapi).success();
    } catch (WxErrorException e) {
        e.printStackTrace();
    }
    return new HttpResult().failure("获取微信签名数据失败!");
}
Also used : WxJsapiSignature(me.chanjar.weixin.common.bean.WxJsapiSignature) HttpResult(com.github.liuweijw.commons.pay.beans.HttpResult) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException in project fw-cloud-framework by liuweijw.

the class WxAuthorizeController method openId.

// 如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE
@RequestMapping(value = "/openId", method = RequestMethod.GET)
public String openId(HttpServletRequest request, @RequestParam("code") String code, @RequestParam("state") String state, @RequestParam("from") Integer from, @RequestParam("wechatId") String wechatId, @RequestParam("t") Long t) {
    long start = System.currentTimeMillis();
    String openId = "";
    try {
        boolean isSopeBase = from.intValue() == 1;
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxService.oauth2getAccessToken(code);
        openId = wxMpOAuth2AccessToken.getOpenId();
        log.info("【wxauth.openId】:state|" + state);
        // 采用异步方式拉取用户信息
        // taskExecutor.execute(() -> {})
        long mqStart = System.currentTimeMillis();
        log.info("【wxauth.openId】发送MQ:" + mqStart);
        WechatNotifyBean wechatNotifyBean = new WechatNotifyBean();
        wechatNotifyBean.setSopeBase(isSopeBase);
        wechatNotifyBean.setWechatId(wechatId);
        wechatNotifyBean.setWxMpOAuth2AccessToken(wxMpOAuth2AccessToken);
        rabbitTemplate.convertAndSend(MqQueueConstant.WECHAT_QUEUE, wechatNotifyBean);
        log.info("【wxauth.openId】发送MQ耗时:" + (System.currentTimeMillis() - mqStart));
        log.info("【wxauth.openId】:openId|" + openId);
    } catch (WxErrorException ex) {
        ex.printStackTrace();
        log.info("【wxauth.openId】exception:" + ex.getError().getErrorMsg());
    }
    String returnUrl = "";
    if (state.length() == 32 && !state.startsWith("http")) {
        // key
        UrlInfoBean urlInfoBean = urlInfoService.findFromCacheByUuid(state);
        returnUrl = urlInfoBean.getUrl();
    } else {
        returnUrl = state;
    }
    String redirectUrl = RequestUtil.buildAppendURLParams(RequestUtil.buildURLParams(returnUrl, OPENID), OPENID + "=" + openId, "t=" + t);
    log.info("【wxauth.openId】:redirect|" + redirectUrl);
    long end = System.currentTimeMillis();
    log.info("【wxauth.openId】耗时:" + (end - start));
    log.info("【wxauth.authorize】请求从第三方应用到跳转授权开始[" + t + "],耗时:" + (end - t));
    return "redirect:" + redirectUrl;
}
Also used : WxMpOAuth2AccessToken(me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken) WechatNotifyBean(com.github.liuweijw.business.wechat.beans.WechatNotifyBean) UrlInfoBean(com.github.liuweijw.business.wechat.beans.UrlInfoBean) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException in project fw-cloud-framework by liuweijw.

the class WxAuthorizeController method refreshToken.

@RequestMapping(value = "/refreshToken", method = RequestMethod.GET)
public R<WxMpOAuth2AccessToken> refreshToken(HttpServletRequest request, @RequestParam("refreshToken") String refreshToken) {
    if (StringHelper.isBlank(refreshToken))
        return new R<WxMpOAuth2AccessToken>().failure("请求参数[refreshToken]不存在!");
    log.info("【wxauth】:refreshToken|" + refreshToken);
    WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
    try {
        wxMpOAuth2AccessToken = wxService.oauth2refreshAccessToken(refreshToken);
        return new R<WxMpOAuth2AccessToken>().data(wxMpOAuth2AccessToken).success();
    } catch (WxErrorException e) {
        e.printStackTrace();
    }
    return new R<WxMpOAuth2AccessToken>().failure("微信refreshToken刷新失败!");
}
Also used : WxMpOAuth2AccessToken(me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken) R(com.github.liuweijw.commons.base.R) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException in project fw-cloud-framework by liuweijw.

the class WxMessageController method sendWeixinCurrencyTemplateMessage.

@ResponseBody
@RequestMapping(value = "/sendCurrency/{wechatId}")
public HttpResult sendWeixinCurrencyTemplateMessage(@PathVariable("wechatId") String wechatId, @RequestParam("message") String message) {
    if (StringHelper.isBlank(message))
        return new HttpResult().failure("发送消息内容不能为空");
    WechatInfo wechatInfo = wechatInfoService.findByWechatId(wechatId);
    if (null == wechatInfo)
        return new HttpResult().failure("公众号wechatId[" + wechatId + "]不存在");
    log.info("公众号消息发送:|wechatId[{}]|reqKey[{}]|message[{}]", wechatId, wechatInfo.getReqKey(), message);
    String decryptMessage = Crypt.getInstance().decrypt(message, wechatInfo.getReqKey());
    if (StringHelper.isBlank(decryptMessage))
        return new HttpResult().failure("发送消息内容签名不正确");
    MsgBean msgBean = JSONObject.parseObject(decryptMessage, MsgBean.class);
    if (null == msgBean)
        return new HttpResult().failure("发送消息内容转换失败");
    try {
        boolean isOk = messageService.sendWeixinCurrencyTemplateMessage(msgBean);
        if (isOk)
            return new HttpResult().success("SUCCESS");
    } catch (WxErrorException e) {
        e.printStackTrace();
        return new HttpResult().failure("发送消息失败[" + e.getMessage() + "]");
    }
    return new HttpResult().failure("消息发送失败!");
}
Also used : WechatInfo(com.github.liuweijw.business.wechat.domain.WechatInfo) MsgBean(com.github.liuweijw.commons.pay.beans.MsgBean) HttpResult(com.github.liuweijw.commons.pay.beans.HttpResult) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with WxErrorException

use of me.chanjar.weixin.common.error.WxErrorException in project fw-cloud-framework by liuweijw.

the class WxMessageController method sendWeixinTemplateMessage.

@ResponseBody
@Deprecated
@RequestMapping(value = "/send/{wechatId}")
public HttpResult sendWeixinTemplateMessage(@PathVariable("wechatId") String wechatId, @RequestParam("message") String message) {
    if (StringHelper.isBlank(message))
        return new HttpResult().failure("发送消息内容不能为空");
    WechatInfo wechatInfo = wechatInfoService.findByWechatId(wechatId);
    if (null == wechatInfo)
        return new HttpResult().failure("公众号wechatId[" + wechatId + "]不存在");
    log.info("公众号消息发送:|wechatId[{}]|reqKey[{}]|message[{}]", wechatId, wechatInfo.getReqKey(), message);
    String decryptMessage = Crypt.getInstance().decrypt(message, wechatInfo.getReqKey());
    if (StringHelper.isBlank(decryptMessage))
        return new HttpResult().failure("发送消息内容签名不正确");
    MsgBean msgBean = JSONObject.parseObject(decryptMessage, MsgBean.class);
    if (null == msgBean)
        return new HttpResult().failure("发送消息内容转换失败");
    WxTemplateEnum wxTemplateEnum = WxTemplateEnum.of(msgBean.getTemplateId());
    try {
        boolean isOk = messageService.sendWeixinTemplateMessage(wxTemplateEnum, msgBean);
        if (isOk)
            return new HttpResult().success("SUCCESS");
    } catch (WxErrorException e) {
        e.printStackTrace();
        return new HttpResult().failure("发送消息失败[" + e.getMessage() + "]");
    }
    return new HttpResult().failure("消息发送失败!");
}
Also used : WechatInfo(com.github.liuweijw.business.wechat.domain.WechatInfo) MsgBean(com.github.liuweijw.commons.pay.beans.MsgBean) HttpResult(com.github.liuweijw.commons.pay.beans.HttpResult) WxTemplateEnum(com.github.liuweijw.commons.pay.enums.WxTemplateEnum) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WxErrorException (me.chanjar.weixin.common.error.WxErrorException)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 WxMpUser (me.chanjar.weixin.mp.bean.result.WxMpUser)4 HttpResult (com.github.liuweijw.commons.pay.beans.HttpResult)3 WxMpXmlOutMessage (me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage)3 WxMpOAuth2AccessToken (me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 TextBuilder (com.github.binarywang.demo.wx.mp.builder.TextBuilder)2 WechatInfo (com.github.liuweijw.business.wechat.domain.WechatInfo)2 MsgBean (com.github.liuweijw.commons.pay.beans.MsgBean)2 BaseWxService (com.github.binarywang.demo.wx.mp.service.BaseWxService)1 UrlInfoBean (com.github.liuweijw.business.wechat.beans.UrlInfoBean)1 WechatNotifyBean (com.github.liuweijw.business.wechat.beans.WechatNotifyBean)1 TextBuilder (com.github.liuweijw.business.wechat.builder.TextBuilder)1 AuthInfo (com.github.liuweijw.business.wechat.domain.AuthInfo)1 R (com.github.liuweijw.commons.base.R)1 WxTemplateEnum (com.github.liuweijw.commons.pay.enums.WxTemplateEnum)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 WxJsapiSignature (me.chanjar.weixin.common.bean.WxJsapiSignature)1