天天看点

go-zero 实践 - 实现获取验证码(七)

作者:IT技术帮

一、开发调试配置

在goland里配置运行、调试,以便可以debug

go-zero 实践 - 实现获取验证码(七)

调试时如果出现警告:undefined behavior - version of Delve is too old for Go version 1.20.3

则安装新版本delve

go install github.com/go-delve/delve/cmd/dlv@latest           

完成后会在{GOPATH}\bin下产生 dlv.exe,注意:通过go env 命令可以查看gopath路径

将dlv.exe复制到{GoLand安装目录}\plugins\go\lib\dlv\windows下的div.exe文件

重启goland即可

二、实现获取验证码

参照simple-admin-core实现验证码获取

1、在systemmanage\api\desc下定义captcha.api

import "base.api"

// The information of captcha | 验证码数据
type CaptchaInfo {
    CaptchaId string `json:"captchaId"`
    ImgPath   string `json:"imgPath"`
}

// The response data of captcha | 验证码返回数据
type CaptchaResp {
    BaseDataInfo

    // The menu authorization data | 菜单授权信息数据
    Data CaptchaInfo `json:"data"`
}

@server(
    group: captcha
)

service SystemManage {
    // Get captcha | 获取验证码
    @handler getCaptcha
    get /captcha returns (CaptchaResp)
}
           

2、修改all.api

import "base.api"
import "user.api"
import "role.api"
import "captcha.api"           

3、生成logic及handler

在systemmanage\api下执行

goctls api go --api ./desc/all.api --dir ./ --style=gozero           

4、修改config及svc

package config

import (
	"github.com/suyuan32/simple-admin-common/config"
	"github.com/suyuan32/simple-admin-common/plugins/casbin"
	"github.com/suyuan32/simple-admin-common/utils/captcha"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/zrpc"
)

type Config struct {
	rest.RestConf
	Auth            rest.AuthConf
	DatabaseConf    config.DatabaseConf
	RedisConf       redis.RedisConf
	CasbinConf      casbin.CasbinConf
	SystemManageRpc zrpc.RpcClientConf
	Captcha         captcha.Conf
}
           
package svc

import (
	"github.com/mojocn/base64Captcha"
	"github.com/suyuan32/simple-admin-common/utils/captcha"
	"github.com/zeromicro/go-zero/zrpc"
	"go-zero-my/systemmanage/api/internal/config"
	i18n2 "go-zero-my/systemmanage/api/internal/i18n"
	"go-zero-my/systemmanage/api/internal/middleware"
	"go-zero-my/systemmanage/rpc/systemmanageclient"

	"github.com/suyuan32/simple-admin-common/i18n"

	"github.com/casbin/casbin/v2"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/rest"
)

type ServiceContext struct {
	Config          config.Config
	Casbin          *casbin.Enforcer
	Authority       rest.Middleware
	Trans           *i18n.Translator
	SystemManageRpc systemmanageclient.SystemManage
	Captcha         *base64Captcha.Captcha
}

func NewServiceContext(c config.Config) *ServiceContext {

	rds := redis.MustNewRedis(c.RedisConf)

	cbn := c.CasbinConf.MustNewCasbinWithRedisWatcher(c.DatabaseConf.Type, c.DatabaseConf.GetDSN(), c.RedisConf)

	trans := i18n.NewTranslator(i18n2.LocaleFS)

	return &ServiceContext{
		Config:          c,
		Authority:       middleware.NewAuthorityMiddleware(cbn, rds, trans).Handle,
		Trans:           trans,
		SystemManageRpc: systemmanageclient.NewSystemManage(zrpc.MustNewClient(c.SystemManageRpc)),
		Captcha:         captcha.MustNewRedisCaptcha(c.Captcha, rds), //redis存储验证码信息
	}
}
           

5、修改logic文件的GetCaptcha函数,参照simple-admin-core

func (l *GetCaptchaLogic) GetCaptcha() (resp *types.CaptchaResp, err error) {
	if id, b64s, err := l.svcCtx.Captcha.Generate(); err != nil {
		logx.Errorw("fail to generate captcha", logx.Field("detail", err.Error()))
		return &types.CaptchaResp{
			BaseDataInfo: types.BaseDataInfo{Code: errorcode.Internal, Msg: l.svcCtx.Trans.Trans(l.ctx, i18n.Failed)},
			Data:         types.CaptchaInfo{},
		}, nil
	} else {
		resp = &types.CaptchaResp{
			BaseDataInfo: types.BaseDataInfo{Msg: l.svcCtx.Trans.Trans(l.ctx, i18n.Success)},
			Data: types.CaptchaInfo{
				CaptchaId: id,
				ImgPath:   b64s,
			},
		}
		return resp, nil
	}
}           

三、获取验证码测试

1、启动etcd、redis

2、在goland里启动systemmanage RPC、API服务

3、在浏览器访问

http://localhost:8080/captcha

如下结果

go-zero 实践 - 实现获取验证码(七)

redis存储

go-zero 实践 - 实现获取验证码(七)

继续阅读