You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
387 lines
17 KiB
387 lines
17 KiB
package middleware
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gin_server_admin/commonus"
|
|
"gin_server_admin/global"
|
|
"gin_server_admin/model/common/response"
|
|
"gin_server_admin/model/hrsystem"
|
|
"gin_server_admin/service"
|
|
"gin_server_admin/utils/redishandel"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/mitchellh/mapstructure"
|
|
)
|
|
|
|
// 鉴权
|
|
func MyAuthentication() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// userKey := c.Request.Header.Get("user-key")
|
|
// userToken := c.Request.Header.Get("user-token")
|
|
// userAgent := c.Request.Header.Get("User-Agent")
|
|
|
|
// if userKey == "" || userToken == "" || userAgent == "" {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 1}, "未登录或非法访问", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
// userAgent = "250"
|
|
// userKeyInt, userKeyIntErr := strconv.ParseInt(userKey, 10, 64)
|
|
// if userKeyIntErr != nil {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 2}, "未登录或非法访问", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
// global.GVA_INDEX_USERKEY = userKeyInt
|
|
|
|
// redisClient := redishandel.RunRedis()
|
|
// tokenInfo, isTrues := redisClient.HashGetAll("system:Identification_" + global.GVA_CONFIG.RedisPrefix.Alias + "_" + userKey)
|
|
// var myCustomIdentify commonus.MyCustomLogonIdentify
|
|
// if isTrues != true {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 3}, "您的帐户异地登陆或令牌失效", c)
|
|
// c.Abort()
|
|
// return
|
|
// } else {
|
|
// tokenErr := mapstructure.Decode(tokenInfo, &myCustomIdentify)
|
|
// if tokenErr != nil {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 3}, "您的帐户异地登陆或令牌失效", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
// var md5JiaMi commonus.Md5Encryption
|
|
// md5JiaMi.Md5EncryptionInit(userAgent)
|
|
// md5Token := md5JiaMi.Md5EncryptionAlgorithm()
|
|
// sha1Str := myCustomIdentify.UserKey + myCustomIdentify.UserNumber + myCustomIdentify.UserPwd + md5Token
|
|
// sha1Token := commonus.Sha1Encryption(sha1Str)
|
|
// // fmt.Printf("token=========>%v---->%v---->%v\n", md5Token, sha1Token, userAgent)
|
|
// if sha1Token != userToken {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 3, "userAgent": userAgent, "sha1Token": sha1Token, "userToken": userToken, "tokenInfo": tokenInfo}, "授权已过期", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
// }
|
|
// redisClient.SetRedisTime(86400)
|
|
// // redisClient.SetRedisTime(60)
|
|
// writeRedisData := map[string]interface{}{
|
|
// "userkey": myCustomIdentify.UserKey,
|
|
// "usernumber": myCustomIdentify.UserNumber,
|
|
// "userpwd": myCustomIdentify.UserPwd,
|
|
// "usertoken": myCustomIdentify.UserToken,
|
|
// }
|
|
|
|
// redisClient.HashMsetAdd("system:Identification_"+global.GVA_CONFIG.RedisPrefix.Alias+"_"+userKey, writeRedisData)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
var mycasbinService = service.ServiceGroupApp.SystemServiceGroup.CasbinService
|
|
|
|
// 拦截器
|
|
func MyCasbinHandler() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// waitUse, _ := utils.GetClaims(c)
|
|
// 获取请求的URI
|
|
// obj := c.Request.URL.RequestURI()
|
|
// 获取请求方法
|
|
// act := c.Request.Method
|
|
// 获取用户的角色
|
|
// sub := waitUse.AuthorityId
|
|
// e := mycasbinService.Casbin()
|
|
// 判断策略中是否存在
|
|
// success, _ := e.Enforce(sub, obj, act)
|
|
|
|
// fmt.Printf("=====>%v===========>%v===========>%v===========>%v\n", e, obj, act, e)
|
|
|
|
// if global.GVA_CONFIG.System.Env == "develop" || success {
|
|
// c.Next()
|
|
// } else {
|
|
// response.FailWithDetailed(gin.H{}, "权限不足", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
redisFileKey := "ScanCode:UserInfo:LoginUser_" + global.GVA_CONFIG.RedisPrefix.Alias + "_" + global.GVA_SCANCODE_USERNUMBER
|
|
redisClient := redishandel.RunRedis()
|
|
redisClient.SetRedisDb(5)
|
|
tokenInfo, isTrues := redisClient.HashGetAll(redisFileKey)
|
|
|
|
if isTrues == true {
|
|
mapstructure.Decode(tokenInfo, &global.GVA_SCANCODE_USERINFO)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func SystemAuthentication() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// userKey := c.Request.Header.Get("user-key")
|
|
// userToken := c.Request.Header.Get("user-token")
|
|
// userAgent := c.Request.Header.Get("User-Agent")
|
|
|
|
// if userKey == "" || userToken == "" || userAgent == "" {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 101}, "未登录或非法访问", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
|
|
// userKeyInt, userKeyIntErr := strconv.ParseInt(userKey, 10, 64)
|
|
// if userKeyIntErr != nil {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 2}, "未登录或非法访问", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
// global.GVA_ADMIN_USERKEY = userKeyInt
|
|
|
|
// redisClient := redishandel.RunRedis()
|
|
// tokenInfo, isTrues := redisClient.HashGetAll("system:SystemIdentification_" + global.GVA_CONFIG.RedisPrefix.Alias + "_" + userKey)
|
|
// var myCustomIdentify commonus.MyCustomAdminLogonIdentify
|
|
// if isTrues != true {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 102}, "您的帐户异地登陆或令牌失效", c)
|
|
// c.Abort()
|
|
// return
|
|
// } else {
|
|
// tokenErr := mapstructure.Decode(tokenInfo, &myCustomIdentify)
|
|
// if tokenErr != nil {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 103, "tokenErr": tokenErr}, "您的帐户异地登陆或令牌失效", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
// userAgent = "250"
|
|
// var md5JiaMi commonus.Md5Encryption
|
|
// md5JiaMi.Md5EncryptionInit(userAgent)
|
|
// md5Token := md5JiaMi.Md5EncryptionAlgorithm()
|
|
// sha1Str := myCustomIdentify.UserKey + myCustomIdentify.UserNumber + myCustomIdentify.UserPwd + md5Token
|
|
// sha1Token := commonus.Sha1Encryption(sha1Str)
|
|
// // fmt.Printf("token=========>%v---->%v---->%v\n", md5Token, sha1Token, userAgent)
|
|
// if sha1Token != userToken {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 104}, "授权已过期", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
// }
|
|
// redisClient.SetRedisTime(86400)
|
|
// writeRedisData := map[string]interface{}{
|
|
// "userkey": myCustomIdentify.UserKey,
|
|
// "usernumber": myCustomIdentify.UserNumber,
|
|
// "userpwd": myCustomIdentify.UserPwd,
|
|
// "usertoken": myCustomIdentify.UserToken,
|
|
// "jurisdiction": myCustomIdentify.Jurisdiction,
|
|
// "menuOper": myCustomIdentify.MenuOper,
|
|
// "wand": myCustomIdentify.Wand,
|
|
// }
|
|
// var surisdictionStr []string
|
|
// var surisdictionInt []int64
|
|
// jsonErr := json.Unmarshal([]byte(myCustomIdentify.Jurisdiction), &surisdictionStr)
|
|
// if jsonErr == nil {
|
|
// for _, jurVal := range surisdictionStr {
|
|
// jurValInt, jurValErr := strconv.ParseInt(jurVal, 10, 64)
|
|
// if jurValErr == nil {
|
|
// surisdictionInt = append(surisdictionInt, jurValInt)
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
// global.Gva_Authority_Authentication = surisdictionInt
|
|
// var menuOperStr []string
|
|
// var menuOperInts []int64
|
|
// jsonErrSun := json.Unmarshal([]byte(myCustomIdentify.MenuOper), &menuOperStr)
|
|
// if jsonErrSun == nil {
|
|
// for _, menuOperVal := range menuOperStr {
|
|
// menuOperInt, menuOperErr := strconv.ParseInt(menuOperVal, 10, 64)
|
|
// if menuOperErr == nil {
|
|
// menuOperInts = append(menuOperInts, menuOperInt)
|
|
// }
|
|
|
|
// }
|
|
// }
|
|
// global.Gva_Authority_Authentication_Subsidiary = menuOperInts
|
|
// redisClient.HashMsetAdd("system:SystemIdentification_"+global.GVA_CONFIG.RedisPrefix.Alias+"_"+userKey, writeRedisData)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// 拦截器
|
|
func SystemInterceptor() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
obj := c.Request.URL.RequestURI()
|
|
// 获取请求方法
|
|
act := c.Request.Method
|
|
|
|
fmt.Printf("obj-------------->%v <===========> act--------------------->%v\n", obj, act)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// 扫码登录身份验证
|
|
func ScanCodeLogin() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
userKey := c.Request.Header.Get("user-key")
|
|
userToken := c.Request.Header.Get("user-token")
|
|
userAgent := c.Request.Header.Get("User-Agent")
|
|
|
|
if userKey == "" || userToken == "" || userAgent == "" {
|
|
response.FailWithDetailed(gin.H{"reload": true, "code": 1001}, "未登录或非法访问", c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
redisFileKey := "ScanCode:Authentication:LoginApi_" + global.GVA_CONFIG.RedisPrefix.Alias + "_" + userKey
|
|
fmt.Printf("userKey-------------->%v--------redisFileKey-------------->%v\n", userKey, redisFileKey)
|
|
|
|
// userKeyInt, userKeyIntErr := strconv.ParseInt(userKey, 10, 64)
|
|
// if userKeyIntErr != nil {
|
|
// response.FailWithDetailed(gin.H{"reload": true, "code": 2, "userKey": userKey}, "未登录或非法访问", c)
|
|
// c.Abort()
|
|
// return
|
|
// }
|
|
global.GVA_ADMIN_USERKEY = userKey
|
|
|
|
redisClient := redishandel.RunRedis()
|
|
redisClient.SetRedisDb(5)
|
|
|
|
tokenInfo, isTrues := redisClient.HashGetAll(redisFileKey)
|
|
var myCustomIdentify commonus.ScanCodeLogin
|
|
|
|
if isTrues != true {
|
|
response.FailWithDetailed(gin.H{"reload": true, "code": 1002, "key": redisFileKey, "tokenInfo": tokenInfo, "redisClient": redisClient, "userKey": userKey}, "您的帐户异地登陆或令牌失效", c)
|
|
c.Abort()
|
|
return
|
|
} else {
|
|
tokenErr := mapstructure.Decode(tokenInfo, &myCustomIdentify)
|
|
if tokenErr != nil {
|
|
response.FailWithDetailed(gin.H{"reload": true, "code": 1003, "tokenErr": tokenErr}, "您的令牌失效", c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
// userAgent = "250"
|
|
userAgent = global.GVA_CONFIG.MyConfig.AppKey
|
|
var md5JiaMi commonus.Md5Encryption
|
|
md5JiaMi.Md5EncryptionInit(userAgent)
|
|
md5Token := md5JiaMi.Md5EncryptionAlgorithm()
|
|
|
|
var md5JiaMiNumber commonus.Md5Encryption
|
|
md5JiaMiNumber.Md5EncryptionInit(myCustomIdentify.UserNumber)
|
|
userKeyCode := md5JiaMiNumber.Md5EncryptionAlgorithm()
|
|
|
|
sha1Str := userKeyCode + myCustomIdentify.UserNumber + myCustomIdentify.UserPwd + md5Token
|
|
sha1Token := commonus.Sha1Encryption(sha1Str)
|
|
// fmt.Printf("token=========>%v---->%v---->%v---->%v---->%v\n", userKeyCode, myCustomIdentify.UserNumber, myCustomIdentify.UserPwd, md5Token, sha1Token)
|
|
if sha1Token != userToken {
|
|
response.FailWithDetailed(gin.H{"reload": true, "code": 104, "sha1Token": sha1Token, "userToken": userToken}, "授权已过期", c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
}
|
|
global.GVA_SCANCODE_USERNUMBER = myCustomIdentify.UserNumber
|
|
|
|
writeRedisData := map[string]interface{}{
|
|
"userkey": myCustomIdentify.UserKey,
|
|
"key": myCustomIdentify.Key,
|
|
"usernumber": myCustomIdentify.UserNumber,
|
|
"userpwd": myCustomIdentify.UserPwd,
|
|
"usertoken": myCustomIdentify.UserToken,
|
|
"jurisdiction": myCustomIdentify.Jurisdiction,
|
|
"menuOper": myCustomIdentify.MenuOper,
|
|
"wand": myCustomIdentify.Wand,
|
|
}
|
|
var surisdictionStr []string
|
|
var surisdictionInt []int64
|
|
jsonErr := json.Unmarshal([]byte(myCustomIdentify.Jurisdiction), &surisdictionStr)
|
|
if jsonErr == nil {
|
|
for _, jurVal := range surisdictionStr {
|
|
jurValInt, jurValErr := strconv.ParseInt(jurVal, 10, 64)
|
|
if jurValErr == nil {
|
|
surisdictionInt = append(surisdictionInt, jurValInt)
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
global.Gva_Authority_Authentication = surisdictionInt
|
|
var menuOperStr []string
|
|
var menuOperInts []int64
|
|
// jsonErrSun := json.Unmarshal([]byte(myCustomIdentify.MenuOper), &menuOperStr)
|
|
menuOperStr = strings.Split(myCustomIdentify.MenuOper, ",")
|
|
// if jsonErrSun == nil {
|
|
for _, menuOperVal := range menuOperStr {
|
|
menuOperInt, menuOperErr := strconv.ParseInt(menuOperVal, 10, 64)
|
|
if menuOperErr == nil {
|
|
menuOperInts = append(menuOperInts, menuOperInt)
|
|
}
|
|
|
|
}
|
|
// }
|
|
// fmt.Printf("菜单----JWT---->%v---->%v---->%v\n", menuOperInts, myCustomIdentify.MenuOper, menuOperStr)
|
|
global.Gva_Authority_Authentication_Subsidiary = menuOperInts
|
|
redisClient.SetRedisTime(10800)
|
|
redisClient.HashMsetAdd(redisFileKey, writeRedisData)
|
|
|
|
var userCont hrsystem.ManCont
|
|
userErrd := userCont.GetCont(map[string]interface{}{"`number`": myCustomIdentify.UserNumber, "`password`": myCustomIdentify.UserPwd})
|
|
if userErrd != nil {
|
|
response.Result(1000002, userErrd, "登陆失败! 该账号不存在!", c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
//缓存写入个人信息
|
|
redisMyContKey := fmt.Sprintf("ScanCode:Authentication:UserCont_%v_%v", global.GVA_CONFIG.RedisPrefix.Alias, userCont.Number)
|
|
myCont := commonus.MapOut()
|
|
myCont["id"] = userCont.Id
|
|
myCont["number"] = userCont.Number //员工工号
|
|
myCont["name"] = userCont.Name //姓名
|
|
myCont["icon"] = userCont.Icon //头像
|
|
myCont["hire_class"] = userCont.HireClass //雇佣类型(1:雇佣入职;2:再入职;)
|
|
myCont["emp_type"] = userCont.EmpType //用工关系(1:临时工 , 2:编外人员 ;3:实习&实习生;4:试用员工;5:待分配;6:待岗;7:临时调入;8:正式员工;9:长期病假;10:停薪留职;11:退休;12:辞职;13:辞退;14:离职)
|
|
myCont["company"] = userCont.Company //入职公司
|
|
myCont["maindeparment"] = userCont.MainDeparment //主部门
|
|
myCont["sun_main_department"] = userCont.SunMainDeparment //二级主部门
|
|
myCont["deparment"] = userCont.Deparment //部门
|
|
myCont["admin_org"] = userCont.AdminOrg //所属行政组织
|
|
myCont["teamid"] = userCont.TeamId //班组
|
|
myCont["position"] = userCont.Position //职位
|
|
myCont["job_class"] = userCont.JobClass //职务分类
|
|
myCont["job_id"] = userCont.JobId //职务
|
|
myCont["job_leve"] = userCont.JobLeve //职务等级
|
|
myCont["wechat"] = userCont.Wechat //微信UserId
|
|
myCont["work_wechat"] = userCont.WorkWechat //企业微信UserId
|
|
myCont["state"] = userCont.State //状态(1:启用;2:禁用;3:删除)
|
|
myCont["key"] = userCont.Key //key
|
|
myCont["is_admin"] = userCont.IsAdmin //是否为管理员(1:不是;2:分公司;3:集团管理员;4:超级管
|
|
myCont["password"] = userCont.Password //密码
|
|
myCont["role"] = userCont.Role //角色
|
|
myCont["idcardno"] = userCont.Idcardno //身份证号
|
|
myCont["passportno"] = userCont.Passportno //护照号码
|
|
myCont["globalroaming"] = userCont.Globalroaming //国际区号
|
|
myCont["mobilephone"] = userCont.Mobilephone //手机号码
|
|
myCont["email"] = userCont.Email //电子邮件
|
|
myCont["gender"] = userCont.Gender //性别(1:男性;2:女性;3:中性)
|
|
myCont["birthday"] = userCont.Birthday //birthday
|
|
myCont["myfolk"] = userCont.Myfolk //民族
|
|
myCont["nativeplace"] = userCont.Nativeplace //籍贯
|
|
myCont["idcardstartdate"] = userCont.Idcardstartdate //身份证有效期开始
|
|
myCont["idcardenddate"] = userCont.Idcardenddate //身份证有效期结束
|
|
myCont["idcardaddress"] = userCont.Idcardaddress //身份证地址
|
|
myCont["idcardIssued"] = userCont.IdcardIssued //身份证签发机关
|
|
myCont["health"] = userCont.Health //健康状况(1:良好;2:一般;3:较弱,4:有生理缺陷;5:残废)
|
|
myCont["maritalstatus"] = userCont.Maritalstatus //婚姻状况(1:未婚;2:已婚;3:丧偶;4:离异)
|
|
myCont["internaltelephone"] = userCont.Internaltelephone //内线电话
|
|
myCont["currentresidence"] = userCont.Currentresidence //现居住地址
|
|
myCont["constellationing"] = userCont.Constellation //星座(1:白羊座;2:金牛座;3:双子座;4:巨蟹座;5:狮子座;6:处女座;7:天枰座;8:天蝎座;9:射手座;10:摩羯座;11:水瓶座;12:双鱼座)
|
|
myCont["isdoubleworker"] = userCont.Isdoubleworker //是否双职工(1:是;2:否)
|
|
myCont["isveterans"] = userCont.Isveterans //是否为退役军人(1:是;2:否)
|
|
myCont["veteransnumber"] = userCont.Veteransnumber //退役证编号
|
|
myCont["jobstartdate"] = userCont.Jobstartdate //参加工作日期
|
|
myCont["entrydate"] = userCont.Entrydate //入职日期
|
|
myCont["probationperiod"] = userCont.Probationperiod //试用期
|
|
myCont["planformaldate"] = userCont.Planformaldate //预计转正日期
|
|
myCont["political_outlook"] = userCont.PoliticalOutlook //政治面貌(1:群众;2:无党派;3:台盟会员;4:九三社员;5:致公党员;6:农工党员;7:民进会员;8:民建会员;9:民盟盟员;10:民革会员,11:共青团员;12:预备党员;13:中共党员)
|
|
redisClient.HashMsetAdd(redisMyContKey, myCont)
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|