479 lines
11 KiB
Go
479 lines
11 KiB
Go
package model
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/globalsign/mgo/bson"
|
|
|
|
"mongo.games.com/game/common"
|
|
)
|
|
|
|
// 登录次数,最后登录最后登出时间并不准确
|
|
|
|
type Account struct {
|
|
AccountId bson.ObjectId `bson:"_id"`
|
|
SnId int32 // 玩家账号直接在这里生成
|
|
UserName string // 账号
|
|
PassWord string // 昵称密码
|
|
TelPassWord string // 帐号密码
|
|
BackPassWord string // 备份账号密码
|
|
Platform string // 平台
|
|
Channel string // 包渠道
|
|
Tel string // 电话号码
|
|
Params string // 其他参数
|
|
DeviceOs string // 系统
|
|
DeviceInfo string // 设备信息
|
|
LoginTimes int // 登录次数
|
|
State int64 // 冻结到期时间戳
|
|
RegisteTime time.Time // 注册时间
|
|
LastLoginTime time.Time // 最后一次登录时间
|
|
LastLogoutTime time.Time // 最后一次登出时间
|
|
Flag int32 // 二次推广用户标记
|
|
Remark string // 备注信息
|
|
AccountType int32 // 1.google 2.facebook
|
|
RegisterTs int64 // 注册时间戳
|
|
TelPassWordExpire int64 // 手机验证码登录过期时间戳
|
|
ChannelId string // 推广渠道
|
|
ClientVer int32 // 客户端版本
|
|
|
|
TagKey int32 //包标识关键字
|
|
PackegeTag string //包标识
|
|
Package string //包名 android:包名 ios:bundleid
|
|
}
|
|
|
|
func NewAccount() *Account {
|
|
account := &Account{AccountId: bson.NewObjectId()}
|
|
return account
|
|
}
|
|
|
|
type AccIsExistArg struct {
|
|
AccId string
|
|
Username string
|
|
Password string
|
|
Tel string
|
|
Platform string
|
|
Ts int64
|
|
LoginType int32
|
|
TagKey int32
|
|
VerifyToken bool
|
|
CodeValid bool
|
|
}
|
|
type AccRet struct {
|
|
Acc *Account
|
|
Tag int
|
|
}
|
|
|
|
func AccountIsExist(userName, tel, passWord, platform string, ts int64, logintype int32, tagkey int32, verifyToken, codeValid bool) (*Account, int) {
|
|
if rpcCli == nil {
|
|
return nil, 0
|
|
}
|
|
|
|
args := &AccIsExistArg{
|
|
Username: userName,
|
|
Password: passWord,
|
|
Tel: tel,
|
|
Platform: platform,
|
|
Ts: ts,
|
|
LoginType: logintype,
|
|
TagKey: tagkey,
|
|
VerifyToken: verifyToken,
|
|
CodeValid: codeValid,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.AccountIsExist", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return nil, 0
|
|
}
|
|
return ret.Acc, ret.Tag
|
|
}
|
|
|
|
type InsertAccountParam struct {
|
|
Platform string // 平台
|
|
UserName string // 用户名
|
|
Password string // 密码,编码后的
|
|
TelPassword string // 账号密码,编码后的
|
|
Channel string // 包渠道
|
|
Params string // 其它参数
|
|
DeviceOs string // 操作系统
|
|
DeviceInfo string // 设备信息
|
|
ChannelId string // 推广渠道
|
|
AccountType int32 // 账号类型
|
|
Tel string // 手机号
|
|
ClientVer int32 // 客户端版本
|
|
}
|
|
|
|
func InsertAccount(param *InsertAccountParam) (*Account, int) {
|
|
if rpcCli == nil {
|
|
return nil, 0
|
|
}
|
|
|
|
acc := NewAccount()
|
|
if acc == nil {
|
|
return nil, 0
|
|
}
|
|
|
|
tCur := time.Now()
|
|
acc.UserName = param.UserName
|
|
acc.PassWord = param.Password
|
|
acc.TelPassWord = param.TelPassword
|
|
acc.Platform = param.Platform
|
|
acc.Channel = param.Channel
|
|
acc.Params = param.Params
|
|
acc.DeviceOs = param.DeviceOs
|
|
acc.DeviceInfo = param.DeviceInfo
|
|
acc.ChannelId = param.ChannelId
|
|
acc.AccountType = param.AccountType
|
|
acc.Tel = param.Tel
|
|
acc.ClientVer = param.ClientVer
|
|
|
|
acc.LastLoginTime = tCur
|
|
acc.RegisteTime = tCur
|
|
acc.LoginTimes = 1
|
|
acc.RegisterTs = tCur.Unix()
|
|
acc.TelPassWordExpire = tCur.AddDate(0, 0, common.TelLoginValidDays).Unix()
|
|
ret := &AccRet{}
|
|
|
|
err := rpcCli.CallWithTimeout("AccountSvc.InsertAccount", acc, ret, time.Second*30)
|
|
if err != nil {
|
|
return nil, 0
|
|
}
|
|
return ret.Acc, ret.Tag
|
|
}
|
|
|
|
type BindTelAccountArgs struct {
|
|
Platform string
|
|
AccId string
|
|
Tel string
|
|
TagKey int32
|
|
}
|
|
|
|
type BindTelAccountRet struct {
|
|
Code int // 0成功 1失败
|
|
Acc *Account
|
|
}
|
|
|
|
func BindTelAccount(accId, tel, platform string) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
|
|
args := &BindTelAccountArgs{
|
|
Platform: platform,
|
|
AccId: accId,
|
|
Tel: tel,
|
|
}
|
|
ret := &BindTelAccountRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.BindTelAccount", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return fmt.Errorf("BindTelAccount err:%v tel:%v accid:%v", err, tel, accId)
|
|
}
|
|
if ret.Code != 0 {
|
|
return fmt.Errorf("BindTelAccount err:%v tel:%v accid:%v", err, tel, accId)
|
|
}
|
|
if ret.Acc != nil && ret.Acc.Tel != "" {
|
|
return errors.New("Tel is Bind")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func LogoutAccount(acc *Account) error {
|
|
if acc == nil {
|
|
return nil
|
|
}
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.LogoutAccount", acc, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type AccFreezeArg struct {
|
|
AccId string
|
|
Platform string
|
|
FreezeTime int
|
|
}
|
|
|
|
func FreezeAccount(plt, accId string, freezeTime int) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
args := &AccFreezeArg{
|
|
AccId: accId,
|
|
Platform: plt,
|
|
FreezeTime: freezeTime,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.FreezeAccount", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UnfreezeAccount(plt, accId string) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
args := &AccFreezeArg{
|
|
AccId: accId,
|
|
Platform: plt,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.UnfreezeAccount", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type AccIdArg struct {
|
|
AccId string
|
|
Platform string
|
|
}
|
|
|
|
type UserNameArg struct {
|
|
UserName string
|
|
Platform string
|
|
}
|
|
|
|
func GetAccount(plt, accId string) (*Account, error) {
|
|
if rpcCli == nil {
|
|
return nil, ErrRPClientNoConn
|
|
}
|
|
args := &AccIdArg{
|
|
AccId: accId,
|
|
Platform: plt,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.GetAccount", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ret.Acc, err
|
|
}
|
|
|
|
func GetAccountByName(plt, username string) (*Account, error) {
|
|
if rpcCli == nil {
|
|
return nil, ErrRPClientNoConn
|
|
}
|
|
args := &UserNameArg{
|
|
UserName: username,
|
|
Platform: plt,
|
|
}
|
|
var ret AccRet
|
|
err := rpcCli.CallWithTimeout("AccountSvc.GetAccountByUserName", args, &ret, time.Second*30)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ret.Acc, err
|
|
}
|
|
|
|
type UserSnidArg struct {
|
|
Snid int32
|
|
Platform string
|
|
}
|
|
|
|
func GetAccountBySnid(plt string, snid int32) (*Account, error) {
|
|
if rpcCli == nil {
|
|
return nil, ErrRPClientNoConn
|
|
}
|
|
args := &UserSnidArg{
|
|
Snid: snid,
|
|
Platform: plt,
|
|
}
|
|
var ret AccRet
|
|
err := rpcCli.CallWithTimeout("AccountSvc.GetAccountBySnId", args, &ret, time.Second*30)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ret.Acc, err
|
|
}
|
|
|
|
// 删除账号
|
|
func RemoveAccount(plt, accId string) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
args := &AccIdArg{
|
|
AccId: accId,
|
|
Platform: plt,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.RemoveAccount", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// pwd暂时用明文.需要使用MD5加密。参照task_login.go
|
|
func EditAccountPwd(plt, accId, pwd string) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
args := &Account{
|
|
AccountId: bson.ObjectIdHex(accId),
|
|
PassWord: pwd,
|
|
Platform: plt,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.EditAccountPwd", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ResetBackAccountPwd(plt, accId string) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
args := &AccIdArg{
|
|
AccId: accId,
|
|
Platform: plt,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.ResetBackAccountPwd", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GenAccountPwd(pwd string) string {
|
|
//md5(原始密码+AppId)
|
|
raw := fmt.Sprintf("%v%v", pwd, common.GetAppId())
|
|
h := md5.New()
|
|
io.WriteString(h, raw)
|
|
hashsum := hex.EncodeToString(h.Sum(nil))
|
|
return hashsum
|
|
}
|
|
|
|
/*
|
|
* 修改帐号密码
|
|
*/
|
|
func UpdatePlayerPassword(plt, accId, oldpassword, passWord string) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
args := &Account{
|
|
AccountId: bson.ObjectIdHex(accId),
|
|
PassWord: passWord,
|
|
BackPassWord: oldpassword,
|
|
Platform: plt,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.UpdatePlayerPassword", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
* 修改Token帐号密码
|
|
*/
|
|
func UpdatePlayerTokenPassword(plt, accId, passWord string) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
args := &Account{
|
|
AccountId: bson.ObjectIdHex(accId),
|
|
PassWord: passWord,
|
|
Platform: plt,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.UpdatePlayerTokenPassword", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
* 找回密码
|
|
*/
|
|
func GetBackPlayerPassword(tel, passWord, platform string, tagkey int32) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
args := &Account{
|
|
Tel: tel,
|
|
Platform: platform,
|
|
TagKey: tagkey,
|
|
PassWord: passWord,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.GetBackPlayerPassword", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UpdateAccountPlatformInfo(account string, platform, channel, promoter, inviterid, promoterTree int32, packTag string) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
args := &Account{
|
|
AccountId: bson.ObjectIdHex(account),
|
|
Platform: strconv.Itoa(int(platform)),
|
|
Channel: strconv.Itoa(int(channel)),
|
|
PackegeTag: packTag,
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.UpdateAccountPlatformInfo", args, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetRobotAccounts(limit int) []Account {
|
|
if rpcCli == nil {
|
|
return nil
|
|
}
|
|
|
|
var accs []Account
|
|
err := rpcCli.CallWithTimeout("AccountSvc.GetRobotAccounts", limit, &accs, time.Second*30)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return accs
|
|
}
|
|
|
|
func SaveToDelBackupAccount(acc *Account) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.SaveToDelBackupAccount", acc, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UpdateAccount(acc *Account) error {
|
|
if rpcCli == nil {
|
|
return ErrRPClientNoConn
|
|
}
|
|
ret := &AccRet{}
|
|
err := rpcCli.CallWithTimeout("AccountSvc.UpdateAccount", acc, ret, time.Second*30)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|