| | |
| | | import com.nq.utils.pay.CmcPayOuterRequestUtil; |
| | | import com.nq.utils.redis.RedisKeyConstant; |
| | | import com.nq.utils.redis.RedisShardedPoolUtils; |
| | | import okhttp3.*; |
| | | import org.apache.commons.lang3.RandomStringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | String verificationCode = RandomStringUtils.randomNumeric(6); |
| | | |
| | | try { |
| | | // 将验证码存入 Redis,设置 30 秒过期时间 |
| | | String redisKey = RedisKeyConstant.verification_code + ":" + telephone; // 使用电话号码作为唯一标识符 |
| | | RedisShardedPoolUtils.setEx(redisKey, verificationCode, 30); |
| | | } catch (Exception e) { |
| | | // 捕获异常并记录日志,避免程序崩溃 |
| | | System.err.println("Error storing verification code in Redis: " + e.getMessage()); |
| | | // 你可以根据需要做进一步的异常处理,比如重新抛出异常 |
| | | } |
| | | OkHttpClient client = new OkHttpClient(); |
| | | |
| | | MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); |
| | | RequestBody body = RequestBody.create(mediaType, "sp_id=697778&mobile="+telephone+"&content="+verificationCode+"&password=fe663ee62f83ddb353fdc31ee7c88378"); |
| | | Request request = new Request.Builder() |
| | | .url("http://47.242.44.254:9511/api/send-sms-single") |
| | | .post(body) |
| | | .addHeader("content-type", "application/x-www-form-urlencoded") |
| | | .build(); |
| | | |
| | | Response response = client.newCall(request).execute(); |
| | | if(response.isSuccessful()){ |
| | | try { |
| | | // 将验证码存入 Redis,设置 30 秒过期时间 |
| | | String redisKey = RedisKeyConstant.verification_code + ":" + telephone; // 使用电话号码作为唯一标识符 |
| | | RedisShardedPoolUtils.setEx(redisKey, verificationCode, 30); |
| | | } catch (Exception e) { |
| | | // 捕获异常并记录日志,避免程序崩溃 |
| | | System.err.println("Error storing verification code in Redis: " + e.getMessage()); |
| | | // 你可以根据需要做进一步的异常处理,比如重新抛出异常 |
| | | } |
| | | // 返回验证码 |
| | | return verificationCode; |
| | | } |
| | | } catch (Exception e) { |
| | | // 异常处理 |
| | | System.err.println("短信发送异常: " + e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | // 返回验证码 |
| | | return verificationCode; |
| | | return "000000"; |
| | | } |
| | | |
| | | |