SmsServer.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Servers;
  3. use End01here\EasySms\EasySmsService;
  4. /**
  5. * Redis数据缓存类
  6. */
  7. class SmsServer
  8. {
  9. /**
  10. * 错误信息
  11. * @var string
  12. */
  13. private $errorMsg = '';
  14. private function __construct()
  15. {
  16. }
  17. /**
  18. * 创建对象
  19. * @return SmsServer
  20. */
  21. static function creatServer()
  22. {
  23. return new SmsServer();
  24. }
  25. /**
  26. * @return string
  27. */
  28. public function getErrorMsg(): string
  29. {
  30. return $this->errorMsg;
  31. }
  32. /**
  33. * 发送短信
  34. * @param $phone_num
  35. * @param $send_type
  36. * @return bool
  37. * @throws \End01here\EasySms\Exceptions\CodeErrorException
  38. * @throws \End01here\EasySms\Exceptions\GatewayErrorException
  39. * @throws \End01here\EasySms\Exceptions\MessageException
  40. */
  41. function sendCode($phone_num,$send_type='default')
  42. {
  43. try {
  44. $phone = EasySmsService::getPhoneSever($phone_num);
  45. $code_server = EasySmsService::getCodeSever($phone)->setCode($send_type);
  46. $message_server = EasySmsService::getMessageServer();
  47. $content = '您好,你的验证码:' . $code_server->getCode();
  48. // $content = '您好,你的验证码:[var] ' ;
  49. $message_server->setContent($content);
  50. $send_info = EasySmsService::getSmsServer()->creatQybSms()->send($phone, $message_server);
  51. }catch (\Exception $e){
  52. $this->errorMsg=$e->getMessage();
  53. return false;
  54. }
  55. if (!empty($send_info['code']) && $send_info['code'] != 1) {
  56. $this->errorMsg = '短信发送失败';
  57. return false;
  58. }
  59. return true;
  60. }
  61. /**
  62. * 验证码验证
  63. * @param $phone_num
  64. * @param $code
  65. * @param $send_type
  66. * @return bool
  67. */
  68. public function verifyCode($phone_num, $code,$send_type='default')
  69. {
  70. $phone = EasySmsService::getPhoneSever($phone_num);
  71. $code_sever= EasySmsService::getCodeSever($phone);
  72. $ret_code = $code_sever->verifyCode($code,$send_type);
  73. $this->errorMsg=$code_sever->getErrorMsg();
  74. return $ret_code;
  75. }
  76. }