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, appUrl string, appIcon string, redirectUri 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 model.AppUrl = appUrl model.AppIcon = appIcon model.RedirectUri = redirectUri 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 } /** 功能:插入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 } /** 功能:删除一个接入系统 */ func DelApp(appId string) error { model := new(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, appUrl string, appIcon string, redirectUri 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.AppUrl = appUrl model.AppIcon = appIcon model.RedirectUri = redirectUri model.SortId = sortId _, err = db.ID(appId).Update(model) //插入REDIS缓存 err = insertRedisCache(model.AccessKey, model.SecretKey, model.RedirectUri) 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 }