package com.jsowell.common.util; import com.github.qcloudsms.SmsSingleSender; import com.github.qcloudsms.SmsSingleSenderResult; import com.github.qcloudsms.httpclient.HTTPException; import com.jsowell.common.constant.CacheConstants; import com.jsowell.common.core.redis.RedisCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.io.IOException; /** * 发送短信验证码工具类 */ @Component public class SMSUtil { private static RedisCache redisCache; @Autowired public void setRedisCache(RedisCache redisCache) { SMSUtil.redisCache = redisCache; } // 短信应用SDK AppKey private static final String APP_KEY = "8ebcf52de98416814b440891350cd594"; // 短信模板ID,需要在短信应用中申请 private static final int TEMPLATE_ID = 1002460; // 短信应用SDK AppID 1400开头 private static final int APP_ID = 1400536771; // 签名,使用的是签名内容,而不是签名ID private static final String SMS_SIGN = "举视新能源"; // 国家码 如 86 为中国 private static final String NATION_CODE = "86"; public static String sendSMS(HttpServletRequest request, String phoneNumber) throws HTTPException, IOException { String reStr = "success"; //定义返回值 //随机生成六位验证码 String code = RandomUtil.getSMSVerificationCode(); //参数,一定要对应短信模板中的参数顺序和个数, String[] params = {code}; //创建ssender对象 SmsSingleSender ssender = new SmsSingleSender(APP_ID, APP_KEY); //发送 SmsSingleSenderResult result = ssender.sendWithParam(NATION_CODE, phoneNumber, TEMPLATE_ID, params, SMS_SIGN, "", ""); if (result.result != 0) { reStr = "error"; } else { // 改为保存redis String redisKey = CacheConstants.SMS_VERIFICATION_CODE_KEY + phoneNumber; redisCache.setCacheObject(redisKey, code, CacheConstants.cache_expire_time_10m); } return reStr; } public static void main(String[] args) { System.out.println(); } }