|
|
|
/**
|
|
|
|
* @Author: jager
|
|
|
|
* @Email: lhj168os@gmail.com
|
|
|
|
* @File: handles
|
|
|
|
* @Date: 2021/12/31 4:43 下午
|
|
|
|
* @package: dao
|
|
|
|
* @Version: v1.0.0
|
|
|
|
*
|
|
|
|
* @Description:
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/jageros/hawox/errcode"
|
|
|
|
"github.com/jageros/hawox/httpx"
|
|
|
|
"github.com/jageros/hawox/jwt"
|
|
|
|
"github.com/jageros/hawox/logx"
|
|
|
|
"github.com/jageros/hawox/uuid"
|
|
|
|
"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)
|
|
|
|
|
|
|
|
logx.Infof("phone=%s pwd=%s", phone, password)
|
|
|
|
|
|
|
|
_, 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) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Info(ctx *gin.Context) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|