pg_im_service.go 4.1 KB
package domainService

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

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

func (ptr *PgImService) InitOrUpdateUserIMInfo(userId int64, flag int) (*domain.Im, error) {
	var (
		checkImRequest  *im.CheckImRequest = &im.CheckImRequest{}
		IsCreated                          = false
		checkImResponse *im.CheckImResponse
	)
	userBaseRepository, _ := repository.NewUserBaseRepository(ptr.transactionContext)
	userBase, err := userBaseRepository.FindOne(map[string]interface{}{"account": fmt.Sprintf("%v", userId)})
	if err != nil || userBase == nil || userBase.Status != int(domain.UserStatusEnable) {
		return nil, fmt.Errorf("账号不存在")
	}
	if userBase.Im != nil && len(userBase.Im.Accid) > 0 {
		IsCreated = true
		if flag == domain.RefreshWhenNotExists {
			return userBase.Im, nil
		}
	} else {
		id, err := repository.IdWorker.NextId()
		if err != nil {
			return nil, err
		}
		userBase.Im = &domain.Im{
			Accid: fmt.Sprintf("%v", id),
		}
	}
	checkImRequest = &im.CheckImRequest{
		UserId:       userId,
		ImId:         userBase.Im.Accid,
		Uname:        userBase.UserInfo.UserName,
		CustomerImId: fmt.Sprintf("%v", userBase.Im.CsAccountId),
		IsCreated:    IsCreated,
	}
	if checkImResponse, err = CheckIm(checkImRequest); err != nil {
		return nil, err
	}
	if len(userBase.Im.CsAccountId) == 0 {
		userBase.Im.CsAccountId = fmt.Sprintf("%v", getRandomCustomerAccount(userId))
	}
	userBase.Im.ImToken = checkImResponse.ImToken
	if userBase, err = userBaseRepository.Save(userBase); err != nil {

	}
	return userBase.Im, nil
}

// 检查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) int64 {
	total, customers := len(constant.CUSTOMER_ACCOUNT), constant.CUSTOMER_ACCOUNT
	if total == 0 {
		return 0
	}
	index := (int(userId) & 0xFF) % int(total)
	if int(index) < len(customers) {
		return customers[index]
	}
	return customers[0]
}

func NewPgImService(transactionContext *pgTransaction.TransactionContext) (*PgImService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &PgImService{
			transactionContext: transactionContext,
		}, nil
	}
}