feature/init_member #1

Merged
daniel.w merged 7 commits from feature/init_member into main 2024-08-22 14:26:04 +00:00
6 changed files with 214 additions and 6 deletions
Showing only changes of commit 994597cd94 - Show all commits

View File

@ -48,8 +48,8 @@ build-docker:
.PHONY: gen-my-sql-model .PHONY: gen-my-sql-model
gen-my-sql-model: # 建立 rpc 資料庫 gen-my-sql-model: # 建立 rpc 資料庫
goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020011_account_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i '' goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020011_account_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020011_account_uid_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i '' goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020012_account_uid_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020011_user_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i '' goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020013_user_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
goctl model mysql ddl -c no -s ./generate/database/mysql/20230719061241_machine_node.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i '' goctl model mysql ddl -c no -s ./generate/database/mysql/20230719061241_machine_node.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
@echo "Generate mysql model files successfully" @echo "Generate mysql model files successfully"

View File

@ -12,5 +12,6 @@ CREATE TABLE `user_table` (
`update_time` BIGINT NOT NULL DEFAULT 0, `update_time` BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
INDEX `idx_create_time` (`create_time` ASC), INDEX `idx_create_time` (`create_time` ASC),
INDEX `idx_nick_name` (`nick_name` ASC),
UNIQUE INDEX `uk_uid` (`uid` ASC) UNIQUE INDEX `uk_uid` (`uid` ASC)
)ENGINE=InnoDB COMMENT='使用者資訊'; )ENGINE=InnoDB COMMENT='使用者資訊';

View File

@ -85,9 +85,10 @@ message UpdateUserInfoReq {
optional string language = 2; optional string language = 2;
optional string currency = 3; optional string currency = 3;
optional string nick_name = 4; optional string nick_name = 4;
optional VerifyType verify_type = 5; optional string avatar = 5;
optional AlarmType alarm_type = 6; optional VerifyType verify_type = 6;
optional MemberStatus status = 7; optional AlarmType alarm_type = 7;
optional MemberStatus status = 8;
} }
message GetUIDByAccountReq { message GetUIDByAccountReq {
@ -102,6 +103,7 @@ message GetUidByAccountResp {
message UpdateTokenReq { message UpdateTokenReq {
string account = 1; string account = 1;
string token = 2; string token = 2;
int64 platform=3;
} }
message GenerateRefreshCodeReq { message GenerateRefreshCodeReq {

2
go.mod
View File

@ -13,6 +13,7 @@ require (
go.mongodb.org/mongo-driver v1.16.0 go.mongodb.org/mongo-driver v1.16.0
go.uber.org/goleak v1.3.0 go.uber.org/goleak v1.3.0
go.uber.org/mock v0.4.0 go.uber.org/mock v0.4.0
golang.org/x/crypto v0.25.0
google.golang.org/grpc v1.65.0 google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2 google.golang.org/protobuf v1.34.2
) )
@ -90,7 +91,6 @@ require (
go.uber.org/automaxprocs v1.5.3 // indirect go.uber.org/automaxprocs v1.5.3 // indirect
go.uber.org/multierr v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.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/net v0.27.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.0 // indirect golang.org/x/sync v0.7.0 // indirect

View File

@ -1,6 +1,9 @@
package model package model
import ( import (
"context"
"database/sql"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"
) )
@ -12,6 +15,7 @@ type (
// and implement the added methods in customAccountModel. // and implement the added methods in customAccountModel.
AccountModel interface { AccountModel interface {
accountModel accountModel
UpdateTokenByLoginID(ctx context.Context, account string, token string) error
} }
customAccountModel struct { customAccountModel struct {
@ -25,3 +29,18 @@ func NewAccountModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option)
defaultAccountModel: newAccountModel(conn, c, opts...), defaultAccountModel: newAccountModel(conn, c, opts...),
} }
} }
func (m *defaultAccountModel) UpdateTokenByLoginID(ctx context.Context, account string, token string) error {
data, err := m.FindOneByAccount(ctx, account)
if err != nil {
return err
}
accountAccountKey := fmt.Sprintf("%s%v", cacheAccountAccountPrefix, data.Account)
accountIdKey := fmt.Sprintf("%s%v", cacheAccountIdPrefix, data.Id)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set `token` = ? where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, token, data.Id)
}, accountAccountKey, accountIdKey)
return err
}

View File

@ -1,8 +1,15 @@
package model package model
import ( import (
"app-cloudep-member-server/gen_result/pb/member"
"context"
"database/sql"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
"time"
) )
var _ UserTableModel = (*customUserTableModel)(nil) var _ UserTableModel = (*customUserTableModel)(nil)
@ -12,11 +19,27 @@ type (
// and implement the added methods in customUserTableModel. // and implement the added methods in customUserTableModel.
UserTableModel interface { UserTableModel interface {
userTableModel userTableModel
FindOneByNickName(ctx context.Context, uid string) (*UserTable, error)
ListMembers(ctx context.Context, params *UserQueryParams) ([]*UserTable, error)
Count(ctx context.Context) (int64, error)
UpdateStatus(ctx context.Context, uid string, status int32) error
UpdateSome(ctx context.Context, newData *member.UpdateUserInfoReq) error
} }
customUserTableModel struct { customUserTableModel struct {
*defaultUserTableModel *defaultUserTableModel
} }
UserQueryParams struct {
RoleId *string
VerifyType *int32
AlarmType *int32
Status *int32
CreateStartTime *int64
CreateEndTime *int64
PageSize int64
PageIndex int64
}
) )
// NewUserTableModel returns a model for the database table. // NewUserTableModel returns a model for the database table.
@ -25,3 +48,166 @@ func NewUserTableModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Optio
defaultUserTableModel: newUserTableModel(conn, c, opts...), defaultUserTableModel: newUserTableModel(conn, c, opts...),
} }
} }
func (m *defaultUserTableModel) FindOneByNickName(ctx context.Context, nickName string) (*UserTable, error) {
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, nickName)
var resp UserTable
err := m.QueryRowIndexCtx(ctx, &resp, userTableUidKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `nick_name` = ? limit 1", userTableRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, nickName); err != nil {
return nil, err
}
return resp.Id, nil
}, m.queryPrimary)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultUserTableModel) ListMembers(ctx context.Context, params *UserQueryParams) ([]*UserTable, error) {
query := fmt.Sprintf("select %s from %s", userTableRows, m.table)
var args = make([]any, 0, 10)
var conditions []string
if params.RoleId != nil {
conditions = append(conditions, "role_id = ?")
args = append(args, *params.RoleId)
}
if params.VerifyType != nil {
conditions = append(conditions, "verify_type = ?")
args = append(args, *params.VerifyType)
}
if params.AlarmType != nil {
conditions = append(conditions, "alarm_type = ?")
args = append(args, *params.AlarmType)
}
if params.Status != nil {
conditions = append(conditions, "status = ?")
args = append(args, *params.Status)
}
if params.CreateStartTime != nil {
conditions = append(conditions, "create_time >= ?")
args = append(args, *params.CreateStartTime)
}
if params.CreateEndTime != nil {
conditions = append(conditions, "create_time <= ?")
args = append(args, *params.CreateEndTime)
}
// 加入條件到查詢語句中
if len(conditions) > 0 {
query += " WHERE " + strings.Join(conditions, " AND ")
}
// 分頁處理
offset := (params.PageIndex - 1) * params.PageSize
query += " LIMIT ? OFFSET ?"
args = append(args, params.PageSize, offset)
var users []*UserTable
fmt.Println("query:", query)
fmt.Println("args:", args)
err := m.QueryRowsNoCacheCtx(ctx, &users, query, args...)
if err != nil {
return nil, err
}
return users, nil
}
func (m *defaultUserTableModel) Count(ctx context.Context) (int64, error) {
var count int64
query := fmt.Sprintf("select count(*) from %s", m.table)
err := m.QueryRowNoCacheCtx(ctx, &count, query)
if err != nil {
return 0, err
}
return count, nil
}
func (m *defaultUserTableModel) UpdateStatus(ctx context.Context, uid string, status int32) error {
data, err := m.FindOneByUid(ctx, uid)
if err != nil {
return err
}
userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, data.Id)
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set `status` = ? where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, status, data.Id)
}, userTableIdKey, userTableUidKey)
return err
}
func (m *defaultUserTableModel) UpdateSome(ctx context.Context, newData *member.UpdateUserInfoReq) error {
data, err := m.FindOneByUid(ctx, newData.Uid)
if err != nil {
return err
}
// 初始化缓存键
userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, data.Id)
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid)
query := fmt.Sprintf("update %s set ", m.table)
var args []interface{}
var updates []string
if newData.VerifyType != nil {
updates = append(updates, "verify_type = ?")
args = append(args, *newData.VerifyType)
}
if newData.AlarmType != nil {
updates = append(updates, "alarm_type = ?")
args = append(args, *newData.AlarmType)
}
if newData.Status != nil {
updates = append(updates, "status = ?")
args = append(args, *newData.Status)
}
if newData.Language != nil {
updates = append(updates, "language = ?")
args = append(args, *newData.Language)
}
if newData.Currency != nil {
updates = append(updates, "currency = ?")
args = append(args, *newData.Currency)
}
if newData.NickName != nil {
updates = append(updates, "nick_name = ?")
args = append(args, *newData.NickName)
}
if newData.Avatar != nil {
updates = append(updates, "avatar = ?")
args = append(args, *newData.Avatar)
}
if len(updates) == 0 {
return nil
}
update := time.Now().UTC().Unix()
updates = append(updates, "update_time = ?")
args = append(args, &update)
query += strings.Join(updates, ", ") + " where `id` = ?"
args = append(args, data.Id)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
return conn.ExecCtx(ctx, query, args...)
}, userTableIdKey, userTableUidKey)
return err
}