init: init refactor
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
Name: user-api
|
||||
Host: 0.0.0.0
|
||||
Port: 9001
|
||||
|
||||
Auth:
|
||||
AccessSecret: 9149f2eb-d517-4a50-a03a-231dbcf0d872
|
||||
AccessExpire: 7200
|
||||
|
||||
# user-rpc 服务配置
|
||||
UserRpc:
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 192.168.100.127:2379
|
||||
Key: user.rpc
|
||||
@@ -0,0 +1,18 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
Auth struct {
|
||||
AccessSecret string
|
||||
AccessExpire int64
|
||||
}
|
||||
UserRpc zrpc.RpcClientConf
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/user/api/internal/logic/auth"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 手机号登录
|
||||
func LoginByPhoneHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.LoginByPhoneReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := auth.NewLoginByPhoneLogic(r.Context(), svcCtx)
|
||||
resp, err := l.LoginByPhone(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.OkWithData(w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/user/api/internal/logic/auth"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 账号密码登录
|
||||
func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.LoginReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := auth.NewLoginLogic(r.Context(), svcCtx)
|
||||
resp, err := l.Login(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.OkWithData(w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/user/api/internal/logic/auth"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 微信小程序登录
|
||||
func MiniLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.MiniLoginReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := auth.NewMiniLoginLogic(r.Context(), svcCtx)
|
||||
resp, err := l.MiniLogin(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.OkWithData(w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
auth "sundynix-micro-go/app/user/api/internal/handler/auth"
|
||||
user "sundynix-micro-go/app/user/api/internal/handler/user"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
// 账号密码登录
|
||||
Method: http.MethodPost,
|
||||
Path: "/login",
|
||||
Handler: auth.LoginHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 手机号登录
|
||||
Method: http.MethodPost,
|
||||
Path: "/loginByPhone",
|
||||
Handler: auth.LoginByPhoneHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 微信小程序登录
|
||||
Method: http.MethodPost,
|
||||
Path: "/miniLogin",
|
||||
Handler: auth.MiniLoginHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithPrefix("/api/user"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
// 修改密码
|
||||
Method: http.MethodPut,
|
||||
Path: "/changePassword",
|
||||
Handler: user.ChangePasswordHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 删除用户
|
||||
Method: http.MethodDelete,
|
||||
Path: "/delete",
|
||||
Handler: user.DeleteUserHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 获取当前用户信息
|
||||
Method: http.MethodGet,
|
||||
Path: "/info",
|
||||
Handler: user.GetUserInfoHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 用户列表
|
||||
Method: http.MethodPost,
|
||||
Path: "/list",
|
||||
Handler: user.GetUserListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 获取位置信息
|
||||
Method: http.MethodGet,
|
||||
Path: "/location",
|
||||
Handler: user.GetLocationHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 更新用户信息
|
||||
Method: http.MethodPut,
|
||||
Path: "/update",
|
||||
Handler: user.UpdateUserHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 获取天气信息
|
||||
Method: http.MethodGet,
|
||||
Path: "/weather",
|
||||
Handler: user.GetWeatherHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
|
||||
rest.WithPrefix("/api/user"),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/user/api/internal/logic/user"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 修改密码
|
||||
func ChangePasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.ChangePasswordReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := user.NewChangePasswordLogic(r.Context(), svcCtx)
|
||||
err := l.ChangePassword(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.Ok(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/user/api/internal/logic/user"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 删除用户
|
||||
func DeleteUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.IdsReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := user.NewDeleteUserLogic(r.Context(), svcCtx)
|
||||
err := l.DeleteUser(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.Ok(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/user/api/internal/logic/user"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 获取位置信息
|
||||
func GetLocationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.LocationReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := user.NewGetLocationLogic(r.Context(), svcCtx)
|
||||
err := l.GetLocation(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.Ok(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/logic/user"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 获取当前用户信息
|
||||
func GetUserInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
l := user.NewGetUserInfoLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetUserInfo()
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.OkWithData(w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/user/api/internal/logic/user"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 用户列表
|
||||
func GetUserListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UserListReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := user.NewGetUserListLogic(r.Context(), svcCtx)
|
||||
err := l.GetUserList(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.Ok(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/user/api/internal/logic/user"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 获取天气信息
|
||||
func GetWeatherHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.WeatherReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := user.NewGetWeatherLogic(r.Context(), svcCtx)
|
||||
err := l.GetWeather(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.Ok(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/user/api/internal/logic/user"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 更新用户信息
|
||||
func UpdateUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateUserReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := user.NewUpdateUserLogic(r.Context(), svcCtx)
|
||||
err := l.UpdateUser(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.Ok(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
jwtUtil "sundynix-micro-go/common/utils/jwt"
|
||||
|
||||
jwtv5 "github.com/golang-jwt/jwt/v5"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LoginByPhoneLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewLoginByPhoneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginByPhoneLogic {
|
||||
return &LoginByPhoneLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LoginByPhoneLogic) LoginByPhone(req *types.LoginByPhoneReq) (resp *types.LoginResp, err error) {
|
||||
// 1. 调用微信接口获取手机号
|
||||
// TODO: 从配置中获取access_token
|
||||
accessToken := "" // 需要通过微信API获取
|
||||
apiURL := "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken
|
||||
|
||||
data := map[string]interface{}{"code": req.Code}
|
||||
jsonData, _ := json.Marshal(data)
|
||||
|
||||
httpResp, err := http.Post(apiURL, "application/json", nil)
|
||||
if err != nil {
|
||||
l.Errorf("获取手机号失败: %v", err)
|
||||
return nil, fmt.Errorf("获取手机号失败")
|
||||
}
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(httpResp.Body)
|
||||
var dataMap map[string]interface{}
|
||||
_ = json.Unmarshal(body, &dataMap)
|
||||
_ = jsonData
|
||||
_ = url.Values{}
|
||||
|
||||
// 2. 通过 user-rpc 查询用户
|
||||
userResp, err := l.svcCtx.UserRpc.GetUserByOpenId(l.ctx, &user.GetUserByOpenIdReq{
|
||||
OpenId: req.OpenId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("用户不存在")
|
||||
}
|
||||
|
||||
// 3. 如果需要更新手机号
|
||||
phoneInfo, ok := dataMap["phone_info"].(map[string]interface{})
|
||||
if ok {
|
||||
phoneNumber, _ := phoneInfo["phoneNumber"].(string)
|
||||
if phoneNumber != "" && userResp.User.Phone == "" {
|
||||
_, _ = l.svcCtx.UserRpc.UpdateUser(l.ctx, &user.UpdateUserReq{
|
||||
Id: userResp.User.Id,
|
||||
Phone: phoneNumber,
|
||||
})
|
||||
userResp.User.Phone = phoneNumber
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 生成Token
|
||||
j := jwtUtil.NewJWT(l.svcCtx.Config.Auth.AccessSecret)
|
||||
claims := jwtUtil.CustomClaims{
|
||||
BaseClaims: jwtUtil.BaseClaims{
|
||||
ID: userResp.User.Id,
|
||||
Account: userResp.User.Account,
|
||||
},
|
||||
BufferTime: 3600,
|
||||
RegisteredClaims: jwtv5.RegisteredClaims{
|
||||
ExpiresAt: jwtv5.NewNumericDate(time.Now().Add(time.Duration(l.svcCtx.Config.Auth.AccessExpire) * time.Second)),
|
||||
Issuer: "sundynix",
|
||||
},
|
||||
}
|
||||
token, _ := j.CreateToken(claims)
|
||||
|
||||
return &types.LoginResp{
|
||||
Token: token,
|
||||
UserInfo: userResp.User,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
jwtUtil "sundynix-micro-go/common/utils/jwt"
|
||||
|
||||
jwtv5 "github.com/golang-jwt/jwt/v5"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type LoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
|
||||
return &LoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
|
||||
// 通过 user-rpc 查询用户 (account登录暂用GetUserByOpenId的方式,后续需补充account查询RPC)
|
||||
// 这里简化处理:直接在api层查询并验证密码
|
||||
// TODO: 后续应该在user-rpc中增加LoginByAccount方法
|
||||
|
||||
_ = req
|
||||
_ = l
|
||||
|
||||
return nil, status.Error(codes.Unimplemented, "账号密码登录功能开发中")
|
||||
}
|
||||
|
||||
// generateToken 生成JWT Token的辅助方法
|
||||
func generateToken(config svc.ServiceContext, userInfo *user.UserInfo) (string, error) {
|
||||
j := jwtUtil.NewJWT(config.Config.Auth.AccessSecret)
|
||||
claims := jwtUtil.CustomClaims{
|
||||
BaseClaims: jwtUtil.BaseClaims{
|
||||
ID: userInfo.Id,
|
||||
Account: userInfo.Account,
|
||||
},
|
||||
BufferTime: 3600,
|
||||
RegisteredClaims: jwtv5.RegisteredClaims{
|
||||
Audience: jwtv5.ClaimStrings{"sundynix"},
|
||||
NotBefore: jwtv5.NewNumericDate(time.Now().Add(-1000)),
|
||||
ExpiresAt: jwtv5.NewNumericDate(time.Now().Add(time.Duration(config.Config.Auth.AccessExpire) * time.Second)),
|
||||
Issuer: "sundynix",
|
||||
},
|
||||
}
|
||||
token, err := j.CreateToken(claims)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("生成Token失败: %w", err)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
jwtUtil "sundynix-micro-go/common/utils/jwt"
|
||||
|
||||
jwtv5 "github.com/golang-jwt/jwt/v5"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type MiniLoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 微信小程序登录
|
||||
func NewMiniLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniLoginLogic {
|
||||
return &MiniLoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// WxCode2SessionResp 微信code2session响应
|
||||
type WxCode2SessionResp struct {
|
||||
Openid string `json:"openid"`
|
||||
SessionKey string `json:"session_key"`
|
||||
Unionid string `json:"unionid"`
|
||||
Errcode int `json:"errcode"`
|
||||
Errmsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
func (l *MiniLoginLogic) MiniLogin(req *types.MiniLoginReq) (resp *types.LoginResp, err error) {
|
||||
// 1. 调用微信接口获取openid和session_key
|
||||
// TODO: 从配置中获取AppId和AppSecret,当前先用固定值
|
||||
appID := "wxb463820bf36dd5d6"
|
||||
appSecret := "731784a74c76c6d31fa00bb847af2c7d"
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("appid", appID)
|
||||
params.Set("secret", appSecret)
|
||||
params.Set("js_code", req.Code)
|
||||
params.Set("grant_type", "authorization_code")
|
||||
fullURL := "https://api.weixin.qq.com/sns/jscode2session?" + params.Encode()
|
||||
|
||||
httpResp, err := http.Get(fullURL)
|
||||
if err != nil {
|
||||
l.Errorf("微信登录接口请求失败: %v", err)
|
||||
return nil, fmt.Errorf("微信登录接口请求失败")
|
||||
}
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(httpResp.Body)
|
||||
if err != nil {
|
||||
l.Errorf("读取微信接口响应失败: %v", err)
|
||||
return nil, fmt.Errorf("读取微信登录接口响应失败")
|
||||
}
|
||||
|
||||
var wxResp WxCode2SessionResp
|
||||
if err = json.Unmarshal(body, &wxResp); err != nil {
|
||||
l.Errorf("解析微信接口响应失败: %v", err)
|
||||
return nil, fmt.Errorf("解析微信登录接口响应失败")
|
||||
}
|
||||
|
||||
if wxResp.Errcode != 0 {
|
||||
l.Errorf("微信接口返回错误: errcode=%d, errmsg=%s", wxResp.Errcode, wxResp.Errmsg)
|
||||
return nil, fmt.Errorf("微信登录失败: %s", wxResp.Errmsg)
|
||||
}
|
||||
|
||||
if wxResp.Openid == "" {
|
||||
return nil, fmt.Errorf("openid为空")
|
||||
}
|
||||
|
||||
// 2. 通过 user-rpc 创建或获取用户
|
||||
createResp, err := l.svcCtx.UserRpc.CreateUser(l.ctx, &user.CreateUserReq{
|
||||
Name: "",
|
||||
OpenId: wxResp.Openid,
|
||||
SessionKey: wxResp.SessionKey,
|
||||
ClientId: req.ClientId,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("创建用户失败: %v", err)
|
||||
return nil, fmt.Errorf("登录失败")
|
||||
}
|
||||
|
||||
// 3. 生成JWT Token
|
||||
j := jwtUtil.NewJWT(l.svcCtx.Config.Auth.AccessSecret)
|
||||
claims := jwtUtil.CustomClaims{
|
||||
BaseClaims: jwtUtil.BaseClaims{
|
||||
ID: createResp.User.Id,
|
||||
Account: createResp.User.Account,
|
||||
},
|
||||
BufferTime: 3600,
|
||||
RegisteredClaims: jwtv5.RegisteredClaims{
|
||||
Audience: jwtv5.ClaimStrings{"sundynix"},
|
||||
NotBefore: jwtv5.NewNumericDate(time.Now().Add(-1000)),
|
||||
ExpiresAt: jwtv5.NewNumericDate(time.Now().Add(time.Duration(l.svcCtx.Config.Auth.AccessExpire) * time.Second)),
|
||||
Issuer: "sundynix",
|
||||
},
|
||||
}
|
||||
token, err := j.CreateToken(claims)
|
||||
if err != nil {
|
||||
l.Errorf("生成Token失败: %v", err)
|
||||
return nil, fmt.Errorf("登录失败")
|
||||
}
|
||||
|
||||
return &types.LoginResp{
|
||||
Token: token,
|
||||
UserInfo: createResp.User,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ChangePasswordLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
func NewChangePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangePasswordLogic {
|
||||
return &ChangePasswordLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ChangePasswordLogic) ChangePassword(req *types.ChangePasswordReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeleteUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 删除用户
|
||||
func NewDeleteUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserLogic {
|
||||
return &DeleteUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeleteUserLogic) DeleteUser(req *types.IdsReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetLocationLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取位置信息
|
||||
func NewGetLocationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLocationLogic {
|
||||
return &GetLocationLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetLocationLogic) GetLocation(req *types.LocationReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
pb "sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetUserInfoLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
|
||||
return &GetUserInfoLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetUserInfoLogic) GetUserInfo() (resp *types.LoginResp, err error) {
|
||||
// 从JWT claims中获取userId
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
if userId == "" || userId == "<nil>" {
|
||||
return nil, fmt.Errorf("用户未登录")
|
||||
}
|
||||
|
||||
userResp, err := l.svcCtx.UserRpc.GetUserById(l.ctx, &pb.GetUserByIdReq{
|
||||
Id: userId,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("获取用户信息失败: %v", err)
|
||||
return nil, fmt.Errorf("获取用户信息失败")
|
||||
}
|
||||
|
||||
// 将proto消息转换为map以便JSON序列化
|
||||
userJSON, _ := json.Marshal(userResp.User)
|
||||
var userInfo interface{}
|
||||
_ = json.Unmarshal(userJSON, &userInfo)
|
||||
|
||||
return &types.LoginResp{
|
||||
UserInfo: userInfo,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetUserListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 用户列表
|
||||
func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
|
||||
return &GetUserListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetUserListLogic) GetUserList(req *types.UserListReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetWeatherLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取天气信息
|
||||
func NewGetWeatherLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWeatherLogic {
|
||||
return &GetWeatherLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetWeatherLogic) GetWeather(req *types.WeatherReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
pb "sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
|
||||
return &UpdateUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateUserLogic) UpdateUser(req *types.UpdateUserReq) error {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
if userId == "" || userId == "<nil>" {
|
||||
return fmt.Errorf("用户未登录")
|
||||
}
|
||||
|
||||
_, err := l.svcCtx.UserRpc.UpdateUser(l.ctx, &pb.UpdateUserReq{
|
||||
Id: userId,
|
||||
Name: req.Name,
|
||||
Account: req.Account,
|
||||
Phone: req.Phone,
|
||||
AvatarId: req.AvatarId,
|
||||
NickName: req.NickName,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("更新用户失败: %v", err)
|
||||
return fmt.Errorf("更新用户失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package svc
|
||||
|
||||
import (
|
||||
"sundynix-micro-go/app/user/api/internal/config"
|
||||
"sundynix-micro-go/app/user/rpc/userservice"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
UserRpc userservice.UserService
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
UserRpc: userservice.NewUserService(zrpc.MustNewClient(c.UserRpc)),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
|
||||
package types
|
||||
|
||||
type ChangePasswordReq struct {
|
||||
OldPassword string `json:"oldPassword"`
|
||||
NewPassword string `json:"newPassword"`
|
||||
}
|
||||
|
||||
type IdReq struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
type IdsReq struct {
|
||||
Ids []string `json:"ids"`
|
||||
}
|
||||
|
||||
type LocationReq struct {
|
||||
Longitude string `form:"longitude"`
|
||||
Latitude string `form:"latitude"`
|
||||
}
|
||||
|
||||
type LoginByPhoneReq struct {
|
||||
Code string `json:"code"`
|
||||
OpenId string `json:"openId"`
|
||||
ClientId string `json:"clientId"`
|
||||
}
|
||||
|
||||
type LoginReq struct {
|
||||
Account string `json:"account"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type LoginResp struct {
|
||||
Token string `json:"token"`
|
||||
UserInfo interface{} `json:"userInfo"`
|
||||
}
|
||||
|
||||
type MiniLoginReq struct {
|
||||
Code string `json:"code"`
|
||||
ClientId string `json:"clientId"`
|
||||
}
|
||||
|
||||
type UpdateUserReq struct {
|
||||
Name string `json:"name,optional"`
|
||||
Account string `json:"account,optional"`
|
||||
Phone string `json:"phone,optional"`
|
||||
AvatarId string `json:"avatarId,optional"`
|
||||
NickName string `json:"nickName,optional"`
|
||||
}
|
||||
|
||||
type UserListReq struct {
|
||||
Current int `json:"current,optional"`
|
||||
PageSize int `json:"pageSize,optional"`
|
||||
Account string `json:"account,optional"`
|
||||
Phone string `json:"phone,optional"`
|
||||
}
|
||||
|
||||
type WeatherReq struct {
|
||||
Adcode string `form:"adcode"`
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
syntax = "v1"
|
||||
|
||||
info (
|
||||
title: "用户服务API"
|
||||
desc: "用户登录、信息管理等HTTP接口"
|
||||
author: "sundynix"
|
||||
version: "v1.0.0"
|
||||
)
|
||||
|
||||
type (
|
||||
// 微信小程序登录
|
||||
MiniLoginReq {
|
||||
Code string `json:"code"`
|
||||
ClientId string `json:"clientId"`
|
||||
}
|
||||
// 手机号登录
|
||||
LoginByPhoneReq {
|
||||
Code string `json:"code"`
|
||||
OpenId string `json:"openId"`
|
||||
ClientId string `json:"clientId"`
|
||||
}
|
||||
// 账号密码登录
|
||||
LoginReq {
|
||||
Account string `json:"account"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
// 登录响应
|
||||
LoginResp {
|
||||
Token string `json:"token"`
|
||||
UserInfo interface{} `json:"userInfo"`
|
||||
}
|
||||
// 更新用户
|
||||
UpdateUserReq {
|
||||
Name string `json:"name,optional"`
|
||||
Account string `json:"account,optional"`
|
||||
Phone string `json:"phone,optional"`
|
||||
AvatarId string `json:"avatarId,optional"`
|
||||
NickName string `json:"nickName,optional"`
|
||||
}
|
||||
// 修改密码
|
||||
ChangePasswordReq {
|
||||
OldPassword string `json:"oldPassword"`
|
||||
NewPassword string `json:"newPassword"`
|
||||
}
|
||||
// 用户列表查询
|
||||
UserListReq {
|
||||
Current int `json:"current,optional"`
|
||||
PageSize int `json:"pageSize,optional"`
|
||||
Account string `json:"account,optional"`
|
||||
Phone string `json:"phone,optional"`
|
||||
}
|
||||
// 通用ID请求
|
||||
IdReq {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
// 批量ID请求
|
||||
IdsReq {
|
||||
Ids []string `json:"ids"`
|
||||
}
|
||||
// 获取位置
|
||||
LocationReq {
|
||||
Longitude string `form:"longitude"`
|
||||
Latitude string `form:"latitude"`
|
||||
}
|
||||
// 获取天气
|
||||
WeatherReq {
|
||||
Adcode string `form:"adcode"`
|
||||
}
|
||||
)
|
||||
|
||||
// ========== 无需鉴权的接口 ==========
|
||||
@server (
|
||||
prefix: /api/user
|
||||
group: auth
|
||||
)
|
||||
service user-api {
|
||||
@doc "微信小程序登录"
|
||||
@handler MiniLogin
|
||||
post /miniLogin (MiniLoginReq) returns (LoginResp)
|
||||
|
||||
@doc "手机号登录"
|
||||
@handler LoginByPhone
|
||||
post /loginByPhone (LoginByPhoneReq) returns (LoginResp)
|
||||
|
||||
@doc "账号密码登录"
|
||||
@handler Login
|
||||
post /login (LoginReq) returns (LoginResp)
|
||||
}
|
||||
|
||||
// ========== 需要鉴权的接口 ==========
|
||||
@server (
|
||||
prefix: /api/user
|
||||
group: user
|
||||
jwt: Auth
|
||||
)
|
||||
service user-api {
|
||||
@doc "获取当前用户信息"
|
||||
@handler GetUserInfo
|
||||
get /info returns (LoginResp)
|
||||
|
||||
@doc "更新用户信息"
|
||||
@handler UpdateUser
|
||||
put /update (UpdateUserReq)
|
||||
|
||||
@doc "修改密码"
|
||||
@handler ChangePassword
|
||||
put /changePassword (ChangePasswordReq)
|
||||
|
||||
@doc "用户列表"
|
||||
@handler GetUserList
|
||||
post /list (UserListReq)
|
||||
|
||||
@doc "删除用户"
|
||||
@handler DeleteUser
|
||||
delete /delete (IdsReq)
|
||||
|
||||
@doc "获取位置信息"
|
||||
@handler GetLocation
|
||||
get /location (LocationReq)
|
||||
|
||||
@doc "获取天气信息"
|
||||
@handler GetWeather
|
||||
get /weather (WeatherReq)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/config"
|
||||
"sundynix-micro-go/app/user/api/internal/handler"
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/user-api.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"sundynix-micro-go/common/model"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SundynixUser 用户基础表(仅存储认证和通用信息)
|
||||
type SundynixUser struct {
|
||||
model.BaseModel
|
||||
TenantID string `gorm:"size:20;column:tenant_id" json:"tenantId"`
|
||||
ClientID string `gorm:"size:20;column:client_id" json:"clientId"`
|
||||
Name string `gorm:"size:100;column:name" json:"name"`
|
||||
Account string `gorm:"size:50;column:account" json:"account"`
|
||||
Password string `gorm:"size:100;column:password" json:"-"`
|
||||
NickName string `gorm:"size:100;column:nick_name" json:"nickName"`
|
||||
Phone string `gorm:"size:20;column:phone" json:"phone"`
|
||||
SessionKey string `gorm:"size:80;column:session_key" json:"-"`
|
||||
UnionID string `gorm:"size:80;column:union_id" json:"unionId"`
|
||||
OpenID string `gorm:"size:80;column:open_id;index" json:"openId"`
|
||||
SaOpenID string `gorm:"size:80;column:sa_open_id" json:"saOpenId"`
|
||||
AvatarID string `gorm:"size:50;column:avatar_id" json:"avatarId"`
|
||||
Gender int `gorm:"default:0;column:gender" json:"gender"`
|
||||
LastLoginIP string `gorm:"size:20;column:last_login_ip" json:"lastLoginIp"`
|
||||
LastLoginAt *time.Time `gorm:"column:last_login_at" json:"lastLoginAt"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (SundynixUser) TableName() string {
|
||||
return "sundynix_user"
|
||||
}
|
||||
|
||||
// SundynixUserRole 用户角色关联表
|
||||
type SundynixUserRole struct {
|
||||
UserID string `gorm:"size:50;primaryKey;column:user_id" json:"userId"`
|
||||
RoleID string `gorm:"size:50;primaryKey;column:role_id" json:"roleId"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (SundynixUserRole) TableName() string {
|
||||
return "sundynix_user_role"
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
Name: user.rpc
|
||||
ListenOn: 0.0.0.0:9101
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 192.168.100.127:2379
|
||||
Key: user.rpc
|
||||
|
||||
# MySQL
|
||||
DB:
|
||||
DataSource: root:root@tcp(192.168.100.127:3307)/sundynix_micro_go?charset=utf8mb4&parseTime=True&loc=Local
|
||||
|
||||
# Redis
|
||||
Cache:
|
||||
- Host: 127.0.0.1:6379
|
||||
Pass: sundynix
|
||||
Type: node
|
||||
|
||||
# JWT
|
||||
JwtAuth:
|
||||
AccessSecret: 9149f2eb-d517-4a50-a03a-231dbcf0d872
|
||||
AccessExpire: 7200
|
||||
|
||||
# 微信小程序配置
|
||||
WxMini:
|
||||
AppId: wxb463820bf36dd5d6
|
||||
AppSecret: 731784a74c76c6d31fa00bb847af2c7d
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
DB struct {
|
||||
DataSource string
|
||||
}
|
||||
Cache []struct {
|
||||
Host string
|
||||
Pass string
|
||||
Type string
|
||||
}
|
||||
JwtAuth struct {
|
||||
AccessSecret string
|
||||
AccessExpire int64
|
||||
}
|
||||
WxMini struct {
|
||||
AppId string
|
||||
AppSecret string
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
"sundynix-micro-go/common/utils/hash"
|
||||
"sundynix-micro-go/common/utils/uniqueid"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CreateUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
|
||||
return &CreateUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
func (l *CreateUserLogic) CreateUser(in *user.CreateUserReq) (*user.CreateUserResp, error) {
|
||||
// 如果有OpenID,先检查是否存在
|
||||
if in.OpenId != "" {
|
||||
var existing model.SundynixUser
|
||||
err := l.svcCtx.DB.Where("open_id = ?", in.OpenId).First(&existing).Error
|
||||
if err == nil {
|
||||
// 用户已存在,更新session_key
|
||||
if in.SessionKey != "" {
|
||||
l.svcCtx.DB.Model(&existing).UpdateColumn("session_key", in.SessionKey)
|
||||
}
|
||||
return &user.CreateUserResp{
|
||||
User: convertUserToProto(&existing),
|
||||
}, nil
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
l.Errorf("查询用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询用户失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 创建新用户
|
||||
name := in.Name
|
||||
if name == "" {
|
||||
name = uniqueid.GenerateName("用户")
|
||||
}
|
||||
|
||||
newUser := model.SundynixUser{
|
||||
Name: name,
|
||||
OpenID: in.OpenId,
|
||||
SessionKey: in.SessionKey,
|
||||
ClientID: in.ClientId,
|
||||
Phone: in.Phone,
|
||||
}
|
||||
|
||||
// 如果有密码则加密
|
||||
if in.Phone != "" {
|
||||
newUser.Password = hash.BcryptHash(in.Phone) // 默认密码为手机号
|
||||
}
|
||||
|
||||
if err := l.svcCtx.DB.Create(&newUser).Error; err != nil {
|
||||
l.Errorf("创建用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "创建用户失败")
|
||||
}
|
||||
|
||||
return &user.CreateUserResp{
|
||||
User: convertUserToProto(&newUser),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GetUserByIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetUserByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByIdLogic {
|
||||
return &GetUserByIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID获取用户信息
|
||||
func (l *GetUserByIdLogic) GetUserById(in *user.GetUserByIdReq) (*user.GetUserByIdResp, error) {
|
||||
var u model.SundynixUser
|
||||
err := l.svcCtx.DB.Where("id = ?", in.Id).First(&u).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, status.Error(codes.NotFound, "用户不存在")
|
||||
}
|
||||
l.Errorf("查询用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询用户失败")
|
||||
}
|
||||
|
||||
return &user.GetUserByIdResp{
|
||||
User: convertUserToProto(&u),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// convertUserToProto 将GORM模型转换为proto消息
|
||||
func convertUserToProto(u *model.SundynixUser) *user.UserInfo {
|
||||
info := &user.UserInfo{
|
||||
Id: u.ID,
|
||||
TenantId: u.TenantID,
|
||||
ClientId: u.ClientID,
|
||||
Name: u.Name,
|
||||
Account: u.Account,
|
||||
NickName: u.NickName,
|
||||
Phone: u.Phone,
|
||||
SessionKey: u.SessionKey,
|
||||
UnionId: u.UnionID,
|
||||
OpenId: u.OpenID,
|
||||
SaOpenId: u.SaOpenID,
|
||||
AvatarId: u.AvatarID,
|
||||
Gender: int32(u.Gender),
|
||||
CreatedAt: u.CreatedAt.Unix(),
|
||||
UpdatedAt: u.UpdatedAt.Unix(),
|
||||
}
|
||||
if u.LastLoginAt != nil {
|
||||
info.LastLoginAt = u.LastLoginAt.Unix()
|
||||
}
|
||||
return info
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GetUserByOpenIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetUserByOpenIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByOpenIdLogic {
|
||||
return &GetUserByOpenIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 根据OpenId获取用户信息
|
||||
func (l *GetUserByOpenIdLogic) GetUserByOpenId(in *user.GetUserByOpenIdReq) (*user.GetUserByOpenIdResp, error) {
|
||||
var u model.SundynixUser
|
||||
err := l.svcCtx.DB.Where("open_id = ?", in.OpenId).First(&u).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, status.Error(codes.NotFound, "用户不存在")
|
||||
}
|
||||
l.Errorf("查询用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询用户失败")
|
||||
}
|
||||
|
||||
return &user.GetUserByOpenIdResp{
|
||||
User: convertUserToProto(&u),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type UpdateUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
|
||||
return &UpdateUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
func (l *UpdateUserLogic) UpdateUser(in *user.UpdateUserReq) (*user.CommonResp, error) {
|
||||
var u model.SundynixUser
|
||||
if err := l.svcCtx.DB.Where("id = ?", in.Id).First(&u).Error; err != nil {
|
||||
return nil, status.Error(codes.NotFound, "用户不存在")
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{}
|
||||
if in.Name != "" {
|
||||
updates["name"] = in.Name
|
||||
}
|
||||
if in.Account != "" {
|
||||
updates["account"] = in.Account
|
||||
}
|
||||
if in.Phone != "" {
|
||||
updates["phone"] = in.Phone
|
||||
}
|
||||
if in.AvatarId != "" {
|
||||
updates["avatar_id"] = in.AvatarId
|
||||
}
|
||||
if in.NickName != "" {
|
||||
updates["nick_name"] = in.NickName
|
||||
}
|
||||
|
||||
if len(updates) > 0 {
|
||||
if err := l.svcCtx.DB.Model(&u).Updates(updates).Error; err != nil {
|
||||
l.Errorf("更新用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "更新用户失败")
|
||||
}
|
||||
}
|
||||
|
||||
return &user.CommonResp{
|
||||
Code: 200,
|
||||
Msg: "更新成功",
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type VerifyTokenLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewVerifyTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VerifyTokenLogic {
|
||||
return &VerifyTokenLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 验证Token有效性
|
||||
func (l *VerifyTokenLogic) VerifyToken(in *user.VerifyTokenReq) (*user.VerifyTokenResp, error) {
|
||||
claims, err := l.svcCtx.JWT.ParseToken(in.Token)
|
||||
if err != nil {
|
||||
return &user.VerifyTokenResp{
|
||||
Valid: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 检查Redis黑名单
|
||||
if l.svcCtx.Redis != nil {
|
||||
blacklistKey := "jwt:blacklist:" + claims.BaseClaims.ID
|
||||
val, _ := l.svcCtx.Redis.Get(l.ctx, blacklistKey).Result()
|
||||
if val == in.Token {
|
||||
return &user.VerifyTokenResp{
|
||||
Valid: false,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return &user.VerifyTokenResp{
|
||||
Valid: true,
|
||||
UserId: claims.BaseClaims.ID,
|
||||
Account: claims.BaseClaims.Account,
|
||||
ExpiresAt: claims.ExpiresAt.Unix(),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
// Source: user.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/rpc/internal/logic"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
)
|
||||
|
||||
type UserServiceServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
user.UnimplementedUserServiceServer
|
||||
}
|
||||
|
||||
func NewUserServiceServer(svcCtx *svc.ServiceContext) *UserServiceServer {
|
||||
return &UserServiceServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID获取用户信息
|
||||
func (s *UserServiceServer) GetUserById(ctx context.Context, in *user.GetUserByIdReq) (*user.GetUserByIdResp, error) {
|
||||
l := logic.NewGetUserByIdLogic(ctx, s.svcCtx)
|
||||
return l.GetUserById(in)
|
||||
}
|
||||
|
||||
// 根据OpenId获取用户信息
|
||||
func (s *UserServiceServer) GetUserByOpenId(ctx context.Context, in *user.GetUserByOpenIdReq) (*user.GetUserByOpenIdResp, error) {
|
||||
l := logic.NewGetUserByOpenIdLogic(ctx, s.svcCtx)
|
||||
return l.GetUserByOpenId(in)
|
||||
}
|
||||
|
||||
// 验证Token有效性
|
||||
func (s *UserServiceServer) VerifyToken(ctx context.Context, in *user.VerifyTokenReq) (*user.VerifyTokenResp, error) {
|
||||
l := logic.NewVerifyTokenLogic(ctx, s.svcCtx)
|
||||
return l.VerifyToken(in)
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
func (s *UserServiceServer) CreateUser(ctx context.Context, in *user.CreateUserReq) (*user.CreateUserResp, error) {
|
||||
l := logic.NewCreateUserLogic(ctx, s.svcCtx)
|
||||
return l.CreateUser(in)
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
func (s *UserServiceServer) UpdateUser(ctx context.Context, in *user.UpdateUserReq) (*user.CommonResp, error) {
|
||||
l := logic.NewUpdateUserLogic(ctx, s.svcCtx)
|
||||
return l.UpdateUser(in)
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/config"
|
||||
"sundynix-micro-go/common/utils/jwt"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
Redis *redis.Client
|
||||
JWT *jwt.JWT
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
// 初始化GORM
|
||||
db, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{})
|
||||
if err != nil {
|
||||
logx.Errorf("连接数据库失败: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 自动迁移
|
||||
if err := db.AutoMigrate(&model.SundynixUser{}, &model.SundynixUserRole{}); err != nil {
|
||||
logx.Errorf("数据库迁移失败: %v", err)
|
||||
}
|
||||
|
||||
// 初始化Redis
|
||||
var rdb *redis.Client
|
||||
if len(c.Cache) > 0 {
|
||||
rdb = redis.NewClient(&redis.Options{
|
||||
Addr: c.Cache[0].Host,
|
||||
Password: c.Cache[0].Pass,
|
||||
})
|
||||
}
|
||||
|
||||
// 初始化JWT
|
||||
jwtUtil := jwt.NewJWT(c.JwtAuth.AccessSecret)
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
Redis: rdb,
|
||||
JWT: jwtUtil,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package user;
|
||||
|
||||
option go_package = "./user";
|
||||
|
||||
// ---------- 通用消息 ----------
|
||||
|
||||
message CommonResp {
|
||||
int64 code = 1;
|
||||
string msg = 2;
|
||||
}
|
||||
|
||||
// ---------- 用户信息 ----------
|
||||
|
||||
message UserInfo {
|
||||
string id = 1;
|
||||
string tenantId = 2;
|
||||
string clientId = 3;
|
||||
string name = 4;
|
||||
string account = 5;
|
||||
string nickName = 6;
|
||||
string phone = 7;
|
||||
string sessionKey = 8;
|
||||
string unionId = 9;
|
||||
string openId = 10;
|
||||
string saOpenId = 11;
|
||||
string avatarId = 12;
|
||||
int32 gender = 13;
|
||||
string country = 14;
|
||||
string province = 15;
|
||||
string city = 16;
|
||||
string language = 17;
|
||||
int32 isVip = 18;
|
||||
int64 vipExpireAt = 19;
|
||||
string lastLoginIp = 20;
|
||||
int64 lastLoginAt = 21;
|
||||
int64 createdAt = 22;
|
||||
int64 updatedAt = 23;
|
||||
string avatarUrl = 24;
|
||||
}
|
||||
|
||||
// ---------- 请求/响应消息 ----------
|
||||
|
||||
message GetUserByIdReq {
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
message GetUserByIdResp {
|
||||
UserInfo user = 1;
|
||||
}
|
||||
|
||||
message GetUserByOpenIdReq {
|
||||
string openId = 1;
|
||||
}
|
||||
|
||||
message GetUserByOpenIdResp {
|
||||
UserInfo user = 1;
|
||||
}
|
||||
|
||||
message VerifyTokenReq {
|
||||
string token = 1;
|
||||
}
|
||||
|
||||
message VerifyTokenResp {
|
||||
bool valid = 1;
|
||||
string userId = 2;
|
||||
string account = 3;
|
||||
int64 expiresAt = 4;
|
||||
}
|
||||
|
||||
message CreateUserReq {
|
||||
string name = 1;
|
||||
string openId = 2;
|
||||
string sessionKey = 3;
|
||||
string clientId = 4;
|
||||
string phone = 5;
|
||||
}
|
||||
|
||||
message CreateUserResp {
|
||||
UserInfo user = 1;
|
||||
}
|
||||
|
||||
message UpdateUserReq {
|
||||
string id = 1;
|
||||
string name = 2;
|
||||
string account = 3;
|
||||
string phone = 4;
|
||||
string avatarId = 5;
|
||||
string nickName = 6;
|
||||
}
|
||||
|
||||
// ---------- 服务定义 ----------
|
||||
|
||||
service UserService {
|
||||
// 根据ID获取用户信息
|
||||
rpc GetUserById(GetUserByIdReq) returns (GetUserByIdResp);
|
||||
// 根据OpenId获取用户信息
|
||||
rpc GetUserByOpenId(GetUserByOpenIdReq) returns (GetUserByOpenIdResp);
|
||||
// 验证Token有效性
|
||||
rpc VerifyToken(VerifyTokenReq) returns (VerifyTokenResp);
|
||||
// 创建用户
|
||||
rpc CreateUser(CreateUserReq) returns (CreateUserResp);
|
||||
// 更新用户信息
|
||||
rpc UpdateUser(UpdateUserReq) returns (CommonResp);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/user/rpc/internal/config"
|
||||
"sundynix-micro-go/app/user/rpc/internal/server"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/user.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
|
||||
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
user.RegisterUserServiceServer(grpcServer, server.NewUserServiceServer(ctx))
|
||||
|
||||
if c.Mode == service.DevMode || c.Mode == service.TestMode {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
})
|
||||
defer s.Stop()
|
||||
|
||||
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||
s.Start()
|
||||
}
|
||||
@@ -0,0 +1,943 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.11
|
||||
// protoc v7.34.1
|
||||
// source: pb/user.proto
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type CommonResp struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Code int64 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
|
||||
Msg string `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CommonResp) Reset() {
|
||||
*x = CommonResp{}
|
||||
mi := &file_pb_user_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *CommonResp) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CommonResp) ProtoMessage() {}
|
||||
|
||||
func (x *CommonResp) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[0]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CommonResp.ProtoReflect.Descriptor instead.
|
||||
func (*CommonResp) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *CommonResp) GetCode() int64 {
|
||||
if x != nil {
|
||||
return x.Code
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *CommonResp) GetMsg() string {
|
||||
if x != nil {
|
||||
return x.Msg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type UserInfo struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
TenantId string `protobuf:"bytes,2,opt,name=tenantId,proto3" json:"tenantId,omitempty"`
|
||||
ClientId string `protobuf:"bytes,3,opt,name=clientId,proto3" json:"clientId,omitempty"`
|
||||
Name string `protobuf:"bytes,4,opt,name=name,proto3" json:"name,omitempty"`
|
||||
Account string `protobuf:"bytes,5,opt,name=account,proto3" json:"account,omitempty"`
|
||||
NickName string `protobuf:"bytes,6,opt,name=nickName,proto3" json:"nickName,omitempty"`
|
||||
Phone string `protobuf:"bytes,7,opt,name=phone,proto3" json:"phone,omitempty"`
|
||||
SessionKey string `protobuf:"bytes,8,opt,name=sessionKey,proto3" json:"sessionKey,omitempty"`
|
||||
UnionId string `protobuf:"bytes,9,opt,name=unionId,proto3" json:"unionId,omitempty"`
|
||||
OpenId string `protobuf:"bytes,10,opt,name=openId,proto3" json:"openId,omitempty"`
|
||||
SaOpenId string `protobuf:"bytes,11,opt,name=saOpenId,proto3" json:"saOpenId,omitempty"`
|
||||
AvatarId string `protobuf:"bytes,12,opt,name=avatarId,proto3" json:"avatarId,omitempty"`
|
||||
Gender int32 `protobuf:"varint,13,opt,name=gender,proto3" json:"gender,omitempty"`
|
||||
Country string `protobuf:"bytes,14,opt,name=country,proto3" json:"country,omitempty"`
|
||||
Province string `protobuf:"bytes,15,opt,name=province,proto3" json:"province,omitempty"`
|
||||
City string `protobuf:"bytes,16,opt,name=city,proto3" json:"city,omitempty"`
|
||||
Language string `protobuf:"bytes,17,opt,name=language,proto3" json:"language,omitempty"`
|
||||
IsVip int32 `protobuf:"varint,18,opt,name=isVip,proto3" json:"isVip,omitempty"`
|
||||
VipExpireAt int64 `protobuf:"varint,19,opt,name=vipExpireAt,proto3" json:"vipExpireAt,omitempty"`
|
||||
LastLoginIp string `protobuf:"bytes,20,opt,name=lastLoginIp,proto3" json:"lastLoginIp,omitempty"`
|
||||
LastLoginAt int64 `protobuf:"varint,21,opt,name=lastLoginAt,proto3" json:"lastLoginAt,omitempty"`
|
||||
CreatedAt int64 `protobuf:"varint,22,opt,name=createdAt,proto3" json:"createdAt,omitempty"`
|
||||
UpdatedAt int64 `protobuf:"varint,23,opt,name=updatedAt,proto3" json:"updatedAt,omitempty"`
|
||||
AvatarUrl string `protobuf:"bytes,24,opt,name=avatarUrl,proto3" json:"avatarUrl,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *UserInfo) Reset() {
|
||||
*x = UserInfo{}
|
||||
mi := &file_pb_user_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *UserInfo) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*UserInfo) ProtoMessage() {}
|
||||
|
||||
func (x *UserInfo) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[1]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use UserInfo.ProtoReflect.Descriptor instead.
|
||||
func (*UserInfo) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetTenantId() string {
|
||||
if x != nil {
|
||||
return x.TenantId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetClientId() string {
|
||||
if x != nil {
|
||||
return x.ClientId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetName() string {
|
||||
if x != nil {
|
||||
return x.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetAccount() string {
|
||||
if x != nil {
|
||||
return x.Account
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetNickName() string {
|
||||
if x != nil {
|
||||
return x.NickName
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetPhone() string {
|
||||
if x != nil {
|
||||
return x.Phone
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetSessionKey() string {
|
||||
if x != nil {
|
||||
return x.SessionKey
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetUnionId() string {
|
||||
if x != nil {
|
||||
return x.UnionId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetOpenId() string {
|
||||
if x != nil {
|
||||
return x.OpenId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetSaOpenId() string {
|
||||
if x != nil {
|
||||
return x.SaOpenId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetAvatarId() string {
|
||||
if x != nil {
|
||||
return x.AvatarId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetGender() int32 {
|
||||
if x != nil {
|
||||
return x.Gender
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetCountry() string {
|
||||
if x != nil {
|
||||
return x.Country
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetProvince() string {
|
||||
if x != nil {
|
||||
return x.Province
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetCity() string {
|
||||
if x != nil {
|
||||
return x.City
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetLanguage() string {
|
||||
if x != nil {
|
||||
return x.Language
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetIsVip() int32 {
|
||||
if x != nil {
|
||||
return x.IsVip
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetVipExpireAt() int64 {
|
||||
if x != nil {
|
||||
return x.VipExpireAt
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetLastLoginIp() string {
|
||||
if x != nil {
|
||||
return x.LastLoginIp
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetLastLoginAt() int64 {
|
||||
if x != nil {
|
||||
return x.LastLoginAt
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetCreatedAt() int64 {
|
||||
if x != nil {
|
||||
return x.CreatedAt
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetUpdatedAt() int64 {
|
||||
if x != nil {
|
||||
return x.UpdatedAt
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *UserInfo) GetAvatarUrl() string {
|
||||
if x != nil {
|
||||
return x.AvatarUrl
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetUserByIdReq struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetUserByIdReq) Reset() {
|
||||
*x = GetUserByIdReq{}
|
||||
mi := &file_pb_user_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetUserByIdReq) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetUserByIdReq) ProtoMessage() {}
|
||||
|
||||
func (x *GetUserByIdReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[2]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetUserByIdReq.ProtoReflect.Descriptor instead.
|
||||
func (*GetUserByIdReq) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *GetUserByIdReq) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetUserByIdResp struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
User *UserInfo `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetUserByIdResp) Reset() {
|
||||
*x = GetUserByIdResp{}
|
||||
mi := &file_pb_user_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetUserByIdResp) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetUserByIdResp) ProtoMessage() {}
|
||||
|
||||
func (x *GetUserByIdResp) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[3]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetUserByIdResp.ProtoReflect.Descriptor instead.
|
||||
func (*GetUserByIdResp) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *GetUserByIdResp) GetUser() *UserInfo {
|
||||
if x != nil {
|
||||
return x.User
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type GetUserByOpenIdReq struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
OpenId string `protobuf:"bytes,1,opt,name=openId,proto3" json:"openId,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetUserByOpenIdReq) Reset() {
|
||||
*x = GetUserByOpenIdReq{}
|
||||
mi := &file_pb_user_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetUserByOpenIdReq) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetUserByOpenIdReq) ProtoMessage() {}
|
||||
|
||||
func (x *GetUserByOpenIdReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[4]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetUserByOpenIdReq.ProtoReflect.Descriptor instead.
|
||||
func (*GetUserByOpenIdReq) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *GetUserByOpenIdReq) GetOpenId() string {
|
||||
if x != nil {
|
||||
return x.OpenId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetUserByOpenIdResp struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
User *UserInfo `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetUserByOpenIdResp) Reset() {
|
||||
*x = GetUserByOpenIdResp{}
|
||||
mi := &file_pb_user_proto_msgTypes[5]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetUserByOpenIdResp) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetUserByOpenIdResp) ProtoMessage() {}
|
||||
|
||||
func (x *GetUserByOpenIdResp) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[5]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetUserByOpenIdResp.ProtoReflect.Descriptor instead.
|
||||
func (*GetUserByOpenIdResp) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{5}
|
||||
}
|
||||
|
||||
func (x *GetUserByOpenIdResp) GetUser() *UserInfo {
|
||||
if x != nil {
|
||||
return x.User
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type VerifyTokenReq struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *VerifyTokenReq) Reset() {
|
||||
*x = VerifyTokenReq{}
|
||||
mi := &file_pb_user_proto_msgTypes[6]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *VerifyTokenReq) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*VerifyTokenReq) ProtoMessage() {}
|
||||
|
||||
func (x *VerifyTokenReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[6]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use VerifyTokenReq.ProtoReflect.Descriptor instead.
|
||||
func (*VerifyTokenReq) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{6}
|
||||
}
|
||||
|
||||
func (x *VerifyTokenReq) GetToken() string {
|
||||
if x != nil {
|
||||
return x.Token
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type VerifyTokenResp struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Valid bool `protobuf:"varint,1,opt,name=valid,proto3" json:"valid,omitempty"`
|
||||
UserId string `protobuf:"bytes,2,opt,name=userId,proto3" json:"userId,omitempty"`
|
||||
Account string `protobuf:"bytes,3,opt,name=account,proto3" json:"account,omitempty"`
|
||||
ExpiresAt int64 `protobuf:"varint,4,opt,name=expiresAt,proto3" json:"expiresAt,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *VerifyTokenResp) Reset() {
|
||||
*x = VerifyTokenResp{}
|
||||
mi := &file_pb_user_proto_msgTypes[7]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *VerifyTokenResp) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*VerifyTokenResp) ProtoMessage() {}
|
||||
|
||||
func (x *VerifyTokenResp) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[7]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use VerifyTokenResp.ProtoReflect.Descriptor instead.
|
||||
func (*VerifyTokenResp) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{7}
|
||||
}
|
||||
|
||||
func (x *VerifyTokenResp) GetValid() bool {
|
||||
if x != nil {
|
||||
return x.Valid
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (x *VerifyTokenResp) GetUserId() string {
|
||||
if x != nil {
|
||||
return x.UserId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *VerifyTokenResp) GetAccount() string {
|
||||
if x != nil {
|
||||
return x.Account
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *VerifyTokenResp) GetExpiresAt() int64 {
|
||||
if x != nil {
|
||||
return x.ExpiresAt
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type CreateUserReq struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
OpenId string `protobuf:"bytes,2,opt,name=openId,proto3" json:"openId,omitempty"`
|
||||
SessionKey string `protobuf:"bytes,3,opt,name=sessionKey,proto3" json:"sessionKey,omitempty"`
|
||||
ClientId string `protobuf:"bytes,4,opt,name=clientId,proto3" json:"clientId,omitempty"`
|
||||
Phone string `protobuf:"bytes,5,opt,name=phone,proto3" json:"phone,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CreateUserReq) Reset() {
|
||||
*x = CreateUserReq{}
|
||||
mi := &file_pb_user_proto_msgTypes[8]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *CreateUserReq) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CreateUserReq) ProtoMessage() {}
|
||||
|
||||
func (x *CreateUserReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[8]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CreateUserReq.ProtoReflect.Descriptor instead.
|
||||
func (*CreateUserReq) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{8}
|
||||
}
|
||||
|
||||
func (x *CreateUserReq) GetName() string {
|
||||
if x != nil {
|
||||
return x.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CreateUserReq) GetOpenId() string {
|
||||
if x != nil {
|
||||
return x.OpenId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CreateUserReq) GetSessionKey() string {
|
||||
if x != nil {
|
||||
return x.SessionKey
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CreateUserReq) GetClientId() string {
|
||||
if x != nil {
|
||||
return x.ClientId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CreateUserReq) GetPhone() string {
|
||||
if x != nil {
|
||||
return x.Phone
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type CreateUserResp struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
User *UserInfo `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CreateUserResp) Reset() {
|
||||
*x = CreateUserResp{}
|
||||
mi := &file_pb_user_proto_msgTypes[9]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *CreateUserResp) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CreateUserResp) ProtoMessage() {}
|
||||
|
||||
func (x *CreateUserResp) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[9]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CreateUserResp.ProtoReflect.Descriptor instead.
|
||||
func (*CreateUserResp) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{9}
|
||||
}
|
||||
|
||||
func (x *CreateUserResp) GetUser() *UserInfo {
|
||||
if x != nil {
|
||||
return x.User
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type UpdateUserReq struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
|
||||
Account string `protobuf:"bytes,3,opt,name=account,proto3" json:"account,omitempty"`
|
||||
Phone string `protobuf:"bytes,4,opt,name=phone,proto3" json:"phone,omitempty"`
|
||||
AvatarId string `protobuf:"bytes,5,opt,name=avatarId,proto3" json:"avatarId,omitempty"`
|
||||
NickName string `protobuf:"bytes,6,opt,name=nickName,proto3" json:"nickName,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *UpdateUserReq) Reset() {
|
||||
*x = UpdateUserReq{}
|
||||
mi := &file_pb_user_proto_msgTypes[10]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *UpdateUserReq) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*UpdateUserReq) ProtoMessage() {}
|
||||
|
||||
func (x *UpdateUserReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_user_proto_msgTypes[10]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use UpdateUserReq.ProtoReflect.Descriptor instead.
|
||||
func (*UpdateUserReq) Descriptor() ([]byte, []int) {
|
||||
return file_pb_user_proto_rawDescGZIP(), []int{10}
|
||||
}
|
||||
|
||||
func (x *UpdateUserReq) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UpdateUserReq) GetName() string {
|
||||
if x != nil {
|
||||
return x.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UpdateUserReq) GetAccount() string {
|
||||
if x != nil {
|
||||
return x.Account
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UpdateUserReq) GetPhone() string {
|
||||
if x != nil {
|
||||
return x.Phone
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UpdateUserReq) GetAvatarId() string {
|
||||
if x != nil {
|
||||
return x.AvatarId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UpdateUserReq) GetNickName() string {
|
||||
if x != nil {
|
||||
return x.NickName
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_pb_user_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_pb_user_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\rpb/user.proto\x12\x04user\"2\n" +
|
||||
"\n" +
|
||||
"CommonResp\x12\x12\n" +
|
||||
"\x04code\x18\x01 \x01(\x03R\x04code\x12\x10\n" +
|
||||
"\x03msg\x18\x02 \x01(\tR\x03msg\"\x90\x05\n" +
|
||||
"\bUserInfo\x12\x0e\n" +
|
||||
"\x02id\x18\x01 \x01(\tR\x02id\x12\x1a\n" +
|
||||
"\btenantId\x18\x02 \x01(\tR\btenantId\x12\x1a\n" +
|
||||
"\bclientId\x18\x03 \x01(\tR\bclientId\x12\x12\n" +
|
||||
"\x04name\x18\x04 \x01(\tR\x04name\x12\x18\n" +
|
||||
"\aaccount\x18\x05 \x01(\tR\aaccount\x12\x1a\n" +
|
||||
"\bnickName\x18\x06 \x01(\tR\bnickName\x12\x14\n" +
|
||||
"\x05phone\x18\a \x01(\tR\x05phone\x12\x1e\n" +
|
||||
"\n" +
|
||||
"sessionKey\x18\b \x01(\tR\n" +
|
||||
"sessionKey\x12\x18\n" +
|
||||
"\aunionId\x18\t \x01(\tR\aunionId\x12\x16\n" +
|
||||
"\x06openId\x18\n" +
|
||||
" \x01(\tR\x06openId\x12\x1a\n" +
|
||||
"\bsaOpenId\x18\v \x01(\tR\bsaOpenId\x12\x1a\n" +
|
||||
"\bavatarId\x18\f \x01(\tR\bavatarId\x12\x16\n" +
|
||||
"\x06gender\x18\r \x01(\x05R\x06gender\x12\x18\n" +
|
||||
"\acountry\x18\x0e \x01(\tR\acountry\x12\x1a\n" +
|
||||
"\bprovince\x18\x0f \x01(\tR\bprovince\x12\x12\n" +
|
||||
"\x04city\x18\x10 \x01(\tR\x04city\x12\x1a\n" +
|
||||
"\blanguage\x18\x11 \x01(\tR\blanguage\x12\x14\n" +
|
||||
"\x05isVip\x18\x12 \x01(\x05R\x05isVip\x12 \n" +
|
||||
"\vvipExpireAt\x18\x13 \x01(\x03R\vvipExpireAt\x12 \n" +
|
||||
"\vlastLoginIp\x18\x14 \x01(\tR\vlastLoginIp\x12 \n" +
|
||||
"\vlastLoginAt\x18\x15 \x01(\x03R\vlastLoginAt\x12\x1c\n" +
|
||||
"\tcreatedAt\x18\x16 \x01(\x03R\tcreatedAt\x12\x1c\n" +
|
||||
"\tupdatedAt\x18\x17 \x01(\x03R\tupdatedAt\x12\x1c\n" +
|
||||
"\tavatarUrl\x18\x18 \x01(\tR\tavatarUrl\" \n" +
|
||||
"\x0eGetUserByIdReq\x12\x0e\n" +
|
||||
"\x02id\x18\x01 \x01(\tR\x02id\"5\n" +
|
||||
"\x0fGetUserByIdResp\x12\"\n" +
|
||||
"\x04user\x18\x01 \x01(\v2\x0e.user.UserInfoR\x04user\",\n" +
|
||||
"\x12GetUserByOpenIdReq\x12\x16\n" +
|
||||
"\x06openId\x18\x01 \x01(\tR\x06openId\"9\n" +
|
||||
"\x13GetUserByOpenIdResp\x12\"\n" +
|
||||
"\x04user\x18\x01 \x01(\v2\x0e.user.UserInfoR\x04user\"&\n" +
|
||||
"\x0eVerifyTokenReq\x12\x14\n" +
|
||||
"\x05token\x18\x01 \x01(\tR\x05token\"w\n" +
|
||||
"\x0fVerifyTokenResp\x12\x14\n" +
|
||||
"\x05valid\x18\x01 \x01(\bR\x05valid\x12\x16\n" +
|
||||
"\x06userId\x18\x02 \x01(\tR\x06userId\x12\x18\n" +
|
||||
"\aaccount\x18\x03 \x01(\tR\aaccount\x12\x1c\n" +
|
||||
"\texpiresAt\x18\x04 \x01(\x03R\texpiresAt\"\x8d\x01\n" +
|
||||
"\rCreateUserReq\x12\x12\n" +
|
||||
"\x04name\x18\x01 \x01(\tR\x04name\x12\x16\n" +
|
||||
"\x06openId\x18\x02 \x01(\tR\x06openId\x12\x1e\n" +
|
||||
"\n" +
|
||||
"sessionKey\x18\x03 \x01(\tR\n" +
|
||||
"sessionKey\x12\x1a\n" +
|
||||
"\bclientId\x18\x04 \x01(\tR\bclientId\x12\x14\n" +
|
||||
"\x05phone\x18\x05 \x01(\tR\x05phone\"4\n" +
|
||||
"\x0eCreateUserResp\x12\"\n" +
|
||||
"\x04user\x18\x01 \x01(\v2\x0e.user.UserInfoR\x04user\"\x9b\x01\n" +
|
||||
"\rUpdateUserReq\x12\x0e\n" +
|
||||
"\x02id\x18\x01 \x01(\tR\x02id\x12\x12\n" +
|
||||
"\x04name\x18\x02 \x01(\tR\x04name\x12\x18\n" +
|
||||
"\aaccount\x18\x03 \x01(\tR\aaccount\x12\x14\n" +
|
||||
"\x05phone\x18\x04 \x01(\tR\x05phone\x12\x1a\n" +
|
||||
"\bavatarId\x18\x05 \x01(\tR\bavatarId\x12\x1a\n" +
|
||||
"\bnickName\x18\x06 \x01(\tR\bnickName2\xbb\x02\n" +
|
||||
"\vUserService\x12:\n" +
|
||||
"\vGetUserById\x12\x14.user.GetUserByIdReq\x1a\x15.user.GetUserByIdResp\x12F\n" +
|
||||
"\x0fGetUserByOpenId\x12\x18.user.GetUserByOpenIdReq\x1a\x19.user.GetUserByOpenIdResp\x12:\n" +
|
||||
"\vVerifyToken\x12\x14.user.VerifyTokenReq\x1a\x15.user.VerifyTokenResp\x127\n" +
|
||||
"\n" +
|
||||
"CreateUser\x12\x13.user.CreateUserReq\x1a\x14.user.CreateUserResp\x123\n" +
|
||||
"\n" +
|
||||
"UpdateUser\x12\x13.user.UpdateUserReq\x1a\x10.user.CommonRespB\bZ\x06./userb\x06proto3"
|
||||
|
||||
var (
|
||||
file_pb_user_proto_rawDescOnce sync.Once
|
||||
file_pb_user_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_pb_user_proto_rawDescGZIP() []byte {
|
||||
file_pb_user_proto_rawDescOnce.Do(func() {
|
||||
file_pb_user_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_pb_user_proto_rawDesc), len(file_pb_user_proto_rawDesc)))
|
||||
})
|
||||
return file_pb_user_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_pb_user_proto_msgTypes = make([]protoimpl.MessageInfo, 11)
|
||||
var file_pb_user_proto_goTypes = []any{
|
||||
(*CommonResp)(nil), // 0: user.CommonResp
|
||||
(*UserInfo)(nil), // 1: user.UserInfo
|
||||
(*GetUserByIdReq)(nil), // 2: user.GetUserByIdReq
|
||||
(*GetUserByIdResp)(nil), // 3: user.GetUserByIdResp
|
||||
(*GetUserByOpenIdReq)(nil), // 4: user.GetUserByOpenIdReq
|
||||
(*GetUserByOpenIdResp)(nil), // 5: user.GetUserByOpenIdResp
|
||||
(*VerifyTokenReq)(nil), // 6: user.VerifyTokenReq
|
||||
(*VerifyTokenResp)(nil), // 7: user.VerifyTokenResp
|
||||
(*CreateUserReq)(nil), // 8: user.CreateUserReq
|
||||
(*CreateUserResp)(nil), // 9: user.CreateUserResp
|
||||
(*UpdateUserReq)(nil), // 10: user.UpdateUserReq
|
||||
}
|
||||
var file_pb_user_proto_depIdxs = []int32{
|
||||
1, // 0: user.GetUserByIdResp.user:type_name -> user.UserInfo
|
||||
1, // 1: user.GetUserByOpenIdResp.user:type_name -> user.UserInfo
|
||||
1, // 2: user.CreateUserResp.user:type_name -> user.UserInfo
|
||||
2, // 3: user.UserService.GetUserById:input_type -> user.GetUserByIdReq
|
||||
4, // 4: user.UserService.GetUserByOpenId:input_type -> user.GetUserByOpenIdReq
|
||||
6, // 5: user.UserService.VerifyToken:input_type -> user.VerifyTokenReq
|
||||
8, // 6: user.UserService.CreateUser:input_type -> user.CreateUserReq
|
||||
10, // 7: user.UserService.UpdateUser:input_type -> user.UpdateUserReq
|
||||
3, // 8: user.UserService.GetUserById:output_type -> user.GetUserByIdResp
|
||||
5, // 9: user.UserService.GetUserByOpenId:output_type -> user.GetUserByOpenIdResp
|
||||
7, // 10: user.UserService.VerifyToken:output_type -> user.VerifyTokenResp
|
||||
9, // 11: user.UserService.CreateUser:output_type -> user.CreateUserResp
|
||||
0, // 12: user.UserService.UpdateUser:output_type -> user.CommonResp
|
||||
8, // [8:13] is the sub-list for method output_type
|
||||
3, // [3:8] is the sub-list for method input_type
|
||||
3, // [3:3] is the sub-list for extension type_name
|
||||
3, // [3:3] is the sub-list for extension extendee
|
||||
0, // [0:3] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_pb_user_proto_init() }
|
||||
func file_pb_user_proto_init() {
|
||||
if File_pb_user_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_pb_user_proto_rawDesc), len(file_pb_user_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 11,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_pb_user_proto_goTypes,
|
||||
DependencyIndexes: file_pb_user_proto_depIdxs,
|
||||
MessageInfos: file_pb_user_proto_msgTypes,
|
||||
}.Build()
|
||||
File_pb_user_proto = out.File
|
||||
file_pb_user_proto_goTypes = nil
|
||||
file_pb_user_proto_depIdxs = nil
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.6.1
|
||||
// - protoc v7.34.1
|
||||
// source: pb/user.proto
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.64.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
UserService_GetUserById_FullMethodName = "/user.UserService/GetUserById"
|
||||
UserService_GetUserByOpenId_FullMethodName = "/user.UserService/GetUserByOpenId"
|
||||
UserService_VerifyToken_FullMethodName = "/user.UserService/VerifyToken"
|
||||
UserService_CreateUser_FullMethodName = "/user.UserService/CreateUser"
|
||||
UserService_UpdateUser_FullMethodName = "/user.UserService/UpdateUser"
|
||||
)
|
||||
|
||||
// UserServiceClient is the client API for UserService service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type UserServiceClient interface {
|
||||
// 根据ID获取用户信息
|
||||
GetUserById(ctx context.Context, in *GetUserByIdReq, opts ...grpc.CallOption) (*GetUserByIdResp, error)
|
||||
// 根据OpenId获取用户信息
|
||||
GetUserByOpenId(ctx context.Context, in *GetUserByOpenIdReq, opts ...grpc.CallOption) (*GetUserByOpenIdResp, error)
|
||||
// 验证Token有效性
|
||||
VerifyToken(ctx context.Context, in *VerifyTokenReq, opts ...grpc.CallOption) (*VerifyTokenResp, error)
|
||||
// 创建用户
|
||||
CreateUser(ctx context.Context, in *CreateUserReq, opts ...grpc.CallOption) (*CreateUserResp, error)
|
||||
// 更新用户信息
|
||||
UpdateUser(ctx context.Context, in *UpdateUserReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
}
|
||||
|
||||
type userServiceClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewUserServiceClient(cc grpc.ClientConnInterface) UserServiceClient {
|
||||
return &userServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *userServiceClient) GetUserById(ctx context.Context, in *GetUserByIdReq, opts ...grpc.CallOption) (*GetUserByIdResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetUserByIdResp)
|
||||
err := c.cc.Invoke(ctx, UserService_GetUserById_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userServiceClient) GetUserByOpenId(ctx context.Context, in *GetUserByOpenIdReq, opts ...grpc.CallOption) (*GetUserByOpenIdResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetUserByOpenIdResp)
|
||||
err := c.cc.Invoke(ctx, UserService_GetUserByOpenId_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userServiceClient) VerifyToken(ctx context.Context, in *VerifyTokenReq, opts ...grpc.CallOption) (*VerifyTokenResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(VerifyTokenResp)
|
||||
err := c.cc.Invoke(ctx, UserService_VerifyToken_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userServiceClient) CreateUser(ctx context.Context, in *CreateUserReq, opts ...grpc.CallOption) (*CreateUserResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CreateUserResp)
|
||||
err := c.cc.Invoke(ctx, UserService_CreateUser_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userServiceClient) UpdateUser(ctx context.Context, in *UpdateUserReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CommonResp)
|
||||
err := c.cc.Invoke(ctx, UserService_UpdateUser_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UserServiceServer is the server API for UserService service.
|
||||
// All implementations must embed UnimplementedUserServiceServer
|
||||
// for forward compatibility.
|
||||
type UserServiceServer interface {
|
||||
// 根据ID获取用户信息
|
||||
GetUserById(context.Context, *GetUserByIdReq) (*GetUserByIdResp, error)
|
||||
// 根据OpenId获取用户信息
|
||||
GetUserByOpenId(context.Context, *GetUserByOpenIdReq) (*GetUserByOpenIdResp, error)
|
||||
// 验证Token有效性
|
||||
VerifyToken(context.Context, *VerifyTokenReq) (*VerifyTokenResp, error)
|
||||
// 创建用户
|
||||
CreateUser(context.Context, *CreateUserReq) (*CreateUserResp, error)
|
||||
// 更新用户信息
|
||||
UpdateUser(context.Context, *UpdateUserReq) (*CommonResp, error)
|
||||
mustEmbedUnimplementedUserServiceServer()
|
||||
}
|
||||
|
||||
// UnimplementedUserServiceServer must be embedded to have
|
||||
// forward compatible implementations.
|
||||
//
|
||||
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||
// pointer dereference when methods are called.
|
||||
type UnimplementedUserServiceServer struct{}
|
||||
|
||||
func (UnimplementedUserServiceServer) GetUserById(context.Context, *GetUserByIdReq) (*GetUserByIdResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method GetUserById not implemented")
|
||||
}
|
||||
func (UnimplementedUserServiceServer) GetUserByOpenId(context.Context, *GetUserByOpenIdReq) (*GetUserByOpenIdResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method GetUserByOpenId not implemented")
|
||||
}
|
||||
func (UnimplementedUserServiceServer) VerifyToken(context.Context, *VerifyTokenReq) (*VerifyTokenResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method VerifyToken not implemented")
|
||||
}
|
||||
func (UnimplementedUserServiceServer) CreateUser(context.Context, *CreateUserReq) (*CreateUserResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method CreateUser not implemented")
|
||||
}
|
||||
func (UnimplementedUserServiceServer) UpdateUser(context.Context, *UpdateUserReq) (*CommonResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method UpdateUser not implemented")
|
||||
}
|
||||
func (UnimplementedUserServiceServer) mustEmbedUnimplementedUserServiceServer() {}
|
||||
func (UnimplementedUserServiceServer) testEmbeddedByValue() {}
|
||||
|
||||
// UnsafeUserServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to UserServiceServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeUserServiceServer interface {
|
||||
mustEmbedUnimplementedUserServiceServer()
|
||||
}
|
||||
|
||||
func RegisterUserServiceServer(s grpc.ServiceRegistrar, srv UserServiceServer) {
|
||||
// If the following call panics, it indicates UnimplementedUserServiceServer was
|
||||
// embedded by pointer and is nil. This will cause panics if an
|
||||
// unimplemented method is ever invoked, so we test this at initialization
|
||||
// time to prevent it from happening at runtime later due to I/O.
|
||||
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||
t.testEmbeddedByValue()
|
||||
}
|
||||
s.RegisterService(&UserService_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _UserService_GetUserById_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetUserByIdReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServiceServer).GetUserById(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: UserService_GetUserById_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServiceServer).GetUserById(ctx, req.(*GetUserByIdReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _UserService_GetUserByOpenId_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetUserByOpenIdReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServiceServer).GetUserByOpenId(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: UserService_GetUserByOpenId_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServiceServer).GetUserByOpenId(ctx, req.(*GetUserByOpenIdReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _UserService_VerifyToken_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(VerifyTokenReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServiceServer).VerifyToken(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: UserService_VerifyToken_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServiceServer).VerifyToken(ctx, req.(*VerifyTokenReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _UserService_CreateUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CreateUserReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServiceServer).CreateUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: UserService_CreateUser_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServiceServer).CreateUser(ctx, req.(*CreateUserReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _UserService_UpdateUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(UpdateUserReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServiceServer).UpdateUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: UserService_UpdateUser_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServiceServer).UpdateUser(ctx, req.(*UpdateUserReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// UserService_ServiceDesc is the grpc.ServiceDesc for UserService service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var UserService_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "user.UserService",
|
||||
HandlerType: (*UserServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "GetUserById",
|
||||
Handler: _UserService_GetUserById_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetUserByOpenId",
|
||||
Handler: _UserService_GetUserByOpenId_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "VerifyToken",
|
||||
Handler: _UserService_VerifyToken_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "CreateUser",
|
||||
Handler: _UserService_CreateUser_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "UpdateUser",
|
||||
Handler: _UserService_UpdateUser_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "pb/user.proto",
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
// Source: user.proto
|
||||
|
||||
package userservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
CommonResp = user.CommonResp
|
||||
CreateUserReq = user.CreateUserReq
|
||||
CreateUserResp = user.CreateUserResp
|
||||
GetUserByIdReq = user.GetUserByIdReq
|
||||
GetUserByIdResp = user.GetUserByIdResp
|
||||
GetUserByOpenIdReq = user.GetUserByOpenIdReq
|
||||
GetUserByOpenIdResp = user.GetUserByOpenIdResp
|
||||
UpdateUserReq = user.UpdateUserReq
|
||||
UserInfo = user.UserInfo
|
||||
VerifyTokenReq = user.VerifyTokenReq
|
||||
VerifyTokenResp = user.VerifyTokenResp
|
||||
|
||||
UserService interface {
|
||||
// 根据ID获取用户信息
|
||||
GetUserById(ctx context.Context, in *GetUserByIdReq, opts ...grpc.CallOption) (*GetUserByIdResp, error)
|
||||
// 根据OpenId获取用户信息
|
||||
GetUserByOpenId(ctx context.Context, in *GetUserByOpenIdReq, opts ...grpc.CallOption) (*GetUserByOpenIdResp, error)
|
||||
// 验证Token有效性
|
||||
VerifyToken(ctx context.Context, in *VerifyTokenReq, opts ...grpc.CallOption) (*VerifyTokenResp, error)
|
||||
// 创建用户
|
||||
CreateUser(ctx context.Context, in *CreateUserReq, opts ...grpc.CallOption) (*CreateUserResp, error)
|
||||
// 更新用户信息
|
||||
UpdateUser(ctx context.Context, in *UpdateUserReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
}
|
||||
|
||||
defaultUserService struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserService(cli zrpc.Client) UserService {
|
||||
return &defaultUserService{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID获取用户信息
|
||||
func (m *defaultUserService) GetUserById(ctx context.Context, in *GetUserByIdReq, opts ...grpc.CallOption) (*GetUserByIdResp, error) {
|
||||
client := user.NewUserServiceClient(m.cli.Conn())
|
||||
return client.GetUserById(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 根据OpenId获取用户信息
|
||||
func (m *defaultUserService) GetUserByOpenId(ctx context.Context, in *GetUserByOpenIdReq, opts ...grpc.CallOption) (*GetUserByOpenIdResp, error) {
|
||||
client := user.NewUserServiceClient(m.cli.Conn())
|
||||
return client.GetUserByOpenId(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 验证Token有效性
|
||||
func (m *defaultUserService) VerifyToken(ctx context.Context, in *VerifyTokenReq, opts ...grpc.CallOption) (*VerifyTokenResp, error) {
|
||||
client := user.NewUserServiceClient(m.cli.Conn())
|
||||
return client.VerifyToken(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
func (m *defaultUserService) CreateUser(ctx context.Context, in *CreateUserReq, opts ...grpc.CallOption) (*CreateUserResp, error) {
|
||||
client := user.NewUserServiceClient(m.cli.Conn())
|
||||
return client.CreateUser(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
func (m *defaultUserService) UpdateUser(ctx context.Context, in *UpdateUserReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
client := user.NewUserServiceClient(m.cli.Conn())
|
||||
return client.UpdateUser(ctx, in, opts...)
|
||||
}
|
||||
Reference in New Issue
Block a user