add: some user api and all api desc

This commit is contained in:
wwweww
2026-02-27 19:17:01 +08:00
parent a0c720eb2f
commit 5930fb0dde
156 changed files with 9457 additions and 1086 deletions
+2 -2
View File
@@ -10,6 +10,6 @@ import (
type Config struct {
rest.RestConf
UsercenterRpcConf zrpc.RpcClientConf
SnowflakeRpcConf zrpc.RpcClientConf
UsercenterRpcConf zrpc.RpcClientConf
UserVerificationRpc zrpc.RpcClientConf
}
@@ -1,54 +0,0 @@
package contextx
import (
"context"
"errors"
)
func WithRequestId(c context.Context, requestId string) context.Context {
return context.WithValue(c, "request_id", requestId)
}
func RequestIdFrom(c context.Context) (string, error) {
requestID, ok := c.Value("request_id").(string)
if !ok {
return "", errors.New("request_id not found in context")
}
return requestID, nil
}
func WithToken(c context.Context, token string) context.Context {
return context.WithValue(c, "token", token)
}
func TokenFrom(c context.Context) (string, error) {
token, ok := c.Value("token").(string)
if !ok {
return "", errors.New("token not found in context")
}
return token, nil
}
func WithUserID(c context.Context, id int64) context.Context {
return context.WithValue(c, "user_id", id)
}
func UserIDFrom(c context.Context) (int64, error) {
if userID, ok := c.Value("user_id").(int64); !ok {
return 0, errors.New("user_id not found in context")
} else {
return userID, nil
}
}
func WithRequestID(c context.Context, requestID string) context.Context {
return context.WithValue(c, "request_id", requestID)
}
func RequestIDFrom(c context.Context) (string, error) {
if requestID, ok := c.Value("request_id").(string); !ok {
return "", errors.New("request_id not found in context")
} else {
return requestID, nil
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package auth
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/auth"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
)
// 忘记密码-发送验证码
func ForgotPasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ForgotPasswordReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := auth.NewForgotPasswordLogic(r.Context(), svcCtx)
resp, err := l.ForgotPassword(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -1,19 +1,19 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
package auth
import (
"juwan-backend/app/users/api/internal/logic/user"
"net/http"
"juwan-backend/app/users/api/internal/logic/auth"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/common/utils"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 用户登录接口
// 用户登录
func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.LoginReq
@@ -22,14 +22,13 @@ func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := user.NewLoginLogic(r.Context(), svcCtx)
l := auth.NewLoginLogic(r.Context(), svcCtx)
resp, err := l.Login(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, utils.NewErrorResp(400, err))
httpx.ErrorCtx(r.Context(), w, err)
} else {
token := resp.Token
resp.Token = ""
token := resp.RefreshToken
resp.RefreshToken = ""
http.SetCookie(w, &http.Cookie{
Name: "JToken",
Value: token,
@@ -1,18 +1,19 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
package auth
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/user"
"juwan-backend/app/users/api/internal/logic/auth"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 用户登出
// 退出登录
func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.LogoutReq
@@ -21,7 +22,8 @@ func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := user.NewLogoutLogic(r.Context(), svcCtx)
// TODO: add userId from http header x-auth-user-id
l := auth.NewLogoutLogic(r.Context(), svcCtx)
resp, err := l.Logout(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -0,0 +1,48 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package auth
import (
"net/http"
"juwan-backend/app/users/api/internal/logic/auth"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 用户注册
func RegisterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RegisterReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := auth.NewRegisterLogic(r.Context(), svcCtx)
resp, err := l.Register(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
token := resp.RefreshToken
resp.RefreshToken = ""
http.SetCookie(w, &http.Cookie{
Name: "JToken",
Value: token,
Quoted: false,
Path: "/",
Domain: "",
RawExpires: "",
MaxAge: 691200,
Secure: false,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Partitioned: false,
})
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package auth
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/auth"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
)
// 重置密码
func ResetPasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ResetPasswordReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := auth.NewResetPasswordLogic(r.Context(), svcCtx)
resp, err := l.ResetPassword(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
+126 -45
View File
@@ -6,7 +6,10 @@ package handler
import (
"net/http"
auth "juwan-backend/app/users/api/internal/handler/auth"
user "juwan-backend/app/users/api/internal/handler/user"
verification_admin "juwan-backend/app/users/api/internal/handler/verification_admin"
verification_user "juwan-backend/app/users/api/internal/handler/verification_user"
"juwan-backend/app/users/api/internal/svc"
"github.com/zeromicro/go-zero/rest"
@@ -14,41 +17,44 @@ import (
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.Logger},
[]rest.Route{
{
// 用户登出
Method: http.MethodPost,
Path: "/:userId/logout",
Handler: user.LogoutHandler(serverCtx),
},
{
// 修改用户密码
Method: http.MethodPut,
Path: "/:userId/password",
Handler: user.UpdatePasswordHandler(serverCtx),
},
{
// 修改密码-使用验证码
Method: http.MethodPut,
Path: "/forgot-password/reset",
Handler: user.UpdatePasswordByVcodeHandler(serverCtx),
},
{
// 用户登录接口
Method: http.MethodPost,
Path: "/login",
Handler: user.LoginHandler(serverCtx),
},
{
// 用户注册接口
Method: http.MethodPost,
Path: "/register",
Handler: user.RegisterHandler(serverCtx),
},
}...,
),
[]rest.Route{
{
// 忘记密码-发送验证码
Method: http.MethodPost,
Path: "/forgot-password",
Handler: auth.ForgotPasswordHandler(serverCtx),
},
{
// 用户登录
Method: http.MethodPost,
Path: "/login",
Handler: auth.LoginHandler(serverCtx),
},
{
// 用户注册
Method: http.MethodPost,
Path: "/register",
Handler: auth.RegisterHandler(serverCtx),
},
{
// 重置密码
Method: http.MethodPost,
Path: "/reset-password",
Handler: auth.ResetPasswordHandler(serverCtx),
},
},
rest.WithPrefix("/api/v1/auth"),
)
server.AddRoutes(
[]rest.Route{
{
// 退出登录
Method: http.MethodPost,
Path: "/logout",
Handler: auth.LogoutHandler(serverCtx),
},
},
rest.WithPrefix("/api/v1/auth"),
)
@@ -57,16 +63,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
[]rest.Middleware{serverCtx.Logger},
[]rest.Route{
{
// 获取用户信息
Method: http.MethodGet,
Path: "/:userId",
Handler: user.GetUserInfoHandler(serverCtx),
// 关注用户
Method: http.MethodPost,
Path: "/:id/follow",
Handler: user.FollowUserHandler(serverCtx),
},
{
// 修改用户信息
Method: http.MethodPut,
Path: "/:userId",
Handler: user.UpdateUserInfoHandler(serverCtx),
// 取消关注用户
Method: http.MethodDelete,
Path: "/:id/follow",
Handler: user.UnfollowUserHandler(serverCtx),
},
{
// 获取当前登录用户信息
@@ -75,13 +81,88 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Handler: user.GetMeHandler(serverCtx),
},
{
// 更改当前登录用户信息
// 更新个人资料
Method: http.MethodPut,
Path: "/me",
Handler: user.UpdateMeHandler(serverCtx),
},
{
// 更新通知偏好
Method: http.MethodPut,
Path: "/me/preferences/notifications",
Handler: user.UpdateNotificationSettingsHandler(serverCtx),
},
{
// 更新主题偏好
Method: http.MethodPut,
Path: "/me/preferences/theme",
Handler: user.UpdateThemeSettingsHandler(serverCtx),
},
{
// 切换当前激活角色
Method: http.MethodPost,
Path: "/me/switch-role",
Handler: user.SwitchRoleHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/v1/user"),
rest.WithPrefix("/api/v1/users"),
)
server.AddRoutes(
[]rest.Route{
{
// 获取指定用户信息
Method: http.MethodGet,
Path: "/:id",
Handler: user.GetUserInfoHandler(serverCtx),
},
},
rest.WithPrefix("/api/v1/users"),
)
server.AddRoutes(
[]rest.Route{
{
// 管理员获取认证申请列表 (分页)
Method: http.MethodGet,
Path: "/verifications",
Handler: verification_admin.GetVerificationsHandler(serverCtx),
},
{
// 管理员通过申请
Method: http.MethodPost,
Path: "/verifications/:id/approve",
Handler: verification_admin.ApproveVerificationHandler(serverCtx),
},
{
// 管理员驳回申请
Method: http.MethodPost,
Path: "/verifications/:id/reject",
Handler: verification_admin.RejectVerificationHandler(serverCtx),
},
},
rest.WithPrefix("/api/v1/admin"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.Logger},
[]rest.Route{
{
// 提交或修改角色认证申请 (支持幂等更新)
Method: http.MethodPost,
Path: "/me/verification",
Handler: verification_user.ApplyVerificationHandler(serverCtx),
},
{
// 获取我的所有认证状态
Method: http.MethodGet,
Path: "/me/verification",
Handler: verification_user.GetMyVerificationsHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/v1/users"),
)
}
@@ -12,17 +12,17 @@ import (
"juwan-backend/app/users/api/internal/types"
)
// 修改用户密码
func UpdatePasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
// 关注用户
func FollowUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UpdatePasswordReq
var req types.FollowUserReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewUpdatePasswordLogic(r.Context(), svcCtx)
resp, err := l.UpdatePassword(&req)
l := user.NewFollowUserLogic(r.Context(), svcCtx)
resp, err := l.FollowUser(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
@@ -1,96 +0,0 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"juwan-backend/app/users/api/internal/contextx"
"juwan-backend/common/utils"
"net/http"
"strconv"
"juwan-backend/app/users/api/internal/logic/user"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 用户注册接口
func RegisterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := normalizeRegisterBody(r); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
var req types.RegisterReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
requestId := r.Header.Get("X-Request-ID")
//regCtx := context.WithValue(r.Context(), "request_id", requestId)
regCtx := contextx.WithRequestId(r.Context(), requestId)
if requestId == "" {
httpx.ErrorCtx(r.Context(), w, errors.New("bad request"))
}
l := user.NewRegisterLogic(regCtx, svcCtx)
resp, err := l.Register(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, utils.NewErrorResp(400, err))
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
func normalizeRegisterBody(r *http.Request) error {
body, err := io.ReadAll(r.Body)
if err != nil {
return err
}
defer r.Body.Close()
if len(body) == 0 {
r.Body = io.NopCloser(bytes.NewReader(body))
return nil
}
var payload map[string]any
if err := json.Unmarshal(body, &payload); err != nil {
r.Body = io.NopCloser(bytes.NewReader(body))
return nil
}
vcode, exists := payload["vcode"]
if exists {
switch value := vcode.(type) {
case string:
parsed, convErr := strconv.Atoi(value)
if convErr != nil {
return fmt.Errorf("invalid vcode format")
}
payload["vcode"] = parsed
case float64:
payload["vcode"] = int(value)
}
}
normalized, err := json.Marshal(payload)
if err != nil {
return err
}
r.Body = io.NopCloser(bytes.NewReader(normalized))
r.ContentLength = int64(len(normalized))
return nil
}
@@ -12,17 +12,17 @@ import (
"juwan-backend/app/users/api/internal/types"
)
// 修改密码-使用验证码
func UpdatePasswordByVcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
// 切换当前激活角色
func SwitchRoleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ResetPasswordByVcode
var req types.SwitchRoleReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewUpdatePasswordByVcodeLogic(r.Context(), svcCtx)
resp, err := l.UpdatePasswordByVcode(&req)
l := user.NewSwitchRoleLogic(r.Context(), svcCtx)
resp, err := l.SwitchRole(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/user"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
)
// 取消关注用户
func UnfollowUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UnfollowUserReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewUnfollowUserLogic(r.Context(), svcCtx)
resp, err := l.UnfollowUser(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,33 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"net/http"
"juwan-backend/app/users/api/internal/logic/user"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 更新通知偏好
func UpdateNotificationSettingsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UpdateNotifySettingsReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewUpdateNotificationSettingsLogic(r.Context(), svcCtx)
resp, err := l.UpdateNotificationSettings(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/user"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
)
// 更新主题偏好
func UpdateThemeSettingsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UpdateThemeSettingsReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewUpdateThemeSettingsLogic(r.Context(), svcCtx)
resp, err := l.UpdateThemeSettings(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_admin
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/verification_admin"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
)
// 管理员通过申请
func ApproveVerificationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.VerificationIdReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := verification_admin.NewApproveVerificationLogic(r.Context(), svcCtx)
resp, err := l.ApproveVerification(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_admin
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/verification_admin"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
)
// 管理员获取认证申请列表 (分页)
func GetVerificationsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetPendingListReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := verification_admin.NewGetVerificationsLogic(r.Context(), svcCtx)
resp, err := l.GetVerifications(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_admin
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/verification_admin"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
)
// 管理员驳回申请
func RejectVerificationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RejectVerificationReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := verification_admin.NewRejectVerificationLogic(r.Context(), svcCtx)
resp, err := l.RejectVerification(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_user
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/verification_user"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
)
// 提交或修改角色认证申请 (支持幂等更新)
func ApplyVerificationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ApplyVerificationReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := verification_user.NewApplyVerificationLogic(r.Context(), svcCtx)
resp, err := l.ApplyVerification(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,25 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_user
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/users/api/internal/logic/verification_user"
"juwan-backend/app/users/api/internal/svc"
)
// 获取我的所有认证状态
func GetMyVerificationsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := verification_user.NewGetMyVerificationsLogic(r.Context(), svcCtx)
resp, err := l.GetMyVerifications()
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,33 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package auth
import (
"context"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ForgotPasswordLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 忘记密码-发送验证码
func NewForgotPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ForgotPasswordLogic {
return &ForgotPasswordLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ForgotPasswordLogic) ForgotPassword(req *types.ForgotPasswordReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
return
}
@@ -1,15 +1,15 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
package auth
import (
"context"
"errors"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/app/users/rpc/usercenter"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -20,7 +20,7 @@ type LoginLogic struct {
svcCtx *svc.ServiceContext
}
// 用户登录接口
// 用户登录
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
return &LoginLogic{
Logger: logx.WithContext(ctx),
@@ -30,6 +30,7 @@ func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic
}
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
// todo: add your logic here and delete this line
if len(req.Username) < 3 || len(req.Password) < 8 || len(req.Password) > 20 {
return nil, errors.New("the information is illegal")
}
@@ -50,10 +51,8 @@ func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err erro
}
return &types.LoginResp{
UserId: res.Id,
Username: res.Username,
Email: res.Email,
Token: res.Token,
Expires: int64((7 * 24 * time.Hour).Seconds()),
AccessToken: "",
RefreshToken: res.Token,
User: types.User{},
}, nil
}
@@ -1,15 +1,16 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
package auth
import (
"context"
"errors"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils/contextx"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/app/users/rpc/usercenter"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -20,7 +21,7 @@ type LogoutLogic struct {
svcCtx *svc.ServiceContext
}
// 用户登出
// 退出登录
func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic {
return &LogoutLogic{
Logger: logx.WithContext(ctx),
@@ -29,15 +30,19 @@ func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogi
}
}
func (l *LogoutLogic) Logout(req *types.LogoutReq) (resp *types.LogoutResp, err error) {
if req.UserId <= 0 {
func (l *LogoutLogic) Logout(_ *types.LogoutReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userId, err := contextx.UserIDFrom(l.ctx)
if err != nil {
return nil, errors.New("illegal id")
}
if userId <= 0 {
return nil, errors.New("invalid userId")
}
_, err = l.svcCtx.UserRpc.Logout(l.ctx, &usercenter.LogoutReq{UserId: req.UserId})
_, err = l.svcCtx.UserRpc.Logout(l.ctx, &usercenter.LogoutReq{UserId: userId})
if err != nil {
return nil, err
}
return &types.LogoutResp{Message: "logout success"}, nil
return &types.EmptyResp{}, nil
}
@@ -1,19 +1,19 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
package auth
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/contextx"
"juwan-backend/app/users/rpc/pb"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils/contextx"
"juwan-backend/common/utils/pwdUtils"
"regexp"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/app/users/rpc/pb"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -24,7 +24,7 @@ type RegisterLogic struct {
svcCtx *svc.ServiceContext
}
// 用户注册接口
// 用户注册
func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
return &RegisterLogic{
Logger: logx.WithContext(ctx),
@@ -52,7 +52,7 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
return nil, errors.New("user already exists")
}
hashedPassword, err := utils.HashPassword(req.Password)
hashedPassword, err := pwdUtils.HashPassword(req.Password)
if err != nil {
return nil, errors.New("hash password failed")
}
@@ -63,7 +63,7 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
return nil, errors.New("contextx.RequestIdFrom failed")
}
_, err = l.svcCtx.UserRpc.Register(l.ctx, &usercenter.RegisterReq{
res, err := l.svcCtx.UserRpc.Register(l.ctx, &usercenter.RegisterReq{
Username: req.Username,
Passwd: hashedPassword,
Phone: req.Username,
@@ -78,9 +78,8 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
// 返回响应
return &types.RegisterResp{
UserId: 0,
Username: req.Username,
Email: req.Email,
Message: "register success",
AccessToken: "",
RefreshToken: res.Res,
User: types.User{},
}, nil
}
@@ -1,14 +1,14 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
package auth
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/contextx"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils"
"juwan-backend/common/utils/contextx"
"juwan-backend/common/utils/pwdUtils"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
@@ -16,29 +16,28 @@ import (
"github.com/zeromicro/go-zero/core/logx"
)
type UpdatePasswordByVcodeLogic struct {
type ResetPasswordLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 修改密码-使用验证
func NewUpdatePasswordByVcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordByVcodeLogic {
return &UpdatePasswordByVcodeLogic{
// 重置密
func NewResetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetPasswordLogic {
return &ResetPasswordLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdatePasswordByVcodeLogic) UpdatePasswordByVcode(req *types.ResetPasswordByVcode) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordReq) (resp *types.EmptyResp, err error) {
requestId, err := contextx.RequestIdFrom(l.ctx)
if err != nil {
logx.Errorf("get request id from context failed, err:%v.", err)
return nil, errors.New("bad request")
}
hashedPassword, err := utils.HashPassword(req.Password)
hashedPassword, err := pwdUtils.HashPassword(req.NewPassword)
if err != nil {
logx.Errorf("hash password failed, err:%v.", err)
return nil, errors.New("bad password")
@@ -0,0 +1,34 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"context"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type FollowUserLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 关注用户
func NewFollowUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FollowUserLogic {
return &FollowUserLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *FollowUserLogic) FollowUser(req *types.FollowUserReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
return
}
@@ -6,9 +6,10 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/contextx"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/converter"
"juwan-backend/common/utils/contextx"
"time"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
@@ -31,7 +32,7 @@ func NewGetMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMeLogic
}
}
func (l *GetMeLogic) GetMe() (resp *types.UserInfo, err error) {
func (l *GetMeLogic) GetMe() (resp *types.User, err error) {
userId, err := contextx.UserIDFrom(l.ctx)
if err != nil {
return nil, errors.New("illegal id")
@@ -43,6 +44,8 @@ func (l *GetMeLogic) GetMe() (resp *types.UserInfo, err error) {
return nil, errors.New("get user by id error")
}
err = converter.StructToStruct(user, &resp)
createAt := time.Unix(user.Users.CreatedAt, 0)
resp.CreatedAt = createAt.Format(time.DateTime)
if err != nil {
return nil, errors.New("to struct error")
}
@@ -6,12 +6,11 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/converter"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/app/users/rpc/usercenter"
"github.com/jinzhu/copier"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -30,21 +29,21 @@ func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUs
}
}
func (l *GetUserInfoLogic) GetUserInfo(req *types.GetUserInfoReq) (resp types.UserInfo, err error) {
func (l *GetUserInfoLogic) GetUserInfo(req *types.GetUserReq) (resp types.User, err error) {
pbUser, err := l.svcCtx.UserRpc.GetUsersById(l.ctx, &usercenter.GetUsersByIdReq{
Id: req.UserId,
Id: req.Id,
})
if err != nil {
return types.UserInfo{}, errors.New("failed to get user info by userid")
}
user := types.UserInfo{}
err = converter.StructToStruct(&pbUser.Users, &user)
if err != nil {
logx.Errorf("struct to user info failed, err:%v.", err)
return types.UserInfo{}, errors.New("failed to get user info by userid")
return types.User{}, errors.New("failed to get user info by userid")
}
user := types.User{}
err = copier.Copy(&user, &pbUser.Users)
if err != nil {
logx.Errorf("struct to user info failed, err:%v.", err)
return types.User{}, errors.New("failed to get user info by userid")
}
//req.UserId
return user, nil
}
@@ -0,0 +1,51 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"context"
"errors"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils/contextx"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/protobuf/proto"
)
type SwitchRoleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 切换当前激活角色
func NewSwitchRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SwitchRoleLogic {
return &SwitchRoleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SwitchRoleLogic) SwitchRole(req *types.SwitchRoleReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
id, err := contextx.UserIDFrom(l.ctx)
if err != nil {
logx.Errorf("get user id from context: %v", err)
return nil, errors.New("illegal id")
}
_, err = l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
Id: id,
CurrentRole: proto.String(req.Role),
})
if err != nil {
logx.Errorf("update user info by id: %v", err)
return nil, errors.New("update user info by userid")
}
return
}
@@ -0,0 +1,34 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"context"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UnfollowUserLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 取消关注用户
func NewUnfollowUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UnfollowUserLogic {
return &UnfollowUserLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UnfollowUserLogic) UnfollowUser(req *types.UnfollowUserReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
return
}
@@ -6,9 +6,10 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/contextx"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/converter"
"juwan-backend/common/utils/contextx"
"strings"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
@@ -31,16 +32,16 @@ func NewUpdateMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMe
}
}
func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserInfoReq) (resp *types.UserInfo, err error) {
func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserProfileReq) (resp *types.UpdateUserProfileReq, err error) {
userId, err := contextx.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
res, err := l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
Id: userId,
Nickname: req.Nickname,
Avatar: req.Avatar,
Bio: req.Bio,
Nickname: proto_string(req.Nickname),
Avatar: proto_string(req.Avatar),
Bio: proto_string(req.Bio),
VerifiedRoles: nil,
})
if err != nil {
@@ -53,3 +54,10 @@ func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserInfoReq) (resp *types.User
}
return
}
func proto_string(s string) *string {
if len(s) == 0 || strings.Contains(s, " ") {
return nil
}
return &s
}
@@ -0,0 +1,34 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"context"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateNotificationSettingsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 更新通知偏好
func NewUpdateNotificationSettingsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateNotificationSettingsLogic {
return &UpdateNotificationSettingsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdateNotificationSettingsLogic) UpdateNotificationSettings(req *types.UpdateNotifySettingsReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
return
}
@@ -1,72 +0,0 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/contextx"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
var ChangeUserPassFailed = errors.New("change user pass failed")
type UpdatePasswordLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 修改用户密码
func NewUpdatePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordLogic {
return &UpdatePasswordLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdatePasswordLogic) UpdatePassword(req *types.UpdatePasswordReq) (resp *types.UpdatePasswordResp, err error) {
// todo: add your logic here and delete this line
userId, err := contextx.UserIDFrom(l.ctx)
if err != nil {
logx.Errorf("get user id from context failed, err:%v.", err)
return nil, ChangeUserPassFailed
}
user, err := l.svcCtx.UserRpc.GetUsersById(l.ctx, &usercenter.GetUsersByIdReq{
Id: userId,
})
if err != nil {
logx.Errorf("get user info failed, err:%v.", err)
return nil, ChangeUserPassFailed
}
oldPasswd, err := utils.HashPassword(req.OldPassword)
if err != nil {
logx.Errorf("hash old password failed, err:%v.", err)
return nil, ChangeUserPassFailed
}
if oldPasswd != user.Users.PasswordHash {
return nil, ChangeUserPassFailed
}
_, err = l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
Id: userId,
Username: &user.Users.Username,
PasswordHash: &req.NewPassword,
})
if err != nil {
logx.Errorf("update user password failed, err:%v.", err)
return nil, ChangeUserPassFailed
}
return
}
@@ -0,0 +1,34 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"context"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateThemeSettingsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 更新主题偏好
func NewUpdateThemeSettingsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateThemeSettingsLogic {
return &UpdateThemeSettingsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdateThemeSettingsLogic) UpdateThemeSettings(req *types.UpdateThemeSettingsReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
return
}
@@ -1,48 +0,0 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package user
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/contextx"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateUserInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 修改用户信息
func NewUpdateUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserInfoLogic {
return &UpdateUserInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdateUserInfoLogic) UpdateUserInfo(req *types.UpdateUserInfoReq) (resp *types.UpdateUserInfoResp, err error) {
userId, err := contextx.UserIDFrom(l.ctx)
if err != nil {
return nil, errors.New("user not found")
}
_, err = l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
Id: userId,
Nickname: req.Nickname,
Avatar: req.Avatar,
Bio: req.Bio,
})
if err != nil {
return nil, errors.New("update user info failed")
}
return
}
@@ -0,0 +1,52 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_admin
import (
"context"
"juwan-backend/app/user_verifications/rpc/pb"
"juwan-backend/common/utils/contextx"
"time"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ApproveVerificationLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 管理员通过申请
func NewApproveVerificationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApproveVerificationLogic {
return &ApproveVerificationLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
var (
APPROVE = "approved"
)
func (l *ApproveVerificationLogic) ApproveVerification(req *types.VerificationIdReq) (resp *types.VerificationEmptyResp, err error) {
adminId, err := contextx.AdminIdFrom(l.ctx)
if err != nil {
return nil, err
}
_, err = l.svcCtx.UserVerificationsRpc.UpdateUserVerifications(l.ctx, &pb.UpdateUserVerificationsReq{
Id: req.Id,
Status: &APPROVE,
ReviewedBy: adminId,
ReviewedAt: time.Now().Unix(),
})
if err != nil {
return nil, err
}
return
}
@@ -0,0 +1,66 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_admin
import (
"context"
"juwan-backend/app/user_verifications/rpc/pb"
"juwan-backend/common/utils/contextx"
"time"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/jinzhu/copier"
"github.com/zeromicro/go-zero/core/logx"
)
type GetVerificationsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 管理员获取认证申请列表 (分页)
func NewGetVerificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetVerificationsLogic {
return &GetVerificationsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetVerificationsLogic) GetVerifications(req *types.GetPendingListReq) (resp *types.GetPendingListResp, err error) {
_, err = contextx.AdminIdFrom(l.ctx)
if err != nil {
return nil, err
}
verifications, err := l.svcCtx.UserVerificationsRpc.SearchUserVerifications(l.ctx, &pb.SearchUserVerificationsReq{
Page: req.Page,
Limit: req.Size,
Role: req.Role,
Status: req.Status,
})
if err != nil {
return nil, err
}
var searchResults []types.VerificationItem
for _, v := range verifications.UserVerifications {
temp := types.VerificationItem{}
err = copier.Copy(&temp, v)
if err != nil {
logx.Errorf("copy verification item err: %s", err.Error())
continue
}
temp.CreatedAt = time.Unix(v.CreatedAt, 0).Format(time.DateTime)
temp.ReviewedAt = time.Unix(v.ReviewedAt, 0).Format(time.DateTime)
searchResults = append(searchResults, temp)
}
resp = &types.GetPendingListResp{
List: searchResults,
Total: 0,
}
return
}
@@ -0,0 +1,52 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_admin
import (
"context"
"juwan-backend/app/user_verifications/rpc/pb"
"juwan-backend/common/utils/contextx"
"time"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type RejectVerificationLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 管理员驳回申请
func NewRejectVerificationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RejectVerificationLogic {
return &RejectVerificationLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
var REJECTED = "rejected"
func (l *RejectVerificationLogic) RejectVerification(req *types.RejectVerificationReq) (resp *types.VerificationEmptyResp, err error) {
// todo: add your logic here and delete this line
adminId, err := contextx.AdminIdFrom(l.ctx)
if err != nil {
return nil, err
}
_, err = l.svcCtx.UserVerificationsRpc.UpdateUserVerifications(l.ctx, &pb.UpdateUserVerificationsReq{
Id: req.Id,
Status: &REJECTED,
RejectReason: &req.Reason,
ReviewedBy: adminId,
ReviewedAt: time.Now().Unix(),
})
if err != nil {
return nil, err
}
return
}
@@ -0,0 +1,68 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_user
import (
"context"
"encoding/json"
"errors"
"juwan-backend/app/user_verifications/rpc/pb"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/common/utils/contextx"
"github.com/zeromicro/go-zero/core/logx"
)
type ApplyVerificationLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 提交或修改角色认证申请 (支持幂等更新)
func NewApplyVerificationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApplyVerificationLogic {
return &ApplyVerificationLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ApplyVerificationLogic) ApplyVerification(req *types.ApplyVerificationReq) (resp *types.VerificationEmptyResp, err error) {
userId, err := contextx.UserIDFrom(l.ctx)
if err != nil {
logx.Errorf("get user id from context: %v", err)
return nil, contextx.ERRILLEGALUSER
}
verifications, err := l.svcCtx.UserVerificationsRpc.SearchUserVerifications(l.ctx, &pb.SearchUserVerificationsReq{
UserId: userId,
})
if err != nil {
logx.Errorf("search user verifications: %v", err)
return nil, errors.New("search user verifications failed")
}
materials, err := json.Marshal(req.Materials)
if err != nil {
logx.Errorf("marshal materials: %v", err)
return nil, err
}
if verifications == nil || len(verifications.UserVerifications) == 0 {
// 如果没有则增加
_, err = l.svcCtx.UserVerificationsRpc.AddUserVerifications(l.ctx, &pb.AddUserVerificationsReq{
Role: req.Role,
Materials: string(materials),
})
if err != nil {
logx.Errorf("add user verifications: %v", err)
return nil, errors.New("add user verifications failed")
}
} else {
}
return &types.VerificationEmptyResp{}, nil
}
@@ -0,0 +1,68 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package verification_user
import (
"context"
"juwan-backend/app/user_verifications/rpc/pb"
"juwan-backend/common/utils/contextx"
"time"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"github.com/jinzhu/copier"
"github.com/zeromicro/go-zero/core/logx"
)
type GetMyVerificationsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取我的所有认证状态
func NewGetMyVerificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyVerificationsLogic {
return &GetMyVerificationsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetMyVerificationsLogic) GetMyVerifications() (resp *types.GetMyVerificationsResp, err error) {
// todo: add your logic here and delete this line
userId, err := contextx.UserIDFrom(l.ctx)
if err != nil {
logx.Errorf("get user id from context: %v", err)
return nil, contextx.ERRILLEGALUSER
}
verifications, err := l.svcCtx.UserVerificationsRpc.SearchUserVerifications(l.ctx, &pb.SearchUserVerificationsReq{
UserId: userId,
Page: 1,
Limit: 100, // assuming a user won't have more than 100 verification records, adjust as needed
})
if err != nil {
return nil, err
}
var searchResults []types.VerificationItem
for _, v := range verifications.UserVerifications {
temp := types.VerificationItem{}
err = copier.Copy(&temp, v)
if err != nil {
logx.Errorf("copy verification item err: %s", err.Error())
continue
}
temp.CreatedAt = time.Unix(v.CreatedAt, 0).Format(time.DateTime)
temp.ReviewedAt = time.Unix(v.ReviewedAt, 0).Format(time.DateTime)
searchResults = append(searchResults, temp)
}
resp = &types.GetMyVerificationsResp{
List: searchResults,
}
return
}
@@ -0,0 +1,22 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package middleware
import "net/http"
type JwtAuthMiddleware struct {
}
func NewJwtAuthMiddleware() *JwtAuthMiddleware {
return &JwtAuthMiddleware{}
}
func (m *JwtAuthMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// TODO generate middleware implement function, delete after code implementation
// Passthrough to next handler if need
next(w, r)
}
}
+9 -10
View File
@@ -4,28 +4,27 @@
package svc
import (
"juwan-backend/app/snowflake/rpc/snowflake"
"juwan-backend/app/user_verifications/rpc/userverifications"
"juwan-backend/app/users/api/internal/config"
"juwan-backend/app/users/api/internal/middleware"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/snowflakex"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/zrpc"
)
type ServiceContext struct {
Config config.Config
Logger rest.Middleware
UserRpc usercenter.Usercenter
SnowflakeRpc snowflake.SnowflakeServiceClient
Config config.Config
Logger rest.Middleware
UserRpc usercenter.Usercenter
UserVerificationsRpc userverifications.UserVerificationsZrpcClient
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
Logger: middleware.NewLoggerMiddleware().Handle,
UserRpc: usercenter.NewUsercenter(zrpc.MustNewClient(c.UsercenterRpcConf)),
SnowflakeRpc: snowflakex.NewClient(c.SnowflakeRpcConf),
Config: c,
Logger: middleware.NewLoggerMiddleware().Handle,
UserRpc: usercenter.NewUsercenter(zrpc.MustNewClient(c.UsercenterRpcConf)),
UserVerificationsRpc: userverifications.NewUserVerificationsZrpcClient(zrpc.MustNewClient(c.UserVerificationRpc)),
}
}
+101 -53
View File
@@ -3,89 +3,137 @@
package types
type ApplyVerificationReq struct {
Role string `json:"role"` // 申请什么角色
Materials map[string]string `json:"materials"` // 证明材料键值对 {"idCardFront": "http...", "license": "http..."}
}
type EmptyResp struct {
}
type ErrorResp struct {
Code int `json:"code"`
Message string `json:"message"`
type FollowUserReq struct {
Id int64 `path:"id"`
}
type GetUserInfoReq struct {
UserId int64 `path:"userId" binding:"required,gt=0"`
type ForgotPasswordReq struct {
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
}
type GetMyVerificationsResp struct {
List []VerificationItem `json:"list"`
}
type GetPendingListReq struct {
Page int64 `form:"page,default=1"`
Size int64 `form:"size,default=20"`
Role string `form:"role,optional"` // 筛选角色
Status string `form:"status,optional"` // 筛选状态,默认 pending
}
type GetPendingListResp struct {
List []VerificationItem `json:"list"`
Total int64 `json:"total"`
}
type GetUserReq struct {
Id int64 `path:"id"`
}
type LoginReq struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
Phone string `json:"phone,omitempty"` // 手机号登录
Username string `json:"username,omitempty"` // 或用户名登录
Password string `json:"password"`
Remember bool `json:"remember,optional"`
}
type LoginResp struct {
UserId int64 `json:"userId"`
Username string `json:"username"`
Email string `json:"email"`
Token string `json:"token"`
Expires int64 `json:"expires"`
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken"`
User User `json:"user"`
}
type LogoutReq struct {
UserId int64 `path:"userId" binding:"required,gt=0"`
Token string `header:"Authorization" binding:"required"`
}
type LogoutResp struct {
Message string `json:"message"`
}
type RegisterReq struct {
Username string `json:"username" binding:"required,min=3,max=50"`
Password string `json:"password" binding:"required,min=6,max=128"`
Email string `json:"email,omitempty" binding:"omitempty,email"`
Phone string `json:"phone,omitempty" binding:"omitempty,len=11"`
Vcode int32 `json:"vcode"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Username string `json:"username"`
Password string `json:"password"`
Vcode string `json:"vcode,omitempty"` // 验证码
}
type RegisterResp struct {
UserId int64 `json:"userId"`
Username string `json:"username"`
Email string `json:"email"`
Message string `json:"message"`
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken"`
User User `json:"user"`
}
type ResetPasswordByVcode struct {
Email string `json:"email"`
Password string `json:"password"`
Vcode string `json:"vcode"`
type RejectVerificationReq struct {
Id int64 `path:"id"`
Reason string `json:"reason"` // 必填:驳回原因
}
type UpdatePasswordReq struct {
UserId int64 `path:"userId" binding:"required,gt=0"`
OldPassword string `json:"oldPassword" binding:"required"`
NewPassword string `json:"newPassword" binding:"required,min=6,max=128"`
type ResetPasswordReq struct {
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Vcode string `json:"vcode"`
NewPassword string `json:"newPassword"`
}
type UpdatePasswordResp struct {
Message string `json:"message"`
type SwitchRoleReq struct {
Role string `json:"role"` // 目标角色
}
type UpdateUserInfoReq struct {
Nickname *string `json:"nickname,omitempty"`
Avatar *string `json:"avatar,omitempty"`
Bio *string `json:"bio,omitempty"`
type UnfollowUserReq struct {
Id int64 `path:"id"`
}
type UpdateUserInfoResp struct {
UserId int64 `json:"userId"`
Message string `json:"message"`
type UpdateNotifySettingsReq struct {
Order bool `json:"order,optional"`
Community bool `json:"community,optional"`
System bool `json:"system,optional"`
}
type UserInfo struct {
UserId int64 `json:"userId"`
Username string `json:"username"`
Email string `json:"email"`
Phone string `json:"phone"`
Avatar string `json:"avatar"`
Status int `json:"status"`
CreateAt int64 `json:"createAt"`
UpdateAt int64 `json:"updateAt"`
type UpdateThemeSettingsReq struct {
Theme string `json:"theme"` // dark, light
}
type UpdateUserProfileReq struct {
Nickname string `json:"nickname,optional"`
Avatar string `json:"avatar,optional"`
Bio string `json:"bio,optional"`
}
type User struct {
Id int64 `json:"id"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Role string `json:"role"` // consumer, player, owner, admin
VerifiedRoles []string `json:"verifiedRoles"` // e.g. ["consumer", "player"]
VerificationStatus map[string]string `json:"verificationStatus"` // e.g. {"player": "approved"}
Phone string `json:"phone,omitempty"`
Bio string `json:"bio,omitempty"`
CreatedAt string `json:"createdAt"` // ISO 8601
}
type VerificationEmptyResp struct {
}
type VerificationIdReq struct {
Id int64 `path:"id"` // 注意:这是 user_verifications.id
}
type VerificationItem struct {
Id int64 `json:"id"` // 认证记录ID (主键,用于管理员操作)
UserId int64 `json:"userId"` // 申请人ID (外键)
UserNickname string `json:"userNickname"` // 冗余显示,方便前端展示
Role string `json:"role"` // 申请角色: player, owner
Status string `json:"status"` // pending, approved, rejected
Materials map[string]string `json:"materials"` // 核心字段:对应 DB 的 JSONB
RejectReason string `json:"rejectReason"` // 驳回原因
CreatedAt string `json:"createdAt"` // 申请时间
ReviewedAt string `json:"reviewedAt"` // 审核时间
}