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+SK
ak := xid.New() ak := xid.New()
appKey := ak.String() //新增就生成一个secret appKey := ak.String() //新增就生成一个secret
@ -44,9 +44,13 @@ func AddApp(appCode string, appName string, appUrl string, appIcon string, redir
model.AppName = appName model.AppName = appName
model.AccessKey = appKey model.AccessKey = appKey
model.SecretKey = appSecret model.SecretKey = appSecret
model.AppUrl = appUrl if sortId == 0 {
model.AppIcon = appIcon c, err := getMaxSortId()
model.RedirectUri = redirectUri if err != nil {
return err
}
sortId = int32(c)
}
model.SortId = sortId model.SortId = sortId
model.BUse = 1 model.BUse = 1
_, err := db.Insert(&model) _, err := db.Insert(&model)
@ -59,76 +63,91 @@ func AddApp(appCode string, appName string, appUrl string, appIcon string, redir
} }
/** /**
REDIS
*/ */
func insertRedisCache(accessKey string, secreKey string, redirectUri string) error { func DelApp(appId string) error {
resp, err := http.PostForm("http://127.0.0.1/oauth2/AddClient", model := new(models.TAppBase)
url.Values{"access_key": {accessKey}, "secret_key": {secreKey}, "redirect_uri": {redirectUri}}) _, err := db.Where("app_id = ?", appId).Get(&model)
if err != nil { //删除REDIS缓存
return err deleteRedisCache(model.AccessKey)
} //删除物理记录
_, err = db.ID(appId).Delete(model)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err return err
}
fmt.Println(string(body))
return nil
} }
/** /**
REDIS
*/ */
func deleteRedisCache(accessKey string) error { func UpdateApp(appId string, appCode string, appName string, sortId int32) error {
//插入REDIS缓存 model := new(models.TAppBase)
resp, err := http.PostForm("http://127.0.0.1/oauth2/DelClient", //修改REDIS缓存
url.Values{"access_key": {accessKey}}) _, err := db.Where("app_id = ?", appId).Get(&model)
if err != nil { //删除REDIS缓存
deleteRedisCache(model.AccessKey)
model.AppCode = appCode
model.AppName = appName
model.SortId = sortId
_, err = db.ID(appId).Update(model)
return err return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
fmt.Println(string(body))
return nil
} }
/** /**
*/ */
func DelApp(appId string) error { func UpdateSso(appId string, redirectUri string, logoutUri string) error {
model := new(models.TAppBase) model := new(models.TAppBase)
//修改REDIS缓存
_, err := db.Where("app_id = ?", appId).Get(&model) _, err := db.Where("app_id = ?", appId).Get(&model)
//删除REDIS缓存 //删除REDIS缓存
deleteRedisCache(model.AccessKey) deleteRedisCache(model.AccessKey)
//删除物理记录 model.RedirectUri = redirectUri
_, err = db.ID(appId).Delete(model) model.LogoutUri = logoutUri
_, err = db.ID(appId).Update(model)
//插入REDIS缓存
err = insertRedisCache(model.AccessKey, model.SecretKey, model.RedirectUri)
return err return err
} }
/** /**
*/ */
func UpdateApp(appId string, appCode string, appName string, appUrl string, appIcon string, redirectUri string, sortId int32) error { func ClearSso(appId string) error {
model := new(models.TAppBase) model := new(models.TAppBase)
//修改REDIS缓存 //修改REDIS缓存
_, err := db.Where("app_id = ?", appId).Get(&model) _, err := db.Where("app_id = ?", appId).Get(&model)
//删除REDIS缓存 //删除REDIS缓存
deleteRedisCache(model.AccessKey) 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
}
model.AppCode = appCode /**
model.AppName = appName
*/
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.AppUrl = appUrl
model.AppIcon = appIcon model.AppIcon = appIcon
model.RedirectUri = redirectUri
model.SortId = sortId
_, err = db.ID(appId).Update(model) _, err = db.ID(appId).Update(model)
return err
}
//插入REDIS缓存 /**
err = insertRedisCache(model.AccessKey, model.SecretKey, model.RedirectUri)
*/
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 return err
} }
@ -164,3 +183,54 @@ func ListApp(keyword string, page int, limit int) ([]map[string]interface{}, int
list, count, err := SqlKit.Query(sql) list, count, err := SqlKit.Query(sql)
return list, count, err 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