Explorar o código

feat: 验证码

jiangzixin hai 1 ano
pai
achega
12e2dbbb83

+ 26 - 19
server/api/modules/v1/controllers/SurveyController.php

@@ -5,8 +5,11 @@ namespace api\modules\v1\controllers;
 
 
 
 
 use api\common\controllers\Controller;
 use api\common\controllers\Controller;
+use common\helpers\CodeImgGenerate;
+use common\helpers\Util;
 use common\modules\config\models\Config;
 use common\modules\config\models\Config;
 use common\models\Survey;
 use common\models\Survey;
+use Yii;
 use yii\helpers\ArrayHelper;
 use yii\helpers\ArrayHelper;
 use api\common\behaviors\QueryParamAuth;
 use api\common\behaviors\QueryParamAuth;
 use yii\captcha\CaptchaAction;
 use yii\captcha\CaptchaAction;
@@ -29,18 +32,22 @@ class SurveyController extends Controller {
     
     
     public function actionCaptcha()
     public function actionCaptcha()
     {
     {
-     
-        $obj = new CaptchaAction(\Yii::$app->controller->id,\Yii::$app->controller);
-     
-        $obj->width = 100;
-        $obj->minLength = 3;
-        $obj->maxLength = 3;
-        $obj->offset   = 3;
-        $code = $obj->getVerifyCode(true);
-      //echo $code;die;
-        // 生成图片
-        echo  $obj->run();
-        die;
+//        $obj = new CaptchaAction(\Yii::$app->controller->id,\Yii::$app->controller);
+//
+//        $obj->width = 100;
+//        $obj->minLength = 3;
+//        $obj->maxLength = 3;
+//        $obj->offset   = 3;
+//        $code = $obj->getVerifyCode(true);
+//      //echo $code;die;
+//        // 生成图片
+//        echo  $obj->run();
+//        die;
+
+        $CodeImgGenerate = new CodeImgGenerate(\Yii::$app->controller->id,\Yii::$app->controller);
+        $codeInfo = $CodeImgGenerate->inline();                  // 验证码二进制流
+        header("Content-type: image/png");                       // 输出图片
+        exit($codeInfo);
     }
     }
 
 
     /**
     /**
@@ -203,14 +210,14 @@ class SurveyController extends Controller {
         if(empty($type)) $type = 'industry';
         if(empty($type)) $type = 'industry';
         
         
 		$survey = Survey::find()->where(['type' => $type])->all();
 		$survey = Survey::find()->where(['type' => $type])->all();
-		
-		$captcha = new CaptchaAction(\Yii::$app->controller->id,\Yii::$app->controller);
-       
-        $result = $captcha->validate($data['code'], false);
+
+        $captcha = new CodeImgGenerate(\Yii::$app->controller->id,\Yii::$app->controller);
+
+        $result = $captcha->validate($data['code']);
         
         
-        // if(!$result){
-        //     return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '验证码错误'];
-        // }
+         if(!$result){
+             return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '验证码错误'];
+         }
 
 
 		$list = [];
 		$list = [];
 		foreach ($survey as $key=>$value) {
 		foreach ($survey as $key=>$value) {

+ 60 - 0
server/common/helpers/CodeImgGenerate.php

@@ -0,0 +1,60 @@
+<?php
+namespace common\helpers;
+
+use yii\caching\Cache;
+use yii\captcha\CaptchaAction;
+
+class CodeImgGenerate extends CaptchaAction
+{
+    private $verifycode;
+
+    public function __construct($id, $controller)
+    {
+        parent::__construct($id, $controller);
+        $this->init();
+        // 更多api请访问yii\captcha\CaptchaAction类文档
+        // 这里可以初始化默认样式
+        $this->maxLength = 4;            // 最大显示个数
+        $this->minLength = 4;            // 最少显示个数
+        $this->backColor = 0x000000;     // 背景颜色
+        $this->foreColor = 0x00ff00;     // 字体颜色
+        $this->width = 100;               // 宽度
+        $this->height = 58;              // 高度
+        $this->backColor = Util::captcha_color(1);    // 背景颜色
+        $this->foreColor = Util::captcha_color(2);    // 字体颜色
+        $this->padding  = 0;                          // 间距
+        $this->offset = 4;                            // 字符之间的偏移量
+    }
+
+    /**
+     * [返回图片二进制]
+     * @return [type] [description]
+     */
+    public function inline()
+    {
+        return $this->renderImage($this->getPhrase());
+    }
+
+    /**
+     * [返回图片验证码]
+     * @return [type] [description]
+     */
+    public function getPhrase()
+    {
+        if(!$this->verifycode){
+            $this->verifycode = $this->generateVerifyCode();
+            \Yii::$app->cache->set($this->getSessionKey() . '/' . $this->verifycode, $this->verifycode);
+        }
+
+        return $this->verifycode;
+    }
+
+    public function validate($input, $caseSensitive = false)
+    {
+        $code = \Yii::$app->cache->get($this->getSessionKey() . '/' . $input);
+        $valid = strcasecmp($input, $code) === 0;
+
+        return $valid;
+    }
+}
+?>

+ 21 - 0
server/common/helpers/Util.php

@@ -204,4 +204,25 @@ class Util
         }
         }
         return $base64_file;
         return $base64_file;
     }
     }
+
+    /**
+     * [ 返回随机颜色 ]
+     * @param  integer $type [description]
+     * @return [type]        [description]
+     */
+    public static function captcha_color($type=1)
+    {
+        if(!in_array($type, array(1,2))) $type=1;
+        if($type==1) {
+            // 背景颜色
+            $bg_color_arr=array('15595519','16316664');
+            $bg=$bg_color_arr[array_rand($bg_color_arr)];
+            return (int) '0x'.$bg;
+        } else {
+            // 字体颜色
+            $text_color_arr=array('12326852','2185586');
+            $tc=$text_color_arr[array_rand($text_color_arr)];
+            return (int) '0x'.$tc;
+        }
+    }
 }
 }