根因:access_token每2小时过期,前端收到401直接清token跳登录,没有用refresh_token续期 后端修复: - adminauth模块新增 POST /admin/auth/refresh 接口 - Service 注入 JWTManager,支持 admin refresh token 换新 token pair - Refresh 方法验证 subjectType=admin + tokenType=refresh 前端修复: - 401 拦截器核心改造:收到401先调 refresh 接口续期 - 加 isRefreshing 锁 + pendingRequests 队列防止并发刷新 - refresh 用原生 axios.post 避免拦截器递归 - 成功则更新 localStorage + 重试原请求,失败才清 token 跳登录 - 排除 /auth/refresh 自身避免死循环 - 支持 /admin/ 请求独立 token 管理 - auth.ts/adminAuth.ts 新增手动 refreshUserToken/refreshAdminSession - 路由守卫给所有需登录路由添加 meta.requiresAuth - 守卫同时支持 PC 端 /login 和移动端 /m/login
160 lines
4.6 KiB
Go
160 lines
4.6 KiB
Go
package order
|
|
|
|
import "errors"
|
|
|
|
var (
|
|
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
|
ErrInvalidRentHours = errors.New("invalid rent hours")
|
|
ErrListingUnavailable = errors.New("listing unavailable")
|
|
ErrCannotRentOwnListing = errors.New("cannot rent own listing")
|
|
ErrOrderCannotCancel = errors.New("order cannot cancel")
|
|
ErrOrderCannotHandoff = errors.New("order cannot handoff")
|
|
ErrOrderCannotReceive = errors.New("order cannot receive")
|
|
ErrOrderCannotComplete = errors.New("order cannot complete")
|
|
ErrCheckoutCannotSubmit = errors.New("checkout cannot submit")
|
|
ErrCheckoutCannotConfirm = errors.New("checkout cannot confirm")
|
|
ErrCheckoutCannotCounter = errors.New("checkout cannot counter")
|
|
ErrInvalidCheckoutAmount = errors.New("invalid checkout amount")
|
|
ErrPermissionDenied = errors.New("permission denied")
|
|
)
|
|
|
|
const internalOrderHours = 24
|
|
|
|
type Service struct {
|
|
repo *Repository
|
|
}
|
|
|
|
func NewService(repo *Repository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
func (s *Service) Create(userID uint64, req CreateRequest) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if req.ListingID == 0 {
|
|
return nil, ErrInvalidRentHours
|
|
}
|
|
return s.repo.Create(userID, req)
|
|
}
|
|
|
|
func (s *Service) Cancel(userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.Cancel(userID, orderID)
|
|
}
|
|
|
|
func (s *Service) SubmitHandoff(userID uint64, orderID uint64, req SubmitHandoffRequest) (*HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Content == "" {
|
|
return nil, ErrOrderCannotHandoff
|
|
}
|
|
return s.repo.SubmitHandoff(userID, orderID, req)
|
|
}
|
|
|
|
func (s *Service) ConfirmReceive(userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ConfirmReceive(userID, orderID)
|
|
}
|
|
|
|
func (s *Service) HandoffRecords(userID uint64, orderID uint64) ([]HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.HandoffRecords(userID, orderID)
|
|
}
|
|
|
|
func (s *Service) SubmitCheckout(userID uint64, orderID uint64, req SubmitCheckoutRequest) (*HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Content == "" {
|
|
return nil, ErrCheckoutCannotSubmit
|
|
}
|
|
return s.repo.SubmitCheckout(userID, orderID, req)
|
|
}
|
|
|
|
func (s *Service) ConfirmCheckout(userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ConfirmCheckout(userID, orderID)
|
|
}
|
|
|
|
func (s *Service) CounterCheckout(userID uint64, orderID uint64, req CounterCheckoutRequest) (*CheckoutDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return nil, ErrCheckoutCannotCounter
|
|
}
|
|
return s.repo.CounterCheckout(userID, orderID, req)
|
|
}
|
|
|
|
func (s *Service) AcceptCheckout(userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AcceptCheckout(userID, orderID)
|
|
}
|
|
|
|
func (s *Service) ListForUser(userID uint64, page int, pageSize int) (*PaginatedOrders, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ListForUser(userID, page, pageSize)
|
|
}
|
|
|
|
func (s *Service) ListAdmin(page int, pageSize int) (*PaginatedOrders, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ListAdmin(page, pageSize)
|
|
}
|
|
|
|
func (s *Service) FindAdmin(orderID uint64) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.FindAdmin(orderID)
|
|
}
|
|
|
|
func (s *Service) HandoffRecordsAdmin(orderID uint64) ([]HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.HandoffRecordsAdmin(orderID)
|
|
}
|
|
|
|
func (s *Service) AdminClose(adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrOrderCannotComplete
|
|
}
|
|
return s.repo.AdminClose(adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminMarkAbnormal(adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrOrderCannotComplete
|
|
}
|
|
return s.repo.AdminMarkAbnormal(adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) FindForUser(userID uint64, orderID uint64) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.FindForUser(userID, orderID)
|
|
}
|