mirror of https://github.com/rocboss/paopao-ce
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.
489 lines
12 KiB
489 lines
12 KiB
2 years ago
|
// Copyright 2022 ROC. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package broker
|
||
3 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"regexp"
|
||
|
"strings"
|
||
|
"time"
|
||
|
"unicode/utf8"
|
||
|
|
||
3 years ago
|
"github.com/gin-gonic/gin"
|
||
3 years ago
|
"github.com/gofrs/uuid"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/conf"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
3 years ago
|
"github.com/rocboss/paopao-ce/pkg/convert"
|
||
|
"github.com/rocboss/paopao-ce/pkg/errcode"
|
||
|
"github.com/rocboss/paopao-ce/pkg/util"
|
||
2 years ago
|
"github.com/sirupsen/logrus"
|
||
3 years ago
|
)
|
||
|
|
||
2 years ago
|
const _MaxCaptchaTimes = 2
|
||
3 years ago
|
|
||
|
type PhoneCaptchaReq struct {
|
||
|
Phone string `json:"phone" form:"phone" binding:"required"`
|
||
|
ImgCaptcha string `json:"img_captcha" form:"img_captcha" binding:"required"`
|
||
|
ImgCaptchaID string `json:"img_captcha_id" form:"img_captcha_id" binding:"required"`
|
||
|
}
|
||
|
|
||
|
type UserPhoneBindReq struct {
|
||
|
Phone string `json:"phone" form:"phone" binding:"required"`
|
||
|
Captcha string `json:"captcha" form:"captcha" binding:"required"`
|
||
|
}
|
||
|
|
||
|
type AuthRequest struct {
|
||
|
Username string `json:"username" form:"username" binding:"required"`
|
||
|
Password string `json:"password" form:"password" binding:"required"`
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
type RegisterRequest struct {
|
||
|
Username string `json:"username" form:"username" binding:"required"`
|
||
|
Password string `json:"password" form:"password" binding:"required"`
|
||
|
}
|
||
|
|
||
|
type ChangePasswordReq struct {
|
||
|
Password string `json:"password" form:"password" binding:"required"`
|
||
|
OldPassword string `json:"old_password" form:"old_password" binding:"required"`
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
type ChangeNicknameReq struct {
|
||
|
Nickname string `json:"nickname" form:"nickname" binding:"required"`
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
type ChangeAvatarReq struct {
|
||
|
Avatar string `json:"avatar" form:"avatar" binding:"required"`
|
||
|
}
|
||
|
|
||
2 years ago
|
type ChangeUserStatusReq struct {
|
||
|
ID int64 `json:"id" form:"id" binding:"required"`
|
||
|
Status int `json:"status" form:"status" binding:"required"`
|
||
|
}
|
||
|
|
||
2 years ago
|
type RequestingFriendReq struct {
|
||
|
UserId int64 `json:"user_id" binding:"required"`
|
||
|
Greetings string `json:"greetings" binding:"required"`
|
||
|
}
|
||
|
|
||
|
type AddFriendReq struct {
|
||
|
UserId int64 `json:"user_id" binding:"required"`
|
||
|
}
|
||
|
|
||
|
type RejectFriendReq struct {
|
||
|
UserId int64 `json:"user_id" binding:"required"`
|
||
|
}
|
||
|
|
||
|
type DeleteFriendReq struct {
|
||
|
UserId int64 `json:"user_id"`
|
||
|
}
|
||
|
|
||
|
type UserProfileResp struct {
|
||
|
ID int64 `json:"id"`
|
||
|
Nickname string `json:"nickname"`
|
||
|
Username string `json:"username"`
|
||
|
Status int `json:"status"`
|
||
|
Avatar string `json:"avatar"`
|
||
|
IsAdmin bool `json:"is_admin"`
|
||
|
IsFriend bool `json:"is_friend"`
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
_LoginErrKey = "PaoPaoUserLoginErr"
|
||
|
_MaxLoginErrTimes = 10
|
||
|
)
|
||
3 years ago
|
|
||
3 years ago
|
// DoLogin 用户认证
|
||
2 years ago
|
func DoLogin(ctx *gin.Context, param *AuthRequest) (*core.User, error) {
|
||
2 years ago
|
user, err := ds.GetUserByUsername(param.Username)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, errcode.UnauthorizedAuthNotExist
|
||
|
}
|
||
|
|
||
|
if user.Model != nil && user.ID > 0 {
|
||
2 years ago
|
if errTimes, err := conf.Redis.Get(ctx, fmt.Sprintf("%s:%d", _LoginErrKey, user.ID)).Result(); err == nil {
|
||
|
if convert.StrTo(errTimes).MustInt() >= _MaxLoginErrTimes {
|
||
3 years ago
|
return nil, errcode.TooManyLoginError
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 对比密码是否正确
|
||
3 years ago
|
if ValidPassword(user.Password, param.Password, user.Salt) {
|
||
3 years ago
|
|
||
2 years ago
|
if user.Status == core.UserStatusClosed {
|
||
3 years ago
|
return nil, errcode.UserHasBeenBanned
|
||
|
}
|
||
|
|
||
|
// 清空登录计数
|
||
2 years ago
|
conf.Redis.Del(ctx, fmt.Sprintf("%s:%d", _LoginErrKey, user.ID))
|
||
3 years ago
|
return user, nil
|
||
|
}
|
||
|
|
||
|
// 登录错误计数
|
||
2 years ago
|
_, err = conf.Redis.Incr(ctx, fmt.Sprintf("%s:%d", _LoginErrKey, user.ID)).Result()
|
||
3 years ago
|
if err == nil {
|
||
2 years ago
|
conf.Redis.Expire(ctx, fmt.Sprintf("%s:%d", _LoginErrKey, user.ID), time.Hour).Result()
|
||
3 years ago
|
}
|
||
|
|
||
|
return nil, errcode.UnauthorizedAuthFailed
|
||
|
}
|
||
|
|
||
|
return nil, errcode.UnauthorizedAuthNotExist
|
||
|
}
|
||
|
|
||
3 years ago
|
// ValidPassword 检查密码是否一致
|
||
|
func ValidPassword(dbPassword, password, salt string) bool {
|
||
3 years ago
|
return strings.Compare(dbPassword, util.EncodeMD5(util.EncodeMD5(password)+salt)) == 0
|
||
|
}
|
||
|
|
||
3 years ago
|
// CheckStatus 检测用户权限
|
||
2 years ago
|
func CheckStatus(user *core.User) bool {
|
||
|
return user.Status == core.UserStatusNormal
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// ValidUsername 验证用户
|
||
|
func ValidUsername(username string) error {
|
||
3 years ago
|
// 检测用户是否合规
|
||
|
if utf8.RuneCountInString(username) < 3 || utf8.RuneCountInString(username) > 12 {
|
||
|
return errcode.UsernameLengthLimit
|
||
|
}
|
||
|
|
||
|
if !regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(username) {
|
||
|
return errcode.UsernameCharLimit
|
||
|
}
|
||
|
|
||
|
// 重复检查
|
||
2 years ago
|
user, _ := ds.GetUserByUsername(username)
|
||
3 years ago
|
|
||
|
if user.Model != nil && user.ID > 0 {
|
||
|
return errcode.UsernameHasExisted
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// CheckPassword 密码检查
|
||
|
func CheckPassword(password string) error {
|
||
3 years ago
|
// 检测用户是否合规
|
||
|
if utf8.RuneCountInString(password) < 6 || utf8.RuneCountInString(password) > 16 {
|
||
|
return errcode.PasswordLengthLimit
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// CheckPhoneCaptcha 验证手机验证码
|
||
|
func CheckPhoneCaptcha(phone, captcha string) *errcode.Error {
|
||
2 years ago
|
// 如果禁止phone verify 则允许通过任意验证码
|
||
|
if DisablePhoneVerify {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
c, err := ds.GetLatestPhoneCaptcha(phone)
|
||
3 years ago
|
if err != nil {
|
||
|
return errcode.ErrorPhoneCaptcha
|
||
|
}
|
||
|
|
||
|
if c.Captcha != captcha {
|
||
|
return errcode.ErrorPhoneCaptcha
|
||
|
}
|
||
|
|
||
|
if c.ExpiredOn < time.Now().Unix() {
|
||
|
return errcode.ErrorPhoneCaptcha
|
||
|
}
|
||
|
|
||
2 years ago
|
if c.UseTimes >= _MaxCaptchaTimes {
|
||
3 years ago
|
return errcode.MaxPhoneCaptchaUseTimes
|
||
|
}
|
||
|
|
||
|
// 更新检测次数
|
||
2 years ago
|
ds.UsePhoneCaptcha(c)
|
||
3 years ago
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// CheckPhoneExist 检测手机号是否存在
|
||
|
func CheckPhoneExist(uid int64, phone string) bool {
|
||
2 years ago
|
u, err := ds.GetUserByPhone(phone)
|
||
3 years ago
|
if err != nil {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if u.Model == nil || u.ID == 0 {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if u.ID == uid {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
}
|
||
|
|
||
3 years ago
|
// EncryptPasswordAndSalt 密码加密&生成salt
|
||
|
func EncryptPasswordAndSalt(password string) (string, string) {
|
||
3 years ago
|
salt := uuid.Must(uuid.NewV4()).String()[:8]
|
||
|
password = util.EncodeMD5(util.EncodeMD5(password) + salt)
|
||
|
|
||
|
return password, salt
|
||
|
}
|
||
|
|
||
3 years ago
|
// Register 用户注册
|
||
2 years ago
|
func Register(username, password string) (*core.User, error) {
|
||
3 years ago
|
password, salt := EncryptPasswordAndSalt(password)
|
||
3 years ago
|
|
||
2 years ago
|
user := &core.User{
|
||
3 years ago
|
Nickname: username,
|
||
|
Username: username,
|
||
|
Password: password,
|
||
3 years ago
|
Avatar: GetRandomAvatar(),
|
||
3 years ago
|
Salt: salt,
|
||
2 years ago
|
Status: core.UserStatusNormal,
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
user, err := ds.CreateUser(user)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return user, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func RequestingFriend(user *core.User, param *RequestingFriendReq) error {
|
||
2 years ago
|
if _, err := ds.GetUserByID(param.UserId); err != nil {
|
||
|
return errcode.NotExistFriendId
|
||
|
}
|
||
|
return ds.RequestingFriend(user.ID, param.UserId, param.Greetings)
|
||
|
}
|
||
|
|
||
2 years ago
|
func AddFriend(user *core.User, param *AddFriendReq) error {
|
||
2 years ago
|
if _, err := ds.GetUserByID(param.UserId); err != nil {
|
||
|
return errcode.NotExistFriendId
|
||
|
}
|
||
|
return ds.AddFriend(user.ID, param.UserId)
|
||
|
}
|
||
|
|
||
2 years ago
|
func RejectFriend(user *core.User, param *RejectFriendReq) error {
|
||
2 years ago
|
if _, err := ds.GetUserByID(param.UserId); err != nil {
|
||
|
return errcode.NotExistFriendId
|
||
|
}
|
||
|
return ds.RejectFriend(user.ID, param.UserId)
|
||
|
}
|
||
|
|
||
2 years ago
|
func DeleteFriend(user *core.User, param *DeleteFriendReq) error {
|
||
2 years ago
|
if _, err := ds.GetUserByID(param.UserId); err != nil {
|
||
|
return errcode.NotExistFriendId
|
||
|
}
|
||
|
return ds.DeleteFriend(user.ID, param.UserId)
|
||
|
}
|
||
|
|
||
2 years ago
|
func GetContacts(user *core.User, offset int, limit int) (*core.ContactList, error) {
|
||
2 years ago
|
return ds.GetContacts(user.ID, offset, limit)
|
||
|
}
|
||
|
|
||
3 years ago
|
// GetUserInfo 获取用户信息
|
||
2 years ago
|
func GetUserInfo(param *AuthRequest) (*core.User, error) {
|
||
2 years ago
|
user, err := ds.GetUserByUsername(param.Username)
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if user.Model != nil && user.ID > 0 {
|
||
|
return user, nil
|
||
|
}
|
||
|
|
||
|
return nil, errcode.UnauthorizedAuthNotExist
|
||
|
}
|
||
|
|
||
2 years ago
|
func GetUserByID(id int64) (*core.User, error) {
|
||
2 years ago
|
user, err := ds.GetUserByID(id)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if user.Model != nil && user.ID > 0 {
|
||
|
return user, nil
|
||
|
}
|
||
|
|
||
|
return nil, errcode.NoExistUsername
|
||
|
}
|
||
|
|
||
2 years ago
|
func GetUserByUsername(user *core.User, username string) (*UserProfileResp, error) {
|
||
2 years ago
|
other, err := ds.GetUserByUsername(username)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
2 years ago
|
var resp *UserProfileResp
|
||
2 years ago
|
if other.Model != nil && other.ID > 0 {
|
||
2 years ago
|
resp = &UserProfileResp{
|
||
2 years ago
|
ID: other.ID,
|
||
|
Nickname: other.Nickname,
|
||
|
Username: other.Username,
|
||
|
Status: other.Status,
|
||
|
Avatar: other.Avatar,
|
||
|
IsAdmin: other.IsAdmin,
|
||
|
IsFriend: !(user == nil || user.ID == other.ID),
|
||
|
}
|
||
|
} else {
|
||
|
return nil, errcode.NoExistUsername
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
if user != nil && user.ID != other.ID {
|
||
|
resp.IsFriend = ds.IsFriend(user.ID, other.ID)
|
||
|
}
|
||
|
return resp, nil
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// UpdateUserInfo 更新用户信息
|
||
2 years ago
|
func UpdateUserInfo(user *core.User) *errcode.Error {
|
||
2 years ago
|
if err := ds.UpdateUser(user); err != nil {
|
||
2 years ago
|
return errcode.ServerError
|
||
|
}
|
||
|
return nil
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func ChangeUserAvatar(user *core.User, avatar string) (err *errcode.Error) {
|
||
2 years ago
|
defer func() {
|
||
|
if err != nil {
|
||
|
deleteOssObjects([]string{avatar})
|
||
|
}
|
||
|
}()
|
||
|
|
||
2 years ago
|
if err := ds.CheckAttachment(avatar); err != nil {
|
||
|
return errcode.InvalidParams
|
||
|
}
|
||
2 years ago
|
|
||
|
if err := oss.PersistObject(oss.ObjectKey(avatar)); err != nil {
|
||
|
logrus.Errorf("service.ChangeUserAvatar persist object failed: %s", err)
|
||
|
return errcode.ServerError
|
||
|
}
|
||
|
|
||
2 years ago
|
user.Avatar = avatar
|
||
2 years ago
|
err = UpdateUserInfo(user)
|
||
|
return
|
||
2 years ago
|
}
|
||
|
|
||
3 years ago
|
// GetUserCollections 获取用户收藏列表
|
||
2 years ago
|
func GetUserCollections(userID int64, offset, limit int) ([]*core.PostFormated, int64, error) {
|
||
2 years ago
|
collections, err := ds.GetUserPostCollections(userID, offset, limit)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
2 years ago
|
totalRows, err := ds.GetUserPostCollectionCount(userID)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
2 years ago
|
var posts []*core.Post
|
||
3 years ago
|
for _, collection := range collections {
|
||
2 years ago
|
posts = append(posts, collection.Post)
|
||
3 years ago
|
}
|
||
2 years ago
|
postsFormated, err := ds.MergePosts(posts)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
|
return postsFormated, totalRows, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// GetUserStars 获取用户点赞列表
|
||
2 years ago
|
func GetUserStars(userID int64, offset, limit int) ([]*core.PostFormated, int64, error) {
|
||
2 years ago
|
stars, err := ds.GetUserPostStars(userID, offset, limit)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
2 years ago
|
totalRows, err := ds.GetUserPostStarCount(userID)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
2 years ago
|
var posts []*core.Post
|
||
2 years ago
|
for _, star := range stars {
|
||
|
posts = append(posts, star.Post)
|
||
3 years ago
|
}
|
||
2 years ago
|
postsFormated, err := ds.MergePosts(posts)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
|
return postsFormated, totalRows, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// GetUserWalletBills 获取用户账单列表
|
||
2 years ago
|
func GetUserWalletBills(userID int64, offset, limit int) ([]*core.WalletStatement, int64, error) {
|
||
2 years ago
|
bills, err := ds.GetUserWalletBills(userID, offset, limit)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
2 years ago
|
totalRows, err := ds.GetUserWalletBillCount(userID)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
|
return bills, totalRows, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// SendPhoneCaptcha 发送短信验证码
|
||
|
func SendPhoneCaptcha(ctx *gin.Context, phone string) error {
|
||
3 years ago
|
|
||
2 years ago
|
err := ds.SendPhoneCaptcha(phone)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 写入计数缓存
|
||
2 years ago
|
conf.Redis.Incr(ctx, "PaoPaoSmsCaptcha:"+phone).Result()
|
||
3 years ago
|
|
||
|
currentTime := time.Now()
|
||
|
endTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 23, 59, 59, 0, currentTime.Location())
|
||
|
|
||
2 years ago
|
conf.Redis.Expire(ctx, "PaoPaoSmsCaptcha:"+phone, endTime.Sub(currentTime))
|
||
3 years ago
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// GetSuggestUsers 根据关键词获取用户推荐
|
||
|
func GetSuggestUsers(keyword string) ([]string, error) {
|
||
2 years ago
|
users, err := ds.GetUsersByKeyword(keyword)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
usernames := []string{}
|
||
|
for _, user := range users {
|
||
|
usernames = append(usernames, user.Username)
|
||
|
}
|
||
|
|
||
|
return usernames, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// GetSuggestTags 根据关键词获取标签推荐
|
||
|
func GetSuggestTags(keyword string) ([]string, error) {
|
||
2 years ago
|
tags, err := ds.GetTagsByKeyword(keyword)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
ts := []string{}
|
||
|
for _, t := range tags {
|
||
|
ts = append(ts, t.Tag)
|
||
|
}
|
||
|
|
||
|
return ts, nil
|
||
|
}
|
||
2 years ago
|
|
||
|
func IsFriend(userId, friendId int64) bool {
|
||
|
return ds.IsFriend(userId, friendId)
|
||
|
}
|
||
2 years ago
|
|
||
|
// checkPermision 检查是否拥有者或管理员
|
||
2 years ago
|
func checkPermision(user *core.User, targetUserId int64) *errcode.Error {
|
||
2 years ago
|
if user == nil || (user.ID != targetUserId && !user.IsAdmin) {
|
||
|
return errcode.NoPermission
|
||
|
}
|
||
|
return nil
|
||
|
}
|