feat(hjm/violation): 实现违章记录编辑功能
- 添加编辑模式支持,通过路由参数id判断是否为编辑状态 - 实现获取违章记录详情并填充表单功能 - 区分新增和编辑模式的提交逻辑- 编辑模式下禁用车辆编号输入框- 添加页面间通信机制,支持列表页刷新 - 实现违章记录删除功能 - 更新页面标题根据模式动态显示 - 修改按钮文字根据操作状态显示不同文案 - 调整开发环境API基础URL配置 - 优化请求工具中的本地开发地址配置
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
export const ENV_CONFIG = {
|
||||
// 开发环境
|
||||
development: {
|
||||
API_BASE_URL: 'https://cms-api.websoft.top/api',
|
||||
API_BASE_URL: 'http://127.0.0.1/api',
|
||||
APP_NAME: '开发环境',
|
||||
DEBUG: 'true',
|
||||
},
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export default definePageConfig({
|
||||
navigationBarTitleText: '添加违章记录'
|
||||
navigationBarTitleText: '违章记录'
|
||||
})
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
import {useEffect, useState} from "react";
|
||||
import Taro from '@tarojs/taro'
|
||||
import Taro, {useRouter} from '@tarojs/taro'
|
||||
import {
|
||||
Button,
|
||||
TextArea,
|
||||
Cell,
|
||||
Input,
|
||||
} from '@nutui/nutui-react-taro'
|
||||
import {addHjmViolation} from "@/api/hjm/hjmViolation";
|
||||
import {addHjmViolation, getHjmViolation, updateHjmViolation} from "@/api/hjm/hjmViolation";
|
||||
import {HjmViolation} from "@/api/hjm/hjmViolation/model";
|
||||
|
||||
/**
|
||||
* 添加违章记录页面
|
||||
* 添加/编辑违章记录页面
|
||||
*/
|
||||
function Add() {
|
||||
const {params} = useRouter();
|
||||
const [loading, setLoading] = useState<boolean>(false)
|
||||
const [lastSubmitTime, setLastSubmitTime] = useState<number>(0) // 最后提交时间
|
||||
const [isEditMode, setIsEditMode] = useState<boolean>(false) // 是否为编辑模式
|
||||
const [formData, setFormData] = useState<HjmViolation>({
|
||||
code: '',
|
||||
title: '',
|
||||
@@ -26,6 +28,28 @@ function Add() {
|
||||
// 初始化页面数据
|
||||
const initPageData = async () => {
|
||||
try {
|
||||
// 检查是否为编辑模式
|
||||
if (params.id) {
|
||||
setIsEditMode(true)
|
||||
// 获取违章记录详情
|
||||
try {
|
||||
const violation = await getHjmViolation(Number(params.id));
|
||||
if (violation) {
|
||||
setFormData(violation);
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title: '未找到该违章记录',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取违章记录详情失败:', error)
|
||||
Taro.showToast({
|
||||
title: '获取违章记录详情失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
}
|
||||
setLoading(false)
|
||||
} catch (error) {
|
||||
console.error('初始化失败:', error)
|
||||
@@ -36,7 +60,6 @@ function Add() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 提交表单
|
||||
const handleSubmit = async () => {
|
||||
// 防止重复提交 - 检查loading状态
|
||||
@@ -76,52 +99,53 @@ function Add() {
|
||||
return
|
||||
}
|
||||
|
||||
// if (!formData.money?.trim()) {
|
||||
// Taro.showToast({
|
||||
// title: '请输入处罚金额',
|
||||
// icon: 'none'
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// if (!formData.score?.trim()) {
|
||||
// Taro.showToast({
|
||||
// title: '请输入扣分',
|
||||
// icon: 'none'
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
|
||||
setLoading(true)
|
||||
|
||||
// 构建提交数据
|
||||
const submitData: HjmViolation = {
|
||||
...formData,
|
||||
status: 0 // 0未处理, 1已处理
|
||||
}
|
||||
|
||||
addHjmViolation(submitData).then((res) => {
|
||||
console.log(res)
|
||||
Taro.showToast({
|
||||
title: '提交成功',
|
||||
icon: 'success'
|
||||
})
|
||||
// 清空表单
|
||||
setFormData({
|
||||
code: '',
|
||||
title: '',
|
||||
money: '',
|
||||
score: '',
|
||||
comments: ''
|
||||
})
|
||||
try {
|
||||
if (isEditMode) {
|
||||
// 编辑模式 - 更新违章记录
|
||||
await updateHjmViolation(formData);
|
||||
Taro.showToast({
|
||||
title: '更新成功',
|
||||
icon: 'success'
|
||||
})
|
||||
// 通知列表页面刷新
|
||||
Taro.eventCenter.trigger('violationListRefresh')
|
||||
} else {
|
||||
// 新增模式 - 添加违章记录
|
||||
// 构建提交数据
|
||||
const submitData: HjmViolation = {
|
||||
...formData,
|
||||
status: 0 // 0未处理, 1已处理
|
||||
}
|
||||
await addHjmViolation(submitData);
|
||||
Taro.showToast({
|
||||
title: '提交成功',
|
||||
icon: 'success'
|
||||
})
|
||||
// 通知列表页面刷新
|
||||
Taro.eventCenter.trigger('violationListRefresh')
|
||||
// 清空表单
|
||||
setFormData({
|
||||
code: '',
|
||||
title: '',
|
||||
money: '',
|
||||
score: '',
|
||||
comments: ''
|
||||
})
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
Taro.navigateBack()
|
||||
}, 2000)
|
||||
}).finally(() => {
|
||||
} catch (error) {
|
||||
Taro.showToast({
|
||||
title: isEditMode ? '更新失败' : '提交失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
setLoading(false)
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
@@ -151,7 +175,9 @@ function Add() {
|
||||
{/* marginBottom: '12px'*/}
|
||||
{/* }}>*/}
|
||||
{/* <Truck size={18} color="#1890ff"/>*/}
|
||||
{/* <span style={{fontSize: '16px', fontWeight: 'bold'}}>添加违章记录</span>*/}
|
||||
{/* <span style={{fontSize: '16px', fontWeight: 'bold'}}>*/}
|
||||
{/* {isEditMode ? '编辑违章记录' : '添加违章记录'}*/}
|
||||
{/* </span>*/}
|
||||
{/* </div>*/}
|
||||
{/*</div>*/}
|
||||
|
||||
@@ -167,7 +193,9 @@ function Add() {
|
||||
padding: '16px',
|
||||
borderBottom: '1px solid #f0f0f0'
|
||||
}}>
|
||||
<span style={{fontSize: '16px', fontWeight: 'bold'}}>违章信息</span>
|
||||
<span style={{fontSize: '16px', fontWeight: 'bold'}}>
|
||||
{isEditMode ? '编辑违章记录' : '添加违章记录'}
|
||||
</span>
|
||||
</div>
|
||||
<Cell.Group>
|
||||
<Cell title="车辆编号" style={{padding: '12px 16px'}}>
|
||||
@@ -176,6 +204,7 @@ function Add() {
|
||||
value={formData.code}
|
||||
onChange={(value) => setFormData({...formData, code: value})}
|
||||
style={{backgroundColor: '#ffffff', borderRadius: '8px'}}
|
||||
disabled={isEditMode} // 编辑模式下禁用车辆编号输入
|
||||
/>
|
||||
</Cell>
|
||||
<Cell title="违章标题" style={{padding: '12px 16px'}}>
|
||||
@@ -234,7 +263,7 @@ function Add() {
|
||||
disabled={loading}
|
||||
onClick={handleSubmit}
|
||||
>
|
||||
{loading ? '提交中...' : '提交违章记录'}
|
||||
{loading ? (isEditMode ? '更新中...' : '提交中...') : (isEditMode ? '更新违章记录' : '提交违章记录')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -9,8 +9,8 @@ import {
|
||||
Pagination
|
||||
} from '@nutui/nutui-react-taro'
|
||||
import {Search, Calendar, Truck, File, AddCircle} from '@nutui/icons-react-taro'
|
||||
import Taro from '@tarojs/taro'
|
||||
import {pageHjmViolation} from "@/api/hjm/hjmViolation";
|
||||
import Taro, {useDidShow} from '@tarojs/taro'
|
||||
import {pageHjmViolation, removeHjmViolation} from "@/api/hjm/hjmViolation";
|
||||
import {HjmViolation} from "@/api/hjm/hjmViolation/model";
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ const List: React.FC = () => {
|
||||
const [page, setPage] = useState<number>(1)
|
||||
const [limit, setLimit] = useState<number>(10)
|
||||
const [total, setTotal] = useState<number>(0)
|
||||
const [needRefresh, setNeedRefresh] = useState<boolean>(true)
|
||||
|
||||
console.log(refreshing)
|
||||
// 获取状态显示
|
||||
@@ -54,11 +55,11 @@ const List: React.FC = () => {
|
||||
}
|
||||
|
||||
const roleCode = Taro.getStorageSync('RoleCode');
|
||||
if(roleCode == 'kuaidi'){
|
||||
if(Taro.getStorageSync('OrganizationParentId') == 0){
|
||||
if (roleCode == 'kuaidi') {
|
||||
if (Taro.getStorageSync('OrganizationParentId') == 0) {
|
||||
// @ts-ignore
|
||||
where.organizationParentId = Taro.getStorageSync('OrganizationId');
|
||||
}else {
|
||||
} else {
|
||||
// @ts-ignore
|
||||
where.organizationId = Taro.getStorageSync('OrganizationId');
|
||||
}
|
||||
@@ -93,9 +94,30 @@ const List: React.FC = () => {
|
||||
})
|
||||
}
|
||||
|
||||
// 页面显示时触发,包括从其他页面返回
|
||||
useDidShow(() => {
|
||||
if (needRefresh) {
|
||||
reload(false);
|
||||
setNeedRefresh(false);
|
||||
}
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
reload().then()
|
||||
}, [page, limit])
|
||||
// 监听刷新事件
|
||||
const handleRefresh = () => {
|
||||
setNeedRefresh(true);
|
||||
};
|
||||
|
||||
Taro.eventCenter.on('violationListRefresh', handleRefresh);
|
||||
|
||||
// 初始加载数据
|
||||
reload().then();
|
||||
|
||||
// 清理事件监听器
|
||||
return () => {
|
||||
Taro.eventCenter.off('violationListRefresh', handleRefresh);
|
||||
};
|
||||
}, [page, limit]);
|
||||
|
||||
const onPageChange = (current: number) => {
|
||||
setPage(current)
|
||||
@@ -180,10 +202,10 @@ const List: React.FC = () => {
|
||||
border: '1px solid #f0f0f0'
|
||||
}}
|
||||
onClick={() => {
|
||||
Taro.navigateTo({
|
||||
url: `/hjm/violation/detail?id=${item.code}`
|
||||
})
|
||||
}}
|
||||
Taro.navigateTo({
|
||||
url: `/hjm/violation/detail?id=${item.code}`
|
||||
})
|
||||
}}
|
||||
>
|
||||
<div style={{
|
||||
display: 'flex',
|
||||
@@ -261,6 +283,34 @@ const List: React.FC = () => {
|
||||
}
|
||||
</div>
|
||||
)}
|
||||
{item.userId == Taro.getStorageSync('UserId') && (
|
||||
<div className={'flex justify-end mt-4'}>
|
||||
<Space>
|
||||
<Button type="default" onClick={(event: any) => {
|
||||
event.stopPropagation()
|
||||
Taro.navigateTo({
|
||||
url: `/hjm/violation/add?id=${item.id}`
|
||||
})
|
||||
}}>修改</Button>
|
||||
<Button type="primary" onClick={(event: any) => {
|
||||
event.stopPropagation()
|
||||
removeHjmViolation(item.id).then(() => {
|
||||
Taro.showToast({
|
||||
title: '删除成功',
|
||||
icon: 'success'
|
||||
})
|
||||
// 删除成功后重新加载列表
|
||||
reload(false)
|
||||
}).catch((error) => {
|
||||
Taro.showToast({
|
||||
title: error.message || '删除失败',
|
||||
icon: 'error'
|
||||
})
|
||||
})
|
||||
}}>删除</Button>
|
||||
</Space>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
@@ -281,7 +331,7 @@ const List: React.FC = () => {
|
||||
overflow: "hidden",
|
||||
backgroundColor: '#ff0000',
|
||||
}}>
|
||||
<AddCircle size={28} color={'#ffffff'} onClick={onAddInsurance} />
|
||||
<AddCircle size={28} color={'#ffffff'} onClick={onAddInsurance}/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import {BaseUrl, TenantId} from "@/utils/config";
|
||||
let baseUrl = BaseUrl
|
||||
|
||||
if(process.env.NODE_ENV === 'development'){
|
||||
// baseUrl = 'http://localhost:9000/api'
|
||||
baseUrl = 'http://localhost:9200/api'
|
||||
}
|
||||
export function request<T>(options:any) {
|
||||
const token = Taro.getStorageSync('access_token');
|
||||
|
||||
Reference in New Issue
Block a user