123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package api_management
- import (
- "adm-management/errors"
- v1 "adm-management/pb/v1"
- "context"
- "crypto/md5"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "time"
- "git.getensh.com/common/gopkgsv2/id"
- "gorm.io/gorm"
- "git.getensh.com/common/gopkgsv2/database"
- "git.getensh.com/common/gopkgsv2/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- func CreateKey(ctx context.Context, req *v1.CreateKeyRequest) (reply *v1.CreateKeyReply, err error) {
- reply = &v1.CreateKeyReply{}
- // 捕获各个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"))
- }
- }
- }()
- createAt := time.Now().Unix()
- db := database.DB()
- err = db.Exec("INSERT INTO t_adm_key_management(`key`, `desc`, created_at, updated_at) VALUES(?,?,?,?)", generateKey(), req.Desc, createAt, createAt).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return reply, nil
- }
- return reply, errors.SystemError
- }
- return reply, nil
- }
- func generateKey() string {
- uuid, _ := id.GetUniqueID()
- ts := time.Now().Unix()
- text := fmt.Sprintf("%s%d", uuid, ts)
- return MD5(text)
- }
- func MD5(text string) string {
- h := md5.New()
- h.Write([]byte(text))
- return hex.EncodeToString(h.Sum(nil))
- }
|