master
huanghai 5 years ago
parent df18be6559
commit 096b7d7c83

@ -23,7 +23,7 @@ var db = DbUtil.Engine
/**
*/
func AddApp(appCode string, appName string, appUrl string, appIcon string, redirectUri string, sortId int32) error {
func AddApp(appCode string, appName string, sortId int32) error {
//生成AK+SK
ak := xid.New()
appKey := ak.String() //新增就生成一个secret
@ -44,9 +44,13 @@ func AddApp(appCode string, appName string, appUrl string, appIcon string, redir
model.AppName = appName
model.AccessKey = appKey
model.SecretKey = appSecret
model.AppUrl = appUrl
model.AppIcon = appIcon
model.RedirectUri = redirectUri
if sortId == 0 {
c, err := getMaxSortId()
if err != nil {
return err
}
sortId = int32(c)
}
model.SortId = sortId
model.BUse = 1
_, err := db.Insert(&model)
@ -58,44 +62,6 @@ func AddApp(appCode string, appName string, appUrl string, appIcon string, redir
return err
}
/**
REDIS
*/
func insertRedisCache(accessKey string, secreKey string, redirectUri string) error {
resp, err := http.PostForm("http://127.0.0.1/oauth2/AddClient",
url.Values{"access_key": {accessKey}, "secret_key": {secreKey}, "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
}
/**
*/
@ -112,7 +78,7 @@ func DelApp(appId string) error {
/**
*/
func UpdateApp(appId string, appCode string, appName string, appUrl string, appIcon string, redirectUri string, sortId int32) error {
func UpdateApp(appId string, appCode string, appName string, sortId int32) error {
model := new(models.TAppBase)
//修改REDIS缓存
_, err := db.Where("app_id = ?", appId).Get(&model)
@ -121,17 +87,70 @@ func UpdateApp(appId string, appCode string, appName string, appUrl string, appI
model.AppCode = appCode
model.AppName = appName
model.AppUrl = appUrl
model.AppIcon = appIcon
model.RedirectUri = redirectUri
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)
//删除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)
//删除REDIS缓存
deleteRedisCache(model.AccessKey)
model.RedirectUri = ""
model.LogoutUri = ""
_, err = db.ID(appId).Cols("redirect_uri", "logout_uri").Update(model)
//插入REDIS缓存
err = insertRedisCache(model.AccessKey, model.SecretKey, model.RedirectUri)
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
*/
@ -164,3 +183,54 @@ func ListApp(keyword string, page int, limit int) ([]map[string]interface{}, int
list, count, err := SqlKit.Query(sql)
return list, count, 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, secreKey string, redirectUri string) error {
resp, err := http.PostForm("http://127.0.0.1/oauth2/AddClient",
url.Values{"access_key": {accessKey}, "secret_key": {secreKey}, "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
}

Loading…
Cancel
Save