1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Servers;
- use End01here\EasySms\EasySmsService;
- /**
- * Redis数据缓存类
- */
- class SmsServer
- {
- /**
- * 错误信息
- * @var string
- */
- private $errorMsg = '';
- private function __construct()
- {
- }
- /**
- * 创建对象
- * @return SmsServer
- */
- static function creatServer()
- {
- return new SmsServer();
- }
- /**
- * @return string
- */
- public function getErrorMsg(): string
- {
- return $this->errorMsg;
- }
- /**
- * 发送短信
- * @param $phone_num
- * @param $send_type
- * @return bool
- * @throws \End01here\EasySms\Exceptions\CodeErrorException
- * @throws \End01here\EasySms\Exceptions\GatewayErrorException
- * @throws \End01here\EasySms\Exceptions\MessageException
- */
- function sendCode($phone_num,$send_type='default')
- {
- try {
- $phone = EasySmsService::getPhoneSever($phone_num);
- $code_server = EasySmsService::getCodeSever($phone)->setCode($send_type);
- $message_server = EasySmsService::getMessageServer();
- $content = '您好,你的验证码:' . $code_server->getCode();
- // $content = '您好,你的验证码:[var] ' ;
- $message_server->setContent($content);
- $send_info = EasySmsService::getSmsServer()->creatQybSms()->send($phone, $message_server);
- }catch (\Exception $e){
- $this->errorMsg=$e->getMessage();
- return false;
- }
- if (!empty($send_info['code']) && $send_info['code'] != 1) {
- $this->errorMsg = '短信发送失败';
- return false;
- }
- return true;
- }
- /**
- * 验证码验证
- * @param $phone_num
- * @param $code
- * @param $send_type
- * @return bool
- */
- public function verifyCode($phone_num, $code,$send_type='default')
- {
- $phone = EasySmsService::getPhoneSever($phone_num);
- $code_sever= EasySmsService::getCodeSever($phone);
- $ret_code = $code_sever->verifyCode($code,$send_type);
- $this->errorMsg=$code_sever->getErrorMsg();
- return $ret_code;
- }
- }
|