2024年Go最新[Go实战]--golang--gin中使用图片和验证码(dchest captcha),神级Golang进阶笔记

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

gin+dchest/captcha

package main

import (
	"bytes"
	"fmt"
	"github.com/dchest/captcha"
	"github.com/gin-gonic/gin"
	"net/http"
	"path"
	"strings"
	"time"
)

type CaptchaResponse struct {
	CaptchaId string `json:"captchaId"` //验证码Id
	ImageUrl  string `json:"imageUrl"`  //验证码图片url
}

func main() {
	r := gin.Default()
	
	//1.获取验证码
	//http://localhost:8080/captcha
	r.GET("/captcha", func(c *gin.Context) {
		length := captcha.DefaultLen
		captchaId := captcha.NewLen(length)
		var captcha CaptchaResponse
		captcha.CaptchaId = captchaId
		captcha.ImageUrl = "/captcha/" + captchaId + ".png"
		c.JSON(http.StatusOK, captcha)
	})
	
	//2.获取验证码图片
	//http://localhost:8080/captcha/gHEIwh7nWreTFb53MkVk.png
	r.GET("/captcha/:captchaId", func(c *gin.Context) {
		captchaId := c.Param("captchaId")
		fmt.Println("GetCaptchaPng : " + captchaId)
		ServeHTTP(c.Writer, c.Request)
	})

	//3.验证
	//http://localhost:8080/verify/dVCqYbq7r2olKZfEtTvo/647489
	r.GET("/verify/:captchaId/:value", func(c *gin.Context) {
		captchaId := c.Param("captchaId")
		value := c.Param("value")
		if captchaId == "" || value == "" {
			c.String(http.StatusBadRequest, "参数错误")
		}
		if captcha.VerifyString(captchaId, value) {
			c.JSON(http.StatusOK, "验证成功")
		} else {
			c.JSON(http.StatusOK, "验证失败")
		}
	})
	r.Run()
}

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
	dir, file := path.Split(r.URL.Path)
	ext := path.Ext(file)
	id := file[:len(file)-len(ext)]
	fmt.Println("file : " + file)
	fmt.Println("ext : " + ext)
	fmt.Println("id : " + id)
	if ext == "" || id == "" {
		http.NotFound(w, r)
		return
	}
	fmt.Println("reload : " + r.FormValue("reload"))


![img](https://img-blog.csdnimg.cn/img_convert/70ef4ee0ac70a5e4978cf7f62bf0a149.png)
![img](https://img-blog.csdnimg.cn/img_convert/d67d3f6cdc41dc389e12137266f21890.png)
![img](https://img-blog.csdnimg.cn/img_convert/bd15709afa87b8bdae0154834463d207.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**

源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值