package auth import ( "context" "gd_admin/apis" "gd_admin/common.in/storage" "gd_admin/common.in/utils" "gd_admin/errors" "gd_admin/thirdparty/oss" "fmt" "github.com/astaxie/beego/orm" "go.uber.org/zap" ) func AuthGetOSSInfo(ctx context.Context, req *apis.AuthGetOSSInfoReq, reply *apis.AuthGetOSSInfoReply) (err error) { // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) } }() // 参数检查 if req.Uid == 0 { l.Error("argument", zap.String("call", "check"), zap.String("fields", utils.MarshalJsonString(req))) return errors.ArgsError } // 检查用户是否合法 checkUserBaseTask := func(db orm.Ormer) error { var admin_name string sql := fmt.Sprintf("select admin_name from t_gd_admin where id = %d ", req.Uid) if err = db.Raw(sql).QueryRow(&admin_name); err != nil { l.Error("mysql", zap.String("sql", sql), zap.String("error", err.Error())) if err == orm.ErrNoRows { return errors.AdminNotExist } else { return errors.DataBaseError } } return nil } // 更新用户基本信息状态 getOSSInfoTask := func(db orm.Ormer) error { if resp, err := oss.AssumeRole(ctx); err != nil { l.Error("thirdparty", zap.String("call", "oss.AssumeRole"), zap.String("params", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.VendorError } else { reply.AccessKeyId = resp.Credentials.AccessKeyId reply.AccessKeySecret = resp.Credentials.AccessKeySecret reply.SecurityToken = resp.Credentials.SecurityToken reply.Expiration = resp.Credentials.Expiration.Unix() } return nil } // 这里使用事务处理 tasks := []storage.DbaTasker{} tasks = append(tasks, storage.GenerateDbaTask(checkUserBaseTask)) tasks = append(tasks, storage.GenerateDbaTask(getOSSInfoTask)) storage.ExecTrans(tasks...) return nil }