SmsServer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Servers\System;
  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. $wxCode = CommonServer::creatServer()->filtrationStr('wxCode');
  44. if(empty($wxCode)){
  45. $this->errorMsg = '缺少验证信息';
  46. return false;
  47. }
  48. // $openInfo = WeixinServer::creatServer()->getAppletsOpenId($wxCode);
  49. $openInfo = [];
  50. if(!isset($openInfo['openid'])){
  51. $this->errorMsg = '授权验证失败';
  52. return false;
  53. }
  54. $send_type='default';
  55. try {
  56. $phone = EasySmsService::getPhoneSever($phone_num);
  57. $code_server = EasySmsService::getCodeSever($phone)->setCode($send_type);
  58. $message_server = EasySmsService::getMessageServer();
  59. $content = '尊敬的客户,验证码为'.$code_server->getCode().',请妥善保管,10分钟内有效!' ;
  60. $message_server->setContent($content);
  61. $send_info = EasySmsService::getSmsServer()->creatQybSms()->send($phone, $message_server);
  62. }catch (\Exception $e){
  63. $this->errorMsg=$e->getMessage();
  64. return false;
  65. }
  66. if (!empty($send_info['code']) && $send_info['code'] != 1) {
  67. $this->errorMsg = '短信发送失败';
  68. return false;
  69. }
  70. return true;
  71. }
  72. /**
  73. * 发送短信模板内容
  74. * @param $phone_num
  75. * @param $content
  76. * @return bool
  77. */
  78. function sendContent($phone_num,$content)
  79. {
  80. try {
  81. $phone = EasySmsService::getPhoneSever($phone_num);
  82. $message_server = EasySmsService::getMessageServer();
  83. $message_server->setContent($content);
  84. $send_info = EasySmsService::getSmsServer()->creatQybSms()->send($phone, $message_server);
  85. }catch (\Exception $e){
  86. $this->errorMsg=$e->getMessage();
  87. return false;
  88. }
  89. if (!empty($send_info['code']) && $send_info['code'] != 1) {
  90. $this->errorMsg = '短信发送失败';
  91. return false;
  92. }
  93. return true;
  94. }
  95. /**
  96. * 验证码验证
  97. * @param $phone_num
  98. * @param $code
  99. * @param $send_type
  100. * @return bool
  101. */
  102. public function verifyCode($phone_num, $code,$send_type='default')
  103. {
  104. $send_type='default';
  105. $phone = EasySmsService::getPhoneSever($phone_num);
  106. $code_sever= EasySmsService::getCodeSever($phone);
  107. $ret_code = $code_sever->verifyCode($code,$send_type);
  108. $this->errorMsg=$code_sever->getErrorMsg();
  109. return $ret_code;
  110. }
  111. }