一个问题 代码如何优化?

转账和提现都需要做一个验证

this.userInfoService.transferAndWithDraw(pointTransDTO);
UserInfoEntity entity = this.userInfoService.transferAndWithDraw(pointTocashDTO);
/**
 * 转账和提现的验证
 *
 * @return
 */
public <T> UserInfoEntity transferAndWithDraw(T tClass) {
    String userIds = "getUserId";
    String sMsCodes = "getSmsCode";
    String tradePasswords = "getTradePassword";
    Long userId = (Long) this.reflex(tClass, userIds);
    String sMsCode = (String) this.reflex(tClass, sMsCodes);
    String tradePassword = (String) this.reflex(tClass, tradePasswords);

    // 获取验证码
    UserInfoEntity entity = baseDao.selectById(userId);
    String phone = entity.getPhone();
    // 验证手机号
    DepositAndWithdrawalUtil.verifySms(phone, sMsCode, redisUtils);
    //比较密码
    if (!PasswordUtils.matches(tradePassword, entity.getTradePassword())) {
        throw new RenException("交易密码不正确!");
    }
    return entity;
}

// 反射
public <T> Object reflex(T tClass, String str) {
    Object value = null;
    try {
        Method method = tClass.getClass().getMethod(str);
        value = method.invoke(tClass);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}
posted @ 2020-11-14 16:28  yx袁祥  阅读(74)  评论(0)    收藏  举报