accountaction.go 1.33 KB
package actions

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

func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (*pb.PubRsp, 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.Device = "123123"
		account.Create()
	}else {
		return nil, fmt.Errorf("1")
	}

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

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

	if m.Password != utils.Md5V(in.Password) {
		return &pb.CreateTokenRsp{
			Rsp: &pb.PubRsp{
				Code: 2,
				Msg: "password error",
			},
		}, 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{
		Rsp: &pb.PubRsp{
			Code: 0,
		},
		Token: utils.CreateToken(m.AccountInfo),
		GameService: gameInfo,
	}, nil
}