From 352c645ea36891e00d9a3830952fc6e35cbdad0f Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Fri, 22 Nov 2024 10:00:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/hdl/hdl_login.go | 69 +++++++++++++++++++++++++++++++++++++- app/md/md_login.go | 8 +++++ app/router/router.go | 1 + app/svc/svc_aliyun_base.go | 33 ++++++++++++++++++ go.mod | 26 ++++++++++---- 5 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 app/svc/svc_aliyun_base.go diff --git a/app/hdl/hdl_login.go b/app/hdl/hdl_login.go index e37594b..0fbbe45 100644 --- a/app/hdl/hdl_login.go +++ b/app/hdl/hdl_login.go @@ -11,10 +11,71 @@ import ( "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/sms" "github.com/gin-gonic/gin" "time" ) +// SmsSend +// @Summary 发送短信 +// @Tags 发送短信 +// @Description 发送短信 +// @Accept json +// @Produce json +// @Param req body md.SmsSendReq true "注册参数" +// @Success 200 {string} "成功返回" +// @Failure 400 {object} md.Response "具体错误" +// @Router /v1/smsSend [post] +func SmsSend(c *gin.Context) { + var req md.SmsSendReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = svc.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + + userDb := implement.NewUserDb(db.Db) + if utils.InArr(req.Type, []string{"h5Register", "wechatBindPhone", "login"}) == false { + user, err := userDb.UserGetOneByParams(map[string]interface{}{ + "key": "phone", + "value": req.Mobile, + }) + if err != nil { + e.OutErr(c, 400, err) + return + } + if user == nil { + e.OutErr(c, 400, e.NewErr(400, "账号不存在")) + return + } + } + param := map[string]string{ + "lot_number": req.LotNumber, + "gen_time": req.GenTime, + "captcha_output": req.CaptchaOutput, + "pass_token": req.PassToken, + } + //校验图形验证码 + id, key := svc.AliyunCaptchBase(c) + err = sms.AliyunCheckCaptcha(id, key, param) + if err != nil { + e.OutErr(c, 400, e.NewErr(400, "图形验证码校验失败")) + return + } + data := svc.AliyunSmsBase(c, req.Type) + //发送短信 + err = sms.AliyunSendSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], req.Mobile, data["aliyun_sms_sign_name"], data["aliyun_sms_code"], "") + if err != nil { + e.OutErr(c, 400, e.NewErr(400, "发送失败")) + return + } + //发送短信 + e.OutSuc(c, "success", nil) + return +} + // Register // @Summary 注册 // @Tags 注册 @@ -34,7 +95,13 @@ func Register(c *gin.Context) { e.OutErr(c, err1.Code, err1.Error()) return } - + data := svc.AliyunSmsBase(c, req.Type) + //校验短信 + err = sms.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], req.Mobile, req.Code) + if err != nil { + e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试")) + return + } now := time.Now() userDb := implement.NewUserDb(db.Db) user, err := userDb.UserGetOneByParams(map[string]interface{}{ diff --git a/app/md/md_login.go b/app/md/md_login.go index 133fe49..5b52fbf 100644 --- a/app/md/md_login.go +++ b/app/md/md_login.go @@ -1,5 +1,13 @@ package md +type SmsSendReq struct { + Mobile string `json:"mobile" binding:"required"` + Type string `json:"type" example:"h5Register:h5注册页、wechatBindPhone:微信绑定手机、login:登陆、findPwd:找回密码、changePwd:修改密码"` + LotNumber string `json:"lot_number" example:"阿里云图形验证码对应参数"` + GenTime string `json:"gen_time" example:"阿里云图形验证码对应参数"` + CaptchaOutput string `json:"captcha_output" example:"阿里云图形验证码对应参数"` + PassToken string `json:"pass_token" example:"阿里云图形验证码对应参数"` +} type RegisterReq struct { Mobile string `json:"mobile" binding:"required"` Code string `json:"code" example:"验证码"` diff --git a/app/router/router.go b/app/router/router.go index 274d5a8..f86cc12 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -52,6 +52,7 @@ func route(r *gin.RouterGroup) { r.Any("/aesDecryptByECB", hdl.AesDecryptByECB) r.Use(mw.CheckSign) r.Any("/testCreateSign", hdl.TestCreateSign) + r.POST("/smsSend", hdl.SmsSend) r.POST("/register", hdl.Register) r.POST("/login", hdl.Login) r.Use(mw.Auth) // 以下接口需要JWT验证 diff --git a/app/svc/svc_aliyun_base.go b/app/svc/svc_aliyun_base.go new file mode 100644 index 0000000..c029eb4 --- /dev/null +++ b/app/svc/svc_aliyun_base.go @@ -0,0 +1,33 @@ +package svc + +import ( + "applet/app/db" + "applet/app/utils/cache" + "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" + "github.com/gin-gonic/gin" + "github.com/tidwall/gjson" +) + +func AliyunCaptchBase(c *gin.Context) (string, string) { + redisConn := cache.GetPool().Get() + sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + data := sysCfgDb.SysCfgFindWithDb("aliyun_captch_id_android", "aliyun_captch_key_android", "aliyun_captch_id_ios", "aliyun_captch_key_ios", "aliyun_captch_id_h5", "aliyun_captch_key_h5") + if c.GetHeader("platform") == "android" { + return data["aliyun_captch_id_android"], data["aliyun_captch_key_android"] + } + if c.GetHeader("platform") == "iOS" { + return data["aliyun_captch_id_ios"], data["aliyun_captch_key_ios"] + } + if c.GetHeader("platform") == "wap" { + return data["aliyun_captch_id_h5"], data["aliyun_captch_key_h5"] + } + return "", "" +} +func AliyunSmsBase(c *gin.Context, types string) map[string]string { + redisConn := cache.GetPool().Get() + sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + data := sysCfgDb.SysCfgFindWithDb("aliyun_sms_id", "aliyun_sms_secret", "aliyun_sms_code", "aliyun_sms_sign_name") + data["aliyun_sms_code"] = gjson.Get(data["aliyun_sms_code"], types).String() + return data + +} diff --git a/go.mod b/go.mod index 69a0cc0..08773b8 100644 --- a/go.mod +++ b/go.mod @@ -10,8 +10,7 @@ require ( github.com/boombuler/barcode v1.0.1 github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5 github.com/dgrijalva/jwt-go v3.2.0+incompatible - github.com/gin-contrib/sessions v0.0.3 - github.com/gin-gonic/gin v1.9.0 + github.com/gin-gonic/gin v1.9.1 github.com/go-playground/locales v0.14.1 github.com/go-playground/universal-translator v0.18.1 github.com/go-playground/validator/v10 v10.20.0 @@ -34,10 +33,11 @@ require ( require ( code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241121103041-c8eab82bd33d - code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241121102152-7a1c318aed7e + code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241122005808-60ab25ac1d22 code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible + github.com/gin-contrib/sessions v1.0.1 github.com/go-sql-driver/mysql v1.8.1 github.com/gocolly/colly v1.2.0 github.com/shopspring/decimal v1.3.1 @@ -49,12 +49,24 @@ require ( github.com/BurntSushi/toml v1.4.0 // indirect github.com/KyleBanks/depth v1.2.1 // indirect github.com/PuerkitoBio/goquery v1.9.2 // indirect + github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.5 // indirect + github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.10 // indirect + github.com/alibabacloud-go/debug v1.0.1 // indirect + github.com/alibabacloud-go/dypnsapi-20170525/v2 v2.2.3 // indirect + github.com/alibabacloud-go/endpoint-util v1.1.0 // indirect + github.com/alibabacloud-go/openapi-util v0.1.0 // indirect + github.com/alibabacloud-go/tea v1.2.2 // indirect + github.com/alibabacloud-go/tea-utils v1.4.3 // indirect + github.com/alibabacloud-go/tea-utils/v2 v2.0.6 // indirect + github.com/alibabacloud-go/tea-xml v1.1.3 // indirect + github.com/aliyun/credentials-go v1.3.10 // indirect github.com/andybalholm/cascadia v1.3.2 // indirect github.com/antchfx/htmlquery v1.3.3 // indirect github.com/antchfx/xmlquery v1.4.2 // indirect github.com/antchfx/xpath v1.3.2 // indirect github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/clbanning/mxj/v2 v2.5.5 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect @@ -69,9 +81,9 @@ require ( github.com/golang/protobuf v1.5.3 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/gookit/color v1.3.8 // indirect - github.com/gorilla/context v1.1.1 // indirect - github.com/gorilla/securecookie v1.1.1 // indirect - github.com/gorilla/sessions v1.2.1 // indirect + github.com/gorilla/context v1.1.2 // indirect + github.com/gorilla/securecookie v1.1.2 // indirect + github.com/gorilla/sessions v1.2.2 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/kennygrant/sanitize v1.2.4 // indirect @@ -91,6 +103,7 @@ require ( github.com/temoto/robotstxt v1.1.2 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect + github.com/tjfoc/gmsm v1.4.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect go.uber.org/atomic v1.7.0 // indirect @@ -107,6 +120,7 @@ require ( golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/appengine v1.4.0 // indirect google.golang.org/protobuf v1.34.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect honnef.co/go/tools v0.0.1-2020.1.4 // indirect xorm.io/builder v0.3.13 // indirect