刘光辉
10 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<script lang="ts" setup>
import type { ActionItem, BasicColumn } from '@jnpf/ui/vxeTable';
 
import { computed, ref } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicModal, useModal } from '@jnpf/ui/modal';
import { BasicForm, useForm } from '@jnpf/ui/form';
import { BasicVxeTable, TableAction, useVxeTable } from '@jnpf/ui/vxeTable';
 
import { onlineUtils } from '#/utils/jnpf';
import { $t } from '#/locales';
 
defineOptions({ name: 'MyAppDemo' });
 
const { createMessage } = useMessage();
const [registerModal, { openModal: openFormModal, closeModal }] = useModal();
 
// 模拟数据(实际项目替换为 #/api/xxx 的接口)
interface DemoRow {
  id: string;
  fullName: string;
  status: number;
  creatorTime: string;
}
const mockData: DemoRow[] = [
  { id: '1', fullName: '示例记录 A', status: 1, creatorTime: '2026-06-01 10:00:00' },
  { id: '2', fullName: '示例记录 B', status: 0, creatorTime: '2026-06-02 14:30:00' },
  { id: '3', fullName: '示例记录 C', status: 1, creatorTime: '2026-06-03 09:15:00' },
];
const tableData = ref<DemoRow[]>([...mockData]);
 
const columns: BasicColumn[] = [
  { title: '名称', dataIndex: 'fullName', minWidth: 200 },
  { title: '状态', dataIndex: 'status', width: 100, align: 'center' },
  { title: '创建时间', dataIndex: 'creatorTime', width: 180 },
];
 
// 搜索表单(与内置低代码列表页同一套 BasicForm)
const [registerSearchForm, { getFieldsValue }] = useForm({
  labelWidth: 80,
  compact: true,
  showAdvancedButton: false,
  schemas: [
    {
      field: 'keyword',
      label: $t('common.keyword'),
      component: 'Input',
      componentProps: { placeholder: $t('common.enterKeyword'), submitOnPressEnter: true },
    },
  ],
});
 
// useVxeTable:本地数据用 dataSource;immediate:false 避免空 api 触发请求
const [registerTable] = useVxeTable({
  columns,
  immediate: false,
  dataSource: tableData,
  actionColumn: { width: 120, title: '操作', dataIndex: 'action' },
});
 
function reload() {
  const { keyword } = getFieldsValue() || {};
  tableData.value = keyword ? mockData.filter((o) => o.fullName.includes(keyword)) : [...mockData];
}
 
function getTableActions(record: DemoRow): ActionItem[] {
  return [
    { label: $t('common.editText'), onClick: addOrUpdateHandle.bind(null, record) },
    {
      label: $t('common.delText'),
      color: 'error',
      modelConfirm: { onOk: handleDelete.bind(null, record.id) },
    },
  ];
}
 
function handleDelete(id: string) {
  tableData.value = tableData.value.filter((o) => o.id !== id);
  createMessage.success('删除成功');
}
 
// 编辑弹窗:BasicModal + BasicForm,与低代码页面弹窗一致
const editId = ref('');
const getTitle = computed(() => (editId.value ? $t('common.editText') : $t('common.addText')));
const [registerEditForm, { setFieldsValue, validate, resetFields }] = useForm({
  labelWidth: 80,
  schemas: [
    { field: 'fullName', label: '名称', component: 'Input', componentProps: { placeholder: '请输入' }, rules: [{ required: true, message: '必填' }] },
    { field: 'status', label: '状态', component: 'Switch', defaultValue: 1 },
  ],
});
 
function addOrUpdateHandle(row?: DemoRow) {
  resetFields();
  editId.value = row?.id || '';
  if (row) setFieldsValue({ fullName: row.fullName, status: row.status });
  openFormModal(true);
}
 
async function handleSubmit() {
  const values = await validate();
  if (!values) return;
  if (editId.value) {
    tableData.value = tableData.value.map((o) => (o.id === editId.value ? { ...o, ...values } : o));
  } else {
    tableData.value = [
      ...tableData.value,
      { id: `${Date.now()}`, fullName: values.fullName, status: values.status ? 1 : 0, creatorTime: '—' },
    ];
  }
  createMessage.success('保存成功');
  closeModal();
}
 
// 演示 onlineUtils 平台能力
function showUserInfo() {
  const info = onlineUtils.getUserInfo();
  createMessage.success(`当前用户:${info.userName || info.userAccount}`);
}
function showToastDemo() {
  onlineUtils.toast('来自 onlineUtils 的提示', 'success');
}
function openListDemo() {
  // 若配置了 modelId/menuId,会弹出全局低代码列表弹窗(GlobalListModal 监听)
  onlineUtils.openList({ modelId: '', title: '低代码列表示例' });
}
 
reload();
</script>
 
<template>
  <div class="jnpf-content-wrapper">
    <div class="jnpf-content-wrapper-center">
      <!-- 搜索区 -->
      <div class="jnpf-content-wrapper-search-box">
        <BasicForm @register="registerSearchForm" @submit="reload" @reset="reload" />
      </div>
      <!-- 内容区 -->
      <div class="jnpf-content-wrapper-content">
        <BasicVxeTable @register="registerTable">
          <template #tableTitle>
            <a-button type="primary" pre-icon="icon-ym icon-ym-btn-add" @click="addOrUpdateHandle()">{{ $t('common.addText') }}</a-button>
            <a-button @click="showUserInfo">onlineUtils.getUserInfo</a-button>
            <a-button @click="showToastDemo">onlineUtils.toast</a-button>
            <a-button @click="openListDemo">onlineUtils.openList</a-button>
          </template>
          <template #status="{ row }">
            <a-tag :color="row.status ? 'success' : 'default'">{{ row.status ? '启用' : '禁用' }}</a-tag>
          </template>
          <template #action="{ row }">
            <TableAction :actions="getTableActions(row)" />
          </template>
        </BasicVxeTable>
      </div>
    </div>
 
    <!-- 编辑弹窗 -->
    <BasicModal @register="registerModal" :title="getTitle" @ok="handleSubmit" destroy-on-close>
      <BasicForm @register="registerEditForm" />
    </BasicModal>
  </div>
</template>