You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
3.4 KiB
Go
176 lines
3.4 KiB
Go
/**
|
|
* @Author: jager
|
|
* @Email: lhj168os@gmail.com
|
|
* @File: handles
|
|
* @Date: 2021/12/31 4:43 下午
|
|
* @package: dao
|
|
* @Version: v1.0.0
|
|
*
|
|
* @Description:
|
|
*
|
|
*/
|
|
|
|
package user
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jageros/hawox/errcode"
|
|
"github.com/jageros/hawox/httpx"
|
|
"github.com/jageros/hawox/jwt"
|
|
"github.com/jageros/hawox/uuid"
|
|
"idata/internal/apity"
|
|
"idata/internal/service/user/dao"
|
|
"idata/internal/types"
|
|
"idata/internal/utils"
|
|
)
|
|
|
|
func Register(ctx *gin.Context) {
|
|
arg, ok := httpx.BindJsonArgs(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
ph, ok1 := arg["phone"]
|
|
pwd, ok2 := arg["password"]
|
|
if !ok1 || !ok2 {
|
|
httpx.ErrInterrupt(ctx, errcode.InvalidParam)
|
|
return
|
|
}
|
|
phone := ph.(string)
|
|
password := pwd.(string)
|
|
|
|
_, err := userModel.FindOneByPhone(phone)
|
|
if err == nil {
|
|
httpx.ErrInterrupt(ctx, errcode.New(1, "phone has registry"))
|
|
return
|
|
}
|
|
if err != dao.ErrNotFound {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(2, err))
|
|
return
|
|
}
|
|
|
|
uid, err := uuid.NewRandNumStr("user")
|
|
if err != nil {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(3, err))
|
|
return
|
|
}
|
|
|
|
err = RegistryUser(uid, func(u types.RWUser) {
|
|
u.SetPhone(phone)
|
|
u.SetSecret(utils.GenSecret(uid))
|
|
u.SetPassword(password)
|
|
})
|
|
if err != nil {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(4, err))
|
|
return
|
|
}
|
|
token, err := jwt.GenerateToken(uid)
|
|
if err != nil {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(5, err))
|
|
return
|
|
}
|
|
httpx.PkgMsgWrite(ctx, map[string]interface{}{"uid": uid, "token": token})
|
|
}
|
|
|
|
func Login(ctx *gin.Context) {
|
|
arg, ok := httpx.BindJsonArgs(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
ph, ok1 := arg["phone"]
|
|
pwd, ok2 := arg["password"]
|
|
if !ok1 || !ok2 {
|
|
httpx.ErrInterrupt(ctx, errcode.InvalidParam)
|
|
return
|
|
}
|
|
phone := ph.(string)
|
|
password := pwd.(string)
|
|
var verify bool
|
|
var uid string
|
|
err := OperateByPhone(phone, func(u types.RUser) {
|
|
verify = u.VerifyPwd(password)
|
|
uid = u.Uid()
|
|
})
|
|
|
|
if err != nil {
|
|
if err == dao.ErrNotFound {
|
|
err = errors.New("用户未注册")
|
|
}
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
|
|
return
|
|
}
|
|
if !verify {
|
|
httpx.ErrInterrupt(ctx, errcode.VerifyErr)
|
|
return
|
|
}
|
|
|
|
token, err := jwt.GenerateToken(uid)
|
|
if err != nil {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(2, err))
|
|
return
|
|
}
|
|
|
|
httpx.PkgMsgWrite(ctx, map[string]interface{}{"uid": uid, "token": token})
|
|
}
|
|
|
|
func Info(ctx *gin.Context) {
|
|
uid, ok := jwt.Uid(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var data interface{}
|
|
err := Operate(uid, func(u types.RUser) {
|
|
data = u.Info()
|
|
})
|
|
if err != nil {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
|
|
return
|
|
}
|
|
httpx.PkgMsgWrite(ctx, data)
|
|
}
|
|
|
|
func Secret(ctx *gin.Context) {
|
|
uid, ok := jwt.Uid(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var secret string
|
|
err := Operate(uid, func(u types.RUser) {
|
|
secret = u.Secret()
|
|
})
|
|
if err != nil {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
|
|
return
|
|
}
|
|
httpx.PkgMsgWrite(ctx, map[string]interface{}{"secret": secret})
|
|
}
|
|
|
|
func OpenApi(ctx *gin.Context) {
|
|
uid, ok1 := jwt.Uid(ctx)
|
|
apiType, ok2 := httpx.DecodeUrlVal(ctx, "api_type")
|
|
if !ok1 || !ok2 {
|
|
return
|
|
}
|
|
|
|
err := OperateSync(uid, func(u types.RWUser) {
|
|
u.OpenApi(apity.Type(apiType))
|
|
})
|
|
if err != nil {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
|
|
return
|
|
}
|
|
httpx.PkgMsgWrite(ctx, nil)
|
|
}
|
|
|
|
func Auth(uid, secret string) bool {
|
|
var ok bool
|
|
err := Operate(uid, func(u types.RUser) {
|
|
if u.Secret() == secret {
|
|
ok = true
|
|
}
|
|
})
|
|
return err == nil && ok
|
|
}
|