feat 微信登录

This commit is contained in:
2026-01-14 10:53:18 +08:00
parent c4e3365c53
commit 313a93702e
3 changed files with 35 additions and 15 deletions

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.23.1
require ( require (
bou.ke/monkey v1.0.2 bou.ke/monkey v1.0.2
git.hlsq.asia/mmorpg/service-common v0.0.0-20260113014617-7812a3c669d7 git.hlsq.asia/mmorpg/service-common v0.0.0-20260113095415-34b2e45e3d50
github.com/alicebob/miniredis/v2 v2.35.0 github.com/alicebob/miniredis/v2 v2.35.0
github.com/gin-contrib/cors v1.7.6 github.com/gin-contrib/cors v1.7.6
github.com/gin-gonic/gin v1.11.0 github.com/gin-gonic/gin v1.11.0

4
go.sum
View File

@@ -2,8 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA= bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260113014617-7812a3c669d7 h1:sNZWEsAy4G5HEigdnnIHQ4eqmifN3rDPPApeTG4c4h4= git.hlsq.asia/mmorpg/service-common v0.0.0-20260113095415-34b2e45e3d50 h1:T0JZl2N+HMYRgyifEGFXD2y1MZOuoAS1xjnIg/JLGwM=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260113014617-7812a3c669d7/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc= git.hlsq.asia/mmorpg/service-common v0.0.0-20260113095415-34b2e45e3d50/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc=
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI= github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=

View File

@@ -18,8 +18,9 @@ import (
// 这个模块处理用户登录 // 这个模块处理用户登录
type LoginReq struct { type LoginReq struct {
Phone string `json:"phone" binding:"required,min=1"` Phone string `json:"phone"` // 手机号
Code string `json:"code" binding:"required,min=1"` Code string `json:"code"` // 验证码
WxMiniCode string `json:"wxMiniCode"` // 微信小程序登录
} }
type LoginResp struct { type LoginResp struct {
@@ -41,20 +42,39 @@ func Login(c *gin.Context) {
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed)) http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
return return
} }
login, err := client.Login(c, &grpc_pb.LoginReq{
usn, name := "", ""
if req.Phone != "" {
// TODO 校验验证码
login, err := client.PhoneLogin(c, &grpc_pb.PhoneLoginReq{
Phone: req.Phone, Phone: req.Phone,
Code: req.Code, Code: req.Code,
}) })
if err != nil { if err != nil {
log.Errorf("Login Login error: %v", err) log.Errorf("Login PhoneLogin error: %v", err)
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed)) http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
return return
} }
usn, name = login.USN, login.Name
} else if req.WxMiniCode != "" {
login, err := client.WxMiniLogin(c, &grpc_pb.WxMiniLoginReq{
Code: req.WxMiniCode,
})
if err != nil {
log.Errorf("Login WxMiniLogin error: %v", err)
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
return
}
usn, name = login.USN, login.Name
} else {
http_resp.JsonBadRequest(c)
return
}
at, rt, err := genToken(c, login.USN) at, rt, err := genToken(c, usn)
http_resp.JsonOK(c, http_resp.Success(&LoginResp{ http_resp.JsonOK(c, http_resp.Success(&LoginResp{
USN: login.USN, USN: usn,
Name: login.Name, Name: name,
AccessToken: at, AccessToken: at,
RefreshToken: rt, RefreshToken: rt,
})) }))