SmsServer.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Servers\Common;
  3. use End01here\EasySms\EasySmsService;
  4. /**
  5. * 短信发送类
  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. * @return bool
  65. */
  66. function sendPayNotice($phone_num)
  67. {
  68. try {
  69. $phone = EasySmsService::getPhoneSever($phone_num);
  70. $message_server = EasySmsService::getMessageServer();
  71. $content = '您好,官网有新的留言,请您及时查看。';
  72. $message_server->setContent($content);
  73. $send_info = EasySmsService::getSmsServer()->creatQybSms()->send($phone, $message_server);
  74. }catch (\Exception $e){
  75. $this->errorMsg=$e->getMessage();
  76. return false;
  77. }
  78. if (!empty($send_info['code']) && $send_info['code'] != 1) {
  79. $this->errorMsg = '短信发送失败';
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * 验证码验证
  86. * @param $phone_num
  87. * @param $code
  88. * @param $send_type
  89. * @return bool
  90. */
  91. public function verifyCode($phone_num, $code,$send_type='default')
  92. {
  93. $phone = EasySmsService::getPhoneSever($phone_num);
  94. $code_sever= EasySmsService::getCodeSever($phone);
  95. $ret_code = $code_sever->verifyCode($code,$send_type);
  96. $this->errorMsg=$code_sever->getErrorMsg();
  97. return $ret_code;
  98. }
  99. }