pg_im_service.go 3.8 KB
package domainService

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/im"
)

// PgImService 网易云信IM服务
type PgImService struct {
	transactionContext *pgTransaction.TransactionContext
}

func (s *PgImService) InitOrUpdateUserIMInfo(userId int64, name string) (imInfo *domain.Im, err error) {
	var (
		//ImInfoRepository, _                          = factory.CreateImInfoRepository(ctx)
		checkImRequest  *im.CheckImRequest = &im.CheckImRequest{}
		IsCreated                          = false
		checkImResponse *im.CheckImResponse
	)
	var errFind error
	//imInfo, errFind = ImInfoRepository.FindOne(map[string]interface{}{"user_id": userId})
	// 异常
	//if errFind != nil && errFind != domain.QueryNoRow {
	//	err = errFind
	//	return
	//}
	// 不存在
	//if errFind == domain.QueryNoRow {
	//	imInfo = &domain.Im{
	//		UserId:     userId,
	//		CreateTime: time.Now(),
	//	}
	//}
	// 已存在
	if errFind == nil && imInfo != nil {
		IsCreated = true
	}

	if len(imInfo.Accid) == 0 {
		//id, _ := utils.NewSnowflakeId()
		//imInfo.ImId = fmt.Sprintf("%v", id)
	}
	checkImRequest = &im.CheckImRequest{
		UserId:       userId,
		ImId:         imInfo.Accid,
		Uname:        name,
		CustomerImId: fmt.Sprintf("%v", imInfo.CsAccountId),
		IsCreated:    IsCreated,
	}
	if checkImResponse, err = CheckIm(checkImRequest); err != nil {
		return
	}
	if len(imInfo.CsAccountId) == 0 {
		imInfo.CsAccountId = getRandomCustomerAccount(userId)
	}
	imInfo.ImToken = checkImResponse.ImToken
	//if _, err = ImInfoRepository.Save(imInfo); err != nil {
	//	return
	//}
	return
}

// 检查ImToken
func CheckIm(request *im.CheckImRequest) (rsp *im.CheckImResponse, err error) {
	var ()
	rsp = &im.CheckImResponse{}
	if !request.IsCreated {
		if err = imCreate(request, rsp); err != nil {
			return
		}
	} else {
		if err = imUpdate(request, rsp); err != nil {
			return
		}
	}
	if err = imRefreshToken(request, rsp); err != nil {
		return
	}
	return
}

//create
func imCreate(request *im.CheckImRequest, rsp *im.CheckImResponse) (err error) {
	var (
		param im.UserCreate = im.UserCreate{
			Accid: request.ImId,
			Name:  request.Uname,
			Icon:  request.Icon,
		}
		out *im.UserTokenResult
	)
	if out, err = im.CallCreate(param); err != nil {
		return
	}
	if out.Code != 200 || (out.Info.Accid != request.ImId) {
		return im.ErrorFailCall
	}
	rsp.ImToken = out.Info.Token
	return
}

//update user info
func imUpdate(request *im.CheckImRequest, rsp *im.CheckImResponse) (err error) {
	var (
		param im.UserUpdate = im.UserUpdate{
			Accid: request.ImId,
			Name:  request.Uname,
			Icon:  request.Icon,
		}
		out *im.BaseResp
	)
	if out, err = im.CallUpdate(param); err != nil {
		return
	}
	if out.Code != 200 {
		return im.ErrorFailCall
	}
	return
}

//refresh token
func imRefreshToken(request *im.CheckImRequest, rsp *im.CheckImResponse) (err error) {
	var (
		param im.UserRefreshToken = im.UserRefreshToken{
			Accid: request.ImId,
		}
		out *im.UserTokenResult
	)
	if out, err = im.CallRefreshToken(param); err != nil {
		return
	}
	if out.Code != 200 || (out.Info.Accid != request.ImId) {
		return im.ErrorFailCall
	}
	rsp.ImToken = out.Info.Token
	return
}

// 获取客服id
func getRandomCustomerAccount(userId int64) (acid string) {
	//ImCustomerServiceRepository, _ := factory.CreateImCustomerServiceRepository(ctx)
	//total, customers, err := ImCustomerServiceRepository.Find(map[string]interface{}{"sortById": domain.ASC})
	//if err != nil {
	//	log.Error(err)
	//	return 0
	//}
	//if total == 0 {
	//	return 0
	//}
	//index := userId % total
	//if int(index) < len(customers) {
	//	acid, _ = strconv.ParseInt(customers[index].ImId, 10, 64)
	//	return
	//}
	//acid, _ = strconv.ParseInt(customers[0].ImId, 10, 64)
	return
}