| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 | <?phpnamespace App\Services\Common;use App\Exceptions\ResponseException;use App\Jobs\SmsJob;use App\Repositories\SmsTemplateRepository;use Illuminate\Support\Facades\Cache;/** * 短信服务. * Class SmsService * @package App\Services\Common */class SmsService{    protected $utype;    protected $sender_id;    /**     * @var array     */    protected $callback;    /**     * SmsService constructor.     */    public function __construct()    {        $this->utype = 0;        $this->sender_id = 0;        $this->batch_id = 0;        $this->callback = [];    }    /**     * 发送基本短信     * @param string $mobile     * @param string $template_alias     * @param array $params     */    public function sendSms(string $mobile, string $template_alias, array $params = [], $isAuthSms = false, $isVariable = true, $content = '')    {        $smsJob=new SmsJob($mobile, $template_alias, $params, $isAuthSms, $isVariable, $content);        $smsJob->setSender($this->utype, $this->sender_id);        $smsJob->setCallback($this->callback);        dispatch($smsJob);    }    /**     * 发送基本短信     * @param string $mobile     * @param string $template_alias     * @param array $params     */    public function sendSmsStatic(string $mobile, string $template_alias, array $params = [])    {        $smsJob=new SmsJob($mobile, $template_alias, $params, false);        $smsJob->setSender($this->utype, $this->sender_id);        $smsJob->setCallback($this->callback);        dispatch($smsJob);    }    /**     * 发送确认短信,需要缓存验证码code     * @param string $mobile     * @param string $template_alias     * @throws ResponseException     */    public function sendAuthSms(string $mobile, string $template_alias)    {        $is_send=Cache::has($template_alias.'_'.$mobile.'_send');        if ($is_send) {            throw new ResponseException("请稍后再发送短信");        }        $smsJob=new SmsJob($mobile, $template_alias, [], true);        dispatch($smsJob);        Cache::put($template_alias.'_'.$mobile.'_send', 1, 1);    }    /**     * 检查验证短信的验证码是否准确     * @param string $mobile     * @param string $template_alias     * @param string $code     * @return bool     */    public function checkAuthSms(string $mobile, string $template_alias, string $code): bool    {        $cacheCode=Cache::get($template_alias.'_'.$mobile);        if ($code!=$cacheCode) {            return false;        }        Cache::forget($template_alias.'_'.$mobile);        return true;    }    /**     * 设置回调函数     * @param string $obj 需实例对象的完整命名空间     * @param string $method 调用obj对应的方法     * @param array $args 方法里的参数(按回调方法里的参数顺序)     * @return SmsService     */    public function setCallback(string $obj, string $method, array $args)    {        $this->callback[]=[$obj, $method];        $this->callback[]=$args;        return $this;    }    /**     * 设置发送者     * @param $utype 0系统,1企业,2用户     * @param $id: 对应 utype id     * @return SmsService     */    public function setSender($utype, $id)    {        $this->utype=$utype;        $this->sender_id=$id;        return $this;    }}
 |