You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
356 lines
10 KiB
356 lines
10 KiB
package influencer
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"time"
|
|
|
|
"pure/api/sys"
|
|
"pure/global"
|
|
"pure/middleware"
|
|
"pure/model"
|
|
"pure/model/request"
|
|
"pure/model/response"
|
|
"pure/service"
|
|
|
|
"pure/utils"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-redis/redis"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// Login
|
|
// @Summary 登录[v1.0.0]
|
|
// @Security Bearer
|
|
// @Description
|
|
// @Tags auth
|
|
// @Param data body request.UserLogin true "email,password..."
|
|
// @Success 200 {string} string "{"code": 0, "data": [...]}"
|
|
// @Success 200 {string} string "{"code": 1, "message": ""}"
|
|
// @Router /influencer/base/login [post]
|
|
func LoginInfluencer(c *gin.Context) {
|
|
var (
|
|
err error
|
|
l request.UserLogin
|
|
user *model.User
|
|
)
|
|
_ = c.ShouldBindJSON(&l)
|
|
if err := utils.Verify(l, utils.LoginVerify); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
if l.Type == "1" {
|
|
if err := utils.Verify(l, utils.LoginPhoneVerify); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
//校验手机号格式
|
|
if ok, _ := regexp.MatchString(utils.RegPhoneNumber, l.Phone); !ok {
|
|
response.FailWithMessage("手机号码格式不合法", c)
|
|
return
|
|
}
|
|
if l.CountryCode == "" {
|
|
l.CountryCode = "86"
|
|
}
|
|
} else if l.Type == "2" {
|
|
if err := utils.Verify(l, utils.LoginEmailVerify); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
//校验邮箱格式
|
|
if ok, _ := regexp.MatchString(utils.RegEmailNumber, l.Email); !ok {
|
|
response.FailWithMessage("邮箱格式不合法", c)
|
|
return
|
|
}
|
|
} else {
|
|
response.FailWithMessage("登录类型不合法", c)
|
|
return
|
|
}
|
|
if err, user = service.UserLogin(&l); err != nil {
|
|
global.MG_LOG.Error("Login failed! The user name does not exist or the password is wrong!", zap.Any("err", err))
|
|
fmt.Println(err)
|
|
response.FailWithMessage("The user name does not exist or the password is wrong", c)
|
|
return
|
|
}
|
|
// if user.IDForbidden {
|
|
// response.OkWithDetailed(map[string]interface{}{"id_forbidden": user.IDForbidden, "forbidden_time": user.ForbiddenTime.Unix(), "forbidden_reason": user.ForbiddenReason}, "The user forbidden", c)
|
|
// return
|
|
// }
|
|
tokenNext(c, *user)
|
|
}
|
|
|
|
// Register
|
|
// @Summary 注册[v1.0.0]
|
|
// @Security Bearer
|
|
// @Description
|
|
// @Tags auth
|
|
// @Param data body request.UserRegister true "email,password..."
|
|
// @Success 200 {string} string "{"code": 0, "data": "注册成功"}"
|
|
// @Success 200 {string} string "{"code": 1, "message": ""}"
|
|
// @Router /influencer/base/register [post]
|
|
func Register(c *gin.Context) {
|
|
var (
|
|
err error
|
|
l request.UserRegister
|
|
user *model.User
|
|
)
|
|
_ = c.ShouldBindJSON(&l)
|
|
if err := utils.Verify(l, utils.RegisterVerify); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
//校验邮箱格式
|
|
if ok, _ := regexp.MatchString(utils.RegEmailNumber, l.Email); !ok {
|
|
response.FailWithMessage("邮箱格式不合法", c)
|
|
return
|
|
}
|
|
if l.CountryCode == "" {
|
|
l.CountryCode = "86"
|
|
}
|
|
if err, user = service.UserRegister(&l); err != nil {
|
|
global.MG_LOG.Error("Register failed!", zap.Any("err", err))
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
tokenNext(c, *user)
|
|
}
|
|
|
|
// 登录以后签发jwt
|
|
func tokenNext(c *gin.Context, user model.User) {
|
|
j := &middleware.JWT{SigningKey: []byte(global.MG_CONFIG.JWT.SigningKey)} // 唯一签名
|
|
claims := request.UserClaims{
|
|
UUID: user.UUID.String(),
|
|
NickName: user.NickName,
|
|
Email: user.Email,
|
|
Appid: user.Appid,
|
|
Type: user.Type,
|
|
IDForbidden: user.IDForbidden,
|
|
BufferTime: global.MG_CONFIG.JWT.BufferTime, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
|
|
StandardClaims: jwt.StandardClaims{
|
|
NotBefore: time.Now().Unix() - 1000, // 签名生效时间
|
|
ExpiresAt: time.Now().Unix() + global.MG_CONFIG.JWT.ExpiresTime, // 过期时间 7天 配置文件
|
|
Issuer: "qmPlus", // 签名的发行者
|
|
},
|
|
}
|
|
token, err := j.CreateToken(claims)
|
|
if err != nil {
|
|
global.MG_LOG.Error("get token field!", zap.Any("err", err))
|
|
response.FailWithMessage("get token field", c)
|
|
return
|
|
}
|
|
if global.MG_CONFIG.System.UseMultipoint {
|
|
err, jwtStr := service.GetRedisJWT(user.Username)
|
|
if err == redis.Nil {
|
|
if err := service.SetRedisJWT(token, user.Username); err != nil {
|
|
global.MG_LOG.Error("set token failed!", zap.Any("err", err))
|
|
response.FailWithMessage("set token failed", c)
|
|
return
|
|
}
|
|
} else if err != nil {
|
|
global.MG_LOG.Error("get token failed!", zap.Any("err", err))
|
|
response.FailWithMessage("get token failed", c)
|
|
return
|
|
} else {
|
|
if err := service.JsonInBlacklist(model.JwtBlacklist{Jwt: jwtStr}); err != nil {
|
|
global.MG_LOG.Error("jwt作废失败!", zap.Any("err", err))
|
|
response.FailWithMessage("jwt作废失败", c)
|
|
return
|
|
}
|
|
if err := service.SetRedisJWT(token, user.Username); err != nil {
|
|
global.MG_LOG.Error("设置登录状态失败!", zap.Any("err", err))
|
|
response.FailWithMessage("设置登录状态失败", c)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
// if !global.MG_CONFIG.System.UseMultipoint {
|
|
response.OkWithDetailed(response.LoginResponse{
|
|
User: user,
|
|
Token: token,
|
|
ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
|
|
}, "success", c)
|
|
}
|
|
|
|
// @Summary 获取用户基本信息[v1.0.0]
|
|
// @Security Bearer
|
|
// @Description
|
|
// @Tags 网红端-用户
|
|
// @Success 200 {object} model.UserSimple "{"code": 0, "data": [...]}"
|
|
// @Success 200 {string} string "{"code": 1, "message": ""}"
|
|
// @Router /influencer/user/detail [get]
|
|
func GetUserDetail(c *gin.Context) {
|
|
var (
|
|
err error
|
|
userID string
|
|
data interface{}
|
|
)
|
|
userID = sys.GetUserUuid(c)
|
|
err, data = service.GetUserDetail(userID)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
response.OkWithData(data, c)
|
|
}
|
|
|
|
// @Summary 修改用户基本信息
|
|
// @Security Bearer
|
|
// @Description
|
|
// @Tags 网红端-用户
|
|
// @Param data body request.UserDetail false "data..."
|
|
// @Success 200 {string} string "{"code": 0, "data": [...]}"
|
|
// @Success 200 {string} string "{"code": 1, "message": ""}"
|
|
// @Router /influencer/user/detail [put]
|
|
func UpdateUserDetail(c *gin.Context) {
|
|
var (
|
|
err error
|
|
userID string
|
|
data request.UserDetail
|
|
)
|
|
err = c.ShouldBindJSON(&data)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
userID = sys.GetUserUuid(c)
|
|
err = service.UpdateUserDetail(userID, &data)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
response.OkWithMessage("修改成功", c)
|
|
}
|
|
|
|
// BandPhone
|
|
// @Summary 网红绑定手机[v1.0.0]
|
|
// @Security Bearer
|
|
// @Description
|
|
// @Tags 网红端-用户
|
|
// @Param data body request.UserBandPhone true "email,password..."
|
|
// @Success 200 {string} string "{"code": 0, "data": "绑定成功"}"
|
|
// @Success 200 {string} string "{"code": 1, "message": ""}"
|
|
// @Router /influencer/user/bandPhone [post]
|
|
func BandPhone(c *gin.Context) {
|
|
var (
|
|
err error
|
|
l request.UserBandPhone
|
|
userID string
|
|
)
|
|
_ = c.ShouldBindJSON(&l)
|
|
if err := utils.Verify(l, utils.BandPhoneVerify); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
userID = sys.GetUserUuid(c)
|
|
if err = service.UserBandPhone(&l, sys.GetUserAppid(c), userID); err != nil {
|
|
global.MG_LOG.Error("BandPhone failed!", zap.Any("err", err))
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
response.OkWithMessage("绑定成功", c)
|
|
}
|
|
|
|
// PlatformAuth
|
|
// @Summary 平台认证[v1.0.0]
|
|
// @Security Bearer
|
|
// @Description
|
|
// @Tags 网红端-用户
|
|
// @Param data body request.UserPlatformAuth true "email,password..."
|
|
// @Success 200 {string} string "{"code": 0, "data": "绑定成功"}"
|
|
// @Success 200 {string} string "{"code": 1, "message": ""}"
|
|
// @Router /influencer/user/platformAuth [post]
|
|
func PlatformAuth(c *gin.Context) {
|
|
var (
|
|
err error
|
|
l request.UserPlatformAuth
|
|
userID string
|
|
)
|
|
err = c.ShouldBindJSON(&l)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
if err := utils.Verify(l, utils.PlatformAuthVerify); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
userID = sys.GetUserUuid(c)
|
|
if err = service.UserPlatformAuth(&l, userID); err != nil {
|
|
global.MG_LOG.Error("PlatformAuth failed!", zap.Any("err", err))
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
response.OkWithMessage("提交成功", c)
|
|
}
|
|
|
|
// PlatformAuth
|
|
// @Summary 授权登录[v1.0.0]
|
|
// @Security Bearer
|
|
// @Description
|
|
// @Tags auth
|
|
// @Param data body request.UserAuthorized true "email,password..."
|
|
// @Success 200 {string} string "{"code": 0, "data": "绑定成功"}"
|
|
// @Success 200 {string} string "{"code": 1, "message": ""}"
|
|
// @Router /base/authorized [post]
|
|
func Authorized(c *gin.Context) {
|
|
var (
|
|
err error
|
|
l request.UserAuthorized
|
|
user model.User
|
|
)
|
|
_ = c.ShouldBindJSON(&l)
|
|
if user, err = service.UserAuthorized(&l); err != nil {
|
|
global.MG_LOG.Error("Authorized failed!", zap.Any("err", err))
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
tokenNext(c, user)
|
|
}
|
|
|
|
// @Summary 获取用户统计信息[v1.0.0]
|
|
// @Security Bearer
|
|
// @Description
|
|
// @Tags 网红端-用户
|
|
// @Success 200 {object} response.UserStatistics "{"code": 0, "data": [...]}"
|
|
// @Router /influencer/user/statistics [get]
|
|
func GetUserStatistics(c *gin.Context) {
|
|
var (
|
|
err error
|
|
userID string
|
|
data interface{}
|
|
)
|
|
userID = sys.GetUserUuid(c)
|
|
err, data = service.GetUserStatistics(userID)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
response.OkWithData(data, c)
|
|
}
|
|
|
|
// @Summary 注销账户[v1.0.1]
|
|
// @Security Bearer
|
|
// @Description
|
|
// @Tags 网红端-用户
|
|
// @Success 200 {string} string "{"code": 0, "data": "注销成功"}"
|
|
// @Router /influencer/user/logoff [post]
|
|
func UserLogOff(c *gin.Context) {
|
|
var (
|
|
err error
|
|
userID string
|
|
)
|
|
userID = sys.GetUserUuid(c)
|
|
err = service.UserLogOff(userID)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
//token加入黑名单
|
|
token := c.Request.Header.Get("x-token")
|
|
if err := service.JsonInBlacklist(model.JwtBlacklist{Jwt: token}); err != nil {
|
|
global.MG_LOG.Error("jwt作废失败!", zap.Any("err", err))
|
|
response.FailWithMessage("jwt作废失败", c)
|
|
return
|
|
}
|
|
response.OkWithMessage("注销成功", c)
|
|
}
|
|
|