feat:完善信息
This commit is contained in:
99
app/basic/api/base.go
Normal file
99
app/basic/api/base.go
Normal file
@ -0,0 +1,99 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/serve/logger"
|
||||
"SciencesServer/utils"
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
SuccessCode = 200 //成功的状态码
|
||||
FailureCode = 999 //失败的状态码
|
||||
)
|
||||
|
||||
type response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type ApiHandle func(c *gin.Context) interface{}
|
||||
|
||||
func GetSession() ApiHandle {
|
||||
return func(c *gin.Context) interface{} {
|
||||
value, _ := c.Get(config.TokenForSession)
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
func Bind(req interface{}) ApiHandle {
|
||||
return func(c *gin.Context) interface{} {
|
||||
var err error
|
||||
|
||||
if c.Request.Method == "GET" {
|
||||
err = c.ShouldBindQuery(req)
|
||||
} else {
|
||||
err = c.ShouldBindBodyWith(req, binding.JSON)
|
||||
}
|
||||
if err != nil {
|
||||
logger.ErrorF("Request URL【%s】Params Bind Error:%v", c.Request.URL, err)
|
||||
return errors.New("参数异常/缺失")
|
||||
}
|
||||
c.Set("params", utils.AnyToJSON(req))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func APISuccess(data ...interface{}) ApiHandle {
|
||||
return func(c *gin.Context) interface{} {
|
||||
resp := &response{
|
||||
Code: SuccessCode,
|
||||
Message: "ok",
|
||||
}
|
||||
if len(data) > 0 {
|
||||
resp.Data = data[0]
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func APIFailure(err error) ApiHandle {
|
||||
return func(c *gin.Context) interface{} {
|
||||
resp := &response{
|
||||
Code: FailureCode,
|
||||
Message: "failure",
|
||||
}
|
||||
if err != nil {
|
||||
resp.Message = err.Error()
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
c.Abort()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func APIResponse(err error, data ...interface{}) ApiHandle {
|
||||
return func(c *gin.Context) interface{} {
|
||||
resp := &response{
|
||||
Code: SuccessCode,
|
||||
Message: "ok",
|
||||
}
|
||||
if err != nil {
|
||||
resp.Code = FailureCode
|
||||
resp.Message = err.Error()
|
||||
c.JSON(http.StatusOK, resp)
|
||||
c.Abort()
|
||||
return nil
|
||||
}
|
||||
if len(data) > 0 {
|
||||
resp.Data = data[0]
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return nil
|
||||
}
|
||||
}
|
20
app/basic/api/sms.go
Normal file
20
app/basic/api/sms.go
Normal file
@ -0,0 +1,20 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"SciencesServer/app/basic/controller"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Sms struct{}
|
||||
|
||||
func (a *Sms) sCaptcha(c *gin.Context) {
|
||||
form := &struct {
|
||||
Mobile string `json:"mobile" form:"mobile" binding:"required"`
|
||||
}{}
|
||||
if err := Bind(form)(c); err != nil {
|
||||
APIFailure(err.(error))(c)
|
||||
return
|
||||
}
|
||||
err := controller.NewSms()().Captcha(form.Mobile)
|
||||
APIResponse(err)
|
||||
}
|
44
app/basic/controller/sms.go
Normal file
44
app/basic/controller/sms.go
Normal file
@ -0,0 +1,44 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"SciencesServer/app/handle"
|
||||
"SciencesServer/utils"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Sms struct{}
|
||||
|
||||
type SmsHandle func() *Sms
|
||||
|
||||
func (c *Sms) captchaCallback(mobile, code string) error {
|
||||
//content := fmt.Sprintf("【汇安科技】您的验证码是%s,三分钟内有效", code)
|
||||
// 发送短信
|
||||
//_ = platform.NewSms().Send()(platform.SmsSendModeForGroup, &platform.SmsParam{
|
||||
// Mobile: []string{mobile}, Content: content,
|
||||
//})
|
||||
// 执行保存数据库
|
||||
//mSysSmsLogs := model.NewSysSmsLogs()
|
||||
//mSysSmsLogs.Mobile = mobile
|
||||
//mSysSmsLogs.Content = content
|
||||
//mSysSmsLogs.Usage = model2.SysSmsLogsUsageForCaptcha
|
||||
//
|
||||
//return model2.Create(mSysSmsLogs.SysSmsLogs)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Captcha 验证码事件
|
||||
func (c *Sms) Captcha(mobile string) error {
|
||||
if !utils.ValidateMobile(mobile) {
|
||||
return errors.New("手机格式不正确")
|
||||
}
|
||||
if err := handle.NewCaptcha().Sms(6, mobile, c.captchaCallback); err != nil {
|
||||
return errors.New("操作错误")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewSms() SmsHandle {
|
||||
return func() *Sms {
|
||||
return &Sms{}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user