123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace App\Servers\Common;
- use End01here\EasySms\EasySmsService;
- /**
- * 短信发送类
- */
- 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
- * @return bool
- */
- function sendPayNotice($phone_num)
- {
- try {
- $phone = EasySmsService::getPhoneSever($phone_num);
- $message_server = EasySmsService::getMessageServer();
- $content = '您好,官网有新的留言,请您及时查看。';
- $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;
- }
- }
|