112 lines
3.4 KiB
TypeScript
112 lines
3.4 KiB
TypeScript
|
|
import { useEffect, useState } from 'react'
|
||
|
|
import { useNavigate, useLocation } from 'react-router-dom'
|
||
|
|
import { SpinLoading, Toast } from 'antd-mobile'
|
||
|
|
import { approvalApi } from '@/services/api'
|
||
|
|
|
||
|
|
export default function QuickAction() {
|
||
|
|
const navigate = useNavigate()
|
||
|
|
const location = useLocation()
|
||
|
|
const [loading, setLoading] = useState(true)
|
||
|
|
const [status, setStatus] = useState('processing')
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
const processAction = async () => {
|
||
|
|
try {
|
||
|
|
const urlParams = new URLSearchParams(window.location.search)
|
||
|
|
const action = urlParams.get('action') as 'approve' | 'reject' | null
|
||
|
|
const orderId = urlParams.get('order_id')
|
||
|
|
const token = localStorage.getItem('token')
|
||
|
|
|
||
|
|
console.log('🚀 快速审批:', { action, orderId, hasToken: !!token })
|
||
|
|
|
||
|
|
if (!action || !orderId) {
|
||
|
|
setStatus('error')
|
||
|
|
Toast.show('参数错误')
|
||
|
|
setTimeout(() => navigate('/login', { replace: true }), 1500)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查是否登录
|
||
|
|
if (!token) {
|
||
|
|
console.log('⚠️ 未登录,跳转到登录页')
|
||
|
|
setStatus('unauthorized')
|
||
|
|
Toast.show('请先登录')
|
||
|
|
setTimeout(() => {
|
||
|
|
window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname + window.location.search)
|
||
|
|
}, 1500)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
setStatus('processing')
|
||
|
|
|
||
|
|
// 执行审批操作
|
||
|
|
if (action === 'approve') {
|
||
|
|
console.log('✅ 执行通过操作')
|
||
|
|
await approvalApi.approve(Number(orderId), '')
|
||
|
|
Toast.show('已通过')
|
||
|
|
} else if (action === 'reject') {
|
||
|
|
console.log('❌ 执行拒绝操作')
|
||
|
|
await approvalApi.reject(Number(orderId), '已拒绝')
|
||
|
|
Toast.show('已拒绝')
|
||
|
|
}
|
||
|
|
|
||
|
|
setStatus('success')
|
||
|
|
|
||
|
|
// 操作成功后关闭页面(在飞书客户端中)
|
||
|
|
setTimeout(() => {
|
||
|
|
console.log('✅ 操作完成,关闭页面')
|
||
|
|
// 在飞书客户端中,关闭当前页面
|
||
|
|
if ((window as any).tt && (window as any).tt.close) {
|
||
|
|
(window as any).tt.close()
|
||
|
|
} else {
|
||
|
|
// 其他情况跳转到审批列表
|
||
|
|
navigate('/approval', { replace: true })
|
||
|
|
}
|
||
|
|
}, 1000)
|
||
|
|
|
||
|
|
} catch (error: any) {
|
||
|
|
console.error('❌ 操作失败:', error)
|
||
|
|
setStatus('error')
|
||
|
|
Toast.show(error.message || '操作失败')
|
||
|
|
setTimeout(() => navigate('/approval', { replace: true }), 2000)
|
||
|
|
} finally {
|
||
|
|
setLoading(false)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
processAction()
|
||
|
|
}, [])
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div style={{
|
||
|
|
display: 'flex',
|
||
|
|
flexDirection: 'column',
|
||
|
|
alignItems: 'center',
|
||
|
|
justifyContent: 'center',
|
||
|
|
height: '100vh',
|
||
|
|
background: '#f5f5f5'
|
||
|
|
}}>
|
||
|
|
{loading && (
|
||
|
|
<>
|
||
|
|
<SpinLoading color='primary' />
|
||
|
|
<p style={{ marginTop: '16px', color: '#999' }}>处理中...</p>
|
||
|
|
</>
|
||
|
|
)}
|
||
|
|
|
||
|
|
{status === 'success' && (
|
||
|
|
<>
|
||
|
|
<div style={{ fontSize: '48px' }}>✅</div>
|
||
|
|
<p style={{ marginTop: '16px', color: '#52c41a' }}>操作成功</p>
|
||
|
|
</>
|
||
|
|
)}
|
||
|
|
|
||
|
|
{status === 'error' && (
|
||
|
|
<>
|
||
|
|
<div style={{ fontSize: '48px' }}>❌</div>
|
||
|
|
<p style={{ marginTop: '16px', color: '#ff4d4f' }}>操作失败</p>
|
||
|
|
</>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
)
|
||
|
|
}
|