Files
jx-print/services/print_server/app.go
邹宗楠 ecdb628231 打印机
2022-07-11 16:43:16 +08:00

75 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package print_server
import (
"fmt"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-print/dao"
"git.rosy.net.cn/jx-print/globals"
"git.rosy.net.cn/jx-print/model"
putils "git.rosy.net.cn/jx-print/putils"
"github.com/gin-gonic/gin"
"time"
)
func GetApps(c *gin.Context, userID string) (apps []*model.Apps, err error) {
if userID == "" {
return nil, fmt.Errorf("账号信息有误,请重新登录!")
}
return dao.GetApps(globals.GetDB(), 0, userID, "")
}
func AddApp(c *gin.Context, code, name, mobile, userID string) (err error) {
var (
db = globals.GetDB()
now = time.Now()
)
//if rcode := putils.GetKey(mobile); rcode == "" {
// putils.DelKey(mobile)
// return fmt.Errorf("验证码错误!")
//} else if code != rcode {
// putils.DelKey(mobile)
// return fmt.Errorf("验证码错误!")
//}
//putils.DelKey(mobile)
if apps, _ := dao.GetApps(db, 0, userID, ""); len(apps) > 2 {
return fmt.Errorf("同一个账号最多只能建3个app")
}
if apps, _ := dao.GetApps(db, 0, "", mobile); len(apps) > 0 {
return fmt.Errorf("同一个手机号只能建1个app")
}
apps := &model.Apps{
CreatedAt: &now,
UpdatedAt: &now,
DeletedAt: &utils.DefaultTimeValue,
Name: name,
Type: 0,
Status: 1,
AppKey: putils.RandStringBytes(16),
UserID: userID,
Mobile: mobile,
}
err = dao.Insert(db, apps)
return err
}
func DelApp(c *gin.Context, appID int, userID string) (err error) {
var (
db = globals.GetDB()
now = time.Now()
)
if appID == 0 {
return fmt.Errorf("参数错误appID :%v", appID)
}
if apps, err2 := dao.GetApps(db, appID, userID, ""); err2 != nil {
return err2
} else if len(apps) == 0 {
return fmt.Errorf("未查询到此应用app_id :%d, user_id: %s", appID, userID)
} else {
app := apps[0]
app.DeletedAt = &now
err = dao.Update(db, app, model.FieldDeletedAt)
}
return err
}