MSUtil.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package cn.iselab.mooctest.user.util;
  2. import cn.iselab.mooctest.user.constants.VerifyConstants;
  3. import com.cloopen.rest.sdk.CCPRestSmsSDK;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.util.HashMap;
  7. /**
  8. * @Author: xuexb
  9. * @Date: 2018.12.28 13:02
  10. */
  11. public class MSUtil {
  12. private static final Logger LOG = LoggerFactory.getLogger(MSUtil.class);
  13. /**
  14. * @param receiver
  15. * @return null if failed vericode if success
  16. */
  17. public static String sendSmsVerification(String receiver, String veriCode) {
  18. // send short message by invoking SDK
  19. HashMap<String, Object> result = sendSMSSDK(receiver, VerifyConstants.SMS_VERI_TEMPLATE_ID, veriCode, "10");
  20. // Check return result to see if process success
  21. if ("000000".equals(result.get("statusCode"))) {
  22. // success
  23. return (String)result.get("statusCode");
  24. } else {
  25. // fail, output stats code and message
  26. LOG.error("[SMS] Sending short message to " + receiver + " failed.");
  27. LOG.error("[SMS] SMS Error code=" + result.get("statusCode") + " with information= "
  28. + result.get("statusMsg"));
  29. return (String)result.get("statusCode");
  30. }
  31. }
  32. private static HashMap<String, Object> sendSMSSDK(String receiver, String templateID, String... args) {
  33. HashMap<String, Object> result = null;
  34. // Initialize SDK
  35. CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
  36. // Initialize remote server and port
  37. // * Sandbox: restAPI.init("sandboxapp.cloopen.com", "8883");
  38. // * Production: restAPI.init("app.cloopen.com", "8883");
  39. restAPI.init("app.cloopen.com", "8883");
  40. // Initialize ACCOUNT SID & AUTH TOKEN
  41. // * Can be found by viewing developer's account in `Console` on
  42. // * website(http://www.yuntongxun.com)
  43. restAPI.setAccount("aaf98f89521b91a301522abe22af1760", "356fff5595a14e32a2bc2856cc0f0f5a");
  44. // Initialize Target
  45. // * Can be found in Console:
  46. // * "Application - application list - specific app"
  47. restAPI.setAppId("8a48b551522ff931015234f9a0c10e53");
  48. // Send SMS
  49. // * @Parameters
  50. // * @Parameter(0)
  51. // * phone number of receiver(split by ',', can support at most 100
  52. // numbers)
  53. // * @Parameter(1)
  54. // * template id(by default 1), can be found in `Console`
  55. // * @Parameter(2)
  56. // * An array consists of {verify_code, timeout}
  57. result = restAPI.sendTemplateSMS(receiver, templateID, args);
  58. LOG.error("[SMS] SMS Verification result=" + result);
  59. return result;
  60. }
  61. public static String generateRandomVeriCode() {
  62. int veriCode = (int) Math.round(100000 + Math.random() * 899999);
  63. return String.valueOf(veriCode);
  64. }
  65. }