Search in sources :

Example 16 with BusinessException

use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.

the class SessionFormTokenProxy method around.

// 环绕拦截
@Around(value = "@annotation(com.chao.cloud.common.extra.token.annotation.FormToken)")
public Object around(ProceedingJoinPoint pdj) throws Exception {
    Object obj = null;
    HttpServletRequest request = getRequest();
    log.info("[token]----start----------");
    Method method = getMethod(pdj);
    FormToken annotation = method.getAnnotation(FormToken.class);
    String token = null;
    if (annotation != null) {
        boolean needSaveSession = annotation.save();
        // 添加token
        if (needSaveSession) {
            request.getSession(true).setAttribute(FROM_TOKEN_KEY, IdUtil.fastUUID());
        }
        boolean needRemoveSession = annotation.remove();
        // 删除token
        if (needRemoveSession) {
            boolean isRepeat = isRepeatSubmit(request);
            // 获取token
            token = (String) request.getSession(true).getAttribute(FROM_TOKEN_KEY);
            request.getSession(true).removeAttribute(FROM_TOKEN_KEY);
            if (isRepeat) {
                log.warn("请不要重复提交,url:{}", request.getServletPath());
                throw new BusinessException("请不要重复提交");
            }
        }
    }
    // 执行
    try {
        obj = pdj.proceed();
    } catch (Throwable e) {
        if (e instanceof ValidationException && StrUtil.isNotBlank(token) && annotation.remove()) {
            // 参数校验回退
            request.getSession(true).setAttribute(FROM_TOKEN_KEY, token);
        }
        throw new BusinessException(ExceptionUtil.getMessage(e));
    }
    log.info("[token]------end----------");
    return obj;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.chao.cloud.common.exception.BusinessException) ValidationException(javax.validation.ValidationException) FormToken(com.chao.cloud.common.extra.token.annotation.FormToken) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Example 17 with BusinessException

use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.

the class TxSeataImportSelector method selectImports.

@Override
public String[] selectImports(AnnotationMetadata metadata) {
    Class<Object> className = ClassUtil.loadClass(metadata.getClassName());
    // 排除 DataSourceAutoConfiguration 自动注入
    SpringBootApplication boot = AnnotationUtil.getAnnotation(className, SpringBootApplication.class);
    // 修改值
    Class<?>[] exclude = boot.exclude();
    if (!ArrayUtil.contains(exclude, DataSourceAutoConfiguration.class)) {
        Class<? extends Object>[] excludeClass = ArrayUtil.append(exclude, DataSourceAutoConfiguration.class);
        EntityUtil.putAnnotationValue(boot, "exclude", excludeClass);
    }
    // 获取注解值
    EnableTxSeata txSeata = AnnotationUtil.getAnnotation(className, EnableTxSeata.class);
    // 加载相关配置
    ConfigType type = txSeata.value();
    switch(type) {
        case // 0.7.1 只支持nacos
        Nacos:
            // 整合seata-nacos
            Properties properties = this.getNacosProp();
            // 获取配置资源
            try {
                ConfigService configService = NacosFactory.createConfigService(properties);
                // NacosConfiguration
                EntityUtil.setPrivateFinalField(NacosConfiguration.class, "configService", configService);
                // 
                NacosConfiguration configuration = NacosConfiguration.getInstance();
                // ConfigurationFactory
                EntityUtil.setPrivateFinalField(ConfigurationFactory.class, "instance", configuration);
                // 修改注册-nacos
                // EntityUtil.setPrivateFinalField(ConfigurationFactory.class,
                // "DEFAULT_FILE_INSTANCE", configuration);
                EntityUtil.setPrivateFinalField(ConfigurationFactory.class, "CURRENT_FILE_INSTANCE", configuration);
                // 修改服务发现
                NamingService namingService = NamingFactory.createNamingService(properties);
                EntityUtil.setPrivateFinalField(NacosRegistryServiceImpl.class, "naming", namingService);
            } catch (Exception e) {
                log.error("{}", e);
                throw new BusinessException("Nacos-Seata 初始化失败");
            }
            return new String[] { TxSeataConfig.class.getName() };
        default:
            break;
    }
    return null;
}
Also used : Properties(java.util.Properties) DataSourceAutoConfiguration(org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration) BusinessException(com.chao.cloud.common.exception.BusinessException) SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) NacosConfiguration(io.seata.config.nacos.NacosConfiguration) BusinessException(com.chao.cloud.common.exception.BusinessException) ConfigService(com.alibaba.nacos.api.config.ConfigService) NamingService(com.alibaba.nacos.api.naming.NamingService) ConfigType(io.seata.config.ConfigType)

Example 18 with BusinessException

use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.

the class WxAppUserInfoServiceImpl method getUserInfo.

@Override
public WxMaUserInfo getUserInfo(String encryptedData, String iv, String code) {
    try {
        // 获取session_key
        log.info("[encryptedData={}]", encryptedData);
        log.info("[iv={}]", iv);
        WxMaJscode2SessionResult sessionInfo = wxMaUserService.getSessionInfo(code);
        log.info("[sessionInfo={}]", sessionInfo);
        // 获取用户信息
        return wxMaUserService.getUserInfo(sessionInfo.getSessionKey(), getEncryptedData(encryptedData), iv);
    } catch (WxErrorException e) {
        WxError error = e.getError();
        throw new BusinessException(error.getErrorMsg());
    }
}
Also used : WxError(me.chanjar.weixin.common.error.WxError) BusinessException(com.chao.cloud.common.exception.BusinessException) WxMaJscode2SessionResult(cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Example 19 with BusinessException

use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.

the class WxAppUserInfoServiceImpl method getPhoneNoInfo.

@Override
public WxMaPhoneNumberInfo getPhoneNoInfo(String encryptedData, String iv, String code) {
    WxMaJscode2SessionResult sessionInfo = null;
    try {
        // 获取session_key
        sessionInfo = wxMaUserService.getSessionInfo(code);
        log.info("【获取手机号】[SessionKey={}]", sessionInfo.getSessionKey());
        log.info("【获取手机号】[iv={}]", iv);
        log.info("【获取手机号】[encryptedData={}]", encryptedData);
        // 获取用户信息
        return wxMaUserService.getPhoneNoInfo(sessionInfo.getSessionKey(), getEncryptedData(encryptedData), iv);
    } catch (WxErrorException e) {
        log.error("【获取手机号失败】[sessionInfo={}]", sessionInfo);
        log.error("【获取手机号失败】[iv={}]", iv);
        log.error("【获取手机号失败】[encryptedData={}]", encryptedData);
        WxError error = e.getError();
        throw new BusinessException(error.getErrorMsg());
    }
}
Also used : WxError(me.chanjar.weixin.common.error.WxError) BusinessException(com.chao.cloud.common.exception.BusinessException) WxMaJscode2SessionResult(cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Aggregations

BusinessException (com.chao.cloud.common.exception.BusinessException)19 Method (java.lang.reflect.Method)5 InputStream (java.io.InputStream)4 Around (org.aspectj.lang.annotation.Around)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 WxMaJscode2SessionResult (cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 WxError (me.chanjar.weixin.common.error.WxError)2 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)2 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)2 JSONObject (org.json.JSONObject)2 CollUtil (cn.hutool.core.collection.CollUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 EmojiUtil (cn.hutool.extra.emoji.EmojiUtil)1 JSONObject (cn.hutool.json.JSONObject)1 JSONUtil (cn.hutool.json.JSONUtil)1 ConfigService (com.alibaba.nacos.api.config.ConfigService)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 BaseProxy (com.chao.cloud.common.base.BaseProxy)1 Response (com.chao.cloud.common.entity.Response)1