123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package user
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/util"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "gorm.io/gorm"
- "strings"
- "time"
- "xingjia-management-gateway/apis"
- "xingjia-management-gateway/consts"
- "xingjia-management-gateway/errors"
- "xingjia-management-gateway/impl/v1/operation_log"
- dbmodel "xingjia-management-gateway/model"
- )
- func checkUserAddParam(req *apis.UserAddRequest) error {
- switch {
- case req.RealName == "":
- return status.Error(10003, "请输入姓名")
- case req.Password == "":
- return status.Error(10003, "请输入密码")
- case req.User == "":
- return status.Error(10003, "请输入账号")
- case req.UserType != consts.UserTypeNormal && req.UserType != consts.UserTypeTemp:
- return status.Error(10003, "不支持的账号类型")
- case req.UserType == consts.UserTypeTemp && (req.EffectiveStart < 1 || req.EffectiveEnd < 1 || req.EffectiveStart > req.EffectiveEnd):
- return status.Error(10003, "临时账号请输入有效时间")
- case req.SelfId == 0:
- return status.Error(10003, "未知操作者")
- }
- return nil
- }
- func checkAdmin(selfId int64) error {
- p := &dbmodel.TUser{}
- where := map[string]interface{}{
- "id": selfId,
- }
- err := p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- if p.ID == 0 {
- return errors.ErrRecordNotFound
- }
- if p.UserType != consts.UserTypeSuper {
- return errors.PermissionError
- }
- return nil
- }
- func getUserContent(p dbmodel.TUser) string {
- userType := "普通账号"
- effectiveStart := "-"
- effectiveEnd := "-"
- if p.UserType == consts.UserTypeTemp {
- userType = "临时账号"
- effectiveStart = fmt.Sprintf("%d", time.Unix(p.EffectiveStart, 0))
- effectiveEnd = fmt.Sprintf("%d", time.Unix(p.EffectiveEnd, 0))
- }
- m := map[string]interface{}{
- "账号名": p.User,
- "姓名": p.RealName,
- "账号类型": userType,
- "有效开始时间": effectiveStart,
- "有效结束时间": effectiveEnd,
- }
- bytes, _ := json.Marshal(m)
- return string(bytes)
- }
- //
- func UserAdd(ctx context.Context, req *apis.UserAddRequest) (reply *apis.UserAddReply, err error) {
- reply = &apis.UserAddReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- if err = checkUserAddParam(req); err != nil {
- return nil, err
- }
- if err = checkAdmin(req.SelfId); err != nil {
- return nil, err
- }
- now := time.Now()
- passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
- p := &dbmodel.TUser{
- User: req.User,
- Password: passwd,
- EffectiveStart: req.EffectiveStart,
- EffectiveEnd: req.EffectiveEnd,
- UserType: req.UserType,
- CreatedAt: now,
- UpdatedAt: now,
- RealName: req.RealName,
- }
- db := database.DB().Begin()
- err = p.Insert(db)
- defer func() {
- if err != nil {
- db.Rollback()
- }
- }()
- if err != nil {
- if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
- return nil, status.Error(10003, "账号名已存在,请使用其他账号名 或 删除同名账号")
- }
- return nil, errors.DataBaseError
- }
- reply.Uid = p.ID
- // 操作日志
- content := getUserContent(*p)
- logReq := &apis.OperationLogAddRequest{
- OriginContent: "",
- TargetContent: content,
- Uid: req.SelfId,
- User: req.SelfName,
- Module: consts.OperationModuleAccount,
- OperationTime: now.Unix(),
- Operation: consts.OperationActionUserAdd,
- }
- err = operation_log.OperationLogAddHandle(logReq, db)
- if err != nil {
- return nil, err
- }
- db.Commit()
- return reply, nil
- }
|