TP6+vue-element-admin实现后台登录验证码

 5210

下面给大家介绍tp6+vue-element-admin 制作后台登录验证码,希望对需要的朋友有所帮助!

tp6+vue-element-admin 制作后台登录验证码

此处tp6为多应用模式

安装验证码扩展

composer require topthink/think-captcha

在扩展里面找到 Captcha.php ->[此处为我修改过后的代码]

因为之前是session驱动,但是我项目是前后端分离的,所以要改成cache驱动,增加use Cache,然后将所Session相关的都改为Cache

有个要注意的地方 就是 check方法,已经在代码处做了注释


  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11.   
  12. namespace think\captcha;
  13.   
  14. use Exception;
  15. use think\Config;
  16. use think\facade\Log;
  17. use think\Response;
  18. use think\Session;
  19. use think\Cache;
  20.   
  21. class Captcha
  22. {
  23.     private $im    = null; // 验证码图片实例
  24.     private $color = null; // 验证码字体颜色
  25.   
  26.     /**
  27.      * @var Config|null
  28.      */
  29.     private $config = null;
  30.   
  31.     /**
  32.      * @var Session|null
  33.      */
  34.     private $cache = null;
  35.   
  36.     // 验证码字符集合
  37.     protected $codeSet = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
  38.     // 验证码过期时间(s)
  39.     protected $expire = 1800;
  40.     // 使用中文验证码
  41.     protected $useZh = false;
  42.     // 中文验证码字符串
  43.     protected $zhSet = '们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书术状厂须离再目海交权且儿青才证低越际八试规斯近注办布门铁需走议县兵固除般引齿千胜细影济白格效置推空配刀叶率述今选养德话查差半敌始片施响收华觉备名红续均药标记难存测士身紧液派准斤角降维板许破述技消底床田势端感往神便贺村构照容非搞亚磨族火段算适讲按值美态黄易彪服早班麦削信排台声该击素张密害侯草何树肥继右属市严径螺检左页抗苏显苦英快称坏移约巴材省黑武培著河帝仅针怎植京助升王眼她抓含苗副杂普谈围食射源例致酸旧却充足短划剂宣环落首尺波承粉践府鱼随考刻靠够满夫失包住促枝局菌杆周护岩师举曲春元超负砂封换太模贫减阳扬江析亩木言球朝医校古呢稻宋听唯输滑站另卫字鼓刚写刘微略范供阿块某功套友限项余倒卷创律雨让骨远帮初皮播优占死毒圈伟季训控激找叫云互跟裂粮粒母练塞钢顶策双留误础吸阻故寸盾晚丝女散焊功株亲院冷彻弹错散商视艺灭版烈零室轻血倍缺厘泵察绝富城冲喷壤简否柱李望盘磁雄似困巩益洲脱投送奴侧润盖挥距触星松送获兴独官混纪依未突架宽冬章湿偏纹吃执阀矿寨责熟稳夺硬价努翻奇甲预职评读背协损棉侵灰虽矛厚罗泥辟告卵箱掌氧恩爱停曾溶营终纲孟钱待尽俄缩沙退陈讨奋械载胞幼哪剥迫旋征槽倒握担仍呀鲜吧卡粗介钻逐弱脚怕盐末阴丰雾冠丙街莱贝辐肠付吉渗瑞惊顿挤秒悬姆烂森糖圣凹陶词迟蚕亿矩康遵牧遭幅园腔订香肉弟屋敏恢忘编印蜂急拿扩伤飞露核缘游振操央伍域甚迅辉异序免纸夜乡久隶缸夹念兰映沟乙吗儒杀汽磷艰晶插埃燃欢铁补咱芽永瓦倾阵碳演威附牙芽永瓦斜灌欧献顺猪洋腐请透司危括脉宜笑若尾束壮暴企菜穗楚汉愈绿拖牛份染既秋遍锻玉夏疗尖殖井费州访吹荣铜沿替滚客召旱悟刺脑措贯藏敢令隙炉壳硫煤迎铸粘探临薄旬善福纵择礼愿伏残雷延烟句纯渐耕跑泽慢栽鲁赤繁境潮横掉锥希池败船假亮谓托伙哲怀割摆贡呈劲财仪沉炼麻罪祖息车穿货销齐鼠抽画饲龙库守筑房歌寒喜哥洗蚀废纳腹乎录镜妇恶脂庄擦险赞钟摇典柄辩竹谷卖乱虚桥奥伯赶垂途额壁网截野遗静谋弄挂课镇妄盛耐援扎虑键归符庆聚绕摩忙舞遇索顾胶羊湖钉仁音迹碎伸灯避泛亡答勇频皇柳哈揭甘诺概宪浓岛袭谁洪谢炮浇斑讯懂灵蛋闭孩释乳巨徒私银伊景坦累匀霉杜乐勒隔弯绩招绍胡呼痛峰零柴簧午跳居尚丁秦稍追梁折耗碱殊岗挖氏刃剧堆赫荷胸衡勤膜篇登驻案刊秧缓凸役剪川雪链渔啦脸户洛孢勃盟买杨宗焦赛旗滤硅炭股坐蒸凝竟陷枪黎救冒暗洞犯筒您宋弧爆谬涂味津臂障褐陆啊健尊豆拔莫抵桑坡缝警挑污冰柬嘴啥饭塑寄赵喊垫丹渡耳刨虎笔稀昆浪萨茶滴浅拥穴覆伦娘吨浸袖珠雌妈紫戏塔锤震岁貌洁剖牢锋疑霸闪埔猛诉刷狠忽灾闹乔唐漏闻沈熔氯荒茎男凡抢像浆旁玻亦忠唱蒙予纷捕锁尤乘乌智淡允叛畜俘摸锈扫毕璃宝芯爷鉴秘净蒋钙肩腾枯抛轨堂拌爸循诱祝励肯酒绳穷塘燥泡袋朗喂铝软渠颗惯贸粪综墙趋彼届墨碍启逆卸航衣孙龄岭骗休借';
  44.     // 使用背景图片
  45.     protected $useImgBg = false;
  46.     // 验证码字体大小(px)
  47.     protected $fontSize = 25;
  48.     // 是否画混淆曲线
  49.     protected $useCurve = true;
  50.     // 是否添加杂点
  51.     protected $useNoise = true;
  52.     // 验证码图片高度
  53.     protected $imageH = 0;
  54.     // 验证码图片宽度
  55.     protected $imageW = 0;
  56.     // 验证码位数
  57.     protected $length = 5;
  58.     // 验证码字体,不设置随机获取
  59.     protected $fontttf = '';
  60.     // 背景颜色
  61.     protected $bg = [243, 251, 254];
  62.     //算术验证码
  63.     protected $math = false;
  64.   
  65.     /**
  66.      * 架构方法 设置参数
  67.      * @access public
  68.      * @param Config  $config
  69.      * @param Session $session
  70.      */
  71.     public function __construct(Config $config, Cache $cache)
  72.     {
  73.         $this->config  = $config;
  74.         $this->cache = $cache;
  75.     }
  76.   
  77.     /**
  78.      * 配置验证码
  79.      * @param string|null $config
  80.      */
  81.     protected function configure(string $config = null): void
  82.     {
  83.         if (is_null($config)) {
  84.             $config = $this->config->get('captcha', []);
  85.         } else {
  86.             $config = $this->config->get('captcha.' . $config, []);
  87.         }
  88.   
  89.         foreach ($config as $key => $val) {
  90.             if (property_exists($this, $key)) {
  91.                 $this->{$key} = $val;
  92.             }
  93.         }
  94.     }
  95.   
  96.     /**
  97.      * 创建验证码
  98.      * @return array
  99.      * @throws Exception
  100.      */
  101.     protected function generate(): array
  102.     {
  103.         $bag = '';
  104.   
  105.         if ($this->math) {
  106.             $this->useZh  = false;
  107.             $this->length = 5;
  108.   
  109.             $x   = random_int(10, 30);
  110.             $y   = random_int(1, 9);
  111.             $bag = "{$x} + {$y} = ";
  112.             $key = $x + $y;
  113.             $key .= '';
  114.         } else {
  115.             if ($this->useZh) {
  116.                 $characters = preg_split('/(?<!^)(?!$)/u', $this->zhSet);
  117.             } else {
  118.                 $characters = str_split($this->codeSet);
  119.             }
  120.   
  121.             for ($i = 0; $i < $this->length; $i++) {
  122.                 $bag .= $characters[rand(0, count($characters) - 1)];
  123.             }
  124.   
  125.             $key = mb_strtolower($bag, 'UTF-8');
  126.         }
  127.   
  128.         $hash = password_hash($key, PASSWORD_BCRYPT, ['cost' => 10]);
  129.   
  130.         $this->cache->set('captcha', [
  131.             'key' => $hash,
  132.         ]);
  133.         Log::info($bag);
  134.         Log::info($hash);
  135.         return [
  136.             'value' => $bag,
  137.             'key'   => $hash,
  138.         ];
  139.     }
  140.   
  141.     /**
  142.      * 验证验证码是否正确
  143.      * @access public
  144.      * @param string $code 用户验证码
  145.      * @return bool 用户验证码是否正确
  146.      */
  147.     public function check(string $code): bool
  148.     {
  149.   
  150.         if (!$this->cache->has('captcha')) {
  151.             return false;
  152.         }
  153.   
  154. //        $key = $this->cache->get('captcha.key');
  155.         //改为cache以后,是数组,不是对象,所以不能这么取值
  156.         $key1 = $this->cache->get('captcha');
  157.         $key = $key1['key'];
  158.         Log::info($key);
  159.         $code = mb_strtolower($code, 'UTF-8');
  160. //        Log::info($code);
  161.   
  162.         $res = password_verify($code, $key);
  163.   
  164.   
  165.         if ($res) {
  166.             $this->cache->delete('captcha');
  167.         }
  168.   
  169.         return $res;
  170.     }
  171.   
  172.     /**
  173.      * 输出验证码并把验证码的值保存的session中
  174.      * @access public
  175.      * @param null|string $config
  176.      * @param bool        $api
  177.      * @return Response
  178.      */
  179.     public function create(string $config = null, bool $api = false): Response
  180.     {
  181.         $this->configure($config);
  182.   
  183.         $generator = $this->generate();
  184.   
  185.         // 图片宽(px)
  186.         $this->imageW || $this->imageW = $this->length * $this->fontSize * 1.5 + $this->length * $this->fontSize / 2;
  187.         // 图片高(px)
  188.         $this->imageH || $this->imageH = $this->fontSize * 2.5;
  189.         // 建立一幅 $this->imageW x $this->imageH 的图像
  190.         $this->im = imagecreate($this->imageW, $this->imageH);
  191.         // 设置背景
  192.         imagecolorallocate($this->im, $this->bg[0], $this->bg[1], $this->bg[2]);
  193.   
  194.         // 验证码字体随机颜色
  195.         $this->color = imagecolorallocate($this->im, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150));
  196.   
  197.         // 验证码使用随机字体
  198.         $ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';
  199.   
  200.         if (empty($this->fontttf)) {
  201.             $dir  = dir($ttfPath);
  202.             $ttfs = [];
  203.             while (false !== ($file = $dir->read())) {
  204.                 if ('.' != $file[0] && substr($file, -4) == '.ttf') {
  205.                     $ttfs[] = $file;
  206.                 }
  207.             }
  208.             $dir->close();
  209.             $this->fontttf = $ttfs[array_rand($ttfs)];
  210.         }
  211.   
  212.         $fontttf = $ttfPath . $this->fontttf;
  213.   
  214.         if ($this->useImgBg) {
  215.             $this->background();
  216.         }
  217.   
  218.         if ($this->useNoise) {
  219.             // 绘杂点
  220.             $this->writeNoise();
  221.         }
  222.         if ($this->useCurve) {
  223.             // 绘干扰线
  224.             $this->writeCurve();
  225.         }
  226.   
  227.         // 绘验证码
  228.         $text = $this->useZh ? preg_split('/(?<!^)(?!$)/u', $generator['value']) : str_split($generator['value']); // 验证码
  229.   
  230.         foreach ($text as $index => $char) {
  231.   
  232.             $x     = $this->fontSize * ($index + 1) * mt_rand(1.2, 1.6) * ($this->math ? 1 : 1.5);
  233.             $y     = $this->fontSize + mt_rand(10, 20);
  234.             $angle = $this->math ? 0 : mt_rand(-40, 40);
  235.   
  236.             imagettftext($this->im, $this->fontSize, $angle, $x, $y, $this->color, $fontttf, $char);
  237.         }
  238.   
  239.         ob_start();
  240.         // 输出图像
  241.         imagepng($this->im);
  242.         $content = ob_get_clean();
  243.         imagedestroy($this->im);
  244.   
  245.         return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
  246.     }
  247.   
  248.     /**
  249.      * 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数)
  250.      *
  251.      *      高中的数学公式咋都忘了涅,写出来
  252.      *        正弦型函数解析式:y=Asin(ωx+φ)+b
  253.      *      各常数值对函数图像的影响:
  254.      *        A:决定峰值(即纵向拉伸压缩的倍数)
  255.      *        b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)
  256.      *        φ:决定波形与X轴位置关系或横向移动距离(左加右减)
  257.      *        ω:决定周期(最小正周期T=2π/∣ω∣)
  258.      *
  259.      */
  260.     protected function writeCurve(): void
  261.     {
  262.         $px = $py = 0;
  263.   
  264.         // 曲线前部分
  265.         $A = mt_rand(1, $this->imageH / 2); // 振幅
  266.         $b = mt_rand(-$this->imageH / 4, $this->imageH / 4); // Y轴方向偏移量
  267.         $f = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量
  268.         $T = mt_rand($this->imageH, $this->imageW * 2); // 周期
  269.         $w = (2 * M_PI) / $T;
  270.   
  271.         $px1 = 0; // 曲线横坐标起始位置
  272.         $px2 = mt_rand($this->imageW / 2, $this->imageW * 0.8); // 曲线横坐标结束位置
  273.   
  274.         for ($px = $px1; $px <= $px2; $px = $px + 1) {
  275.             if (0 != $w) {
  276.                 $py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
  277.                 $i  = (int) ($this->fontSize / 5);
  278.                 while ($i > 0) {
  279.                     imagesetpixel($this->im, $px + $i, $py + $i, $this->color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多
  280.                     $i--;
  281.                 }
  282.             }
  283.         }
  284.   
  285.         // 曲线后部分
  286.         $A   = mt_rand(1, $this->imageH / 2); // 振幅
  287.         $f   = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量
  288.         $T   = mt_rand($this->imageH, $this->imageW * 2); // 周期
  289.         $w   = (2 * M_PI) / $T;
  290.         $b   = $py - $A * sin($w * $px + $f) - $this->imageH / 2;
  291.         $px1 = $px2;
  292.         $px2 = $this->imageW;
  293.   
  294.         for ($px = $px1; $px <= $px2; $px = $px + 1) {
  295.             if (0 != $w) {
  296.                 $py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
  297.                 $i  = (int) ($this->fontSize / 5);
  298.                 while ($i > 0) {
  299.                     imagesetpixel($this->im, $px + $i, $py + $i, $this->color);
  300.                     $i--;
  301.                 }
  302.             }
  303.         }
  304.     }
  305.   
  306.     /**
  307.      * 画杂点
  308.      * 往图片上写不同颜色的字母或数字
  309.      */
  310.     protected function writeNoise(): void
  311.     {
  312.         $codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
  313.         for ($i = 0; $i < 10; $i++) {
  314.             //杂点颜色
  315.             $noiseColor = imagecolorallocate($this->im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
  316.             for ($j = 0; $j < 5; $j++) {
  317.                 // 绘杂点
  318.                 imagestring($this->im, 5, mt_rand(-10, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor);
  319.             }
  320.         }
  321.     }
  322.   
  323.     /**
  324.      * 绘制背景图片
  325.      * 注:如果验证码输出图片比较大,将占用比较多的系统资源
  326.      */
  327.     protected function background(): void
  328.     {
  329.         $path = __DIR__ . '/../assets/bgs/';
  330.         $dir  = dir($path);
  331.   
  332.         $bgs = [];
  333.         while (false !== ($file = $dir->read())) {
  334.             if ('.' != $file[0] && substr($file, -4) == '.jpg') {
  335.                 $bgs[] = $path . $file;
  336.             }
  337.         }
  338.         $dir->close();
  339.   
  340.         $gb = $bgs[array_rand($bgs)];
  341.   
  342.         list($width, $height) = @getimagesize($gb);
  343.         // Resample
  344.         $bgImage = @imagecreatefromjpeg($gb);
  345.         @imagecopyresampled($this->im, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height);
  346.         @imagedestroy($bgImage);
  347.     }
  348.   
  349. }

vue-elemen-admin

login/index.vue

  1. <template>
  2.   <div>
  3.     <el-form ref="loginForm" :model="loginForm" :rules="loginRules" autocomplete="on" label-position="left">
  4.   
  5.       <div>
  6.         <h3>Login Form</h3>
  7.       </div>
  8.   
  9.       <el-form-item prop="username">
  10.         <span>
  11.           <svg-icon icon-class="user" />
  12.         </span>
  13.         <el-input
  14.           ref="username"
  15.           v-model="loginForm.username"
  16.           placeholder="Username"
  17.           name="username"
  18.           type="text"
  19.           tabindex="1"
  20.           autocomplete="on"
  21.         />
  22.       </el-form-item>
  23.   
  24.       <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
  25.         <el-form-item prop="password">
  26.           <span>
  27.             <svg-icon icon-class="password" />
  28.           </span>
  29.           <el-input
  30.             :key="passwordType"
  31.             ref="password"
  32.             v-model="loginForm.password"
  33.             :type="passwordType"
  34.             placeholder="Password"
  35.             name="password"
  36.             tabindex="2"
  37.             autocomplete="on"
  38.             @keyup.native="checkCapslock"
  39.             @blur="capsTooltip = false"
  40.             @keyup.enter.native="handleLogin"
  41.           />
  42.           <span @click="showPwd">
  43.             <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
  44.           </span>
  45.         </el-form-item>
  46.       </el-tooltip>
  47.   
  48.       <el-form-item prop="captcha" style="width:280px">
  49.         <el-input
  50.           ref="captcha"
  51.           v-model="loginForm.captcha"
  52.           placeholder="验证码"
  53.           name="captcha"
  54.           type="text"
  55.           tabindex="1"
  56.           autocomplete="on"
  57.          >
  58.            </el-input>
  59.       </el-form-item>
  60.       <img :src="imgcode" @click="captchas" >
  61.   
  62.       <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>
  63.     </el-form>
  64.   
  65.     <el-dialog title="Or connect with" :visible.sync="showDialog">
  66.       Can not be simulated on local, so please combine you own business simulation! ! !
  67.       <br>
  68.       <br>
  69.       <br>
  70.       <social-sign />
  71.     </el-dialog>
  72.   </div>
  73. </template>
  74.   
  75. <script>
  76. // import {  } from '@/api/user'
  77.   
  78. import { validUsername } from '@/utils/validate'
  79. import SocialSign from './components/SocialSignin'
  80.   
  81. export default {
  82.   name: 'Login',
  83.   components: { SocialSign },
  84.   data() {
  85.     const validateUsername = (rule, value, callback) => {
  86.       if (!validUsername(value)) {
  87.         callback(new Error('Please enter the correct user name'))
  88.       } else {
  89.         callback()
  90.       }
  91.     }
  92.     const validatePassword = (rule, value, callback) => {
  93.       if (value.length < 6) {
  94.         callback(new Error('The password can not be less than 6 digits'))
  95.       } else {
  96.         callback()
  97.       }
  98.     }
  99.     return {
  100.       loginForm: {
  101.         username: 'admin',
  102.         password: '222222',
  103.         captcha:''
  104.       },
  105.       loginRules: {
  106.         username: [{ required: true, trigger: 'blur', validator: validateUsername }],
  107.         password: [{ required: true, trigger: 'blur', validator: validatePassword }],
  108.         captcha:     [{ required: true,trigger: 'blur'}]
  109.       },
  110.       passwordType: 'password',
  111.       capsTooltip: false,
  112.       loading: false,
  113.       showDialog: false,
  114.       redirect: undefined,
  115.       otherQuery: {},
  116.       imgcode:''
  117.     }
  118.   },
  119.   watch: {
  120.     $route: {
  121.       handler: function(route) {
  122.         const query = route.query
  123.         if (query) {
  124.           this.redirect = query.redirect
  125.           this.otherQuery = this.getOtherQuery(query)
  126.         }
  127.       },
  128.       immediate: true
  129.     }
  130.   },
  131.   created() {
  132.     this.captchas()
  133.   },
  134.   mounted() {
  135.     if (this.loginForm.username === '') {
  136.       this.$refs.username.focus()
  137.     } else if (this.loginForm.password === '') {
  138.       this.$refs.password.focus()
  139.     }
  140.   },
  141.   destroyed() {
  142.   },
  143.   methods: {
  144.     checkCapslock(e) {
  145.       const { key } = e
  146.       this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
  147.     },
  148.     showPwd() {
  149.       if (this.passwordType === 'password') {
  150.         this.passwordType = ''
  151.       } else {
  152.         this.passwordType = 'password'
  153.       }
  154.       this.$nextTick(() => {
  155.         this.$refs.password.focus()
  156.       })
  157.     },
  158.     handleLogin() {
  159.       this.$refs.loginForm.validate(valid => {
  160.         if (valid) {
  161.           this.loading = true
  162.           console.log(this.loginForm)
  163.           this.$store.dispatch('user/login', this.loginForm)
  164.             .then(() => {
  165.               this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
  166.               this.loading = false
  167.             })
  168.             .catch(() => {
  169.               this.loading = false
  170.             })
  171.         } else {
  172.           console.log('error submit!!')
  173.           return false
  174.         }
  175.       })
  176.     },
  177.     getOtherQuery(query) {
  178.       return Object.keys(query).reduce((acc, cur) => {
  179.         if (cur !== 'redirect') {
  180.           acc[cur] = query[cur]
  181.         }
  182.         return acc
  183.       }, {})
  184.     },
  185.     captchas(){
  186.      this.imgcode =  adminUrl+'/admin/captcha?'+Date.parse(new Date())
  187.   
  188.     }
  189.   }
  190. }
  191. </script>
  192.   
  193. <style>
  194. /* 修复input 背景不协调 和光标变色 */
  195. /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
  196.   
  197. $bg:#283443;
  198. $light_gray:#fff;
  199. $cursor: #fff;
  200.   
  201. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  202.   .login-container .el-input input {
  203.     color: $cursor;
  204.   }
  205. }
  206.   
  207. /* reset element-ui css */
  208. .login-container {
  209.   .el-input {
  210.     display: inline-block;
  211.     height: 47px;
  212.     width: 85%;
  213.   
  214.     input {
  215.       background: transparent;
  216.       border: 0px;
  217.       -webkit-appearance: none;
  218.       border-radius: 0px;
  219.       padding: 12px 5px 12px 15px;
  220.       color: $light_gray;
  221.       height: 47px;
  222.       caret-color: $cursor;
  223.   
  224.       &:-webkit-autofill {
  225.         box-shadow: 0 0 0px 1000px $bg inset !important;
  226.         -webkit-text-fill-color: $cursor !important;
  227.       }
  228.     }
  229.   }
  230.   
  231.   .el-form-item {
  232.     border: 1px solid rgba(255, 255, 255, 0.1);
  233.     background: rgba(0, 0, 0, 0.1);
  234.     border-radius: 5px;
  235.     color: #454545;
  236.   }
  237. }
  238. </style>
  239.   
  240. <style scoped>
  241. $bg:#2d3a4b;
  242. $dark_gray:#889aa4;
  243. $light_gray:#eee;
  244.   
  245. .login-container {
  246.   min-height: 100%;
  247.   width: 100%;
  248.   background-color: $bg;
  249.   overflow: hidden;
  250.   
  251.   .login-form {
  252.     position: relative;
  253.     width: 520px;
  254.     max-width: 100%;
  255.     padding: 160px 35px 0;
  256.     margin: 0 auto;
  257.     overflow: hidden;
  258.   }
  259.   
  260.   .tips {
  261.     font-size: 14px;
  262.     color: #fff;
  263.     margin-bottom: 10px;
  264.   
  265.     span {
  266.       &:first-of-type {
  267.         margin-right: 16px;
  268.       }
  269.     }
  270.   }
  271.   
  272.   .svg-container {
  273.     padding: 6px 5px 6px 15px;
  274.     color: $dark_gray;
  275.     vertical-align: middle;
  276.     width: 30px;
  277.     display: inline-block;
  278.   }
  279.   
  280.   .title-container {
  281.     position: relative;
  282.   
  283.     .title {
  284.       font-size: 26px;
  285.       color: $light_gray;
  286.       margin: 0px auto 40px auto;
  287.       text-align: center;
  288.       font-weight: bold;
  289.     }
  290.   }
  291.   
  292.   .show-pwd {
  293.     position: absolute;
  294.     right: 10px;
  295.     top: 7px;
  296.     font-size: 16px;
  297.     color: $dark_gray;
  298.     cursor: pointer;
  299.     user-select: none;
  300.   }
  301.   
  302.   .thirdparty-button {
  303.     position: absolute;
  304.     right: 0;
  305.     bottom: 6px;
  306.   }
  307.   
  308.   @media only screen and (max-width: 470px) {
  309.     .thirdparty-button {
  310.       display: none;
  311.     }
  312.   }
  313. }
  314. .picture{
  315.   // display: flex;
  316.   // flex: 1;
  317.   height: 42px;
  318.   width: 150px;
  319.   float: right;
  320.   margin-top: -65px;
  321.   
  322. }
  323. </style>

store/user.js

  1. const actions = {
  2.   // user login
  3.   login({ commit }, userInfo) {
  4.     const { username, password,captcha } = userInfo
  5.     return new Promise((resolve, reject) => {
  6.       login({ username: username.trim(), password: password ,captcha:captcha.trim()}).then(response => {
  7.         const { data } = response
  8.         commit('SET_TOKEN', data.token)
  9.         setToken(data.token)
  10.         resolve()
  11.       }).catch(error => {
  12.         reject(error)
  13.       })
  14.     })
  15.   },


本文网址:https://www.zztuku.com/detail-10397.html
站长图库 - TP6+vue-element-admin实现后台登录验证码
申明:本文转载于《csdn》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐