github.com/Go-To-Byte/DouSheng/user_center@v0.0.0-20230524130918-ad531c1a3f6a/apps/user/impl/dao.go (about)

     1  // @Author: Ciusyan 2023/1/24
     2  package impl
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  	"github.com/Go-To-Byte/DouSheng/api_rooter/apps/token"
     8  	"github.com/Go-To-Byte/DouSheng/dou_kit/constant"
     9  	"github.com/Go-To-Byte/DouSheng/dou_kit/exception"
    10  
    11  	"github.com/Go-To-Byte/DouSheng/user_center/apps/user"
    12  )
    13  
    14  func newGetUserReq() *getUserReq {
    15  	return &getUserReq{}
    16  }
    17  
    18  // getUserReq 查询用户信息
    19  type getUserReq struct {
    20  	// 用户名
    21  	Username string `json:"username"`
    22  	// IDS
    23  	UserId int64 `json:"user_id"`
    24  }
    25  
    26  // userList 根据用户名称获取用户
    27  func (s *userServiceImpl) userList(ctx context.Context, userIds []int64) ([]*user.UserPo, error) {
    28  
    29  	pos := make([]*user.UserPo, 0)
    30  	if userIds == nil || len(userIds) <= 0 {
    31  		s.l.Errorf("user userList:你的参数可能有问题哟~")
    32  		return pos, nil
    33  	}
    34  
    35  	// 查询
    36  	db := s.db.WithContext(ctx).Where("id IN ?", userIds).Find(&pos)
    37  
    38  	if db.Error != nil {
    39  		return nil, db.Error
    40  	}
    41  
    42  	if db.RowsAffected == 0 {
    43  		return nil, exception.WithStatusCode(constant.WRONG_USER_NOT_EXIST)
    44  	}
    45  
    46  	return pos, nil
    47  }
    48  
    49  // 通过user_id 或 username 查找用户
    50  func (s *userServiceImpl) getUser(ctx context.Context, req *getUserReq) (*user.UserPo, error) {
    51  
    52  	db := s.db.WithContext(ctx)
    53  
    54  	po := user.NewDefaultUserPo()
    55  	if req.Username != "" && req.UserId <= 0 {
    56  		db = db.Where("username = ?", req.Username)
    57  	} else if req.UserId > 0 && req.Username == "" {
    58  		db = db.Where("id = ?", req.UserId)
    59  	} else {
    60  		s.l.Errorf("user userList:你的参数可能有问题哟~")
    61  		return po, nil
    62  	}
    63  
    64  	db = db.Find(po)
    65  
    66  	if db.Error != nil {
    67  		s.l.Errorf("user getUser:%s", db.Error.Error())
    68  		return nil, db.Error
    69  	}
    70  
    71  	return po, nil
    72  }
    73  
    74  // insert 创建用户
    75  func (s *userServiceImpl) insert(ctx context.Context, user *user.UserPo) (*user.UserPo, error) {
    76  
    77  	res := s.db.WithContext(ctx).Create(user)
    78  
    79  	// TODO:统一异常处理
    80  	if res.Error != nil {
    81  		return nil, fmt.Errorf("创建用户失败:%s", res.Error.Error())
    82  	}
    83  
    84  	return user, nil
    85  }
    86  
    87  func (s *userServiceImpl) token(ctx context.Context, po *user.UserPo) (accessToken string) {
    88  	// 颁发Token
    89  	tkReq := token.NewIssueTokenRequest(po)
    90  	tk, err := s.tokenService.IssueToken(ctx, tkReq)
    91  
    92  	// 若Token颁发失败,不要报错,打印日志即可
    93  	if err != nil {
    94  		accessToken = ""
    95  		s.l.Errorf("Token颁发失败:%s", err.Error())
    96  	} else {
    97  		accessToken = tk.AccessToken
    98  		s.l.Infof("Token颁发成功:%s", accessToken)
    99  	}
   100  	return
   101  }