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

3 years ago
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: handles
* @Date: 2021/12/31 4:43
* @package: dao
* @Version: v1.0.0
*
* @Description:
*
*/
package user
import (
3 years ago
"errors"
3 years ago
"github.com/gin-gonic/gin"
3 years ago
"github.com/jageros/hawox/errcode"
"github.com/jageros/hawox/httpx"
"github.com/jageros/hawox/jwt"
"github.com/jageros/hawox/uuid"
3 years ago
"idata/internal/apity"
3 years ago
"idata/internal/service/user/dao"
3 years ago
"idata/internal/types"
3 years ago
"idata/internal/utils"
"strconv"
"time"
3 years ago
)
func Register(ctx *gin.Context) {
3 years ago
arg, ok := httpx.BindJsonArgs(ctx)
if !ok {
return
}
3 years ago
3 years ago
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
}
10 months ago
if !errors.Is(err, dao.ErrNotFound) {
3 years ago
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})
3 years ago
}
func Login(ctx *gin.Context) {
3 years ago
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
}
3 years ago
3 years ago
httpx.PkgMsgWrite(ctx, map[string]interface{}{"uid": uid, "token": token})
3 years ago
}
func Info(ctx *gin.Context) {
3 years ago
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
}
3 years ago
3 years ago
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)
3 years ago
}
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)
3 years ago
}
})
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(-1, err))
return false
}
if !opened {
httpx.ErrInterrupt(ctx, errcode.VerifyErr)
}
return opened
3 years ago
}