| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 | <?php/** * Created by PhpStorm. * User: MADAO * Date: 2018/3/16 * Time: 19:18 */namespace App\Libs;use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Redis;use Overtrue\EasySms\EasySms as Sms;use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;class EasySms extends Sms{    public function __construct(array $config = [])    {        $init = config('sms.init');        $config = array_merge($init, $config);        parent::__construct($config);    }    public function send($to, $message = [], array $gateways = [])    {        if (!is_mobile($to)) {            return response()->json(['message' => '手机号格式错误'], 400);        }        $cacheData = EasySms::getSmsCodeCache($to);        if ($cacheData) {            if (time() - $cacheData['send_time'] <= config('sms.sendTimeout') ?? 60) {                return response()->json(['message' => '短信消息发送不能太过频繁'], 400);            }        }        $smsCode = $this->createSmsCode();        $default = [            'content' => '您好!您的验证码为${code},请在15分钟内填写。',            'template' => 'SMS_162110318',            'data' => [                'code' => $smsCode            ],        ];        $message = array_merge($default, $message);        try {            parent::send($to, $message, $gateways);            $this->setSmsCodeCache($to, $smsCode); //缓存至redis            return response()->json(['message' => '发送成功']);        } catch (NoGatewayAvailableException $exception) {            Log::error(var_export($exception->getResults(), 1));            return response()->json(['message' => '网关发送失败'], 400);        } catch (\Throwable $throwable) {            Log::error(var_export($throwable->getMessage(), 1));            return response()->json(['message' => '短信发送失败'], 400);        }    }    /**     * 设置短信验证码缓存     * @author hjc     * @param $mobile     * @param $smsCode     */    private function setSmsCodeCache($mobile, $smsCode)    {        $cacheKey = 'hash_sms_' . strtolower(config('name')) . '_' . $mobile;        $cacheData = [            'mobile' => $mobile,            'code' => $smsCode,            'send_time' => time()        ];        Redis::hmset($cacheKey, $cacheData);        Redis::expire($cacheKey, config('sms.redisExpire') ?? 600);    }    /**     * 获得短信验证码     * @author hjc     * @param $mobile     * @return mixed     */    public static function getSmsCodeCache($mobile)    {        $cacheKey = 'hash_sms_' . strtolower(config('name')) . '_' . $mobile;        $cacheData = Redis::hgetall($cacheKey);        return $cacheData;    }    /**     * 随机生成的code码     * @author hjc     * @return string     */    private function createSmsCode()    {        $num = config('sms.codeSize') ?? 4; //生成的随机数个数        $isIncludeStr = config('sms.codeIsIncludeStr') ?? false; //是否包含字符串        $value = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];        if ($isIncludeStr) {            $strVal = [                'a', 'b', 'c', 'd', 'e', 'f', 'g',                'h', 'i', 'j', 'k', 'l', 'm', 'n',                'o', 'p', 'q', 'r', 's', 't', 'u',                'v', 'w', 'x', 'y', 'z'            ];            $value = array_merge($value, $strVal);        }        shuffle($value);    //排序打乱        $key = array_rand($value, $num);        $string = '';        foreach ($key as $v) {            $string .= $value[$v];        }        return $string;    }}
 |