一、開發調試配置
在goland裡配置運作、調試,以便可以debug
調試時如果出現警告: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
如下結果
redis存儲