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.

485 lines
12 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) (string, 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 model.AppId, err
}
sortId = int32(c)
}
model.SortId = sortId
model.BUse = 1
_, err := db.Insert(model)
if err != nil {
return model.AppId, err
}
//插入REDIS缓存
err = insertRedisCache(model.AccessKey, model.SecretKey, model.RedirectUri)
return model.AppId, 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_app_base")
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
}
/**
功能修改接入系统的状态1正常-2被禁用
*/
func ChangeAppStatus(appId string, bUse int32) error {
//1、修改数据库
model := new(models.TAppBase)
model.BUse = bUse
_, err := db.ID(appId).Update(model)
if err != nil {
return err
}
//2、获取数据库中的完整数据
_, err = db.Where("app_id = ?", appId).Get(model)
if err != nil {
return err
}
//3、清除主键Redis缓存
var selector = SqlKit.GetBean("t_app_base")
SqlKit.DeleteCacheByIds([]string{appId}, selector)
//4、删除SSO中的缓存数据
if bUse <= 0 {
//删除REDIS缓存
_ = deleteRedisCache(model.AccessKey)
} else {
//插入REDIS缓存
_ = insertRedisCache(model.AccessKey, model.SecretKey, model.RedirectUri)
}
return nil
}
/**
功能:清空统一认证信息
*/
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_app_base")
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) {
fmt.Println(appId)
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
}
/**
* @title GetAppByCode
* @Description 根据系统编码获取系统信息
* @Author wangshuai
* @Date 2020-09-22
* @Param appCode string 数据源CODE
* @Return map[string]interface{} 系统信息
* @Return string 错误信息
*/
func GetAppByCode(appCode string) (map[string]interface{}, error) {
fmt.Println(appCode)
sql := `select * from t_app_base where app_code=?`
list, err := db.SQL(sql, appCode).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("create_time desc").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使用范围准备的实体
*/
type OrgBean struct {
OrgId string `json:"org_id"`
OrgName string `json:"org_name"`
}
type AppRangeBean struct {
FullFlag int `json:"full_flag"`
CityCode string `json:"city_code"`
DistrictList []string `json:"district_list"`
OrgList []OrgBean `json:"org_list"`
}
/**
功能获取指定APP的使用范围对应信息
*/
func GetAppRange(appId string) (AppRangeBean, error) {
var bean AppRangeBean
var districtList = make([]string, 0)
var orgList = make([]OrgBean, 0)
sql := `select range_code from t_app_range where app_id=?`
list, err := db.SQL(sql, appId).Query().List()
if len(list) == 0 {
return bean, errors.New("没有找到此app的接入范围数据")
}
aCode := list[0]["range_code"].(string)
//1、是不是全市
if len(aCode) == 6 && aCode[len(aCode)-2:] == "00" {
bean.FullFlag = 1
//城市code
bean.CityCode = list[0]["range_code"].(string)
//县区列表
bean.DistrictList = make([]string, 0)
//组织机构列表
bean.OrgList = make([]OrgBean, 0)
} else {
bean.FullFlag = 0
//如果是县区
if len(aCode) == 6 {
//查找对应的城市
bean.CityCode = aCode[0:4] + "00"
} else {
//如果是单位+查找对应的城市
sql := `select city_code from t_base_organization where org_id=?`
_list2, err := db.SQL(sql, aCode).Query().List()
if err != nil {
return bean, err
}
if len(_list2) == 0 {
return bean, errors.New("没有找到指定的org_id!")
}
bean.CityCode = _list2[0]["city_code"].(string)
}
//2、填充县区和单位列表
for i := range list {
c := list[i]["range_code"].(string)
//县区
if len(c) == 6 {
districtList = append(districtList, c)
} else { //单位
var o OrgBean
o.OrgId = c
sql = `select org_name from t_base_organization where org_id=?`
list2, err := db.SQL(sql, o.OrgId).Query().List()
if err != nil {
return bean, err
}
o.OrgName = list2[0]["org_name"].(string)
orgList = append(orgList, o)
}
}
}
bean.OrgList = orgList
bean.DistrictList = districtList
return bean, 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()
_, _ = ioutil.ReadAll(resp.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
}