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.
213 lines
4.2 KiB
Go
213 lines
4.2 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"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
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 !errors.Is(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 OpenVip(ctx *gin.Context) {
|
|
uid, ok1 := jwt.Uid(ctx)
|
|
m, ok2 := httpx.DecodeUrlVal(ctx, "month")
|
|
month, err := strconv.Atoi(m)
|
|
if !ok1 || !ok2 || err != nil {
|
|
return
|
|
}
|
|
|
|
err = OperateSync(uid, func(u types.RWUser) {
|
|
u.OpenVip(time.Duration(month) * time.Hour * 24 * 30)
|
|
})
|
|
if err != nil {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
|
|
return
|
|
}
|
|
httpx.PkgMsgWrite(ctx, nil)
|
|
}
|
|
|
|
func Auth(ctx *gin.Context, ty apity.ApiType) bool {
|
|
uid, ok1 := httpx.DecodeUrlVal(ctx, "uid")
|
|
secret, ok2 := httpx.DecodeUrlVal(ctx, "secret")
|
|
if !ok1 || !ok2 {
|
|
return false
|
|
}
|
|
var opened bool
|
|
err := OperateSync(uid, func(u types.RWUser) {
|
|
if u.Secret() != secret {
|
|
return
|
|
}
|
|
|
|
opened = u.ApiCanReq(ty)
|
|
if opened && !u.IsVip() {
|
|
u.SetScore(u.Score() - 1)
|
|
}
|
|
})
|
|
if err != nil {
|
|
httpx.ErrInterrupt(ctx, errcode.WithErrcode(-1, err))
|
|
return false
|
|
}
|
|
if !opened {
|
|
httpx.ErrInterrupt(ctx, errcode.VerifyErr)
|
|
}
|
|
return opened
|
|
}
|