package cp_r import ( "encoding/json" "git.hilo.cn/hilo-common/_const/enum/diamond_e" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/myerr/comerr" "git.hilo.cn/hilo-common/rpc" "git.hilo.cn/hilo-common/sdk/tencentyun" "git.hilo.cn/hilo-common/txop/msg" "github.com/gin-gonic/gin" "hilo-user/_const/enum/cp_e" "hilo-user/cv/cp_cv" "hilo-user/domain/model/cp_m" "hilo-user/domain/model/diamond_m" "hilo-user/domain/model/user_m" "hilo-user/req" "hilo-user/resp" ) // @Tags cp关系 // @Summary 检查用户是否绑定了cp // @Param code query int true "用户code" // @Success 200 {object} cp_cv.CheckCpRelationRes // @Router /v2/cp/relation/check [get] func CheckUserCpRelation(c *gin.Context) (*mycontext.MyContext, error) { myCtx := mycontext.CreateMyContext(c.Keys) userCode := c.Query("code") _, lang, err := req.GetUserIdLang(c, myCtx) if err != nil { return myCtx, err } model := domain.CreateModelContext(myCtx) user, err := user_m.GetUserByCode(model, userCode) if err != nil { return myCtx, err } cp, err := cp_m.GetCp(model, user.ID) if err != nil { return myCtx, err } if cp.Id > 0 { return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter) } resp.ResponseOk(c, cp_cv.CheckCpRelationRes{}) return myCtx, nil } // @Tags cp关系 // @Summary 发送cp邀请 // @Param code formData int true "用户code" // @Success 200 // @Router /v2/cp/relation/invite [post] func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) { myCtx := mycontext.CreateMyContext(c.Keys) userCode := c.Query("code") myUserId, lang, err := req.GetUserIdLang(c, myCtx) if err != nil { return myCtx, err } model := domain.CreateModelContext(myCtx) user, err := user_m.GetUser(model, myUserId) if err != nil { return myCtx, err } userInvite, err := user_m.GetUserByCode(model, userCode) if err != nil { return myCtx, err } // 自己是否有cp了 myCp, err := cp_m.GetCp(model, myUserId) if err != nil { return myCtx, err } if myCp.Id > 0 { return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter) } // 对方是否已经有cp了 inviCp, err := cp_m.GetCp(model, userInvite.ID) if err != nil { return myCtx, err } if inviCp.Id > 0 { return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter) } err = model.Transaction(func(model *domain.Model) error { // 扣费 err = diamond_m.ChangeDiamondAccountDetail(model, diamond_e.CpInvite, userInvite.ID, myUserId, cp_e.CpRelationInviteDiamond) if err != nil { model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err) return err } // 发送私信 type CpInviteMessage struct { Identifier string `json:"identifier"` Msg string `json:"msg"` } data, _ := json.Marshal(CpInviteMessage{ Identifier: "CpInviteMessage", Msg: "Do you want to be CP with me?", }) if err := tencentyun.BatchSendCustomMsg(model, 1, user.ExternalId, []string{userInvite.ExternalId}, string(data), "cp邀请"); err != nil { model.Log.Errorf("BatchSendCustomMsg fail:%v", err) return err } return nil }) if err != nil { model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err) return myCtx, err } // socket 推送弹窗 go rpc.SendCpInviteNotice(userInvite.ID, user.Code, user.Nick, user.Avatar, "Do you want to be CP with me?") resp.ResponseOk(c, cp_cv.CheckCpRelationRes{}) return myCtx, nil }