feature/member #1
8
Makefile
8
Makefile
|
@ -1,2 +1,10 @@
|
|||
#goctl api plugin -plugin goctl-swagger="swagger -filename gateway.json -host dev-api.30cm.net" -api ./generate/api/gateway.api -dir .
|
||||
#goctl api go -api ./generate/api/gateway.api -dir . -style go_zero
|
||||
|
||||
GOFMT ?= gofmt "-s"
|
||||
GOFILES := $(shell find . -name "*.go")
|
||||
|
||||
.PHONY: fmt
|
||||
fmt: # 格式優化
|
||||
$(GOFMT) -w $(GOFILES)
|
||||
goimports -w ./
|
|
@ -1,3 +1,26 @@
|
|||
Name: gateway
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
AccountRpc:
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: member.rpc
|
||||
NotificationRpc:
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: notification.rpc
|
||||
PermissionRpc:
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: permission.rpc
|
||||
Token:
|
||||
Expired: 300s
|
||||
|
||||
RedisCluster:
|
||||
Host: 127.0.0.1:7001
|
||||
Type: cluster
|
||||
|
||||
MailSender: <Kupi Service> noreply@code.30cm.net
|
|
@ -6,6 +6,7 @@ import (
|
|||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
|
17
gateway.json
17
gateway.json
|
@ -371,11 +371,26 @@
|
|||
"uid": {
|
||||
"type": "string",
|
||||
"description": "使用者UID"
|
||||
},
|
||||
"access_token": {
|
||||
"type": "string",
|
||||
"description": "訪問令牌 預設 5 分鐘過期"
|
||||
},
|
||||
"refresh_token": {
|
||||
"type": "string",
|
||||
"description": "刷新令牌 (預設一天過期,只能用一次),當呼叫更新token api 時,會自動把舊的失效,變成新的 refresh_token ,前端要記得過其實協助刷新,刷新不過表示全失效了(重新登入)"
|
||||
},
|
||||
"token_type": {
|
||||
"type": "string",
|
||||
"description": "Bearer"
|
||||
}
|
||||
},
|
||||
"title": "CreateAccountItem",
|
||||
"required": [
|
||||
"uid"
|
||||
"uid",
|
||||
"access_token",
|
||||
"refresh_token",
|
||||
"token_type"
|
||||
]
|
||||
},
|
||||
"CreateAccountRequest": {
|
||||
|
|
|
@ -20,16 +20,28 @@ type BaseResponse {
|
|||
}
|
||||
|
||||
// -------------------------------------------
|
||||
|
||||
type MemberLoginHeader {
|
||||
DeviceID string `header:"device_id"`
|
||||
IpAddress string `header:"ip_address"`
|
||||
Brewser string `header:"brewser"`
|
||||
}
|
||||
|
||||
type CreateAccountRequest {
|
||||
Account string `json:"account" validate:"required, account"` // 帳號名稱
|
||||
Token string `json:"token"` // 密碼或平台token,密碼請 sha256 轉碼
|
||||
TokenCheck string `json:"token_check"` // 密碼或平台token,密碼請 sha256 轉碼
|
||||
Platform string `json:"platform" validate:"oneof=digimon google twitter"` // 平台名稱 digimon, google, twitter
|
||||
AccountType string `json:"account_type" validate:"oneof=1 2 3"`// 帳號類型 1 Email 2. 台灣手機 3. 任意
|
||||
AccountType int64 `json:"account_type" validate:"oneof=1 2 3"`// 帳號類型 1 手機 2 信箱 3 自定義帳號
|
||||
MemberLoginHeader
|
||||
}
|
||||
|
||||
// CreateAccountItem 建立帳號也幫忙登入
|
||||
type CreateAccountItem {
|
||||
UID string `json:"uid"` // 使用者UID
|
||||
AccessToken string `json:"access_token"` // 訪問令牌 預設 5 分鐘過期
|
||||
RefreshToken string `json:"refresh_token"` // 刷新令牌 (預設一天過期,只能用一次),當呼叫更新token api 時,會自動把舊的失效,變成新的 refresh_token ,前端要記得過其實協助刷新,刷新不過表示全失效了(重新登入)
|
||||
TokenType string `json:"token_type"` // Bearer
|
||||
}
|
||||
|
||||
type CreateAccountResp {
|
||||
|
@ -44,6 +56,7 @@ type LoginResp {
|
|||
}
|
||||
|
||||
type LoginItem {
|
||||
UID string `json:"uid"` // Account
|
||||
AccessToken string `json:"access_token"` // 訪問令牌 預設 5 分鐘過期
|
||||
RefreshToken string `json:"refresh_token"` // 刷新令牌 (預設一天過期,只能用一次),當呼叫更新token api 時,會自動把舊的失效,變成新的 refresh_token ,前端要記得過其實協助刷新,刷新不過表示全失效了(重新登入)
|
||||
TokenType string `json:"token_type"` // Bearer
|
||||
|
@ -53,13 +66,14 @@ type LoginReq {
|
|||
Account string `json:"account" validate:"required, account"` // 帳號名稱
|
||||
Token string `json:"token"` // 密碼或平台token,密碼請 sha256 轉碼
|
||||
Platform string `json:"platform" validate:"oneof=digimon google twitter"` // 平台名稱 digimon, google, twitter
|
||||
AccountType string `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 Email 2. 台灣手機 3. 任意
|
||||
AccountType int64 `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 手機 2 信箱 3 自定義帳號
|
||||
MemberLoginHeader
|
||||
}
|
||||
// -------------------------------------------
|
||||
|
||||
type ForgetPasswordCodeReq {
|
||||
Account string `json:"account" validate:"required, account"` // 帳號名稱
|
||||
AccountType string `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 Email 2. 台灣手機 3. 任意
|
||||
AccountType int32 `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 手機 2 信箱 3 自定義帳號
|
||||
}
|
||||
// -------------------------------------------
|
||||
|
||||
|
@ -74,7 +88,12 @@ type UpdatePasswordReq {
|
|||
Token string `json:"token" validate:"required"` // 密碼或平台token,密碼請 sha256 轉碼
|
||||
TokenCheck string `json:"token_check" validate:"required"` // 密碼或平台token,密碼請 sha256 轉碼
|
||||
}
|
||||
|
||||
// -------------------------------------------
|
||||
type UpdateTokenReq {
|
||||
UID string `json:"uid" validate:"required"` // uid
|
||||
Token string `json:"token" validate:"required"` // refresh token
|
||||
MemberLoginHeader
|
||||
}
|
||||
|
||||
@server(
|
||||
group: member
|
||||
|
@ -107,7 +126,7 @@ service gateway {
|
|||
summary:"發送忘記密碼驗證"
|
||||
description: "發送忘記密碼驗證(三分鐘內只能發一次信)"
|
||||
)
|
||||
@handler ForgetPassworCode
|
||||
@handler ForgetPasswordCode
|
||||
post /member/forget-password-code (ForgetPasswordCodeReq) returns (BaseResponse)
|
||||
|
||||
/* @respdoc-400 (BaseResponse) // 輸入的參數錯誤 */
|
||||
|
@ -129,6 +148,17 @@ service gateway {
|
|||
)
|
||||
@handler UpadtePassword
|
||||
put /member/update-password (UpdatePasswordReq) returns (BaseResponse)
|
||||
|
||||
/* @respdoc-400 (BaseResponse) // 輸入的參數錯誤 */
|
||||
/* @respdoc-403 (BaseResponse) // 無效的驗證碼 */
|
||||
/* @respdoc-500 (BaseResponse) // 伺服器出錯 */
|
||||
@doc(
|
||||
summary:"更新Token"
|
||||
description: "用 RefreshToken 換取 AccessToken"
|
||||
)
|
||||
@handler RefreshAccessToken
|
||||
put /member/refresh_access_token (UpdateTokenReq) returns (LoginResp)
|
||||
|
||||
}
|
||||
|
||||
type Header {
|
||||
|
|
67
go.mod
67
go.mod
|
@ -2,27 +2,74 @@ module app-cloudep-portal-api-gateway
|
|||
|
||||
go 1.22.3
|
||||
|
||||
require github.com/zeromicro/go-zero v1.7.0
|
||||
require (
|
||||
code.30cm.net/digimon/library-go/errors v1.0.1
|
||||
github.com/gogo/protobuf v1.3.2
|
||||
github.com/matcornic/hermes/v2 v2.1.0
|
||||
github.com/zeromicro/go-zero v1.7.0
|
||||
)
|
||||
|
||||
require code.30cm.net/digimon/proto-all v0.0.0-20240826014806-3899ef10d82f
|
||||
|
||||
require (
|
||||
github.com/Masterminds/semver v1.4.2 // indirect
|
||||
github.com/Masterminds/sprig v2.16.0+incompatible // indirect
|
||||
github.com/PuerkitoBio/goquery v1.5.0 // indirect
|
||||
github.com/andybalholm/cascadia v1.0.0 // indirect
|
||||
github.com/aokoli/goutils v1.0.1 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/coreos/go-semver v0.3.1 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
|
||||
github.com/fatih/color v1.17.0 // indirect
|
||||
github.com/go-logr/logr v1.4.2 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-openapi/jsonpointer v0.19.6 // indirect
|
||||
github.com/go-openapi/jsonreference v0.20.2 // indirect
|
||||
github.com/go-openapi/swag v0.22.4 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
|
||||
github.com/golang/mock v1.6.0 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/google/gnostic-models v0.6.8 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/google/gofuzz v1.2.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/gorilla/css v1.0.0 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
|
||||
github.com/huandu/xstrings v1.2.0 // indirect
|
||||
github.com/imdario/mergo v0.3.6 // indirect
|
||||
github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0 // indirect
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/openzipkin/zipkin-go v0.4.3 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/prometheus/client_golang v1.19.1 // indirect
|
||||
github.com/prometheus/client_model v0.5.0 // indirect
|
||||
github.com/prometheus/common v0.48.0 // indirect
|
||||
github.com/prometheus/procfs v0.12.0 // indirect
|
||||
github.com/redis/go-redis/v9 v9.6.1 // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.0.1 // indirect
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
|
||||
github.com/vanng822/css v0.0.0-20190504095207-a21e860bcd04 // indirect
|
||||
github.com/vanng822/go-premailer v0.0.0-20191214114701-be27abe028fe // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
|
||||
go.etcd.io/etcd/client/v3 v3.5.15 // indirect
|
||||
go.opentelemetry.io/otel v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
|
||||
|
@ -34,13 +81,31 @@ require (
|
|||
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.24.0 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/automaxprocs v1.5.3 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
go.uber.org/zap v1.24.0 // indirect
|
||||
golang.org/x/crypto v0.25.0 // indirect
|
||||
golang.org/x/net v0.27.0 // indirect
|
||||
golang.org/x/oauth2 v0.20.0 // indirect
|
||||
golang.org/x/sys v0.22.0 // indirect
|
||||
golang.org/x/term v0.22.0 // indirect
|
||||
golang.org/x/text v0.16.0 // indirect
|
||||
golang.org/x/time v0.5.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
|
||||
google.golang.org/grpc v1.65.0 // indirect
|
||||
google.golang.org/protobuf v1.34.2 // indirect
|
||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
k8s.io/api v0.29.3 // indirect
|
||||
k8s.io/apimachinery v0.29.4 // indirect
|
||||
k8s.io/client-go v0.29.3 // indirect
|
||||
k8s.io/klog/v2 v2.110.1 // indirect
|
||||
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
|
||||
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
|
||||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
|
||||
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
|
||||
sigs.k8s.io/yaml v1.3.0 // indirect
|
||||
)
|
||||
|
|
|
@ -1,7 +1,23 @@
|
|||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/rest"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
Token struct {
|
||||
Expired time.Duration
|
||||
}
|
||||
// Redis Cluster
|
||||
RedisCluster redis.RedisConf
|
||||
|
||||
AccountRpc zrpc.RpcClientConf
|
||||
PermissionRpc zrpc.RpcClientConf
|
||||
NotificationRpc zrpc.RpcClientConf
|
||||
MailSender string
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package domain
|
||||
|
||||
const (
|
||||
DefaultCurrency = "NTD"
|
||||
DefaultLanguage = "zh-tw"
|
||||
DefaultGrantType = "client_credentials"
|
||||
DefaultScope = "gateway"
|
||||
)
|
||||
|
||||
type GrantType string
|
||||
|
||||
func (g GrantType) ToString() string {
|
||||
return string(g)
|
||||
}
|
||||
|
||||
const (
|
||||
GrantTypeClientCredentials GrantType = "client_credentials"
|
||||
)
|
|
@ -0,0 +1,31 @@
|
|||
package domain
|
||||
|
||||
type Platform int64
|
||||
|
||||
func (p Platform) ToInt64() int64 {
|
||||
return int64(p)
|
||||
}
|
||||
|
||||
const (
|
||||
PlatformNone Platform = -1
|
||||
)
|
||||
const (
|
||||
PlatformDigimon Platform = iota + 1
|
||||
PlatformGoogle
|
||||
PlatformTwitter
|
||||
)
|
||||
|
||||
var convPlatformCode = map[string]Platform{
|
||||
"digimon": PlatformDigimon,
|
||||
"google": PlatformGoogle,
|
||||
"twitter": PlatformTwitter,
|
||||
}
|
||||
|
||||
func GetPlatformByPlatformCode(code string) Platform {
|
||||
result, ok := convPlatformCode[code]
|
||||
if !ok {
|
||||
return PlatformNone
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package domain
|
||||
|
||||
import "strings"
|
||||
|
||||
type RedisKey string
|
||||
|
||||
const (
|
||||
GenerateVerifyCodeRedisKey RedisKey = "rf_code"
|
||||
)
|
||||
|
||||
func (key RedisKey) ToString() string {
|
||||
return "gateway:" + string(key)
|
||||
}
|
||||
|
||||
func (key RedisKey) With(s ...string) RedisKey {
|
||||
parts := append([]string{string(key)}, s...)
|
||||
|
||||
return RedisKey(strings.Join(parts, ":"))
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package domain
|
||||
|
||||
const (
|
||||
SuccessMsg = "success"
|
||||
SuccessCode = 102000
|
||||
)
|
|
@ -3,9 +3,12 @@ package member
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/logic/member"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
|
@ -20,7 +23,14 @@ func CheckVerifyCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
l := member.NewCheckVerifyCodeLogic(r.Context(), svcCtx)
|
||||
resp, err := l.CheckVerifyCode(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
e := ers.FromGRPCError(err)
|
||||
httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.BaseResponse{
|
||||
Status: types.Status{
|
||||
Code: int64(e.FullCode()),
|
||||
Message: e.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package member
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/logic/member"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
"net/http"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
|
@ -20,7 +21,14 @@ func CreateAccountHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
l := member.NewCreateAccountLogic(r.Context(), svcCtx)
|
||||
resp, err := l.CreateAccount(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
e := ers.FromGRPCError(err)
|
||||
httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.BaseResponse{
|
||||
Status: types.Status{
|
||||
Code: int64(e.FullCode()),
|
||||
Message: e.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
|
|
|
@ -3,13 +3,16 @@ package member
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/logic/member"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func ForgetPassworCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
func ForgetPasswordCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.ForgetPasswordCodeReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
|
@ -17,10 +20,17 @@ func ForgetPassworCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
return
|
||||
}
|
||||
|
||||
l := member.NewForgetPassworCodeLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ForgetPassworCode(&req)
|
||||
l := member.NewForgetPasswordCodeLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ForgetPasswordCode(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
e := ers.FromGRPCError(err)
|
||||
httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.BaseResponse{
|
||||
Status: types.Status{
|
||||
Code: int64(e.FullCode()),
|
||||
Message: e.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
|
@ -3,9 +3,12 @@ package member
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/logic/member"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
|
@ -20,7 +23,14 @@ func InfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
l := member.NewInfoLogic(r.Context(), svcCtx)
|
||||
resp, err := l.Info(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
e := ers.FromGRPCError(err)
|
||||
httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.BaseResponse{
|
||||
Status: types.Status{
|
||||
Code: int64(e.FullCode()),
|
||||
Message: e.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
|
|
|
@ -3,9 +3,12 @@ package member
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/logic/member"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
|
@ -20,7 +23,14 @@ func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
l := member.NewLoginLogic(r.Context(), svcCtx)
|
||||
resp, err := l.Login(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
e := ers.FromGRPCError(err)
|
||||
httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.BaseResponse{
|
||||
Status: types.Status{
|
||||
Code: int64(e.FullCode()),
|
||||
Message: e.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
|
|
|
@ -3,9 +3,12 @@ package member
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/logic/member"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
|
@ -20,7 +23,14 @@ func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
l := member.NewLogoutLogic(r.Context(), svcCtx)
|
||||
resp, err := l.Logout(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
e := ers.FromGRPCError(err)
|
||||
httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.BaseResponse{
|
||||
Status: types.Status{
|
||||
Code: int64(e.FullCode()),
|
||||
Message: e.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package member
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/logic/member"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func RefreshAccessTokenHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateTokenReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := member.NewRefreshAccessTokenLogic(r.Context(), svcCtx)
|
||||
resp, err := l.RefreshAccessToken(&req)
|
||||
if err != nil {
|
||||
e := ers.FromGRPCError(err)
|
||||
httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.BaseResponse{
|
||||
Status: types.Status{
|
||||
Code: int64(e.FullCode()),
|
||||
Message: e.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,9 +3,12 @@ package member
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/logic/member"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
|
@ -20,7 +23,14 @@ func UpadtePasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
l := member.NewUpadtePasswordLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UpadtePassword(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
e := ers.FromGRPCError(err)
|
||||
httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.BaseResponse{
|
||||
Status: types.Status{
|
||||
Code: int64(e.FullCode()),
|
||||
Message: e.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/member/forget-password-code",
|
||||
Handler: member.ForgetPassworCodeHandler(serverCtx),
|
||||
Handler: member.ForgetPasswordCodeHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
|
@ -39,6 +39,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
Path: "/member/update-password",
|
||||
Handler: member.UpadtePasswordHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPut,
|
||||
Path: "/member/refresh_access_token",
|
||||
Handler: member.RefreshAccessTokenHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithPrefix("/api/v1"),
|
||||
rest.WithTimeout(3000*time.Millisecond),
|
||||
|
@ -46,7 +51,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.AuthMiddleware},
|
||||
[]rest.Middleware{serverCtx.AuthMiddleware.Handle},
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
|
|
|
@ -24,7 +24,6 @@ func NewCheckVerifyCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *C
|
|||
}
|
||||
|
||||
func (l *CheckVerifyCodeLogic) CheckVerifyCode(req *types.CheckoutVerifyReq) (resp *types.BaseResponse, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/domain"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
accountRpc "code.30cm.net/digimon/proto-all/pkg/member"
|
||||
permissionRpc "code.30cm.net/digimon/proto-all/pkg/permission"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
@ -24,7 +30,83 @@ func NewCreateAccountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Cre
|
|||
}
|
||||
|
||||
func (l *CreateAccountLogic) CreateAccount(req *types.CreateAccountRequest) (resp *types.CreateAccountResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
// 驗證密碼,兩次密碼要一致
|
||||
if req.Token != req.TokenCheck {
|
||||
return nil, ers.InvalidFormat("password confirmation does not match")
|
||||
}
|
||||
|
||||
// 平台驗證
|
||||
p := domain.GetPlatformByPlatformCode(req.Platform)
|
||||
if p == domain.PlatformNone {
|
||||
return nil, ers.InvalidFormat("platform not found")
|
||||
}
|
||||
|
||||
// 建立帳號
|
||||
_, err = l.svcCtx.AccountRpc.CreateUserAccount(l.ctx, &accountRpc.CreateLoginUserReq{
|
||||
LoginId: req.Account,
|
||||
Platform: p.ToInt64(),
|
||||
Token: req.Token,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 綁定 UID
|
||||
bindResp, err := l.svcCtx.AccountRpc.BindAccount(l.ctx, &accountRpc.BindingUserReq{
|
||||
LoginId: req.Account,
|
||||
Type: req.AccountType,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 綁定使用者基礎資訊
|
||||
if err := l.bindUserInfo(bindResp.Uid); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 發 token
|
||||
token, err := l.generateToken(bindResp.Uid, req.DeviceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 建立回應
|
||||
return &types.CreateAccountResp{
|
||||
Status: types.Status{
|
||||
Code: domain.SuccessCode,
|
||||
Message: domain.SuccessMsg,
|
||||
},
|
||||
Data: types.CreateAccountItem{
|
||||
UID: bindResp.Uid,
|
||||
AccessToken: token.AccessToken,
|
||||
RefreshToken: token.RefreshToken,
|
||||
TokenType: token.TokenType,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// bindUserInfo 綁定使用者基礎資訊
|
||||
func (l *CreateAccountLogic) bindUserInfo(uid string) error {
|
||||
_, err := l.svcCtx.AccountRpc.BindUserInfo(l.ctx, &accountRpc.CreateUserInfoReq{
|
||||
Uid: uid,
|
||||
Currency: domain.DefaultCurrency,
|
||||
Language: domain.DefaultLanguage,
|
||||
NickName: proto.String(uid),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// generateToken 生成 token
|
||||
func (l *CreateAccountLogic) generateToken(uid, deviceID string) (*permissionRpc.TokenResp, error) {
|
||||
return l.svcCtx.TokenRpc.NewToken(l.ctx, &permissionRpc.AuthorizationReq{
|
||||
GrantType: domain.GrantTypeClientCredentials.ToString(),
|
||||
DeviceId: deviceID,
|
||||
Scope: domain.DefaultScope,
|
||||
IsRefreshToken: true,
|
||||
Expires: int32(time.Now().UTC().Add(l.svcCtx.Config.Token.Expired).Unix()),
|
||||
Data: map[string]string{
|
||||
"uid": uid,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ForgetPassworCodeLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewForgetPassworCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ForgetPassworCodeLogic {
|
||||
return &ForgetPassworCodeLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ForgetPassworCodeLogic) ForgetPassworCode(req *types.ForgetPasswordCodeReq) (resp *types.BaseResponse, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
|
@ -0,0 +1,190 @@
|
|||
package member
|
||||
|
||||
import (
|
||||
"app-cloudep-portal-api-gateway/internal/domain"
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
accountRpc "code.30cm.net/digimon/proto-all/pkg/member"
|
||||
notificationRpc "code.30cm.net/digimon/proto-all/pkg/notification"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/matcornic/hermes/v2"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ForgetPasswordCodeLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewForgetPasswordCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ForgetPasswordCodeLogic {
|
||||
return &ForgetPasswordCodeLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ForgetPasswordCodeLogic) ForgetPasswordCode(req *types.ForgetPasswordCodeReq) (*types.BaseResponse, error) {
|
||||
// 限制三分鐘內只可以發送一次
|
||||
accountType := req.AccountType
|
||||
// TODO 目前只可以給信箱驗證,其餘的自動轉換成信箱
|
||||
accountType = 2
|
||||
|
||||
rk := domain.GenerateVerifyCodeRedisKey.With(fmt.Sprintf("%s-%d", req.Account, accountType)).ToString()
|
||||
get, err := l.svcCtx.Redis.Get(rk)
|
||||
if err != nil {
|
||||
// Redis 錯誤,給予適當的提示(可以視情況返回錯誤或繼續)
|
||||
l.Logger.Errorf("Redis error: %v", err)
|
||||
}
|
||||
if get != "" {
|
||||
// 已經發送過驗證碼,返回提示
|
||||
return nil, ers.ArkInternal("verification code already sent, please wait before requesting again")
|
||||
}
|
||||
|
||||
// 確認帳號是否已經註冊過
|
||||
accountResp, err := l.svcCtx.AccountRpc.GetUidByAccount(l.ctx, &accountRpc.GetUIDByAccountReq{
|
||||
Account: req.Account,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 生成重置密碼驗證碼
|
||||
code, err := l.svcCtx.AccountRpc.GenerateRefreshCode(l.ctx, &accountRpc.GenerateRefreshCodeReq{
|
||||
Account: req.Account,
|
||||
CodeType: accountType,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 取得用戶資訊
|
||||
info, err := l.svcCtx.AccountRpc.GetUserInfo(l.ctx, &accountRpc.GetUserInfoReq{Uid: accountResp.Uid})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 準備驗證碼郵件
|
||||
nickName := info.Data.Uid
|
||||
if info.Data.NickName != nil {
|
||||
nickName = *info.Data.NickName
|
||||
}
|
||||
mailContent, title, err := ForgerZHTW(nickName, code.Data.VerifyCode)
|
||||
if err != nil {
|
||||
return nil, ers.InvalidFormat("failed to generate mail content: ", err.Error())
|
||||
}
|
||||
|
||||
// 發送郵件
|
||||
_, err = l.svcCtx.NotificationRpc.SendMail(l.ctx, ¬ificationRpc.SendMailReq{
|
||||
Body: mailContent,
|
||||
Subject: title,
|
||||
To: req.Account,
|
||||
From: l.svcCtx.Config.MailSender,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 設置 Redis 鍵,並設置 3 分鐘的過期時間
|
||||
err = l.svcCtx.Redis.Set(rk, code.Data.VerifyCode)
|
||||
if err != nil {
|
||||
return nil, ers.ArkInternal(err.Error())
|
||||
}
|
||||
err = l.svcCtx.Redis.Expire(rk, 180)
|
||||
if err != nil {
|
||||
return nil, ers.ArkInternal(err.Error())
|
||||
}
|
||||
|
||||
// 返回成功響應
|
||||
return &types.BaseResponse{
|
||||
Status: types.Status{
|
||||
Code: domain.SuccessCode,
|
||||
Message: domain.SuccessMsg,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
type generateForgetPasswordMailBodyReq struct {
|
||||
ProductName string
|
||||
ProductLink string
|
||||
ProductLogo string
|
||||
ProductCopyright string
|
||||
|
||||
BodyName string
|
||||
BodyIntros []string
|
||||
BodyActions []hermes.Action
|
||||
BodyOutros []string
|
||||
BodySignature string
|
||||
}
|
||||
|
||||
func ForgerZHTW(username, verifyCode string) (body, title string, err error) {
|
||||
req := generateForgetPasswordMailBodyReq{
|
||||
ProductName: "Digimon 團隊",
|
||||
ProductLink: "https://code.30cm.net",
|
||||
ProductLogo: "https://storage.googleapis.com/netpute-qa-public/logo_dark.png",
|
||||
ProductCopyright: "© 2024 Digimon Inc. 版權所有",
|
||||
|
||||
BodyName: username,
|
||||
BodyIntros: []string{
|
||||
"您收到此電子郵件是因為我們收到了針對 Digimon 帳戶的密碼重置請求。",
|
||||
},
|
||||
BodyActions: []hermes.Action{
|
||||
{
|
||||
Instructions: "請複製您的驗證碼,到網頁重置",
|
||||
InviteCode: verifyCode,
|
||||
},
|
||||
},
|
||||
BodyOutros: []string{
|
||||
"如果您不要求重設密碼,則無需您採取進一步的措施。",
|
||||
},
|
||||
BodySignature: "無所不在的即時遊戲社群平台",
|
||||
}
|
||||
pr, bo := mustForgetPasswordMail(req)
|
||||
|
||||
body, err = genMailBody(forgetPasswordTemplate{pr, bo})
|
||||
|
||||
return body, "Digimon 平台,重設密碼驗證信", err
|
||||
}
|
||||
|
||||
func mustForgetPasswordMail(req generateForgetPasswordMailBodyReq) (hermes.Product, hermes.Body) {
|
||||
product := hermes.Product{
|
||||
// Appears in header & footer of e-mails
|
||||
Name: req.ProductName,
|
||||
Link: req.ProductLink,
|
||||
Logo: req.ProductLogo,
|
||||
Copyright: req.ProductCopyright,
|
||||
}
|
||||
|
||||
body := hermes.Body{
|
||||
Name: req.BodyName,
|
||||
Intros: req.BodyIntros,
|
||||
Actions: req.BodyActions,
|
||||
Outros: req.BodyOutros,
|
||||
Signature: req.BodySignature,
|
||||
}
|
||||
|
||||
return product, body
|
||||
}
|
||||
|
||||
type forgetPasswordTemplate struct {
|
||||
Product hermes.Product
|
||||
Body hermes.Body
|
||||
}
|
||||
|
||||
func genMailBody(req forgetPasswordTemplate) (body string, err error) {
|
||||
h := hermes.Hermes{
|
||||
Product: req.Product,
|
||||
}
|
||||
email := hermes.Email{
|
||||
Body: req.Body,
|
||||
}
|
||||
emailBody, err := h.GenerateHTML(email)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return emailBody, nil
|
||||
}
|
|
@ -1,7 +1,14 @@
|
|||
package member
|
||||
|
||||
import (
|
||||
"app-cloudep-portal-api-gateway/internal/domain"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
accountRpc "code.30cm.net/digimon/proto-all/pkg/member"
|
||||
permissionRpc "code.30cm.net/digimon/proto-all/pkg/permission"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
@ -24,7 +31,74 @@ 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
|
||||
var result *accountRpc.VerifyAuthResultResp
|
||||
|
||||
return
|
||||
// Step 1 驗證進來的 Token
|
||||
platform := domain.GetPlatformByPlatformCode(req.Platform)
|
||||
switch platform {
|
||||
case domain.PlatformDigimon:
|
||||
// 原始平台驗證
|
||||
result, err = l.svcCtx.AccountRpc.VerifyPlatformAuthResult(l.ctx, &accountRpc.VerifyAuthResultReq{
|
||||
Account: proto.String(req.Account),
|
||||
Token: req.Token,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case domain.PlatformGoogle:
|
||||
result, err = l.svcCtx.AccountRpc.VerifyGoogleAuthResult(l.ctx, &accountRpc.VerifyAuthResultReq{
|
||||
Token: req.Token,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case domain.PlatformTwitter:
|
||||
default:
|
||||
return nil, ers.InvalidFormat("invalid platform")
|
||||
}
|
||||
|
||||
if !result.Status {
|
||||
return nil, ers.Forbidden("failed to validate password ")
|
||||
}
|
||||
|
||||
account, err := l.svcCtx.AccountRpc.GetUidByAccount(l.ctx, &accountRpc.GetUIDByAccountReq{
|
||||
Account: req.Account,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 發 token
|
||||
token, err := l.generateToken(account.Uid, req.DeviceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 建立回應
|
||||
return &types.LoginResp{
|
||||
Status: types.Status{
|
||||
Code: domain.SuccessCode,
|
||||
Message: domain.SuccessMsg,
|
||||
},
|
||||
Data: types.LoginItem{
|
||||
UID: account.Uid,
|
||||
AccessToken: token.AccessToken,
|
||||
RefreshToken: token.RefreshToken,
|
||||
TokenType: token.TokenType,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// generateToken 生成 token
|
||||
func (l *LoginLogic) generateToken(uid, deviceID string) (*permissionRpc.TokenResp, error) {
|
||||
return l.svcCtx.TokenRpc.NewToken(l.ctx, &permissionRpc.AuthorizationReq{
|
||||
GrantType: domain.GrantTypeClientCredentials.ToString(),
|
||||
DeviceId: deviceID,
|
||||
Scope: domain.DefaultScope,
|
||||
IsRefreshToken: true,
|
||||
Expires: int32(time.Now().UTC().Add(l.svcCtx.Config.Token.Expired).Unix()),
|
||||
Data: map[string]string{
|
||||
"uid": uid,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package member
|
||||
|
||||
import (
|
||||
"app-cloudep-portal-api-gateway/internal/domain"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
permissionRpc "code.30cm.net/digimon/proto-all/pkg/permission"
|
||||
|
||||
"app-cloudep-portal-api-gateway/internal/svc"
|
||||
"app-cloudep-portal-api-gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RefreshAccessTokenLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewRefreshAccessTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RefreshAccessTokenLogic {
|
||||
return &RefreshAccessTokenLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *RefreshAccessTokenLogic) RefreshAccessToken(req *types.UpdateTokenReq) (resp *types.LoginResp, err error) {
|
||||
// TODO 看未來有沒有需要把UID 拿去驗證
|
||||
token, err := l.svcCtx.TokenRpc.RefreshToken(l.ctx, &permissionRpc.RefreshTokenReq{
|
||||
Token: req.Token,
|
||||
Scope: domain.DefaultScope,
|
||||
Expires: time.Now().UTC().Add(l.svcCtx.Config.Token.Expired).Unix(),
|
||||
DeviceId: req.DeviceID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 建立回應
|
||||
return &types.LoginResp{
|
||||
Status: types.Status{
|
||||
Code: domain.SuccessCode,
|
||||
Message: domain.SuccessMsg,
|
||||
},
|
||||
Data: types.LoginItem{
|
||||
UID: req.UID,
|
||||
AccessToken: token.Token,
|
||||
RefreshToken: token.OneTimeToken,
|
||||
TokenType: token.TokenType,
|
||||
},
|
||||
}, nil
|
||||
}
|
|
@ -2,14 +2,42 @@ package svc
|
|||
|
||||
import (
|
||||
"app-cloudep-portal-api-gateway/internal/config"
|
||||
"app-cloudep-portal-api-gateway/internal/middleware"
|
||||
|
||||
ers "code.30cm.net/digimon/library-go/errors"
|
||||
"code.30cm.net/digimon/library-go/errors/code"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
accountRpc "code.30cm.net/digimon/proto-all/pkg/member"
|
||||
notificationRpc "code.30cm.net/digimon/proto-all/pkg/notification"
|
||||
permissionRpc "code.30cm.net/digimon/proto-all/pkg/permission"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
|
||||
AuthMiddleware *middleware.AuthMiddleware
|
||||
AccountRpc accountRpc.AccountClient
|
||||
TokenRpc permissionRpc.TokenServiceClient
|
||||
NotificationRpc notificationRpc.SenderServiceClient
|
||||
Redis redis.Redis
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
ers.Scope = code.CloudEPPortalGW
|
||||
|
||||
newRedis, err := redis.NewRedis(c.RedisCluster, redis.Cluster())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
AuthMiddleware: middleware.NewAuthMiddleware(),
|
||||
AccountRpc: accountRpc.NewAccountClient(zrpc.MustNewClient(c.AccountRpc).Conn()),
|
||||
TokenRpc: permissionRpc.NewTokenServiceClient(zrpc.MustNewClient(c.PermissionRpc).Conn()),
|
||||
NotificationRpc: notificationRpc.NewSenderServiceClient(zrpc.MustNewClient(c.NotificationRpc).Conn()),
|
||||
Redis: *newRedis,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,16 +12,26 @@ type BaseResponse struct {
|
|||
Status Status `json:"status"` // 狀態
|
||||
}
|
||||
|
||||
type MemberLoginHeader struct {
|
||||
DeviceID string `header:"device_id"`
|
||||
IpAddress string `header:"ip_address"`
|
||||
Brewser string `header:"brewser"`
|
||||
}
|
||||
|
||||
type CreateAccountRequest struct {
|
||||
Account string `json:"account" validate:"required, account"` // 帳號名稱
|
||||
Token string `json:"token"` // 密碼或平台token,密碼請 sha256 轉碼
|
||||
TokenCheck string `json:"token_check"` // 密碼或平台token,密碼請 sha256 轉碼
|
||||
Platform string `json:"platform" validate:"oneof=digimon google twitter"` // 平台名稱 digimon, google, twitter
|
||||
AccountType string `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 Email 2. 台灣手機 3. 任意
|
||||
AccountType int64 `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 手機 2 信箱 3 自定義帳號
|
||||
MemberLoginHeader
|
||||
}
|
||||
|
||||
type CreateAccountItem struct {
|
||||
UID string `json:"uid"` // 使用者UID
|
||||
AccessToken string `json:"access_token"` // 訪問令牌 預設 5 分鐘過期
|
||||
RefreshToken string `json:"refresh_token"` // 刷新令牌 (預設一天過期,只能用一次),當呼叫更新token api 時,會自動把舊的失效,變成新的 refresh_token ,前端要記得過其實協助刷新,刷新不過表示全失效了(重新登入)
|
||||
TokenType string `json:"token_type"` // Bearer
|
||||
}
|
||||
|
||||
type CreateAccountResp struct {
|
||||
|
@ -35,6 +45,7 @@ type LoginResp struct {
|
|||
}
|
||||
|
||||
type LoginItem struct {
|
||||
UID string `json:"uid"` // Account
|
||||
AccessToken string `json:"access_token"` // 訪問令牌 預設 5 分鐘過期
|
||||
RefreshToken string `json:"refresh_token"` // 刷新令牌 (預設一天過期,只能用一次),當呼叫更新token api 時,會自動把舊的失效,變成新的 refresh_token ,前端要記得過其實協助刷新,刷新不過表示全失效了(重新登入)
|
||||
TokenType string `json:"token_type"` // Bearer
|
||||
|
@ -44,12 +55,13 @@ type LoginReq struct {
|
|||
Account string `json:"account" validate:"required, account"` // 帳號名稱
|
||||
Token string `json:"token"` // 密碼或平台token,密碼請 sha256 轉碼
|
||||
Platform string `json:"platform" validate:"oneof=digimon google twitter"` // 平台名稱 digimon, google, twitter
|
||||
AccountType string `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 Email 2. 台灣手機 3. 任意
|
||||
AccountType int64 `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 手機 2 信箱 3 自定義帳號
|
||||
MemberLoginHeader
|
||||
}
|
||||
|
||||
type ForgetPasswordCodeReq struct {
|
||||
Account string `json:"account" validate:"required, account"` // 帳號名稱
|
||||
AccountType string `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 Email 2. 台灣手機 3. 任意
|
||||
AccountType int32 `json:"account_type" validate:"oneof=1 2 3"` // 帳號類型 1 手機 2 信箱 3 自定義帳號
|
||||
}
|
||||
|
||||
type CheckoutVerifyReq struct {
|
||||
|
@ -64,6 +76,12 @@ type UpdatePasswordReq struct {
|
|||
TokenCheck string `json:"token_check" validate:"required"` // 密碼或平台token,密碼請 sha256 轉碼
|
||||
}
|
||||
|
||||
type UpdateTokenReq struct {
|
||||
UID string `json:"uid" validate:"required"` // uid
|
||||
Token string `json:"token" validate:"required"` // refresh token
|
||||
MemberLoginHeader
|
||||
}
|
||||
|
||||
type Header struct {
|
||||
Uid string `header:"uid"`
|
||||
Token string `header:"token"`
|
||||
|
|
Loading…
Reference in New Issue