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.

150 lines
3.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package DaoJoinApp
import (
"dsSso/Const/ErrorConst"
"dsSso/Dao/DaoCache"
"dsSso/Model"
"dsSso/Utils/DbUtil"
"dsSso/Utils/LogUtil"
"dsSso/Utils/RedisStorage"
"dsSso/Utils/RedisUtil"
"github.com/RangelReale/osin"
"github.com/oklog/ulid"
"github.com/rs/xid"
"math/rand"
"strings"
"time"
)
var joinModel Model.Selector
var tableName = "t_join_app"
var db = DbUtil.Engine
func init() {
_, joinModel = joinModel.Get(tableName)
}
/**
功能:获取统一认证的接入系统列表
作者:黄海
时间2020-02-05
*/
func GetJoinAppList(page int, limit int, keyword string) ([]map[string]interface{}, int32) {
//基础查询语句
baseSql := "select app_id from t_join_app where app_name like ?"
//使用通用方法获取简单表的分页查询SQL语句和求总数SQL语句
keyword = "%" + keyword + "%"
//偏移量
var offset = (page - 1) * limit
//一键加载,最后两个参数:每页多少个+从哪个位置开始,中间的参数为sql语句中的查询参数
list, count := DaoCache.PageData(baseSql, joinModel, keyword, limit, offset)
return list, count
}
/**
功能:增加接入系统的记录
作者:黄海
时间2020-02-21
*/
func AddJoinApp(appName string, redirectUri string) bool {
var appSecret string
//新增
sql := "insert into t_join_app(app_key,app_secret,app_name,redirect_uri) values(?,?,?,?)"
//获取主键ID
ak := xid.New()
appKey := ak.String() //新增就生成一个secret
t := time.Now().UTC()
entropy := rand.New(rand.NewSource(t.UnixNano()))
appSecret = strings.ToLower(ulid.MustNew(ulid.Timestamp(t), entropy).String())
_, err := db.Exec(sql, appKey, appSecret, appName, redirectUri)
if err != nil {
LogUtil.Error(ErrorConst.SqlUpdateError, "插入统一认证表信息出错:"+err.Error())
return false
} else {
//删除redis查询缓存
RedisUtil.DEL(joinModel.RedisPrefix + "_" + appKey)
//增加OAuth2 redis缓存
client := &osin.DefaultClient{
Id: appKey,
Secret: appSecret,
RedirectUri: redirectUri,
}
RedisStorage.OAuth2RedisStorage.CreateClient(client)
return true
}
}
/**
功能:修改接入系统的记录
作者:黄海
时间2020-02-21
*/
func UpdateJoinApp(appId string, appName string, redirectUri string) bool {
var secret string
//获取id对应的密码
sql := "select app_secret from t_join_app where app_id=?"
_map, err :=db.QueryString(sql, appId)
if err != nil {
LogUtil.Error(ErrorConst.SqlQueryError, "查询统一认证表信息出错!")
return false
}
if len(_map) > 0 {
secret = _map[0]["app_secret"]
} else {
LogUtil.Error(ErrorConst.SqlQueryError, "查询统一认证表信息出错,没有找到此id!")
return false
}
//更新语句
sql = "update t_join_app set app_name=?,redirect_uri=? where app_id=?"
_, err = db.Exec(sql, appName, redirectUri, appId)
if err != nil {
LogUtil.Error(ErrorConst.SqlUpdateError, "更新统一认证表信息出错!")
return false
} else {
//更新缓存
client := &osin.DefaultClient{
Id: appId,
Secret: secret,
RedirectUri: redirectUri,
}
RedisStorage.OAuth2RedisStorage.UpdateClient(client)
return true
}
}
/**
功能删除主键为ID的接入第三方统一认证系统
作者:黄海
时间2020-02-21
*/
func DeleteJoinAppById(appId string) bool {
//更新语句
sql := "delete from t_join_app where app_id=?"
db.Exec(sql, appId)
//删除缓存
RedisUtil.DEL(joinModel.RedisPrefix + "_" + appId)
//删除OAuth2的redis缓存
client, _ := RedisStorage.OAuth2RedisStorage.GetClient(appId)
if client != nil {
RedisStorage.OAuth2RedisStorage.DeleteClient(client)
return true
}
return false
}
/**
功能根据appId获取接入第三方统一认证系统的信息
作者:吴缤
日期2020-03-26
*/
func GetJoinAppInfoById(appId string) map[string]interface{} {
ids := []string{appId}
list := DaoCache.GetListByIds(ids, joinModel)
if len(list) > 0 {
return list[0]
} else {
return nil
}
}