add app
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"git.rosy.net.cn/jx-print/globals"
|
"git.rosy.net.cn/jx-print/globals"
|
||||||
"git.rosy.net.cn/jx-print/model"
|
"git.rosy.net.cn/jx-print/model"
|
||||||
"git.rosy.net.cn/jx-print/services"
|
"git.rosy.net.cn/jx-print/services"
|
||||||
@@ -40,16 +39,27 @@ func GetApps(c *gin.Context) {
|
|||||||
type AddAppParam struct {
|
type AddAppParam struct {
|
||||||
Code string `json:"code" form:"code"` //手机验证码
|
Code string `json:"code" form:"code"` //手机验证码
|
||||||
Mobile string `json:"mobile" form:"mobile"` //手机号
|
Mobile string `json:"mobile" form:"mobile"` //手机号
|
||||||
|
Name string `json:"name" form:"name"` //应用名称
|
||||||
}
|
}
|
||||||
|
|
||||||
//新建app POST
|
//新建app POST
|
||||||
func AddApp(c *gin.Context) {
|
func AddApp(c *gin.Context) {
|
||||||
var (
|
var (
|
||||||
|
err error
|
||||||
b, ok bool
|
b, ok bool
|
||||||
token string
|
token string
|
||||||
user *model.User
|
user *model.User
|
||||||
|
param = &AddAppParam{}
|
||||||
)
|
)
|
||||||
globals.SugarLogger.Debugf("Begin API :%s params: %v ip: %s", c.Request.URL, c.Params, c.ClientIP())
|
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 {
|
if token, b = checkToken(c); !b {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -61,7 +71,7 @@ func AddApp(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !callFunc(c, func() (retVal interface{}, errCode string, err error) {
|
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 retVal, "", err
|
||||||
}) {
|
}) {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"github.com/jmoiron/sqlx"
|
"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{}
|
var sqlParams []interface{}
|
||||||
sql := `
|
sql := `
|
||||||
SELECT *
|
SELECT *
|
||||||
@@ -16,6 +16,10 @@ func GetApps(db *sqlx.DB, userID string) (apps []*model.Apps, err error) {
|
|||||||
sql += " AND user_id = ?"
|
sql += " AND user_id = ?"
|
||||||
sqlParams = append(sqlParams, userID)
|
sqlParams = append(sqlParams, userID)
|
||||||
}
|
}
|
||||||
|
if mobile != "" {
|
||||||
|
sql += " AND mobile = ?"
|
||||||
|
sqlParams = append(sqlParams, mobile)
|
||||||
|
}
|
||||||
if err = db.Select(&apps, sql, sqlParams...); err == nil {
|
if err = db.Select(&apps, sql, sqlParams...); err == nil {
|
||||||
return apps, err
|
return apps, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,4 +80,5 @@ type Apps struct {
|
|||||||
Type int `json:"type"` //应用类型
|
Type int `json:"type"` //应用类型
|
||||||
AppKey string `json:"app_key" db:"app_key"` //Key
|
AppKey string `json:"app_key" db:"app_key"` //Key
|
||||||
Status int `json:"status"` //状态
|
Status int `json:"status"` //状态
|
||||||
|
Mobile string `json:"mobile"` //手机号
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,13 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
"git.rosy.net.cn/jx-print/dao"
|
"git.rosy.net.cn/jx-print/dao"
|
||||||
"git.rosy.net.cn/jx-print/globals"
|
"git.rosy.net.cn/jx-print/globals"
|
||||||
"git.rosy.net.cn/jx-print/model"
|
"git.rosy.net.cn/jx-print/model"
|
||||||
|
putils "git.rosy.net.cn/jx-print/utils"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetApps(c *gin.Context, user *model.User) (apps []*model.Apps, err error) {
|
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 == "" {
|
if user.UserID == "" {
|
||||||
return nil, fmt.Errorf("账号信息有误,请重新登录!")
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/dchest/captcha"
|
"github.com/dchest/captcha"
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -28,6 +29,18 @@ func init() {
|
|||||||
uncommonInitialismsReplacer = strings.NewReplacer(uncommonInitialismsForReplacer...)
|
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) {
|
func Captcha(c *gin.Context, length ...int) {
|
||||||
l := captcha.DefaultLen
|
l := captcha.DefaultLen
|
||||||
w, h := 107, 36
|
w, h := 107, 36
|
||||||
|
|||||||
Reference in New Issue
Block a user