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.

112 lines
2.8 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"
"github.com/oklog/ulid"
"github.com/rs/xid"
"github.com/xormplus/builder"
"math/rand"
"strings"
"time"
)
var db = DbUtil.Engine
/**
功能:增加一个接入系统
*/
func AddApp(appCode string, appName string, appUrl string, appIcon string, redirectUri 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
model.AppUrl = appUrl
model.AppIcon = appIcon
model.RedirectUri = redirectUri
model.BUse = 1
_, err := db.Insert(&model)
//插入REDIS缓存
//TODO
return err
}
/**
功能:删除一个接入系统
*/
func DelApp(appId string) error {
model := new(models.TAppBase)
_, err := db.ID(appId).Delete(model)
//删除REDIS缓存
//RedisUtil.DEL("TJoinApp:" + appId)
return err
}
/**
功能:修改一个接入系统
*/
func UpdateApp(appId string, appCode string, appName string, appUrl string, appIcon string, redirectUri string) error {
model := new(models.TAppBase)
model.AppCode = appCode
model.AppName = appName
model.AppUrl = appUrl
model.AppIcon = appIcon
model.RedirectUri = redirectUri
_, err := db.ID(appId).Update(model)
//修改REDIS缓存
//TODO
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
}