accountaction.go 1.22 KB
package actions

import (
	"context"
	"fmt"
	"pro2d/components/jwt"
	"pro2d/conf"
	"pro2d/models"
	"pro2d/protos/pb"
	"pro2d/utils"
)

func (s *LoginServer) RegisterHandler(ctx context.Context, in *pb.Register) (*pb.RegisterRsp, error) {
	ok, account := models.AccountExistByPhone(in.Phone)
	if !ok {
		account.Phone = in.Phone
		account.Password = utils.Md5V(in.Password)
		account.Uid = conf.SnowFlack.NextValStr()
		account.Create()
	}else {
		return nil, fmt.Errorf("1")
	}

	return &pb.RegisterRsp{
		Code: 0,
	}, nil
}

func (s *LoginServer) CreateTokenHandler(ctx context.Context, in *pb.Account) (*pb.CreateTokenRsp, error) {
	m := models.NewAccount(in.Phone)
	if err := m.Load(); err != nil {
		return &pb.CreateTokenRsp{
			Code: 1,
		}, nil
	}

	if m.Password != utils.Md5V(in.Password) {
		return &pb.CreateTokenRsp{
			Code: 2,
		}, nil
	}

	serverInfo := s.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name)
	var gameInfo []*pb.ServiceInfo
	for k, v := range serverInfo {
		gameInfo = append(gameInfo, &pb.ServiceInfo{
			Id:      k,
			Name:    conf.GlobalConf.GameConf.Name,
			Address: v,
		})
	}

	return &pb.CreateTokenRsp{
		Code: 0,
		Uid: m.Uid,
		Token: jwt.CreateToken(m.Account.Uid),
		GameService: gameInfo,
	}, nil
}