user.go 5.06 KB
package models

import (
	"crypto/md5"
	"encoding/hex"
	"errors"
	"github.com/satori/go.uuid"
	"log"
	db "loginserver/database"
	"strings"
)

type UserLoginInfo struct {
	Type     string `json:"login_type"`
	Name     string `json:"login_name"`
	Password string `json:"login_password"`
}

type UserRegisterInfo struct {
	Type     string `json:"register_type"`
	Name     string `json:"register_name"`
	Password string `json:"register_password"`
}

type UserInfo struct {
	Id           int
	UserPassword string
	UserName     string
}

func (u *UserLoginInfo) UserLogin() (result interface{}, err error) {
	var userInfo UserInfo
	switch u.Type {
	case "normal":
		r, err := GetUserInfoByUserName(u.Name)
		if err != nil {
			return r, err
		}
		userInfo = r.(UserInfo)
		if userInfo.UserPassword != MD5(u.Password) {
			err = errors.New("密码错误")
			return "105004", err
		}
		break
	default:
		err = errors.New("不存在的登陆类型")
		return "105000", err
	}
	res := make(map[string]interface{}, 0)
	res["user_id"] = userInfo.Id
	token, err := GetUserAccessToken(userInfo.UserName)
	if err != nil {
		return token, err
	}
	res["token"] = token
	log.Println(u.Name, " login success")
	return res, nil
}

func (u *UserRegisterInfo) UserRegister() (result interface{}, err error) {
	switch u.Type {
	case "account": //账号密码注册
		res, err := CheckUserNameRepeat(u.Name)
		if err != nil {
			return res, err
		}
		errno, err := InsertUser(u.Name, u.Password)
		if err != nil {
			return errno, err
		}
		break
	default:
		err = errors.New("不存在的注册类型")
		return "105001", err
		break
	}
	log.Println(u.Name, " register success")
	return nil, nil
}

//检查用户名是否重复
func CheckUserNameRepeat(userName string) (result interface{}, err error) {
	sql := ` select user_password from icesimba_polysdk_user where user_name = ?`
	r, err := db.Engine.Query(sql, userName)
	if err != nil {
		return "104001", err
	}
	if len(r) == 0 {
		return nil, nil
	}
	err = errors.New("该名称已被使用")
	return "105001", err

}

//获取用户信息
func GetUserInfoByUserName(userName string) (result interface{}, err error) {
	u := make([]UserInfo, 0)
	sql := ` select id,user_name,user_password from icesimba_polysdk_user where  user_name = ? limit 1`
	err = db.Engine.Sql(sql, userName).Find(&u)
	if err != nil {
		return "104001", err
	}
	if len(u) == 0 {
		err = errors.New("不存在的用户名")
		return "105003", err
	}

	return u[0], nil

}

//修改用户密码
func ChangeUserPassword(userName string, originPassword string, newPassword string) (result interface{}, err error) {
	r, err := GetUserInfoByUserName(userName)
	if err != nil {
		return r, err
	}
	password := r.(UserInfo).UserPassword
	if MD5(originPassword) != password {
		err = errors.New("密码错误,无法修改!")
		return "105004", err
	}
	if newPassword == originPassword {
		err = errors.New("新旧密码一致!无法修改")
		return "105002", err
	}
	password = MD5(newPassword)
	sql := ` update icesimba_polysdk_user set user_password = ? where user_name = ?`
	_, err = db.Engine.Exec(sql, password, userName)
	if err != nil {
		return "104001", err
	}
	return
}

//添加用户
func InsertUser(userName string, userPassword string) (result interface{}, err error) {
	password := MD5(userPassword)
	sql := ` insert into icesimba_polysdk_user(user_name,user_password) values(?,?) `
	_, err = db.Engine.Exec(sql, userName, password)
	if err != nil {
		return "104001", err
	}
	return nil, nil

}

//授权
func GetUserAccessToken(userName string) (result string, err error) {
	userInfo, err := GetUserInfoByUserName(userName)
	if err != nil {
		return userInfo.(string), err
	}
	userId := userInfo.(UserInfo).Id
	uuid := uuid.NewV4().String()
	uuid = strings.Replace(uuid, "-", "", -1)
	sql := ` replace into icesimba_user_token(user_id,user_token) values(?,?)`
	_, err = db.Engine.Exec(sql, userId, uuid)
	if err != nil {
		return "104002", err
	}
	return uuid, nil
}

//验证授权
func CheckUserAccessToken(token string) (result string, err error) {
	sql := ` select user_id from icesimba_user_token where user_token = ? limit 1`
	r, err := db.Engine.Query(sql, token)
	if err != nil {
		return "104002", err
	}
	if len(r) == 0 {
		err = errors.New("token已失效")
		return "false", nil
	}
	return "true", nil
}

//生成外部应用授权
func GetThirdpartAccessToken(userId int) (result string, err error) {
	uuid := uuid.NewV4().String()
	sql := ` replace into icesimba_thirdpart_token(user_id,user_thirdpart_token) values(?,?)`
	_, err = db.Engine.Exec(sql, userId, uuid)
	if err != nil {
		return "104002", err
	}
	return "", nil
}

//验证外部应用授权
func CheckThirdpartAccessToken(token string) (result string, err error) {
	sql := ` select user_id from icesimba_user_thirdpart_token where user_thirdpart_token = ? limit 1`
	r, err := db.Engine.Query(sql, token)
	if err != nil {
		return "104002", err
	}
	if len(r) == 0 {
		err = errors.New("token已失效")
		return "false", nil
	}
	return "true", nil
}

func MD5(str string) string {
	w := md5.New()
	w.Write([]byte(str))
	has := w.Sum(nil)
	str = hex.EncodeToString(has)
	return str

}