optimize errors position in web servant

pull/327/head
Michael Li 1 year ago
parent ae22095604
commit ec14345e68
No known key found for this signature in database

@ -57,12 +57,12 @@ type AlipayNotifyReq struct {
func (r *AlipayNotifyReq) Bind(c *gin.Context) mir.Error {
if err := c.Request.ParseForm(); err != nil {
logrus.Errorf("parse form err: %s", err)
return _errRechargeNotifyError
return ErrRechargeNotifyError
}
noti, err := conf.MustAlipayClient().GetTradeNotification(c.Request)
if err != nil {
logrus.Errorf("alipayClient.GetTradeNotification err: %s form: %v", err, c.Request.Form)
return _errRechargeNotifyError
return ErrRechargeNotifyError
}
r.Ctx = c.Request.Context()
r.ID = convert.StrTo(noti.OutTradeNo).MustInt64()

@ -209,7 +209,7 @@ func (r *UploadAttachmentReq) Bind(c *gin.Context) (xerr mir.Error) {
uploadType := c.Request.FormValue("type")
file, fileHeader, err := c.Request.FormFile("file")
if err != nil {
return _errFileUploadFailed
return ErrFileUploadFailed
}
defer func() {
if xerr != nil {

@ -17,7 +17,7 @@ func fileCheck(uploadType string, size int64) mir.Error {
return xerror.InvalidParams
}
if size > 1024*1024*100 {
return _errFileInvalidSize.WithDetails("最大允许100MB")
return ErrFileInvalidSize.WithDetails("最大允许100MB")
}
return nil
}
@ -42,6 +42,6 @@ func getFileExt(s string) (string, mir.Error) {
"application/x-zip-compressed":
return ".zip", nil
default:
return "", _errFileInvalidExt.WithDetails("仅允许 png/jpg/gif/mp4/mov/zip 类型")
return "", ErrFileInvalidExt.WithDetails("仅允许 png/jpg/gif/mp4/mov/zip 类型")
}
}

@ -10,84 +10,84 @@ import (
// nolint
var (
_errUsernameHasExisted = xerror.NewError(20001, "用户名已存在")
_errUsernameLengthLimit = xerror.NewError(20002, "用户名长度3~12")
_errUsernameCharLimit = xerror.NewError(20003, "用户名只能包含字母、数字")
_errPasswordLengthLimit = xerror.NewError(20004, "密码长度6~16")
_errUserRegisterFailed = xerror.NewError(20005, "用户注册失败")
_errUserHasBeenBanned = xerror.NewError(20006, "该账户已被封停")
_errNoPermission = xerror.NewError(20007, "无权限执行该请求")
_errUserHasBindOTP = xerror.NewError(20008, "当前用户已绑定二次验证")
_errUserOTPInvalid = xerror.NewError(20009, "二次验证码验证失败")
_errUserNoBindOTP = xerror.NewError(20010, "当前用户未绑定二次验证")
_errErrorOldPassword = xerror.NewError(20011, "当前用户密码验证失败")
_errErrorCaptchaPassword = xerror.NewError(20012, "图形验证码验证失败")
_errAccountNoPhoneBind = xerror.NewError(20013, "拒绝操作: 账户未绑定手机号")
_errTooManyLoginError = xerror.NewError(20014, "登录失败次数过多,请稍后再试")
_errGetPhoneCaptchaError = xerror.NewError(20015, "短信验证码获取失败")
_errTooManyPhoneCaptchaSend = xerror.NewError(20016, "短信验证码获取次数已达今日上限")
_errExistedUserPhone = xerror.NewError(20017, "该手机号已被绑定")
_errErrorPhoneCaptcha = xerror.NewError(20018, "手机验证码不正确")
_errMaxPhoneCaptchaUseTimes = xerror.NewError(20019, "手机验证码已达最大使用次数")
_errNicknameLengthLimit = xerror.NewError(20020, "昵称长度2~12")
_errNoExistUsername = xerror.NewError(20021, "用户不存在")
_errNoAdminPermission = xerror.NewError(20022, "无管理权限")
_errDisallowUserRegister = xerror.NewError(20023, "系统不允许注册用户")
ErrUsernameHasExisted = xerror.NewError(20001, "用户名已存在")
ErrUsernameLengthLimit = xerror.NewError(20002, "用户名长度3~12")
ErrUsernameCharLimit = xerror.NewError(20003, "用户名只能包含字母、数字")
ErrPasswordLengthLimit = xerror.NewError(20004, "密码长度6~16")
ErrUserRegisterFailed = xerror.NewError(20005, "用户注册失败")
ErrUserHasBeenBanned = xerror.NewError(20006, "该账户已被封停")
ErrNoPermission = xerror.NewError(20007, "无权限执行该请求")
ErrUserHasBindOTP = xerror.NewError(20008, "当前用户已绑定二次验证")
ErrUserOTPInvalid = xerror.NewError(20009, "二次验证码验证失败")
ErrUserNoBindOTP = xerror.NewError(20010, "当前用户未绑定二次验证")
ErrErrorOldPassword = xerror.NewError(20011, "当前用户密码验证失败")
ErrErrorCaptchaPassword = xerror.NewError(20012, "图形验证码验证失败")
ErrAccountNoPhoneBind = xerror.NewError(20013, "拒绝操作: 账户未绑定手机号")
ErrTooManyLoginError = xerror.NewError(20014, "登录失败次数过多,请稍后再试")
ErrGetPhoneCaptchaError = xerror.NewError(20015, "短信验证码获取失败")
ErrTooManyPhoneCaptchaSend = xerror.NewError(20016, "短信验证码获取次数已达今日上限")
ErrExistedUserPhone = xerror.NewError(20017, "该手机号已被绑定")
ErrErrorPhoneCaptcha = xerror.NewError(20018, "手机验证码不正确")
ErrMaxPhoneCaptchaUseTimes = xerror.NewError(20019, "手机验证码已达最大使用次数")
ErrNicknameLengthLimit = xerror.NewError(20020, "昵称长度2~12")
ErrNoExistUsername = xerror.NewError(20021, "用户不存在")
ErrNoAdminPermission = xerror.NewError(20022, "无管理权限")
ErrDisallowUserRegister = xerror.NewError(20023, "系统不允许注册用户")
_errGetPostsFailed = xerror.NewError(30001, "获取动态列表失败")
_errCreatePostFailed = xerror.NewError(30002, "动态发布失败")
_errGetPostFailed = xerror.NewError(30003, "获取动态详情失败")
_errDeletePostFailed = xerror.NewError(30004, "动态删除失败")
_errLockPostFailed = xerror.NewError(30005, "动态锁定失败")
_errGetPostTagsFailed = xerror.NewError(30006, "获取话题列表失败")
_errInvalidDownloadReq = xerror.NewError(30007, "附件下载请求不合法")
_errDownloadReqError = xerror.NewError(30008, "附件下载请求失败")
_errInsuffientDownloadMoney = xerror.NewError(30009, "附件下载失败:账户资金不足")
_errDownloadExecFail = xerror.NewError(30010, "附件下载失败:扣费失败")
_errStickPostFailed = xerror.NewError(30011, "动态置顶失败")
_errVisblePostFailed = xerror.NewError(30012, "更新可见性失败")
ErrGetPostsFailed = xerror.NewError(30001, "获取动态列表失败")
ErrCreatePostFailed = xerror.NewError(30002, "动态发布失败")
ErrGetPostFailed = xerror.NewError(30003, "获取动态详情失败")
ErrDeletePostFailed = xerror.NewError(30004, "动态删除失败")
ErrLockPostFailed = xerror.NewError(30005, "动态锁定失败")
ErrGetPostTagsFailed = xerror.NewError(30006, "获取话题列表失败")
ErrInvalidDownloadReq = xerror.NewError(30007, "附件下载请求不合法")
ErrDownloadReqError = xerror.NewError(30008, "附件下载请求失败")
ErrInsuffientDownloadMoney = xerror.NewError(30009, "附件下载失败:账户资金不足")
ErrDownloadExecFail = xerror.NewError(30010, "附件下载失败:扣费失败")
ErrStickPostFailed = xerror.NewError(30011, "动态置顶失败")
ErrVisblePostFailed = xerror.NewError(30012, "更新可见性失败")
_errGetCommentsFailed = xerror.NewError(40001, "获取评论列表失败")
_errCreateCommentFailed = xerror.NewError(40002, "评论发布失败")
_errGetCommentFailed = xerror.NewError(40003, "获取评论详情失败")
_errDeleteCommentFailed = xerror.NewError(40004, "评论删除失败")
_errCreateReplyFailed = xerror.NewError(40005, "评论回复失败")
_errGetReplyFailed = xerror.NewError(40006, "获取评论详情失败")
_errMaxCommentCount = xerror.NewError(40007, "评论数已达最大限制")
_errGetCommentThumbs = xerror.NewError(40008, "获取评论点赞信息失败")
ErrGetCommentsFailed = xerror.NewError(40001, "获取评论列表失败")
ErrCreateCommentFailed = xerror.NewError(40002, "评论发布失败")
ErrGetCommentFailed = xerror.NewError(40003, "获取评论详情失败")
ErrDeleteCommentFailed = xerror.NewError(40004, "评论删除失败")
ErrCreateReplyFailed = xerror.NewError(40005, "评论回复失败")
ErrGetReplyFailed = xerror.NewError(40006, "获取评论详情失败")
ErrMaxCommentCount = xerror.NewError(40007, "评论数已达最大限制")
ErrGetCommentThumbs = xerror.NewError(40008, "获取评论点赞信息失败")
_errGetMessagesFailed = xerror.NewError(50001, "获取消息列表失败")
_errReadMessageFailed = xerror.NewError(50002, "标记消息已读失败")
_errSendWhisperFailed = xerror.NewError(50003, "私信发送失败")
_errNoWhisperToSelf = xerror.NewError(50004, "不允许给自己发送私信")
_errTooManyWhisperNum = xerror.NewError(50005, "今日私信次数已达上限")
ErrGetMessagesFailed = xerror.NewError(50001, "获取消息列表失败")
ErrReadMessageFailed = xerror.NewError(50002, "标记消息已读失败")
ErrSendWhisperFailed = xerror.NewError(50003, "私信发送失败")
ErrNoWhisperToSelf = xerror.NewError(50004, "不允许给自己发送私信")
ErrTooManyWhisperNum = xerror.NewError(50005, "今日私信次数已达上限")
_errGetCollectionsFailed = xerror.NewError(60001, "获取收藏列表失败")
_errGetStarsFailed = xerror.NewError(60002, "获取点赞列表失败")
ErrGetCollectionsFailed = xerror.NewError(60001, "获取收藏列表失败")
ErrGetStarsFailed = xerror.NewError(60002, "获取点赞列表失败")
_errRechargeReqFail = xerror.NewError(70001, "充值请求失败")
_errRechargeNotifyError = xerror.NewError(70002, "充值回调失败")
_errGetRechargeFailed = xerror.NewError(70003, "充值详情获取失败")
_errUserWalletBillsFailed = xerror.NewError(70004, "用户钱包账单获取失败")
ErrRechargeReqFail = xerror.NewError(70001, "充值请求失败")
ErrRechargeNotifyError = xerror.NewError(70002, "充值回调失败")
ErrGetRechargeFailed = xerror.NewError(70003, "充值详情获取失败")
ErrUserWalletBillsFailed = xerror.NewError(70004, "用户钱包账单获取失败")
_errNoRequestingFriendToSelf = xerror.NewError(80001, "不允许添加自己为好友")
_errNotExistFriendId = xerror.NewError(80002, "好友id不存在")
_errSendRequestingFriendFailed = xerror.NewError(80003, "申请添加朋友请求发送失败")
_errAddFriendFailed = xerror.NewError(80004, "添加好友失败")
_errRejectFriendFailed = xerror.NewError(80005, "拒绝好友失败")
_errDeleteFriendFailed = xerror.NewError(80006, "删除好友失败")
_errGetContactsFailed = xerror.NewError(80007, "获取联系人列表失败")
_errNoActionToSelf = xerror.NewError(80008, "不允许对自己操作")
ErrNoRequestingFriendToSelf = xerror.NewError(80001, "不允许添加自己为好友")
ErrNotExistFriendId = xerror.NewError(80002, "好友id不存在")
ErrSendRequestingFriendFailed = xerror.NewError(80003, "申请添加朋友请求发送失败")
ErrAddFriendFailed = xerror.NewError(80004, "添加好友失败")
ErrRejectFriendFailed = xerror.NewError(80005, "拒绝好友失败")
ErrDeleteFriendFailed = xerror.NewError(80006, "删除好友失败")
ErrGetContactsFailed = xerror.NewError(80007, "获取联系人列表失败")
ErrNoActionToSelf = xerror.NewError(80008, "不允许对自己操作")
_errFollowTopicFailed = xerror.NewError(90001, "关注话题失败")
_errUnfollowTopicFailed = xerror.NewError(90002, "取消关注话题失败")
_errStickTopicFailed = xerror.NewError(90003, "更行话题置顶状态失败")
_errThumbsUpTweetComment = xerror.NewError(90101, "评论点赞失败")
_errThumbsDownTweetComment = xerror.NewError(90102, "评论点踩失败")
_errThumbsUpTweetReply = xerror.NewError(90103, "评论回复点赞失败")
_errThumbsDownTweetReply = xerror.NewError(90104, "评论回复点踩失败")
ErrFollowTopicFailed = xerror.NewError(90001, "关注话题失败")
ErrUnfollowTopicFailed = xerror.NewError(90002, "取消关注话题失败")
ErrStickTopicFailed = xerror.NewError(90003, "更行话题置顶状态失败")
ErrThumbsUpTweetComment = xerror.NewError(90101, "评论点赞失败")
ErrThumbsDownTweetComment = xerror.NewError(90102, "评论点踩失败")
ErrThumbsUpTweetReply = xerror.NewError(90103, "评论回复点赞失败")
ErrThumbsDownTweetReply = xerror.NewError(90104, "评论回复点踩失败")
_errFileUploadFailed = xerror.NewError(10200, "文件上传失败")
_errFileInvalidExt = xerror.NewError(10201, "文件类型不合法")
_errFileInvalidSize = xerror.NewError(10202, "文件大小超限")
ErrFileUploadFailed = xerror.NewError(10200, "文件上传失败")
ErrFileInvalidExt = xerror.NewError(10201, "文件类型不合法")
ErrFileInvalidSize = xerror.NewError(10202, "文件大小超限")
)

@ -30,7 +30,7 @@ func (s *adminSrv) Chain() gin.HandlersChain {
func (s *adminSrv) ChangeUserStatus(req *web.ChangeUserStatusReq) mir.Error {
user, err := s.Ds.GetUserByID(req.ID)
if err != nil || user.Model == nil || user.ID <= 0 {
return _errNoExistUsername
return web.ErrNoExistUsername
}
// 执行更新
user.Status = req.Status

@ -40,7 +40,7 @@ func (s *alipayPubSrv) AlipayNotify(req *web.AlipayNotifyReq) mir.Error {
recharge, err := s.Ds.GetRechargeByID(req.ID)
if err != nil {
logrus.Errorf("GetRechargeByID id:%d err: %s", req.ID, err)
return _errRechargeNotifyError
return web.ErrRechargeNotifyError
}
if recharge.TradeStatus != "TRADE_SUCCESS" {
// 标记为已付款
@ -48,7 +48,7 @@ func (s *alipayPubSrv) AlipayNotify(req *web.AlipayNotifyReq) mir.Error {
defer s.Redis.DelRechargeStatus(req.Ctx, req.TradeNo)
if err != nil {
logrus.Errorf("HandleRechargeSuccess id:%d err: %s", req.ID, err)
return _errRechargeNotifyError
return web.ErrRechargeNotifyError
}
}
}
@ -64,12 +64,12 @@ func (s *alipayPrivSrv) UserWalletBills(req *web.UserWalletBillsReq) (*web.UserW
bills, err := s.Ds.GetUserWalletBills(req.UserId, (req.Page-1)*req.PageSize, req.PageSize)
if err != nil {
logrus.Errorf("GetUserWalletBills err: %s", err)
return nil, _errUserWalletBillsFailed
return nil, web.ErrUserWalletBillsFailed
}
totalRows, err := s.Ds.GetUserWalletBillCount(req.UserId)
if err != nil {
logrus.Errorf("GetUserWalletBillCount err: %s", err)
return nil, _errUserWalletBillsFailed
return nil, web.ErrUserWalletBillsFailed
}
resp := base.PageRespFrom(bills, req.Page, req.PageSize, totalRows)
return (*web.UserWalletBillsResp)(resp), nil
@ -79,7 +79,7 @@ func (s *alipayPrivSrv) UserRechargeLink(req *web.UserRechargeLinkReq) (*web.Use
recharge, err := s.Ds.CreateRecharge(req.User.ID, req.Amount)
if err != nil {
logrus.Errorf("Ds.CreateRecharge err: %v", err)
return nil, _errRechargeReqFail
return nil, web.ErrRechargeReqFail
}
p := alipay.TradePreCreate{}
p.OutTradeNo = fmt.Sprintf("%d", recharge.ID)
@ -89,10 +89,10 @@ func (s *alipayPrivSrv) UserRechargeLink(req *web.UserRechargeLinkReq) (*web.Use
rsp, err := s.alipayClient.TradePreCreate(p)
if err != nil {
logrus.Errorf("client.TradePreCreate err: %v\n", err)
return nil, _errRechargeReqFail
return nil, web.ErrRechargeReqFail
}
if rsp.Content.Code != alipay.CodeSuccess {
return nil, _errRechargeReqFail
return nil, web.ErrRechargeReqFail
}
return &web.UserRechargeLinkResp{
Id: recharge.ID,
@ -104,11 +104,11 @@ func (s *alipayPrivSrv) UserRechargeResult(req *web.UserRechargeResultReq) (*web
recharge, err := s.Ds.GetRechargeByID(req.Id)
if err != nil {
logrus.Errorf("Ds.GetRechargeByID err: %v", err)
return nil, _errGetRechargeFailed
return nil, web.ErrGetRechargeFailed
}
if recharge.UserID != req.UserId {
logrus.Errorf("Ds.GetRechargeByID userId not equel recharge.UserID: %d req.UserId %d", recharge.UserID, req.UserId)
return nil, _errGetRechargeFailed
return nil, web.ErrGetRechargeFailed
}
return &web.UserRechargeResultResp{
Id: recharge.ID,

@ -122,7 +122,7 @@ func (s *coreSrv) GetMessages(req *web.GetMessagesReq) (*web.GetMessagesResp, mi
}
if err != nil {
logrus.Errorf("Ds.GetMessages err: %v\n", err)
return nil, _errGetMessagesFailed
return nil, web.ErrGetMessagesFailed
}
totalRows, _ := s.Ds.GetMessageCount(conditions)
resp := base.PageRespFrom(messages, req.Page, req.PageSize, totalRows)
@ -132,14 +132,14 @@ func (s *coreSrv) GetMessages(req *web.GetMessagesReq) (*web.GetMessagesResp, mi
func (s *coreSrv) ReadMessage(req *web.ReadMessageReq) mir.Error {
message, err := s.Ds.GetMessageByID(req.ID)
if err != nil {
return _errReadMessageFailed
return web.ErrReadMessageFailed
}
if message.ReceiverUserID != req.Uid {
return _errNoPermission
return web.ErrNoPermission
}
if err = s.Ds.ReadMessage(message); err != nil {
logrus.Errorf("Ds.ReadMessage err: %s", err)
return _errReadMessageFailed
return web.ErrReadMessageFailed
}
return nil
}
@ -147,13 +147,13 @@ func (s *coreSrv) ReadMessage(req *web.ReadMessageReq) mir.Error {
func (s *coreSrv) SendUserWhisper(req *web.SendWhisperReq) mir.Error {
// 不允许发送私信给自己
if req.Uid == req.UserID {
return _errNoWhisperToSelf
return web.ErrNoWhisperToSelf
}
// 今日频次限制
ctx := context.Background()
if count, _ := s.Redis.GetCountWhisper(ctx, req.Uid); count >= _MaxWhisperNumDaily {
return _errTooManyWhisperNum
return web.ErrTooManyWhisperNum
}
// 创建私信
@ -166,7 +166,7 @@ func (s *coreSrv) SendUserWhisper(req *web.SendWhisperReq) mir.Error {
})
if err != nil {
logrus.Errorf("Ds.CreateWhisper err: %s", err)
return _errSendWhisperFailed
return web.ErrSendWhisperFailed
}
// 写入当日(自然日)计数缓存
@ -179,12 +179,12 @@ func (s *coreSrv) GetCollections(req *web.GetCollectionsReq) (*web.GetCollection
collections, err := s.Ds.GetUserPostCollections(req.UserId, (req.Page-1)*req.PageSize, req.PageSize)
if err != nil {
logrus.Errorf("Ds.GetUserPostCollections err: %s", err)
return nil, _errGetCollectionsFailed
return nil, web.ErrGetCollectionsFailed
}
totalRows, err := s.Ds.GetUserPostCollectionCount(req.UserId)
if err != nil {
logrus.Errorf("Ds.GetUserPostCollectionCount err: %s", err)
return nil, _errGetCollectionsFailed
return nil, web.ErrGetCollectionsFailed
}
var posts []*core.Post
@ -194,7 +194,7 @@ func (s *coreSrv) GetCollections(req *web.GetCollectionsReq) (*web.GetCollection
postsFormated, err := s.Ds.MergePosts(posts)
if err != nil {
logrus.Errorf("Ds.MergePosts err: %s", err)
return nil, _errGetCollectionsFailed
return nil, web.ErrGetCollectionsFailed
}
resp := base.PageRespFrom(postsFormated, req.Page, req.PageSize, totalRows)
@ -205,23 +205,23 @@ func (s *coreSrv) UserPhoneBind(req *web.UserPhoneBindReq) mir.Error {
// 手机重复性检查
u, err := s.Ds.GetUserByPhone(req.Phone)
if err == nil && u.Model != nil && u.ID != 0 && u.ID != req.User.ID {
return _errExistedUserPhone
return web.ErrExistedUserPhone
}
// 如果禁止phone verify 则允许通过任意验证码
if _enablePhoneVerify {
c, err := s.Ds.GetLatestPhoneCaptcha(req.Phone)
if err != nil {
return _errErrorPhoneCaptcha
return web.ErrErrorPhoneCaptcha
}
if c.Captcha != req.Captcha {
return _errErrorPhoneCaptcha
return web.ErrErrorPhoneCaptcha
}
if c.ExpiredOn < time.Now().Unix() {
return _errErrorPhoneCaptcha
return web.ErrErrorPhoneCaptcha
}
if c.UseTimes >= _MaxCaptchaTimes {
return _errMaxPhoneCaptchaUseTimes
return web.ErrMaxPhoneCaptchaUseTimes
}
// 更新检测次数
s.Ds.UsePhoneCaptcha(c)
@ -242,12 +242,12 @@ func (s *coreSrv) GetStars(req *web.GetStarsReq) (*web.GetStarsResp, mir.Error)
stars, err := s.Ds.GetUserPostStars(req.UserId, (req.Page-1)*req.PageSize, req.PageSize)
if err != nil {
logrus.Errorf("Ds.GetUserPostStars err: %s", err)
return nil, _errGetStarsFailed
return nil, web.ErrGetStarsFailed
}
totalRows, err := s.Ds.GetUserPostStarCount(req.UserId)
if err != nil {
logrus.Errorf("Ds.GetUserPostStars err: %s", err)
return nil, _errGetStarsFailed
return nil, web.ErrGetStarsFailed
}
var posts []*core.Post
@ -257,7 +257,7 @@ func (s *coreSrv) GetStars(req *web.GetStarsReq) (*web.GetStarsResp, mir.Error)
postsFormated, err := s.Ds.MergePosts(posts)
if err != nil {
logrus.Errorf("Ds.MergePosts err: %s", err)
return nil, _errGetStarsFailed
return nil, web.ErrGetStarsFailed
}
resp := base.PageRespFrom(postsFormated, req.Page, req.PageSize, totalRows)
@ -272,7 +272,7 @@ func (s *coreSrv) ChangePassword(req *web.ChangePasswordReq) mir.Error {
// 旧密码校验
user := req.User
if !validPassword(user.Password, req.OldPassword, req.User.Salt) {
return _errErrorOldPassword
return web.ErrErrorOldPassword
}
// 更新入库
user.Password, user.Salt = encryptPasswordAndSalt(req.Password)
@ -311,7 +311,7 @@ func (s *coreSrv) SuggestUsers(req *web.SuggestUsersReq) (*web.SuggestUsersResp,
func (s *coreSrv) ChangeNickname(req *web.ChangeNicknameReq) mir.Error {
if utf8.RuneCountInString(req.Nickname) < 2 || utf8.RuneCountInString(req.Nickname) > 12 {
return _errNicknameLengthLimit
return web.ErrNicknameLengthLimit
}
user := req.User
user.Nickname = req.Nickname

@ -35,7 +35,7 @@ func (s *friendshipSrv) GetContacts(req *web.GetContactsReq) (*web.GetContactsRe
res, err := s.Ds.GetContacts(req.User.ID, (req.Page-1)*req.PageSize, req.PageSize)
if err != nil {
logrus.Errorf("service.GetContacts err: %s", err)
return nil, _errGetContactsFailed
return nil, web.ErrGetContactsFailed
}
resp := base.PageRespFrom(res.Contacts, req.Page, req.PageSize, res.Total)
return (*web.GetContactsResp)(resp), nil
@ -46,14 +46,14 @@ func (s *friendshipSrv) DeleteFriend(req *web.DeleteFriendReq) mir.Error {
return xerror.ServerError
}
if req.User != nil && req.User.ID == req.UserId {
return _errNoActionToSelf
return web.ErrNoActionToSelf
}
if _, err := s.Ds.GetUserByID(req.UserId); err != nil {
return _errNotExistFriendId
return web.ErrNotExistFriendId
}
if err := s.Ds.DeleteFriend(req.User.ID, req.UserId); err != nil {
logrus.Errorf("Ds.DeleteFriend err: %s", err)
return _errDeleteFriendFailed
return web.ErrDeleteFriendFailed
}
return nil
}
@ -63,14 +63,14 @@ func (s *friendshipSrv) RejectFriend(req *web.RejectFriendReq) mir.Error {
return xerror.ServerError
}
if req.User.ID == req.UserId {
return _errNoActionToSelf
return web.ErrNoActionToSelf
}
if _, err := s.Ds.GetUserByID(req.UserId); err != nil {
return _errNotExistFriendId
return web.ErrNotExistFriendId
}
if err := s.Ds.RejectFriend(req.User.ID, req.UserId); err != nil {
logrus.Errorf("Ds.RejectFriend err: %s", err)
return _errRejectFriendFailed
return web.ErrRejectFriendFailed
}
return nil
}
@ -80,14 +80,14 @@ func (s *friendshipSrv) AddFriend(req *web.AddFriendReq) mir.Error {
return xerror.ServerError
}
if req.User.ID == req.UserId {
return _errNoActionToSelf
return web.ErrNoActionToSelf
}
if _, err := s.Ds.GetUserByID(req.UserId); err != nil {
return _errNotExistFriendId
return web.ErrNotExistFriendId
}
if err := s.Ds.AddFriend(req.User.ID, req.UserId); err != nil {
logrus.Errorf("Ds.AddFriend err: %s", err)
return _errAddFriendFailed
return web.ErrAddFriendFailed
}
return nil
}
@ -97,14 +97,14 @@ func (s *friendshipSrv) RequestingFriend(req *web.RequestingFriendReq) mir.Error
return xerror.ServerError
}
if req.User.ID == req.UserId {
return _errNoRequestingFriendToSelf
return web.ErrNoRequestingFriendToSelf
}
if _, err := s.Ds.GetUserByID(req.UserId); err != nil {
return _errNotExistFriendId
return web.ErrNotExistFriendId
}
if err := s.Ds.RequestingFriend(req.User.ID, req.UserId, req.Greetings); err != nil {
logrus.Errorf("Ds.RequestingFriend err: %s", err)
return _errSendRequestingFriendFailed
return web.ErrSendRequestingFriendFailed
}
return nil
}

@ -37,7 +37,7 @@ func (s *looseSrv) Timeline(req *web.TimelineReq) (*web.TimelineResp, mir.Error)
res, err := s.Ds.IndexPosts(req.User, offset, limit)
if err != nil {
logrus.Errorf("Ds.IndexPosts err: %s", err)
return nil, _errGetPostsFailed
return nil, web.ErrGetPostsFailed
}
resp = base.PageRespFrom(res.Tweets, req.Page, req.PageSize, res.Total)
} else {
@ -48,12 +48,12 @@ func (s *looseSrv) Timeline(req *web.TimelineReq) (*web.TimelineResp, mir.Error)
res, err := s.Ts.Search(req.User, q, offset, limit)
if err != nil {
logrus.Errorf("Ts.Search err: %s", err)
return nil, _errGetPostsFailed
return nil, web.ErrGetPostsFailed
}
posts, err := s.Ds.RevampPosts(res.Items)
if err != nil {
logrus.Errorf("Ds.RevampPosts err: %s", err)
return nil, _errGetPostsFailed
return nil, web.ErrGetPostsFailed
}
resp = base.PageRespFrom(posts, req.Page, req.PageSize, res.Total)
}
@ -85,12 +85,12 @@ func (s *looseSrv) GetUserTweets(req *web.GetUserTweetsReq) (*web.GetUserTweetsR
_, posts, err := s.GetTweetList(conditions, (req.Page-1)*req.PageSize, req.PageSize)
if err != nil {
logrus.Errorf("s.GetTweetList err: %s", err)
return nil, _errGetPostsFailed
return nil, web.ErrGetPostsFailed
}
totalRows, err := s.Ds.GetPostCount(conditions)
if err != nil {
logrus.Errorf("s.GetPostCount err: %s", err)
return nil, _errGetPostsFailed
return nil, web.ErrGetPostsFailed
}
resp := base.PageRespFrom(posts, req.Page, req.PageSize, totalRows)
@ -101,10 +101,10 @@ func (s *looseSrv) GetUserProfile(req *web.GetUserProfileReq) (*web.GetUserProfi
he, err := s.Ds.GetUserByUsername(req.Username)
if err != nil {
logrus.Errorf("Ds.GetUserByUsername err: %s", err)
return nil, _errNoExistUsername
return nil, web.ErrNoExistUsername
}
if he.Model == nil && he.ID <= 0 {
return nil, _errNoExistUsername
return nil, web.ErrNoExistUsername
}
// 设定自己不是自己的朋友
isFriend := !(req.User == nil || req.User.ID == he.ID)
@ -146,10 +146,10 @@ func (s *looseSrv) TopicList(req *web.TopicListReq) (*web.TopicListResp, mir.Err
}
default:
// TODO: return good error
err = _errGetPostTagsFailed
err = web.ErrGetPostTagsFailed
}
if err != nil {
return nil, _errGetPostTagsFailed
return nil, web.ErrGetPostTagsFailed
}
return &web.TopicListResp{
Topics: tags,
@ -169,7 +169,7 @@ func (s *looseSrv) TweetComments(req *web.TweetCommentsReq) (*web.TweetCommentsR
comments, err := s.Ds.GetComments(conditions, (req.Page-1)*req.PageSize, req.PageSize)
if err != nil {
return nil, _errGetCommentsFailed
return nil, web.ErrGetCommentsFailed
}
userIDs := []int64{}
@ -181,24 +181,24 @@ func (s *looseSrv) TweetComments(req *web.TweetCommentsReq) (*web.TweetCommentsR
users, err := s.Ds.GetUsersByIDs(userIDs)
if err != nil {
return nil, _errGetCommentsFailed
return nil, web.ErrGetCommentsFailed
}
contents, err := s.Ds.GetCommentContentsByIDs(commentIDs)
if err != nil {
return nil, _errGetCommentsFailed
return nil, web.ErrGetCommentsFailed
}
replies, err := s.Ds.GetCommentRepliesByID(commentIDs)
if err != nil {
return nil, _errGetCommentsFailed
return nil, web.ErrGetCommentsFailed
}
var commentThumbs, replyThumbs cs.CommentThumbsMap
if req.Uid > 0 {
commentThumbs, replyThumbs, err = s.Ds.GetCommentThumbsMap(req.Uid, req.TweetId)
if err != nil {
return nil, _errGetCommentsFailed
return nil, web.ErrGetCommentsFailed
}
}

@ -49,7 +49,7 @@ func (s *privSrv) Chain() gin.HandlersChain {
func (s *privSrv) ThumbsDownTweetReply(req *web.TweetReplyThumbsReq) mir.Error {
if err := s.Ds.ThumbsDownReply(req.Uid, req.TweetId, req.CommentId, req.ReplyId); err != nil {
logrus.Errorf("thumbs down tweet reply error: %s req:%v", err, req)
return _errThumbsDownTweetReply
return web.ErrThumbsDownTweetReply
}
return nil
}
@ -57,7 +57,7 @@ func (s *privSrv) ThumbsDownTweetReply(req *web.TweetReplyThumbsReq) mir.Error {
func (s *privSrv) ThumbsUpTweetReply(req *web.TweetReplyThumbsReq) mir.Error {
if err := s.Ds.ThumbsUpReply(req.Uid, req.TweetId, req.CommentId, req.ReplyId); err != nil {
logrus.Errorf("thumbs up tweet reply error: %s req:%v", err, req)
return _errThumbsUpTweetReply
return web.ErrThumbsUpTweetReply
}
return nil
}
@ -65,7 +65,7 @@ func (s *privSrv) ThumbsUpTweetReply(req *web.TweetReplyThumbsReq) mir.Error {
func (s *privSrv) ThumbsDownTweetComment(req *web.TweetCommentThumbsReq) mir.Error {
if err := s.Ds.ThumbsDownComment(req.Uid, req.TweetId, req.CommentId); err != nil {
logrus.Errorf("thumbs down tweet comment error: %s req:%v", err, req)
return _errThumbsDownTweetComment
return web.ErrThumbsDownTweetComment
}
return nil
}
@ -73,7 +73,7 @@ func (s *privSrv) ThumbsDownTweetComment(req *web.TweetCommentThumbsReq) mir.Err
func (s *privSrv) ThumbsUpTweetComment(req *web.TweetCommentThumbsReq) mir.Error {
if err := s.Ds.ThumbsUpComment(req.Uid, req.TweetId, req.CommentId); err != nil {
logrus.Errorf("thumbs up tweet comment error: %s req:%v", err, req)
return _errThumbsUpTweetComment
return web.ErrThumbsUpTweetComment
}
return nil
}
@ -81,7 +81,7 @@ func (s *privSrv) ThumbsUpTweetComment(req *web.TweetCommentThumbsReq) mir.Error
func (s *privSrv) UnfollowTopic(req *web.UnfollowTopicReq) mir.Error {
if err := s.Ds.UnfollowTopic(req.Uid, req.TopicId); err != nil {
logrus.Errorf("user(%d) unfollow topic(%d) failed: %s", req.Uid, req.TopicId, err)
return _errUnfollowTopicFailed
return web.ErrUnfollowTopicFailed
}
return nil
}
@ -89,7 +89,7 @@ func (s *privSrv) UnfollowTopic(req *web.UnfollowTopicReq) mir.Error {
func (s *privSrv) FollowTopic(req *web.FollowTopicReq) mir.Error {
if err := s.Ds.FollowTopic(req.Uid, req.TopicId); err != nil {
logrus.Errorf("user(%d) follow topic(%d) failed: %s", req.Uid, req.TopicId, err)
return _errFollowTopicFailed
return web.ErrFollowTopicFailed
}
return nil
}
@ -98,7 +98,7 @@ func (s *privSrv) StickTopic(req *web.StickTopicReq) (*web.StickTopicResp, mir.E
status, err := s.Ds.StickTopic(req.Uid, req.TopicId)
if err != nil {
logrus.Errorf("user(%d) stick topic(%d) failed: %s", req.Uid, req.TopicId, err)
return nil, _errStickTopicFailed
return nil, web.ErrStickTopicFailed
}
return &web.StickTopicResp{
StickStatus: status,
@ -114,7 +114,7 @@ func (s *privSrv) UploadAttachment(req *web.UploadAttachmentReq) (*web.UploadAtt
objectUrl, err := s.oss.PutObject(ossSavePath, req.File, req.FileSize, req.ContentType, false)
if err != nil {
logrus.Errorf("oss.putObject err: %s", err)
return nil, _errFileUploadFailed
return nil, web.ErrFileUploadFailed
}
// 构造附件Model
@ -134,7 +134,7 @@ func (s *privSrv) UploadAttachment(req *web.UploadAttachmentReq) (*web.UploadAtt
attachment, err = s.Ds.CreateAttachment(attachment)
if err != nil {
logrus.Errorf("Ds.CreateAttachment err: %s", err)
return nil, _errFileUploadFailed
return nil, web.ErrFileUploadFailed
}
return &web.UploadAttachmentResp{
@ -151,14 +151,14 @@ func (s *privSrv) DownloadAttachmentPrecheck(req *web.DownloadAttachmentPrecheck
content, err := s.Ds.GetPostContentByID(req.ContentID)
if err != nil {
logrus.Errorf("Ds.GetPostContentByID err: %s", err)
return nil, _errInvalidDownloadReq
return nil, web.ErrInvalidDownloadReq
}
resp := &web.DownloadAttachmentPrecheckResp{Paid: true}
if content.Type == core.ContentTypeChargeAttachment {
tweet, err := s.GetTweetBy(content.PostID)
if err != nil {
logrus.Errorf("get tweet err: %v", err)
return nil, _errInvalidDownloadReq
return nil, web.ErrInvalidDownloadReq
}
// 发布者或管理员免费下载
if tweet.UserID == req.User.ID || req.User.IsAdmin {
@ -174,7 +174,7 @@ func (s *privSrv) DownloadAttachment(req *web.DownloadAttachmentReq) (*web.Downl
content, err := s.Ds.GetPostContentByID(req.ContentID)
if err != nil {
logrus.Errorf("s.GetPostContentByID err: %v", err)
return nil, _errInvalidDownloadReq
return nil, web.ErrInvalidDownloadReq
}
// 收费附件
if content.Type == core.ContentTypeChargeAttachment {
@ -207,7 +207,7 @@ func (s *privSrv) DownloadAttachment(req *web.DownloadAttachmentReq) (*web.Downl
signedURL, err := s.oss.SignURL(objectKey, 60)
if err != nil {
logrus.Errorf("client.SignURL err: %v", err)
return nil, _errDownloadReqError
return nil, web.ErrDownloadReqError
}
return &web.DownloadAttachmentResp{
SignedURL: signedURL,
@ -224,7 +224,7 @@ func (s *privSrv) CreateTweet(req *web.CreateTweetReq) (_ *web.CreateTweetResp,
contents, err := persistMediaContents(s.oss, req.Contents)
if err != nil {
return nil, _errCreatePostFailed
return nil, web.ErrCreatePostFailed
}
mediaContents = contents
tags := tagsFrom(req.Tags)
@ -239,7 +239,7 @@ func (s *privSrv) CreateTweet(req *web.CreateTweetReq) (_ *web.CreateTweetResp,
post, err = s.Ds.CreatePost(post)
if err != nil {
logrus.Errorf("Ds.CreatePost err: %s", err)
return nil, _errCreatePostFailed
return nil, web.ErrCreatePostFailed
}
// 创建推文内容
@ -261,7 +261,7 @@ func (s *privSrv) CreateTweet(req *web.CreateTweetReq) (_ *web.CreateTweetResp,
}
if _, err = s.Ds.CreatePostContent(postContent); err != nil {
logrus.Infof("Ds.CreatePostContent err: %s", err)
return nil, _errCreateCommentFailed
return nil, web.ErrCreateCommentFailed
}
}
@ -299,27 +299,27 @@ func (s *privSrv) CreateTweet(req *web.CreateTweetReq) (_ *web.CreateTweetResp,
formatedPosts, err := s.Ds.RevampPosts([]*core.PostFormated{post.Format()})
if err != nil {
logrus.Infof("Ds.RevampPosts err: %s", err)
return nil, _errCreatePostFailed
return nil, web.ErrCreatePostFailed
}
return (*web.CreateTweetResp)(formatedPosts[0]), nil
}
func (s *privSrv) DeleteTweet(req *web.DeleteTweetReq) mir.Error {
if req.User == nil {
return _errNoPermission
return web.ErrNoPermission
}
post, err := s.Ds.GetPostByID(req.ID)
if err != nil {
logrus.Errorf("Ds.GetPostByID err: %s", err)
return _errGetPostFailed
return web.ErrGetPostFailed
}
if post.UserID != req.User.ID && !req.User.IsAdmin {
return _errNoPermission
return web.ErrNoPermission
}
mediaContents, err := s.Ds.DeletePost(post)
if err != nil {
logrus.Errorf("Ds.DeletePost delete post failed: %s", err)
return _errDeletePostFailed
return web.ErrDeletePostFailed
}
// 删除推文的媒体内容
deleteOssObjects(s.oss, mediaContents)
@ -327,7 +327,7 @@ func (s *privSrv) DeleteTweet(req *web.DeleteTweetReq) mir.Error {
s.DeleteSearchPost(post)
if err != nil {
logrus.Errorf("s.DeleteSearchPost failed: %s", err)
return _errDeletePostFailed
return web.ErrDeletePostFailed
}
return nil
}
@ -336,16 +336,16 @@ func (s *privSrv) DeleteCommentReply(req *web.DeleteCommentReplyReq) mir.Error {
reply, err := s.Ds.GetCommentReplyByID(req.ID)
if err != nil {
logrus.Errorf("Ds.GetCommentReplyByID err: %s", err)
return _errGetReplyFailed
return web.ErrGetReplyFailed
}
if req.User.ID != reply.UserID && !req.User.IsAdmin {
return _errNoPermission
return web.ErrNoPermission
}
// 执行删除
err = s.deletePostCommentReply(reply)
if err != nil {
logrus.Errorf("s.deletePostCommentReply err: %s", err)
return _errDeleteCommentFailed
return web.ErrDeleteCommentFailed
}
return nil
}
@ -359,7 +359,7 @@ func (s *privSrv) CreateCommentReply(req *web.CreateCommentReplyReq) (*web.Creat
)
if post, comment, atUserID, err = s.createPostPreHandler(req.CommentID, req.Uid, req.AtUserID); err != nil {
return nil, _errCreateReplyFailed
return nil, web.ErrCreateReplyFailed
}
// 创建评论
@ -374,7 +374,7 @@ func (s *privSrv) CreateCommentReply(req *web.CreateCommentReplyReq) (*web.Creat
reply, err = s.Ds.CreateCommentReply(reply)
if err != nil {
return nil, _errCreateReplyFailed
return nil, web.ErrCreateReplyFailed
}
// 更新Post回复数
@ -432,26 +432,26 @@ func (s *privSrv) DeleteComment(req *web.DeleteCommentReq) mir.Error {
comment, err := s.Ds.GetCommentByID(req.ID)
if err != nil {
logrus.Errorf("Ds.GetCommentByID err: %v\n", err)
return _errGetCommentFailed
return web.ErrGetCommentFailed
}
if req.User.ID != comment.UserID && !req.User.IsAdmin {
return _errNoPermission
return web.ErrNoPermission
}
// 加载post
post, err := s.Ds.GetPostByID(comment.PostID)
if err != nil {
return _errDeleteCommentFailed
return web.ErrDeleteCommentFailed
}
// 更新post回复数
post.CommentCount--
if err := s.Ds.UpdatePost(post); err != nil {
logrus.Errorf("Ds.UpdatePost err: %s", err)
return _errDeleteCommentFailed
return web.ErrDeleteCommentFailed
}
// TODO: 优化删除逻辑事务化删除comment
if err := s.Ds.DeleteComment(comment); err != nil {
logrus.Errorf("Ds.DeleteComment err: %s", err)
return _errDeleteCommentFailed
return web.ErrDeleteCommentFailed
}
return nil
}
@ -478,7 +478,7 @@ func (s *privSrv) CreateComment(req *web.CreateCommentReq) (_ *web.CreateComment
return nil, xerror.ServerError
}
if post.CommentCount >= conf.AppSetting.MaxCommentCount {
return nil, _errMaxCommentCount
return nil, web.ErrMaxCommentCount
}
comment := &core.Comment{
PostID: post.ID,
@ -489,7 +489,7 @@ func (s *privSrv) CreateComment(req *web.CreateCommentReq) (_ *web.CreateComment
comment, err = s.Ds.CreateComment(comment)
if err != nil {
logrus.Errorf("Ds.CreateComment err:%s", err)
return nil, _errCreateCommentFailed
return nil, web.ErrCreateCommentFailed
}
for _, item := range req.Contents {
@ -595,14 +595,14 @@ func (s *privSrv) VisibleTweet(req *web.VisibleTweetReq) (*web.VisibleTweetResp,
}
post, err := s.Ds.GetPostByID(req.ID)
if err != nil {
return nil, _errVisblePostFailed
return nil, web.ErrVisblePostFailed
}
if xerr := checkPermision(req.User, post.UserID); xerr != nil {
return nil, xerr
}
if err = s.Ds.VisiblePost(post, req.Visibility); err != nil {
logrus.Warnf("s.Ds.VisiblePost: %s", err)
return nil, _errVisblePostFailed
return nil, web.ErrVisblePostFailed
}
// 推送Search
@ -618,14 +618,14 @@ func (s *privSrv) StickTweet(req *web.StickTweetReq) (*web.StickTweetResp, mir.E
post, err := s.Ds.GetPostByID(req.ID)
if err != nil {
logrus.Errorf("Ds.GetPostByID err: %v\n", err)
return nil, _errStickPostFailed
return nil, web.ErrStickPostFailed
}
if !req.User.IsAdmin {
return nil, _errNoPermission
return nil, web.ErrNoPermission
}
newStatus := 1 - post.IsTop
if err = s.Ds.StickPost(post); err != nil {
return nil, _errStickPostFailed
return nil, web.ErrStickPostFailed
}
return &web.StickTweetResp{
StickStatus: newStatus,
@ -635,14 +635,14 @@ func (s *privSrv) StickTweet(req *web.StickTweetReq) (*web.StickTweetResp, mir.E
func (s *privSrv) LockTweet(req *web.LockTweetReq) (*web.LockTweetResp, mir.Error) {
post, err := s.Ds.GetPostByID(req.ID)
if err != nil {
return nil, _errLockPostFailed
return nil, web.ErrLockPostFailed
}
if post.UserID != req.User.ID && !req.User.IsAdmin {
return nil, _errNoPermission
return nil, web.ErrNoPermission
}
newStatus := 1 - post.IsLock
if err := s.Ds.LockPost(post); err != nil {
return nil, _errLockPostFailed
return nil, web.ErrLockPostFailed
}
return &web.LockTweetResp{
LockStatus: newStatus,
@ -688,7 +688,7 @@ func (s *privSrv) createPostPreHandler(commentID int64, userID, atUserID int64)
}
if post.CommentCount >= conf.AppSetting.MaxCommentCount {
return nil, nil, atUserID, _errMaxCommentCount
return nil, nil, atUserID, web.ErrMaxCommentCount
}
if userID == atUserID {
@ -715,7 +715,7 @@ func (s *privSrv) createPostStar(postID, userID int64) (*core.PostStar, mir.Erro
// 私密post不可操作
// TODO: 使用统一的permission checker来检查权限问题这里好友可见post就没处理是bug
if post.Visibility == core.PostVisitPrivate && post.UserID != userID {
return nil, _errNoPermission
return nil, web.ErrNoPermission
}
star, err := s.Ds.CreatePostStar(postID, userID)
@ -741,7 +741,7 @@ func (s *privSrv) deletePostStar(star *core.PostStar) mir.Error {
// 私密post特殊处理
// TODO: 使用统一的permission checker来检查权限问题这里好友可见post就没处理是bug
if post.Visibility == core.PostVisitPrivate && post.UserID != star.UserID {
return _errNoPermission
return web.ErrNoPermission
}
if err = s.Ds.DeletePostStar(star); err != nil {
@ -766,7 +766,7 @@ func (s *privSrv) createPostCollection(postID, userID int64) (*core.PostCollecti
// 私密post特殊处理
// TODO: 使用统一的permission checker来检查权限问题这里好友可见post就没处理是bug
if post.Visibility == core.PostVisitPrivate && post.UserID != userID {
return nil, _errNoPermission
return nil, web.ErrNoPermission
}
collection, err := s.Ds.CreatePostCollection(postID, userID)
@ -792,7 +792,7 @@ func (s *privSrv) deletePostCollection(collection *core.PostCollection) mir.Erro
// 私密post特殊处理
// TODO: 使用统一的permission checker来检查权限问题这里好友可见post就没处理是bug
if post.Visibility == core.PostVisitPrivate && post.UserID != collection.UserID {
return _errNoPermission
return web.ErrNoPermission
}
if err = s.Ds.DeletePostCollection(collection); err != nil {
return xerror.ServerError
@ -814,7 +814,7 @@ func (s *privSrv) checkPostAttachmentIsPaid(postID, userID int64) bool {
func (s *privSrv) buyPostAttachment(post *core.Post, user *core.User) mir.Error {
if user.Balance < post.AttachmentPrice {
return _errInsuffientDownloadMoney
return web.ErrInsuffientDownloadMoney
}
// 执行购买
if err := s.Ds.HandlePostAttachmentBought(post, user); err != nil {

@ -45,15 +45,15 @@ type pubSrv struct {
func (s *pubSrv) TweetDetail(req *web.TweetDetailReq) (*web.TweetDetailResp, mir.Error) {
post, err := s.Ds.GetPostByID(req.TweetId)
if err != nil {
return nil, _errGetPostFailed
return nil, web.ErrGetPostFailed
}
postContents, err := s.Ds.GetPostContentsByIDs([]int64{post.ID})
if err != nil {
return nil, _errGetPostFailed
return nil, web.ErrGetPostFailed
}
users, err := s.Ds.GetUsersByIDs([]int64{post.UserID})
if err != nil {
return nil, _errGetPostFailed
return nil, web.ErrGetPostFailed
}
// 数据整合
postFormated := post.Format()
@ -74,13 +74,13 @@ func (s *pubSrv) SendCaptcha(req *web.SendCaptchaReq) mir.Error {
// 验证图片验证码
if captcha, err := s.Redis.GetImgCaptcha(ctx, req.ImgCaptchaID); err != nil || string(captcha) != req.ImgCaptcha {
logrus.Debugf("get captcha err:%s expect:%s got:%s", err, captcha, req.ImgCaptcha)
return _errErrorCaptchaPassword
return web.ErrErrorCaptchaPassword
}
s.Redis.DelImgCaptcha(ctx, req.ImgCaptchaID)
// 今日频次限制
if count, _ := s.Redis.GetCountSmsCaptcha(ctx, req.Phone); count >= _MaxPhoneCaptcha {
return _errTooManyPhoneCaptchaSend
return web.ErrTooManyPhoneCaptchaSend
}
if err := s.Ds.SendPhoneCaptcha(req.Phone); err != nil {
@ -119,7 +119,7 @@ func (s *pubSrv) GetCaptcha() (*web.GetCaptchaResp, mir.Error) {
func (s *pubSrv) Register(req *web.RegisterReq) (*web.RegisterResp, mir.Error) {
if _disallowUserRegister {
return nil, _errDisallowUserRegister
return nil, web.ErrDisallowUserRegister
}
// 用户名检查
if err := s.validUsername(req.Username); err != nil {
@ -128,7 +128,7 @@ func (s *pubSrv) Register(req *web.RegisterReq) (*web.RegisterResp, mir.Error) {
// 密码检查
if err := checkPassword(req.Password); err != nil {
logrus.Errorf("scheckPassword err: %v", err)
return nil, _errUserRegisterFailed
return nil, web.ErrUserRegisterFailed
}
password, salt := encryptPasswordAndSalt(req.Password)
user := &core.User{
@ -142,7 +142,7 @@ func (s *pubSrv) Register(req *web.RegisterReq) (*web.RegisterResp, mir.Error) {
user, err := s.Ds.CreateUser(user)
if err != nil {
logrus.Errorf("Ds.CreateUser err: %s", err)
return nil, _errUserRegisterFailed
return nil, web.ErrUserRegisterFailed
}
return &web.RegisterResp{
UserId: user.ID,
@ -160,12 +160,12 @@ func (s *pubSrv) Login(req *web.LoginReq) (*web.LoginResp, mir.Error) {
if user.Model != nil && user.ID > 0 {
if count, err := s.Redis.GetCountLoginErr(ctx, user.ID); err == nil && count >= _MaxLoginErrTimes {
return nil, _errTooManyLoginError
return nil, web.ErrTooManyLoginError
}
// 对比密码是否正确
if validPassword(user.Password, req.Password, user.Salt) {
if user.Status == core.UserStatusClosed {
return nil, _errUserHasBeenBanned
return nil, web.ErrUserHasBeenBanned
}
// 清空登录计数
s.Redis.DelCountLoginErr(ctx, user.ID)
@ -198,17 +198,17 @@ func (s *pubSrv) Version() (*web.VersionResp, mir.Error) {
func (s *pubSrv) validUsername(username string) mir.Error {
// 检测用户是否合规
if utf8.RuneCountInString(username) < 3 || utf8.RuneCountInString(username) > 12 {
return _errUsernameLengthLimit
return web.ErrUsernameLengthLimit
}
if !regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(username) {
return _errUsernameCharLimit
return web.ErrUsernameCharLimit
}
// 重复检查
user, _ := s.Ds.GetUserByUsername(username)
if user.Model != nil && user.ID > 0 {
return _errUsernameHasExisted
return web.ErrUsernameHasExisted
}
return nil
}

@ -82,7 +82,7 @@ func getRandomAvatar() string {
func checkPassword(password string) mir.Error {
// 检测用户是否合规
if utf8.RuneCountInString(password) < 6 || utf8.RuneCountInString(password) > 16 {
return _errPasswordLengthLimit
return web.ErrPasswordLengthLimit
}
return nil
}
@ -144,7 +144,7 @@ func fileCheck(uploadType string, size int64) mir.Error {
return xerror.InvalidParams
}
if size > 1024*1024*100 {
return _errFileInvalidSize.WithDetails("最大允许100MB")
return web.ErrFileInvalidSize.WithDetails("最大允许100MB")
}
return nil
}
@ -169,7 +169,7 @@ func getFileExt(s string) (string, mir.Error) {
"application/x-zip-compressed":
return ".zip", nil
default:
return "", _errFileInvalidExt.WithDetails("仅允许 png/jpg/gif/mp4/mov/zip 类型")
return "", web.ErrFileInvalidExt.WithDetails("仅允许 png/jpg/gif/mp4/mov/zip 类型")
}
}
@ -202,7 +202,7 @@ func tagsFrom(originTags []string) []string {
// checkPermision 检查是否拥有者或管理员
func checkPermision(user *core.User, targetUserId int64) mir.Error {
if user == nil || (user.ID != targetUserId && !user.IsAdmin) {
return _errNoPermission
return web.ErrNoPermission
}
return nil
}

@ -1,93 +0,0 @@
// 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 web
import (
"github.com/rocboss/paopao-ce/pkg/xerror"
)
// nolint
var (
_errUsernameHasExisted = xerror.NewError(20001, "用户名已存在")
_errUsernameLengthLimit = xerror.NewError(20002, "用户名长度3~12")
_errUsernameCharLimit = xerror.NewError(20003, "用户名只能包含字母、数字")
_errPasswordLengthLimit = xerror.NewError(20004, "密码长度6~16")
_errUserRegisterFailed = xerror.NewError(20005, "用户注册失败")
_errUserHasBeenBanned = xerror.NewError(20006, "该账户已被封停")
_errNoPermission = xerror.NewError(20007, "无权限执行该请求")
_errUserHasBindOTP = xerror.NewError(20008, "当前用户已绑定二次验证")
_errUserOTPInvalid = xerror.NewError(20009, "二次验证码验证失败")
_errUserNoBindOTP = xerror.NewError(20010, "当前用户未绑定二次验证")
_errErrorOldPassword = xerror.NewError(20011, "当前用户密码验证失败")
_errErrorCaptchaPassword = xerror.NewError(20012, "图形验证码验证失败")
_errAccountNoPhoneBind = xerror.NewError(20013, "拒绝操作: 账户未绑定手机号")
_errTooManyLoginError = xerror.NewError(20014, "登录失败次数过多,请稍后再试")
_errGetPhoneCaptchaError = xerror.NewError(20015, "短信验证码获取失败")
_errTooManyPhoneCaptchaSend = xerror.NewError(20016, "短信验证码获取次数已达今日上限")
_errExistedUserPhone = xerror.NewError(20017, "该手机号已被绑定")
_errErrorPhoneCaptcha = xerror.NewError(20018, "手机验证码不正确")
_errMaxPhoneCaptchaUseTimes = xerror.NewError(20019, "手机验证码已达最大使用次数")
_errNicknameLengthLimit = xerror.NewError(20020, "昵称长度2~12")
_errNoExistUsername = xerror.NewError(20021, "用户不存在")
_errNoAdminPermission = xerror.NewError(20022, "无管理权限")
_errDisallowUserRegister = xerror.NewError(20023, "系统不允许注册用户")
_errGetPostsFailed = xerror.NewError(30001, "获取动态列表失败")
_errCreatePostFailed = xerror.NewError(30002, "动态发布失败")
_errGetPostFailed = xerror.NewError(30003, "获取动态详情失败")
_errDeletePostFailed = xerror.NewError(30004, "动态删除失败")
_errLockPostFailed = xerror.NewError(30005, "动态锁定失败")
_errGetPostTagsFailed = xerror.NewError(30006, "获取话题列表失败")
_errInvalidDownloadReq = xerror.NewError(30007, "附件下载请求不合法")
_errDownloadReqError = xerror.NewError(30008, "附件下载请求失败")
_errInsuffientDownloadMoney = xerror.NewError(30009, "附件下载失败:账户资金不足")
_errDownloadExecFail = xerror.NewError(30010, "附件下载失败:扣费失败")
_errStickPostFailed = xerror.NewError(30011, "动态置顶失败")
_errVisblePostFailed = xerror.NewError(30012, "更新可见性失败")
_errGetCommentsFailed = xerror.NewError(40001, "获取评论列表失败")
_errCreateCommentFailed = xerror.NewError(40002, "评论发布失败")
_errGetCommentFailed = xerror.NewError(40003, "获取评论详情失败")
_errDeleteCommentFailed = xerror.NewError(40004, "评论删除失败")
_errCreateReplyFailed = xerror.NewError(40005, "评论回复失败")
_errGetReplyFailed = xerror.NewError(40006, "获取评论详情失败")
_errMaxCommentCount = xerror.NewError(40007, "评论数已达最大限制")
_errGetCommentThumbs = xerror.NewError(40008, "获取评论点赞信息失败")
_errGetMessagesFailed = xerror.NewError(50001, "获取消息列表失败")
_errReadMessageFailed = xerror.NewError(50002, "标记消息已读失败")
_errSendWhisperFailed = xerror.NewError(50003, "私信发送失败")
_errNoWhisperToSelf = xerror.NewError(50004, "不允许给自己发送私信")
_errTooManyWhisperNum = xerror.NewError(50005, "今日私信次数已达上限")
_errGetCollectionsFailed = xerror.NewError(60001, "获取收藏列表失败")
_errGetStarsFailed = xerror.NewError(60002, "获取点赞列表失败")
_errRechargeReqFail = xerror.NewError(70001, "充值请求失败")
_errRechargeNotifyError = xerror.NewError(70002, "充值回调失败")
_errGetRechargeFailed = xerror.NewError(70003, "充值详情获取失败")
_errUserWalletBillsFailed = xerror.NewError(70004, "用户钱包账单获取失败")
_errNoRequestingFriendToSelf = xerror.NewError(80001, "不允许添加自己为好友")
_errNotExistFriendId = xerror.NewError(80002, "好友id不存在")
_errSendRequestingFriendFailed = xerror.NewError(80003, "申请添加朋友请求发送失败")
_errAddFriendFailed = xerror.NewError(80004, "添加好友失败")
_errRejectFriendFailed = xerror.NewError(80005, "拒绝好友失败")
_errDeleteFriendFailed = xerror.NewError(80006, "删除好友失败")
_errGetContactsFailed = xerror.NewError(80007, "获取联系人列表失败")
_errNoActionToSelf = xerror.NewError(80008, "不允许对自己操作")
_errFollowTopicFailed = xerror.NewError(90001, "关注话题失败")
_errUnfollowTopicFailed = xerror.NewError(90002, "取消关注话题失败")
_errStickTopicFailed = xerror.NewError(90003, "更行话题置顶状态失败")
_errThumbsUpTweetComment = xerror.NewError(90101, "评论点赞失败")
_errThumbsDownTweetComment = xerror.NewError(90102, "评论点踩失败")
_errThumbsUpTweetReply = xerror.NewError(90103, "评论回复点赞失败")
_errThumbsDownTweetReply = xerror.NewError(90104, "评论回复点踩失败")
_errFileUploadFailed = xerror.NewError(10200, "文件上传失败")
_errFileInvalidExt = xerror.NewError(10201, "文件类型不合法")
_errFileInvalidSize = xerror.NewError(10202, "文件大小超限")
)
Loading…
Cancel
Save