| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package appApi
- import (
- "MeterService/core/config"
- "MeterService/core/db"
- "strconv"
- )
- type DbAppApi struct {
- Db *db.MyDB
- }
- func (m *DbAppApi) GetList() ([]interface{}, error) {
- array := make([]interface{}, 0)
- arr, ok := m.Db.Query("SELECT api.app_id as AppId, api.type as Type, api.url as Url,app.host as Host FROM t_app_info AS app LEFT JOIN t_app_api_info AS api ON app.app_id = api.app_id WHERE app.is_delete = 0 AND api.app_id IS NOT NULL")
- if ok && arr != nil && len(arr) > 0 {
- for _, v := range arr {
- appId, _ := strconv.Atoi(v["AppId"])
- value := &AppApi{
- AppId: appId,
- Type: v["Type"],
- Host: v["Host"],
- Url: v["Url"],
- }
- array = append(array, value)
- }
- }
- return array, nil
- }
- func (m *DbAppApi) AddOrUpdate(v interface{}) error {
- d := v.(*App)
- if d.AppId == 0 {
- if _, err := m.Db.Exec("INSERT INTO t_app_info(app_secret,app_name, host,isDelete) VALUES(?,?,?,?)", d.AppSecret, d.AppName, d.Host, 0); err != nil {
- return err
- }
- } else {
- if _, err := m.Db.Exec("UPDATE t_app_info SET app_secret=?,app_name=?,host=?,isDelete=? WHERE app_id=?", d.AppSecret, d.AppName, d.Host, d.GetDelete(), d.AppId); err != nil {
- return err
- }
- if _, err := m.Db.Exec("DELETE FROM t_app_api_info WHERE app_id=?", d.AppId); err != nil {
- return err
- }
- }
- if len(d.Apis) > 0 {
- for _, v := range d.Apis {
- _, err := m.Db.Exec("INSERT INTO t_app_api_info(app_id, url, type) VALUES(?,?,?)", d.AppId, v.Url, v.Type)
- if err != nil {
- return err
- }
- }
- }
- return nil
- }
- func (m *DbAppApi) Delete(v interface{}) error {
- d := v.(*App)
- _, err := m.Db.Exec("UPDATE t_app_info SET is_delete=? WHERE app_id=?", 1, d.AppId)
- return err
- }
- func (m *DbAppApi) CheckSecret(d *App) bool {
- if config.C.Vber.Mode == "debug" && d.AppSecret == "" {
- return true
- }
- appId := d.AppId
- if arr, ok := m.Db.Query("SELECT app_id, app_secret as AppSecret FROM t_app_info WHERE app_id=? AND is_delete = 0", appId); !ok {
- return false
- } else {
- if len(arr) != 1 {
- return false
- }
- for _, v := range arr {
- if v["AppSecret"] == d.AppSecret {
- return true
- }
- }
- return false
- }
- }
- func NewAppApiDb() *DbAppApi {
- return &DbAppApi{
- Db: db.GetDb(),
- }
- }
|