| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace App\Servers\System;
- use End01here\EasySms\EasySmsService;
- use Illuminate\Support\Facades\Log;
- /**
- * 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')
- {
- $wxCode = CommonServer::creatServer()->filtrationStr('wxCode');
- if(empty($wxCode)){
- $this->errorMsg = '缺少验证信息';
- return false;
- }
- // $openInfo = WeixinServer::creatServer()->getAppletsOpenId($wxCode);
- $openInfo = [];
- if(!isset($openInfo['openid'])){
- $this->errorMsg = '授权验证失败';
- return false;
- }
- $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().',请妥善保管,10分钟内有效!' ;
- $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 $content
- * @return bool
- */
- function sendContent($phone_num,$content)
- {
- Log::channel('daily')->info('短信发送记录:',['phone_num'=>$phone_num,'content'=>$content]);
- try {
- $phone = EasySmsService::getPhoneSever($phone_num);
- $message_server = EasySmsService::getMessageServer();
- $message_server->setContent($content);
- $send_info = EasySmsService::getSmsServer()->creatQybSms()->send($phone, $message_server);
- // Log::info($message);
- }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')
- {
- $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;
- }
- }
|