diff --git a/controllers/app_controller.go b/controllers/app_controller.go index aa51b98..538bea5 100644 --- a/controllers/app_controller.go +++ b/controllers/app_controller.go @@ -1,7 +1,6 @@ package controllers import ( - "fmt" "git.rosy.net.cn/jx-print/globals" "git.rosy.net.cn/jx-print/model" "git.rosy.net.cn/jx-print/services" @@ -40,16 +39,27 @@ func GetApps(c *gin.Context) { type AddAppParam struct { Code string `json:"code" form:"code"` //手机验证码 Mobile string `json:"mobile" form:"mobile"` //手机号 + Name string `json:"name" form:"name"` //应用名称 } //新建app POST func AddApp(c *gin.Context) { var ( + err error b, ok bool token string user *model.User + param = &AddAppParam{} ) globals.SugarLogger.Debugf("Begin API :%s params: %v ip: %s", c.Request.URL, c.Params, c.ClientIP()) + if err = c.Bind(¶m); err != nil { + c.JSON(http.StatusOK, &CallBack{ + Code: model.ErrCodeNormal, + Desc: err.Error(), + }) + globals.SugarLogger.Debugf("End API :%s error:%v:", c.Request.URL, err) + return + } if token, b = checkToken(c); !b { return } @@ -61,7 +71,7 @@ func AddApp(c *gin.Context) { return } if !callFunc(c, func() (retVal interface{}, errCode string, err error) { - fmt.Println(user) + err = services.AddApp(c, param.Code, param.Name, param.Mobile, user.UserID) return retVal, "", err }) { return diff --git a/dao/app_dao.go b/dao/app_dao.go index 14a96c3..4889d6f 100644 --- a/dao/app_dao.go +++ b/dao/app_dao.go @@ -5,7 +5,7 @@ import ( "github.com/jmoiron/sqlx" ) -func GetApps(db *sqlx.DB, userID string) (apps []*model.Apps, err error) { +func GetApps(db *sqlx.DB, userID, mobile string) (apps []*model.Apps, err error) { var sqlParams []interface{} sql := ` SELECT * @@ -16,6 +16,10 @@ func GetApps(db *sqlx.DB, userID string) (apps []*model.Apps, err error) { sql += " AND user_id = ?" sqlParams = append(sqlParams, userID) } + if mobile != "" { + sql += " AND mobile = ?" + sqlParams = append(sqlParams, mobile) + } if err = db.Select(&apps, sql, sqlParams...); err == nil { return apps, err } diff --git a/model/model.go b/model/model.go index fb0af1a..b75f1a4 100644 --- a/model/model.go +++ b/model/model.go @@ -80,4 +80,5 @@ type Apps struct { Type int `json:"type"` //应用类型 AppKey string `json:"app_key" db:"app_key"` //Key Status int `json:"status"` //状态 + Mobile string `json:"mobile"` //手机号 } diff --git a/services/app.go b/services/app.go index d8e62a2..174424c 100644 --- a/services/app.go +++ b/services/app.go @@ -2,10 +2,13 @@ package services 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/utils" "github.com/gin-gonic/gin" + "time" ) func GetApps(c *gin.Context, user *model.User) (apps []*model.Apps, err error) { @@ -15,5 +18,40 @@ func GetApps(c *gin.Context, user *model.User) (apps []*model.Apps, err error) { if user.UserID == "" { return nil, fmt.Errorf("账号信息有误,请重新登录!") } - return dao.GetApps(globals.GetDB(), user.UserID) + return dao.GetApps(globals.GetDB(), user.UserID, "") +} + +func AddApp(c *gin.Context, code, name, mobile, userID string) (err error) { + var ( + db = globals.GetDB() + now = time.Now() + ) + if rcode, ok := putils.GetKet(mobile).(string); !ok { + putils.DelKey(mobile) + return err + } else if code != rcode { + putils.DelKey(mobile) + return fmt.Errorf("验证码错误!") + } + putils.DelKey(mobile) + + if apps, _ := dao.GetApps(db, userID, ""); len(apps) > 2 { + return fmt.Errorf("同一个账号最多只能建3个app!") + } + if apps, _ := dao.GetApps(db, "", 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 } diff --git a/utils/utils.go b/utils/utils.go index 70d7df4..4c24e1a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -8,6 +8,7 @@ import ( "github.com/dchest/captcha" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" + "math/rand" "net/http" "strings" "time" @@ -28,6 +29,18 @@ func init() { uncommonInitialismsReplacer = strings.NewReplacer(uncommonInitialismsForReplacer...) } +var ( + letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" +) + +func RandStringBytes(n int) string { + b := make([]byte, n) + for i := range b { + b[i] = letterBytes[rand.Intn(len(letterBytes))] + } + return string(b) +} + func Captcha(c *gin.Context, length ...int) { l := captcha.DefaultLen w, h := 107, 36