|
|
package DaoSysLoginPerson
|
|
|
|
|
|
import (
|
|
|
"dsSso/Const"
|
|
|
"dsSso/Const/DefaultConst"
|
|
|
"dsSso/Const/ErrorConst"
|
|
|
"dsSso/Model"
|
|
|
"dsSso/Utils/CommonUtil"
|
|
|
"dsSso/Utils/DbUtil"
|
|
|
"dsSso/Utils/LdapUtil"
|
|
|
"dsSso/Utils/LogUtil"
|
|
|
"dsSso/Utils/RedisUtil"
|
|
|
"dsSso/models"
|
|
|
"fmt"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
var loginModel Model.Selector
|
|
|
var tableName = "t_sys_loginperson"
|
|
|
var db = DbUtil.Engine
|
|
|
|
|
|
func init() {
|
|
|
_, loginModel = loginModel.Get(tableName)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:验证用户名和密码是否正确
|
|
|
作者:黄海
|
|
|
时间:2020-02-05
|
|
|
*/
|
|
|
func Login(username string, password string, ip string) (bool, string, string, string, int, string) {
|
|
|
//查看redis中此人员的登录错误次数记录,如果已超出了规定次数5,则直接拒绝登录。
|
|
|
key := Const.RemainCountRedisPrefix + username
|
|
|
c, err := RedisUtil.EXISTS(key)
|
|
|
if err != nil {
|
|
|
return false, "", "", "", -1, ""
|
|
|
}
|
|
|
//默认有5次尝试机会
|
|
|
var remainCount = 5
|
|
|
//如果存在
|
|
|
if c > 0 {
|
|
|
remainCountStr, err := RedisUtil.GET(key)
|
|
|
if err != nil {
|
|
|
return false, "", "", "", -1, ""
|
|
|
}
|
|
|
remainCount = CommonUtil.ConvertStringToInt(remainCountStr)
|
|
|
if remainCount == 0 {
|
|
|
return false, "", "", "", 0, ""
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//身份号
|
|
|
var identityId = DefaultConst.IdentityId
|
|
|
//人员号
|
|
|
var personId = DefaultConst.PersonId
|
|
|
//姓名
|
|
|
var personName = ""
|
|
|
//通过用户名查找人员ID
|
|
|
var sql = "select * from t_sys_loginperson where login_name=? and b_use=1"
|
|
|
list, err := db.SQL(sql, username).Query().List()
|
|
|
if err != nil {
|
|
|
LogUtil.Error(ErrorConst.SqlQueryError, err.Error())
|
|
|
}
|
|
|
//如果用户名不存在
|
|
|
if len(list) == 0 {
|
|
|
return false, identityId, personId, personName, remainCount, ""
|
|
|
}
|
|
|
|
|
|
record := list[0]
|
|
|
//身份号
|
|
|
identityId = CommonUtil.ConvertInt64ToString(record["identity_id"].(int64))
|
|
|
//人员号
|
|
|
personId = record["person_id"].(string)
|
|
|
//人员姓名
|
|
|
personName = record["person_name"].(string)
|
|
|
//数据库中的密码
|
|
|
databasePassword := record["pwd"].(string)
|
|
|
//微信的openId
|
|
|
wxOpenId := record["wx_open_id"].(string)
|
|
|
|
|
|
//万能密码登录
|
|
|
if password == "DsideaL4r5t6y7u!@#" {
|
|
|
//记录日志
|
|
|
WriteLoginLog(identityId, personId, ip, 2, username) //2为万能密码登录
|
|
|
//返回结果
|
|
|
return true, identityId, personId, personName, remainCount, wxOpenId
|
|
|
} else {
|
|
|
//修改密码的加密算法基于ldap,黄海,于2020-04-27
|
|
|
ldapPassword := LdapUtil.GetLdapPassword(password)
|
|
|
if ldapPassword == databasePassword {
|
|
|
//记录日志
|
|
|
WriteLoginLog(identityId, personId, ip, 1, username)
|
|
|
//返回结果
|
|
|
return true, identityId, personId, personName, remainCount, wxOpenId
|
|
|
} else {
|
|
|
//如果登录失败,则incr,并设置过期时间2小时
|
|
|
RedisUtil.SET(key, CommonUtil.ConvertIntToString(remainCount-1), 2*time.Hour)
|
|
|
//记录日志
|
|
|
WriteLoginLog(identityId, personId, ip, -1, username)
|
|
|
return false, identityId, personId, personName, remainCount - 1, wxOpenId
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:记录登录日志
|
|
|
作者: 黄海
|
|
|
时间:2020-07-08
|
|
|
*/
|
|
|
func WriteLoginLog(identityId string, personId string, ip string, loginState int, loginName string) {
|
|
|
//表名
|
|
|
var tableName string
|
|
|
//管理员或教师
|
|
|
if identityId == "1" || identityId == "2" {
|
|
|
tableName = "t_base_teacher_org"
|
|
|
} else if identityId == "3" {
|
|
|
tableName = "t_base_student"
|
|
|
} else {
|
|
|
tableName = "t_base_parent"
|
|
|
}
|
|
|
//查询出结果
|
|
|
sql := "select province_code,city_code,district_code,bureau_id from " + tableName + " where person_id=?"
|
|
|
list, _ := db.SQL(sql, personId).Query().List()
|
|
|
|
|
|
//调用orm进行保存
|
|
|
var tLog models.TSysLoginpersonLog
|
|
|
tLog.BureauId = list[0]["bureau_id"].(string)
|
|
|
tLog.IdentityId = CommonUtil.ConvertStringToInt32(identityId)
|
|
|
tLog.PersonId = personId
|
|
|
tLog.ProvinceCode = list[0]["province_code"].(string)
|
|
|
tLog.CityCode = list[0]["city_code"].(string)
|
|
|
tLog.DistrictCode = list[0]["district_code"].(string)
|
|
|
tLog.IpAddress = ip
|
|
|
tLog.LoginName = loginName
|
|
|
tLog.LoginState = int32(loginState)
|
|
|
tLog.LoginTime = time.Now()
|
|
|
_, err := db.Insert(tLog)
|
|
|
if err != nil {
|
|
|
fmt.Println(err.Error())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:根据邮箱获取人员ID
|
|
|
作者:黄海
|
|
|
时间:2020-07-29
|
|
|
*/
|
|
|
func GetPersonIdByEmail(email string) (string, error) {
|
|
|
//目前的系统设计,只支持教师有邮箱
|
|
|
sql := `select person_id from t_base_teacher where b_use=1 and dzxx=?`
|
|
|
list, err := db.SQL(sql, email).Query().List()
|
|
|
if err != nil {
|
|
|
return "获取人员信息失败!", err
|
|
|
}
|
|
|
//如果没有找到指定邮箱的人员
|
|
|
if len(list) == 0 {
|
|
|
return "", nil
|
|
|
}
|
|
|
//如果找到了
|
|
|
return list[0]["person_id"].(string), nil
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:根据身份证号获取人员ID
|
|
|
作者:黄海
|
|
|
时间:2020-07-29
|
|
|
*/
|
|
|
func GetPersonIdByIdCard(idCard string) (string, error) {
|
|
|
//目前的系统设计,支持教师+学生
|
|
|
sql := `select person_id from t_base_teacher where b_use=1 and sfzjh=?`
|
|
|
list, err := db.SQL(sql, idCard).Query().List()
|
|
|
if err != nil {
|
|
|
return "获取教师身份证号信息失败!", err
|
|
|
}
|
|
|
//如果没有找到指定身份证号的人员
|
|
|
if len(list) == 0 {
|
|
|
//继续判断是不是在学生的身份证号
|
|
|
sql := `select person_id from t_base_student where b_use=1 and sfzjh=?`
|
|
|
list, err = db.SQL(sql, idCard).Query().List()
|
|
|
if err != nil {
|
|
|
return "获取学生身份证号信息失败!", err
|
|
|
}
|
|
|
if len(list) == 0 {
|
|
|
return "", nil
|
|
|
} else {
|
|
|
//如果找到了
|
|
|
return list[0]["person_id"].(string), nil
|
|
|
}
|
|
|
} else {
|
|
|
//如果找到了
|
|
|
return list[0]["person_id"].(string), nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:根据手机号获取人员ID
|
|
|
作者:黄海
|
|
|
时间:2020-07-29
|
|
|
*/
|
|
|
func GetPersonIdByTel(tel string) (string, error) {
|
|
|
//目前的系统设计,支持教师+家长
|
|
|
sql := `select person_id from t_base_teacher where b_use=1 and lxdh=?`
|
|
|
list, err := db.SQL(sql, tel).Query().List()
|
|
|
if err != nil {
|
|
|
return "获取教师手机号信息失败!", err
|
|
|
}
|
|
|
//如果没有找到指定身份证号的人员
|
|
|
if len(list) == 0 {
|
|
|
//继续判断是不是在学生的身份证号
|
|
|
sql := `select person_id from t_base_parent where b_use=1 and lxdh=?`
|
|
|
list, err = db.SQL(sql, tel).Query().List()
|
|
|
if err != nil {
|
|
|
return "获取家长手机号信息失败!", err
|
|
|
}
|
|
|
if len(list) == 0 {
|
|
|
return "", nil
|
|
|
} else {
|
|
|
//如果找到了
|
|
|
return list[0]["person_id"].(string), nil
|
|
|
}
|
|
|
} else {
|
|
|
//如果找到了
|
|
|
return list[0]["person_id"].(string), nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:根据人员ID获取人员的登录名
|
|
|
作者:黄海
|
|
|
时间:2020-07-29
|
|
|
*/
|
|
|
func GetLoginNameByPersonId(personId string) (string, error) {
|
|
|
sql := `select login_name from t_sys_loginperson where b_use=1 and person_id=?`
|
|
|
list, err := db.SQL(sql, personId).Query().List()
|
|
|
if err != nil {
|
|
|
return "获取人员登录名失败!", err
|
|
|
}
|
|
|
if len(list) == 0 {
|
|
|
return "", nil
|
|
|
}
|
|
|
return list[0]["login_name"].(string), nil
|
|
|
}
|
|
|
|
|
|
// 对OpenId进行检查,如果已存在,则模拟登录,如果不存在,返回false,让其跳转到绑定用户页面
|
|
|
func CheckOpenId(openId string) (bool, error, int64, string) {
|
|
|
sql := `select * from t_sys_loginperson where wx_open_id=?`
|
|
|
list, err := db.SQL(sql, openId).Query().List()
|
|
|
if err != nil {
|
|
|
return false, err, -1, ""
|
|
|
}
|
|
|
if len(list) == 0 {
|
|
|
return false, nil, -1, ""
|
|
|
}
|
|
|
return true, nil, list[0]["identity_id"].(int64), list[0]["person_id"].(string)
|
|
|
}
|
|
|
|
|
|
//绑定微信的用户
|
|
|
func BindWxUser(identityId string, personId string, openid string) (bool, error) {
|
|
|
sql := `update t_sys_loginperson set wx_open_id=? where identity_id=? and person_id=?`
|
|
|
_, err := db.SQL(sql, openid, identityId, personId).Execute()
|
|
|
if err != nil {
|
|
|
return false, err
|
|
|
} else {
|
|
|
return true, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//解除微信绑定的用户
|
|
|
func UnBindWxUser(identityId string, personId string) (bool, error) {
|
|
|
return BindWxUser(identityId, personId, "")
|
|
|
}
|
|
|
|
|
|
//通过人员身份ID和人员编号,获取人员的信息
|
|
|
func GetPersonName(identityId string, personId string) (string, error) {
|
|
|
sql := `select person_name from t_sys_loginperson where identity_id=? and person_id=?`
|
|
|
list, err := db.SQL(sql, identityId, personId).Query().List()
|
|
|
if err != nil {
|
|
|
return "", err
|
|
|
}
|
|
|
if len(list) == 0 {
|
|
|
return "", err
|
|
|
}
|
|
|
return list[0]["person_name"].(string), nil
|
|
|
}
|