group_mic.go 28.8 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
package group_r

import (
	"encoding/json"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/utils"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"hilo-group/_const/enum/diamond_e"
	"hilo-group/_const/enum/group_e"
	"hilo-group/_const/enum/task_e"
	"hilo-group/cv/diamond_cv"
	"hilo-group/cv/group_cv"
	"hilo-group/cv/mic_cv"
	"hilo-group/domain/cache/mic_c"
	"hilo-group/domain/model/diamond_m"
	"hilo-group/domain/model/group_m"
	"hilo-group/domain/model/res_m"
	"hilo-group/domain/model/task_m"
	"hilo-group/domain/service/group_mic_s"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"hilo-group/req"
	"hilo-group/resp"
	"strconv"
	"time"
)

// @Tags 群组
// @Summary 麦位所有信息(支持高频获取,前端可以高频获取,进行校正), v2.5.0_2_211025 版本之前,只能获取5个麦位,兼容于旧版本数据
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid query string true "groupUuid"
// @Success 200 {object} group_cv.CvMic
// @Router /v1/imGroup/mic/all [get]
func GroupMicAllInfoFive(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.Query("groupUuid")

	userId, extendId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	group_m.UpdateMicExpire(model, groupUuid, extendId)
	//加入房间 || 更新房间内有人信息
	//group_m.RoomLivingIn(model, groupUuid, userId)
	//降低频率
	group_m.RoomLivingExpire(model, groupUuid, userId)

	mics, micUsers, err := group_m.GetAllMic(groupUuid, group_e.FiveMicNumType)
	if err != nil {
		return myContext, err
	}
	cvMics, err := group_cv.GetGroupMicAll(mics, micUsers)
	if err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, cvMics)
	return myContext, nil
}

// @Tags 群组
// @Summary 麦位所有信息(支持高频获取,前端可以高频获取,进行校正)(v2.5.0_2_211025 版本之后,根据类型返回5或者10个麦位信息,数量决定了类型)
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid query string true "groupUuid"
// @Success 200 {object} group_cv.CvMic
// @Router /v1/imGroup/mic/all/type [get]
func GroupMicAllInfoTen(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.Query("groupUuid")

	userId, extendId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)

	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	group_m.UpdateMicExpire(model, groupUuid, extendId)

	//加入房间 || 更新房间内有人信息
	//group_m.RoomLivingIn(model, groupUuid, userId)
	//降低频率
	group_m.RoomLivingExpire(model, groupUuid, userId)

	micNumType, err := group_m.GetMicNumType(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	//兼容于旧版本
	n := group_m.GetMicNum(micNumType)
	if n >= 10 {
		micNumType = group_e.TenMicNumType
	} else {
		micNumType = group_e.FiveMicNumType
	}

	mics := []group_m.Mic{}
	micUsers := []group_m.MicUser{}
	mics, micUsers, err = group_m.GetAllMic(groupUuid, micNumType)
	if err != nil {
		return myContext, err
	}
	cvMics, err := group_cv.GetGroupMicAll(mics, micUsers)
	if err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, cvMics)
	return myContext, nil
}

// @Tags 群组
// @Summary 麦位所有信息(支持高频获取,前端可以高频获取,进行校正)(v2.5.0_2_211025 版本之后,根据类型返回5或者10个麦位信息,数量决定了类型)
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid query string true "groupUuid"
// @Success 200 {object} group_cv.CvMic
// @Router /v1/imGroup/mic/all/type/new [get]
func GroupMicAllInfoType(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.Query("groupUuid")

	userId, extendId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	group_m.UpdateMicExpire(model, groupUuid, extendId)

	//加入房间 || 更新房间内有人信息
	//group_m.RoomLivingIn(model, groupUuid, userId)
	//降低频率
	group_m.RoomLivingExpire(model, groupUuid, userId)

	micNumType, err := group_m.GetMicNumType(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	var mics []group_m.Mic
	var micUsers []group_m.MicUser

	mics, micUsers, err = group_m.GetAllMic(groupUuid, micNumType)
	if err != nil {
		return myContext, err
	}
	cvMics, err := group_cv.GetGroupMicAll(mics, micUsers)
	if err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, cvMics)
	return myContext, nil
}

// @Tags 群组
// @Summary 修改麦位数量
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId formData string true "群ID"
// @Param micNumType formData int true "麦位数量类型 1:5个麦位 2:10个麦位, 当:micOn=fales, micNumType值不采纳,传的是0"  //当:micOn=fales, micNumType值不采纳:这是前端要求的
// @Param micOn formData bool true "是否打开麦位"
// @Success 200
// @Router /v1/imGroup/mic/num [put]
func GroupMicNumChange(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, nil
	}
	groupId := c.PostForm("groupId")
	if groupId == "" {
		return myContext, myerr.NewSysError("参数micNumType 不能为空")
	}

	model := domain.CreateModelContext(myContext)
	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	micNumType, err := strconv.ParseUint(c.PostForm("micNumType"), 10, 8)
	if err != nil {
		return myContext, myerr.NewSysError("参数groupId 不能为空")
	}

	micOn, err := strconv.ParseBool(c.PostForm("micOn"))
	if err != nil {
		return myContext, myerr.NewSysError("参数micOn 有误")
	}

	if err := group_mic_s.NewGroupPowerService(myContext).GroupMicNumChange(groupId, userId, group_e.GroupMicNumType(micNumType), micOn); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 查询麦位数量。返回的是枚举。
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId query string true "群ID"
// @Success 200
// @Router /v1/imGroup/mic/num [get]
func GroupMicNum(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	txGroupId := c.Query("groupId")

	groupInfo, err := group_m.GetInfoByTxGroupId(domain.CreateModelContext(myContext), txGroupId)
	if err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, groupInfo.MicNumType)
	return myContext, nil
}

// @Tags 群组
// @Summary 麦上表情信令
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId formData string true "群ID"
// @Param micEmojiId formData int true "麦上表情ID"
// @Success 200
// @Router /v1/imGroup/mic/emoji/msg [post]
func GroupSendMicSystemMsg(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	_, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, nil
	}

	txGroupId := c.PostForm("groupId")
	if txGroupId == "" {
		return myContext, myerr.NewSysError("参数groupId 不能为空")
	}

	micEmojiId, err := strconv.ParseUint(c.PostForm("micEmojiId"), 10, 64)
	if err != nil {
		return myContext, myerr.WrapErr(err)
	}
	//
	resMicEmoji := res_m.ResMicEmoji{}
	if err := mysql.Db.Model(&res_m.ResMicEmoji{}).First(&resMicEmoji, micEmojiId).Error; err != nil {
		return myContext, myerr.WrapErr(err)
	}
	if resMicEmoji.Status == mysql.NOUSER {
		return myContext, myerr.NewSysError("表情已下架")
	}
	buf, err := json.Marshal(mic_cv.CvMicEmoji{
		Id:      resMicEmoji.ID,
		Name:    resMicEmoji.Name,
		IconUrl: resMicEmoji.IconUrl,
		SvagUrl: resMicEmoji.SvagUrl,
	})
	if err != nil {
		return myContext, myerr.WrapErr(err)
	}
	//
	model := domain.CreateModelContext(myContext)

	if err := group_m.MicRPush(model, txGroupId, group_m.GroupSystemMsg{
		MsgId:   group_e.GroupMicEmoji,
		Source:  externalId,
		Content: string(buf),
	}); err != nil {
		return myContext, err
	}

	resp.ResponseOk(c, nil)
	return myContext, nil
}

type ReturnFlag struct {
	Flag bool `json:"flag"`
	//1:邀请上麦 2:未完成任务上麦任务
	Type uint `json:"type"`
	//钻石数量(2:未完成任务上麦任务才有值)
	DiamondNum uint32 `json:"diamondNum"`
}

// @Tags 群组
// @Summary 邀请上麦校验弹窗
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId formData string true "群ID"
// @Success 200 {object} ReturnFlag
// @Router /v1/imGroup/mic/in/invite/dialog [post]
func GroupMicInInviteDialog(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	_, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, nil
	}

	groupId := c.PostForm("groupId")
	if len(groupId) <= 0 {
		return myContext, bizerr.InvalidParameter
	}

	model := domain.CreateModelContext(myContext)
	groupInfo, err := group_m.GetInfoByTxGroupId(model, groupId)
	if err != nil {
		return nil, err
	}
	groupId = groupInfo.ImGroupId

	mics, micUsers, err := group_m.GetAllMic(groupId, groupInfo.MicNumType)
	if err != nil {
		return myContext, err
	}
	cvMics, err := group_cv.GetGroupMicAll(mics, micUsers)
	//用户没有上麦
	micInFlag := false
	//群组麦上有人
	micHasUserFlag := false
	//有空余麦位
	micSpareFlag := false

	for _, r := range cvMics {
		//用户在麦上
		if r.ExternalId != nil && *(r.ExternalId) == externalId {
			micInFlag = true
		}
		//麦上有人
		if r.ExternalId != nil && *(r.ExternalId) != externalId {
			micHasUserFlag = true
		}
		if r.ExternalId == nil && r.Lock == false {
			micSpareFlag = true
		}
	}

	if micInFlag == false && micHasUserFlag == true && micSpareFlag == true {
		resp.ResponseOk(c, ReturnFlag{Flag: true})
		return myContext, nil
	} else {
		resp.ResponseOk(c, ReturnFlag{
			Flag:       false,
			Type:       1,
			DiamondNum: 0,
		})
		return myContext, nil
	}
}

// @Tags 群组
// @Summary 邀请上麦钻石校验弹窗
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId formData string true "群ID"
// @Success 200 {object} ReturnFlag
// @Router /v1/imGroup/mic/task/invite/dialog [post]
func GroupMicTaskInviteDialog(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, nil
	}

	groupId := c.PostForm("groupId")
	if groupId == "" {
		return myContext, myerr.NewSysError("参数groupId 不能为空")
	}

	model := domain.CreateModelContext(myContext)

	groupInfo, err := group_m.GetInfoByTxGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}
	if groupInfo == nil {
		return myContext, bizerr.GroupNotFound
	}
	groupId = groupInfo.ImGroupId

	mics, micUsers, err := group_m.GetAllMic(groupId, groupInfo.MicNumType)
	if err != nil {
		return myContext, err
	}
	cvMics, err := group_cv.GetGroupMicAll(mics, micUsers)
	//用户没有上麦
	micInFlag := false
	//群组麦上有人
	micHasUserFlag := false
	//有空余麦位
	micSpareFlag := false

	for _, r := range cvMics {
		//用户在麦上
		if r.ExternalId != nil && *(r.ExternalId) == externalId {
			micInFlag = true
		}
		//麦上有人
		if r.ExternalId != nil && *(r.ExternalId) != externalId {
			micHasUserFlag = true
		}
		if r.ExternalId == nil && r.Lock == false {
			micSpareFlag = true
		}
	}

	if micInFlag == false && micHasUserFlag == true && micSpareFlag == true {
		// 今天展示过了
		if show, _ := mic_c.IsMicDayInviteDialogShowToday(model, userId); show {
			resp.ResponseOk(c, ReturnFlag{
				Flag: false,
			})
			return myContext, nil
		}

		//产品说:同是否能领取了每日钻石关联起来
		beginTime := utils.GetZeroTime(time.Now())
		endTime := beginTime.AddDate(0, 0, 1)

		var n int64
		if err := mysql.Db.Model(&diamond_m.DiamondAccountDetail{}).Where(&diamond_m.DiamondAccountDetail{
			UserId:    userId,
			AddReduce: mysql.ADD,
		}).Where("Created_Time <= ? and Created_Time >= ?", endTime, beginTime).Where("(Operate_Type = ? or Operate_Type = ?)", diamond_e.DailyInAppVip, diamond_e.DailyInAppCommon).Count(&n).Error; err != nil {
			if err != nil {
				return myContext, nil
			}
		}
		if n > 0 {
			//判断是否完成上麦任务
			taskConfig := task_m.TaskConfig{}
			if err := mysql.Db.Model(&task_m.TaskConfig{}).Where(&task_m.TaskConfig{
				Type: task_e.MicIn,
			}).First(&taskConfig).Error; err != nil {
				if err == gorm.ErrRecordNotFound {
				} else {
					return myContext, myerr.WrapErr(err)
				}
			}
			//任务还存在
			if taskConfig.ID != 0 {
				var n int64
				if err := mysql.Db.Model(&task_m.TaskUser{}).Where(&task_m.TaskUser{
					TaskConfigId: taskConfig.ID,
					UserId:       userId,
					DayStr:       time.Now().Format(utils.COMPACT_DATE_FORMAT),
					HasFinish:    mysql.YES,
				}).Count(&n).Error; err != nil {
					return myContext, myerr.WrapErr(err)
				}
				//存在还没完成
				if n == 0 {
					resp.ResponseOk(c, ReturnFlag{
						Flag:       true,
						Type:       2,
						DiamondNum: taskConfig.Diamond,
					})
					return myContext, nil
				}
			}
		}
		resp.ResponseOk(c, ReturnFlag{
			Flag: true,
			Type: 1,
		})
		return myContext, nil
	} else {
		resp.ResponseOk(c, ReturnFlag{
			Flag:       false,
			Type:       1,
			DiamondNum: 0,
		})
		return myContext, nil
	}
}

// @Tags 群组
// @Summary 上麦
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid formData string true "groupUuid"
// @Param i formData string false "麦序, 空字符串,代表有麦位就上"
// @Success 200
// @Router /v1/imGroup/mic/in [post]
func GroupMicIn(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.PostForm("groupUuid")
	nonce := c.GetHeader("nonce")
	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupInfo, err := group_m.GetInfoByTxGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}
	if groupInfo == nil {
		return myContext, bizerr.GroupNotFound
	}
	groupUuid = groupInfo.ImGroupId

	// 游客,且设置了游客不能上麦,则失败
	// nonce = hilo,服务端内部调用,机器人
	if groupInfo.TouristMic != 1 && nonce != "hilo" {
		role, err := GetGroupRoleById(model, groupInfo.ImGroupId, userId)
		if err != nil {
			return myContext, err
		}
		if role == group_e.GROUP_VISITOR {
			// 旧版本(2.32.0以下),提示升级
			_, major, minor, _, err := req.GetAppVersion(c)
			if err != nil {
				return myContext, err
			}
			if major <= 2 && minor < 32 {
				return myContext, bizerr.UpgradeRequired
			}
			return myContext, bizerr.GroupMicBanTourist
		}
	}
	iStr := c.PostForm("i")
	micIndex := -1
	// 用户已经在麦上,必须先下麦!
	micUser, err := group_m.GetMicUserByExternalId(model, externalId)
	if err != nil {
		return myContext, err
	}
	if micUser != nil {
		if iStr == "" {
			// 非切麦操作
			return myContext, bizerr.GroupMicUserHasIn
		}
		// 切换麦位,先下麦
		if err := group_mic_s.NewGroupPowerService(myContext).GroupMicLeave(groupUuid, micUser.I, userId, externalId); err != nil {
			return myContext, err
		}
	}

	if iStr == "" {
		//判断群组设置上的麦 是否被关闭
		if groupInfo.MicOn == false {
			return myContext, bizerr.GroupInfoMicClosed
		}
		//micNum := 5
		//if groupInfo.MicNumType == group_enum.TenMicNumType {
		//	micNum = 10
		//}
		micNum := group_m.GetMicNum(groupInfo.MicNumType)
		for i := 1; i <= micNum; i++ {
			if err := group_mic_s.NewGroupPowerService(myContext).GroupMicIn(groupUuid, i, userId, externalId); err != nil {
				if i == micNum {
					//最后一次了,依旧错误,则
					return myContext, bizerr.GroupMicInByInviteFail
				}
			} else {
				//成功则跳出
				micIndex = i
				break
			}
		}
	} else {
		i, err := strconv.Atoi(iStr)
		if err != nil {
			return myContext, err
		}
		if err := group_mic_s.NewGroupPowerService(myContext).GroupMicIn(groupUuid, i, userId, externalId); err != nil {
			return myContext, err
		}
		micIndex = i
	}
	data := map[string]interface{}{
		"micIndex": micIndex,
	}
	if nonce == "hilo" {
		resp.ResponseOk(c, data)
	} else {
		resp.ResponseOk(c, nil)
	}
hujiebin's avatar
hujiebin committed
603 604
	// check cp 麦位置
	go group_mic_s.NewGroupPowerService(myContext).CheckCpOnMic(groupUuid)
hujiebin's avatar
hujiebin committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	return myContext, nil
}

func GetGroupRoleById(model *domain.Model, imGroupId string, userId uint64) (role group_e.GroupRoleType, err error) {
	role = group_e.GROUP_VISITOR
	roles, _, err := group_m.GetRolesInGroup(model, imGroupId)
	if err != nil {
		return
	}
	for u, r := range roles {
		if u == userId {
			role = r
			return
		}
	}

	isGroupMember, err := group_m.IsGroupMember(model.Db, imGroupId, userId)
	if err != nil {
		return
	}
	if isGroupMember {
		role = group_e.GROUP_MEMBER
	}
	return
}

// @Tags 群组
// @Summary 邀请上麦
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid formData string true "groupUuid"
// @Param beInvitedExternalId formData string false "被邀请人的ExternalId"
// @Success 200
// @Router /v1/imGroup/mic/invite [post]
func GroupMicInvite(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.PostForm("groupUuid")
	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	beInvitedExternalId := c.PostForm("beInvitedExternalId")
	if err := group_mic_s.NewGroupPowerService(myContext).GroupMicInvite(groupUuid, userId, beInvitedExternalId); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 自己离开麦位, 管理人让他下麦
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid formData string true "groupUuid"
// @Param i formData string true "麦序"
// @Success 200
// @Router /v1/imGroup/mic/leave [post]
func GroupMicLeave(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupId := c.PostForm("groupUuid")
	i, err := strconv.Atoi(c.PostForm("i"))
	if err != nil {
		return myContext, err
	}
	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	_, lang, err := req.GetUserIdLang(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	micUser, err := group_m.GetMicUser(model, groupId, i)
	if err != nil {
		return myContext, err
	}
	if micUser != nil && micUser.UserId != userId { // 抱下麦
		// 检查权限
		if err = CheckOptToSvip6(model, userId, micUser.UserId, lang, 11); err != nil {
			return myContext, err
		}
	}

	if err := group_mic_s.NewGroupPowerService(myContext).GroupMicLeave(groupId, i, userId, externalId); err != nil {
		return myContext, err
	}
hujiebin's avatar
hujiebin committed
708 709
	// check cp麦位置
	go group_mic_s.NewGroupPowerService(myContext).CheckCpLeaveMic(groupId, userId)
hujiebin's avatar
hujiebin committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 麦位加锁
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid formData string true "groupUuid"
// @Param i formData string true "麦序"
// @Success 200
// @Router /v1/imGroup/mic/lock [post]
func GroupMicLock(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.PostForm("groupUuid")
	i, err := strconv.Atoi(c.PostForm("i"))
	if err != nil {
		return myContext, err
	}
	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	if err := group_mic_s.NewGroupPowerService(myContext).GroupMicLock(userId, externalId, groupUuid, i); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 麦位解锁
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid formData string true "groupUuid"
// @Param i formData string true "麦序"
// @Success 200
// @Router /v1/imGroup/mic/unlock [post]
func GroupMicUnLock(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.PostForm("groupUuid")
	i, err := strconv.Atoi(c.PostForm("i"))
	if err != nil {
		return myContext, err
	}
	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	if err := group_mic_s.NewGroupPowerService(myContext).GroupMicUnLock(userId, externalId, groupUuid, i); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 开麦(可以说话)
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid formData string true "groupUuid"
// @Param i formData string true "麦序"
// @Success 200
// @Router /v1/imGroup/mic/speech/open [post]
func GroupMicSpeechOpen(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.PostForm("groupUuid")
	i, err := strconv.Atoi(c.PostForm("i"))
	if err != nil {
		return myContext, err
	}

	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	if err := group_mic_s.NewGroupPowerService(myContext).GroupMicSpeechOpen(userId, externalId, groupUuid, i); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 关麦(不可以说话),管理人禁言这个麦位
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid formData string true "groupUuid"
// @Param i formData string true "麦序"
// @Success 200
// @Router /v1/imGroup/mic/speech/close [post]
func GroupMicSpeechClose(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.PostForm("groupUuid")
	i, err := strconv.Atoi(c.PostForm("i"))
	if err != nil {
		return myContext, err
	}

	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

chenweijian's avatar
chenweijian committed
839 840 841 842 843
	_, lang, err := req.GetUserIdLang(c, myContext)
	if err != nil {
		return myContext, err
	}

hujiebin's avatar
hujiebin committed
844 845 846 847 848 849
	model := domain.CreateModelContext(myContext)
	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

chenweijian's avatar
chenweijian committed
850
	if err := group_mic_s.NewGroupPowerService(myContext).GroupMicSpeechClose(userId, externalId, groupUuid, lang, i); err != nil {
hujiebin's avatar
hujiebin committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 麦位解除静音(打开座位)
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid formData string true "groupUuid"
// @Param i formData string true "麦序"
// @Success 200
// @Router /v1/imGroup/mic/unmute [post]
func GroupMicUnmute(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.PostForm("groupUuid")
	i, err := strconv.Atoi(c.PostForm("i"))
	if err != nil {
		return myContext, err
	}
	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	if err := group_mic_s.NewGroupPowerService(myContext).GroupMicUnMute(userId, externalId, groupUuid, i); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 麦位静音(关闭座位)
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupUuid formData string true "groupUuid"
// @Param i formData string true "麦序"
// @Success 200
// @Router /v1/imGroup/mic/mute [post]
func GroupMicMute(c *gin.Context) (*mycontext.MyContext, error) {
	// todo
	myContext := mycontext.CreateMyContext(c.Keys)
	groupUuid := c.PostForm("groupUuid")
	i, err := strconv.Atoi(c.PostForm("i"))
	if err != nil {
		return myContext, err
	}
	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupUuid, err = group_m.ToImGroupId(model, groupUuid)
	if err != nil {
		return myContext, err
	}

	if err := group_mic_s.NewGroupPowerService(myContext).GroupMicMute(userId, externalId, groupUuid, i); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 麦上用户群发成员广播
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId formData string true "群ID"
// @Param content formData string true "内容"
hujiebin's avatar
hujiebin committed
933
// @Success 200 {object} diamond_cv.CvDiamond
hujiebin's avatar
hujiebin committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
// @Router /v1/imGroup/mic/mass [post]
func GroupMicMass(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}
	groupId := c.PostForm("groupId")
	if len(groupId) <= 0 {
		return myContext, bizerr.InvalidParameter
	}

	model := domain.CreateModelContext(myContext)
	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	content := c.PostForm("content")

	if err := group_mic_s.NewGroupPowerService(myContext).GroupIMMassByInMic(groupId, userId, externalId, content); err != nil {
		return myContext, err
	}
	diamond, err := diamond_cv.GetDiamond(userId)
	if err != nil {
		return nil, myerr.WrapErr(err)
	}
	resp.ResponseOk(c, diamond)
	return myContext, nil
}

// @Tags 群组
// @Summary 用户群发成员广播
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId formData string true "群ID"
// @Param content formData string true "内容"
hujiebin's avatar
hujiebin committed
972
// @Success 200 {object} diamond_cv.CvDiamond
hujiebin's avatar
hujiebin committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
// @Router /v1/imGroup/mgr/mass [post]
func GroupMgrMass(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}
	groupId := c.PostForm("groupId")
	if len(groupId) <= 0 {
		return myContext, bizerr.InvalidParameter
	}

	model := domain.CreateModelContext(myContext)
	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	content := c.PostForm("content")

	if err := group_mic_s.NewGroupPowerService(myContext).GroupIMMassByMgr(groupId, userId, externalId, content); err != nil {
		return myContext, err
	}
	diamond, err := diamond_cv.GetDiamond(userId)
	if err != nil {
		return nil, myerr.WrapErr(err)
	}
	resp.ResponseOk(c, diamond)
	return myContext, nil
}