|
@@ -13,14 +13,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
-namespace think\admin\extend;
|
|
|
+namespace think\admin\service;
|
|
|
+
|
|
|
+use think\admin\Service;
|
|
|
|
|
|
|
|
|
- * 图形验证码扩展
|
|
|
- * Class CaptchaExtend
|
|
|
- * @package app\admin\service
|
|
|
+ * 图形验证码服务
|
|
|
+ * Class CaptchaService
|
|
|
+ * @package think\admin\service
|
|
|
*/
|
|
|
-class CaptchaExtend
|
|
|
+class CaptchaService extends Service
|
|
|
{
|
|
|
private $code;
|
|
|
private $uniqid;
|
|
@@ -34,11 +36,10 @@ class CaptchaExtend
|
|
|
private $fontcolor;
|
|
|
|
|
|
|
|
|
- * 构造方法初始化
|
|
|
- * CaptchaService constructor.
|
|
|
+ * 服务初始化
|
|
|
* @param array $config
|
|
|
*/
|
|
|
- public function __construct($config = [])
|
|
|
+ protected function init($config = [])
|
|
|
{
|
|
|
|
|
|
foreach ($config as $k => $v) if (isset($this->$k)) $this->$k = $v;
|
|
@@ -52,43 +53,9 @@ class CaptchaExtend
|
|
|
|
|
|
$this->font = __DIR__ . '/bin/font.ttf';
|
|
|
|
|
|
- app()->cache->set($this->uniqid, $this->code, 360);
|
|
|
+ $this->app->cache->set($this->uniqid, $this->code, 360);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- * 创建验证码图片
|
|
|
- * @return string
|
|
|
- */
|
|
|
- private function createImage()
|
|
|
- {
|
|
|
-
|
|
|
- $this->img = imagecreatetruecolor($this->width, $this->height);
|
|
|
- $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
|
|
|
- imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
|
|
|
-
|
|
|
- for ($i = 0; $i < 6; $i++) {
|
|
|
- $color = imagecolorallocate($this->img, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50));
|
|
|
- imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
|
|
|
- }
|
|
|
-
|
|
|
- for ($i = 0; $i < 100; $i++) {
|
|
|
- $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
|
|
|
- imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
|
|
|
- }
|
|
|
-
|
|
|
- $_x = $this->width / $this->codelen;
|
|
|
- for ($i = 0; $i < $this->codelen; $i++) {
|
|
|
- $this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
|
|
|
- imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
|
|
|
- }
|
|
|
- ob_start();
|
|
|
- imagepng($this->img);
|
|
|
- $data = ob_get_contents();
|
|
|
- ob_end_clean();
|
|
|
- imagedestroy($this->img);
|
|
|
- return base64_encode($data);
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
|
|
|
* 获取验证码值
|
|
|
* @return string
|
|
@@ -116,35 +83,61 @@ class CaptchaExtend
|
|
|
return $this->uniqid;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
|
|
|
- * 输出图形验证码
|
|
|
- * @return string
|
|
|
+ * 检查验证码是否正确
|
|
|
+ * @param string $code 需要验证的值
|
|
|
+ * @param string $uniqid 验证码编号
|
|
|
+ * @return boolean
|
|
|
*/
|
|
|
- public function __toString()
|
|
|
+ public function check($code, $uniqid = null)
|
|
|
{
|
|
|
- return $this->getData();
|
|
|
+ $_uni = is_string($uniqid) ? $uniqid : input('uniqid', '-');
|
|
|
+ $_val = $this->app->cache->get($_uni);
|
|
|
+ $this->app->cache->delete($_uni);
|
|
|
+ return is_string($_val) && strtolower($_val) === strtolower($code);
|
|
|
}
|
|
|
|
|
|
|
|
|
- * 返回当前对象
|
|
|
- * @return static
|
|
|
+ * 输出图形验证码
|
|
|
+ * @return string
|
|
|
*/
|
|
|
- public static function instance()
|
|
|
+ public function __toString()
|
|
|
{
|
|
|
- return new static();
|
|
|
+ return $this->getData();
|
|
|
}
|
|
|
|
|
|
|
|
|
- * 检查验证码是否正确
|
|
|
- * @param string $code 需要验证的值
|
|
|
- * @param string $uniqid 验证码编号
|
|
|
- * @return boolean
|
|
|
+ * 创建验证码图片
|
|
|
+ * @return string
|
|
|
*/
|
|
|
- public static function check($code, $uniqid = null)
|
|
|
+ private function createImage()
|
|
|
{
|
|
|
- $_uni = is_string($uniqid) ? $uniqid : input('uniqid', '-');
|
|
|
- $_val = app()->cache->get($_uni);
|
|
|
- app()->cache->delete($_uni);
|
|
|
- return is_string($_val) && strtolower($_val) === strtolower($code);
|
|
|
+
|
|
|
+ $this->img = imagecreatetruecolor($this->width, $this->height);
|
|
|
+ $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
|
|
|
+ imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
|
|
|
+
|
|
|
+ for ($i = 0; $i < 6; $i++) {
|
|
|
+ $color = imagecolorallocate($this->img, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50));
|
|
|
+ imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
|
|
|
+ }
|
|
|
+
|
|
|
+ for ($i = 0; $i < 100; $i++) {
|
|
|
+ $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
|
|
|
+ imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
|
|
|
+ }
|
|
|
+
|
|
|
+ $_x = $this->width / $this->codelen;
|
|
|
+ for ($i = 0; $i < $this->codelen; $i++) {
|
|
|
+ $this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
|
|
|
+ imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
|
|
|
+ }
|
|
|
+ ob_start();
|
|
|
+ imagepng($this->img);
|
|
|
+ $data = ob_get_contents();
|
|
|
+ ob_end_clean();
|
|
|
+ imagedestroy($this->img);
|
|
|
+ return base64_encode($data);
|
|
|
}
|
|
|
-}
|
|
|
+}
|