|
|
@@ -0,0 +1,144 @@
|
|
|
+package com.ruoyi.common.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.crypto.digest.MD5;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.ruoyi.common.constant.Constants;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.redis.RedisUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class SmsFeiXinUtil {
|
|
|
+
|
|
|
+ @Value("${feixin.appkey}")
|
|
|
+ private String appkey;
|
|
|
+
|
|
|
+ @Value("${feixin.appsecret}")
|
|
|
+ private String appsecret;
|
|
|
+
|
|
|
+ @Value("${feixin.appcode}")
|
|
|
+ private String appcode;
|
|
|
+
|
|
|
+ @Value("${sms.appListing.phone}")
|
|
|
+ private String appListingPhone;
|
|
|
+
|
|
|
+ @Value("${sms.appListing.code}")
|
|
|
+ private String appListingCode;
|
|
|
+
|
|
|
+ private String queryMoney = "http://api.feixinyun.com:9090/sms/balance/v1";
|
|
|
+
|
|
|
+ private String sendUrl = "http://api.feixinyun.com:9090/sms/batch/v1";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成sign
|
|
|
+ *
|
|
|
+ * @param timestamp
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String generateSign(String appkey, String appsecret, Long timestamp) {
|
|
|
+ return MD5.create().digestHex(appkey + appsecret + timestamp);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取账户余额
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Integer queryFeiXinMoney() {
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("appkey", appkey);
|
|
|
+ params.put("appcode", appcode);
|
|
|
+ params.put("sign", generateSign(appkey, appsecret, timestamp));
|
|
|
+ params.put("timestamp", timestamp);
|
|
|
+ String resultStr = HttpUtil.get(queryMoney, params);
|
|
|
+
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(resultStr);
|
|
|
+ String code = resultJson.getString("code");
|
|
|
+ if (StrUtil.isNotEmpty(code)) {
|
|
|
+ throw new ServiceException(resultJson.getString("desc"), 900001);
|
|
|
+ }
|
|
|
+ return resultJson.getInteger("balance");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 短信发送
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean sendSmsCode(String phone, String code) {
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("appkey", appkey);
|
|
|
+ params.put("appcode", appcode);
|
|
|
+ params.put("sign", generateSign(appkey, appsecret, timestamp));
|
|
|
+ params.put("timestamp", timestamp);
|
|
|
+ params.put("phone", phone);
|
|
|
+ String content = String.format("【宜格服务】验证码:%s,请尽快返回完成验证。如非本人使用,敬请忽略本信息。", code);
|
|
|
+ params.put("msg", content);
|
|
|
+ String resultStr = HttpUtil.post(sendUrl, params.toString());
|
|
|
+ System.out.println(" -----> " + resultStr);
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(resultStr);
|
|
|
+ String resuldCode = resultJson.getString("code");
|
|
|
+ if (resuldCode.equals("00000")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ throw new ServiceException(resultJson.getString("desc"), 90002);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检验验证码是否正确
|
|
|
+ *
|
|
|
+ * @param phone
|
|
|
+ * @param codeOld
|
|
|
+ */
|
|
|
+ public void checkSmsCode(String phone, String codeOld) {
|
|
|
+ //return;
|
|
|
+ String code;
|
|
|
+ //如果手机号是18688888888,则验证码是1024
|
|
|
+ if (appListingPhone.equals(phone)) {
|
|
|
+ code = appListingCode;
|
|
|
+ } else {
|
|
|
+ //获取验证码
|
|
|
+ code = RedisUtils.getCacheObject(Constants.FX_SMS_CAPTCHA_CODE_KEY + phone);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isEmpty(code)) {
|
|
|
+ throw new ServiceException("验证码已过期");
|
|
|
+ } else {
|
|
|
+ if (!code.equals(codeOld)) {
|
|
|
+ throw new ServiceException("验证码不正确");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String appkey = "j18Ldd";
|
|
|
+ String appcode = "1000";
|
|
|
+ String appsecret = "h5TjYE";
|
|
|
+ String phone = "15014059100";
|
|
|
+ String code = "9999";
|
|
|
+ String content = String.format("【宜格服务】验证码:%s,请尽快返回完成验证。如非本人使用,敬请忽略本信息。", code);
|
|
|
+ String sendUrl = "http://api.feixinyun.com:9090/sms/batch/v1";
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("appkey", appkey);
|
|
|
+ params.put("appcode", appcode);
|
|
|
+ params.put("sign", MD5.create().digestHex(appkey + appsecret + timestamp));
|
|
|
+ params.put("timestamp", timestamp);
|
|
|
+ params.put("phone", phone);
|
|
|
+ params.put("msg", content);
|
|
|
+ String resultStr = HttpUtil.post(sendUrl, params.toString());
|
|
|
+ System.out.println(" -----> " + resultStr);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|