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.

364 lines
8.7 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 AccessSystemDao
import (
"dsSupport/Utils/CommonUtil"
"dsSupport/Utils/DbUtil"
"dsSupport/Utils/SqlKit"
"dsSupport/models"
"errors"
"fmt"
"github.com/oklog/ulid"
"github.com/rs/xid"
"github.com/xormplus/builder"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"strings"
"time"
)
var db = DbUtil.Engine
/**
功能:增加一个接入系统
*/
func AddApp(appCode string, appName string, sortId int32) error {
//生成AK+SK
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())
//判断appCode是否存在
sql := `select count(1) as c from t_app_base where app_code=?`
list, _ := db.SQL(sql, appCode).Query().List()
count := list[0]["c"].(int64)
if count > 0 {
return errors.New("系统代码已存在,请重新输入!")
}
//插入数据库数据
model := new(models.TAppBase)
model.AppId = CommonUtil.GetUUID()
model.AppCode = appCode
model.AppName = appName
model.AccessKey = appKey
model.SecretKey = appSecret
if sortId == 0 {
c, err := getMaxSortId()
if err != nil {
return err
}
sortId = int32(c)
}
model.SortId = sortId
model.BUse = 1
_, err := db.Insert(model)
if err != nil {
return err
}
//插入REDIS缓存
err = insertRedisCache(model.AccessKey, model.SecretKey, model.RedirectUri)
return err
}
/**
功能:删除一个接入系统
*/
func DelApp(appId string) error {
var model models.TAppBase
_, err := db.Where("app_id = ?", appId).Get(&model)
//删除REDIS缓存
_ = deleteRedisCache(model.AccessKey)
//删除物理记录
_, err = db.ID(appId).Delete(model)
return err
}
/**
功能:修改一个接入系统
*/
func UpdateApp(appId string, appCode string, appName string, sortId int32) error {
model := new(models.TAppBase)
//修改REDIS缓存
_, err := db.Where("app_id = ?", appId).Get(&model)
//删除REDIS缓存
_ = deleteRedisCache(model.AccessKey)
model.AppCode = appCode
model.AppName = appName
model.SortId = sortId
_, err = db.ID(appId).Update(model)
return err
}
/**
功能:统一认证配置
*/
func UpdateSso(appId string, redirectUri string, logoutUri string) error {
model := new(models.TAppBase)
//修改REDIS缓存
_, err := db.Where("app_id = ?", appId).Get(&model)
//1、清除Redis缓存
var selector = SqlKit.GetBean("t_base_class")
SqlKit.DeleteCacheByIds([]string{appId}, selector)
//删除REDIS缓存
_ = deleteRedisCache(model.AccessKey)
model.RedirectUri = redirectUri
model.LogoutUri = logoutUri
_, err = db.ID(appId).Update(model)
//插入REDIS缓存
err = insertRedisCache(model.AccessKey, model.SecretKey, model.RedirectUri)
return err
}
/**
功能:清空统一认证信息
*/
func ClearSso(appId string) error {
model := new(models.TAppBase)
//修改REDIS缓存
_, err := db.Where("app_id = ?", appId).Get(&model)
//1、清除Redis缓存
var selector = SqlKit.GetBean("t_base_class")
SqlKit.DeleteCacheByIds([]string{appId}, selector)
//删除REDIS缓存
_ = deleteRedisCache(model.AccessKey)
model.RedirectUri = ""
model.LogoutUri = ""
_, err = db.ID(appId).Cols("redirect_uri", "logout_uri").Update(model)
return err
}
/**
功能:集成系统配置
*/
func UpdateIntegration(appId string, appUrl string, appIcon string) error {
model := new(models.TAppBase)
//修改REDIS缓存
_, err := db.Where("app_id = ?", appId).Get(&model)
model.AppUrl = appUrl
model.AppIcon = appIcon
_, err = db.ID(appId).Update(model)
return err
}
/**
功能:清空集成系统信息
*/
func ClearIntegration(appId string) error {
model := new(models.TAppBase)
_, err := db.Where("app_id = ?", appId).Get(&model)
model.AppUrl = ""
model.AppIcon = ""
_, err = db.ID(appId).Cols("app_url", "app_icon").Update(model)
return err
}
/**
功能获取单个App的信息
*/
func GetApp(appId string) (map[string]interface{}, error) {
sql := `select * from t_app_base where app_id=?`
list, err := db.SQL(sql, appId).Query().List()
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, errors.New("没有找到指定的系统编号!")
}
return list[0], nil
}
/**
功能列表查询AppList
*/
func ListApp(keyword string, page int, limit int) ([]map[string]interface{}, int32, error) {
//接收传入参数
var offset = (page - 1) * limit
var myBuilder = builder.Dialect(builder.MYSQL).Select("*").From("t_app_base").
Where(builder.Like{"app_name", keyword})
//获取拼接完成的SQL语句
sql, err := myBuilder.OrderBy("app_name asc").Limit(limit, offset).ToBoundSQL()
if err != nil {
return nil, 0, err
}
//调用多查询字段通用方法
list, count, err := SqlKit.Query(sql)
return list, count, err
}
/**
功能:保存接入系统与身份的关系
*/
func SaveAppIdentity(appId string, identityIds []int) error {
sql := `delete from t_app_identity where app_id=?`
_, err := db.SQL(sql, appId).Execute()
if err != nil {
return err
}
sql = `insert into t_app_identity(app_id,identity_id) values(?,?)`
for i := range identityIds {
_, err = db.SQL(sql, appId, identityIds[i]).Execute()
if err != nil {
return err
}
}
return nil
}
/**
功能获取指定APP的身份对应信息
*/
func GetAppIdentity(appId string) ([]map[string]interface{}, error) {
sql := `select identity_id from t_app_identity where app_id=?`
list, err := db.SQL(sql, appId).Query().List()
return list, err
}
/**
功能:保存接入系统与职务的关系
*/
func SaveAppPosition(appId string, positionIds []string) error {
sql := `delete from t_app_position where app_id=?`
_, err := db.SQL(sql, appId).Execute()
if err != nil {
return err
}
sql = `insert into t_app_position(app_id,position_id) values(?,?)`
for i := range positionIds {
_, err = db.SQL(sql, appId, positionIds[i]).Execute()
if err != nil {
return err
}
}
return nil
}
/**
功能获取指定APP的职务对应信息
*/
func GetAppPosition(appId string) ([]map[string]interface{}, error) {
sql := `select position_id from t_app_position where app_id=?`
list, err := db.SQL(sql, appId).Query().List()
return list, err
}
/**
功能:保存接入系统与学段的关系
*/
func SaveAppStage(appId string, stageIds []string) error {
sql := `delete from t_app_stage where app_id=?`
_, err := db.SQL(sql, appId).Execute()
if err != nil {
return err
}
sql = `insert into t_app_stage(app_id,stage_id) values(?,?)`
if len(stageIds) > 0 {
for i := range stageIds {
_, err = db.SQL(sql, appId, stageIds[i]).Execute()
if err != nil {
return err
}
}
} else {
_, err = db.SQL(sql, appId, "-1").Execute()
if err != nil {
return err
}
}
return nil
}
/**
功能获取指定APP的学段对应信息
*/
func GetAppStage(appId string) ([]map[string]interface{}, error) {
sql := `select stage_id from t_app_stage where app_id=?`
list, err := db.SQL(sql, appId).Query().List()
return list, err
}
/**
功能:保存接入系统与可视范围的关系
*/
func SaveAppRange(appId string, rangeCodes []string) error {
sql := `delete from t_app_range where app_id=?`
_, err := db.SQL(sql, appId).Execute()
if err != nil {
return err
}
sql = `insert into t_app_range(app_id,range_code) values(?,?)`
for i := range rangeCodes {
_, err = db.SQL(sql, appId, rangeCodes[i]).Execute()
if err != nil {
return err
}
}
return nil
}
/**
功能获取指定APP的使用范围对应信息
*/
func GetAppRange(appId string) ([]map[string]interface{}, error) {
sql := `select range_code from t_app_range where app_id=?`
list, err := db.SQL(sql, appId).Query().List()
return list, err
}
/******************************************以下为内部函数***************************/
/**
功能:获取最大排序号
*/
func getMaxSortId() (int64, error) {
sql := `select ifnull(max(sort_id),1) as c from t_app_base`
list, err := db.SQL(sql).Query().List()
if err != nil {
return -1, err
}
return list[0]["c"].(int64), nil
}
/**
功能插入REDIS缓存
*/
func insertRedisCache(accessKey string, secretKey string, redirectUri string) error {
resp, err := http.PostForm("http://127.0.0.1/oauth2/AddClient",
url.Values{"access_key": {accessKey}, "secret_key": {secretKey}, "redirect_uri": {redirectUri}})
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println(string(body))
return nil
}
/**
功能删除REDIS缓存
*/
func deleteRedisCache(accessKey string) error {
//插入REDIS缓存
resp, err := http.PostForm("http://127.0.0.1/oauth2/DelClient",
url.Values{"access_key": {accessKey}})
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
fmt.Println(string(body))
return nil
}