index.vue
6.6 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<template>
<div>
<BasicTable @register="registerTable" v-show="isStatus === 0">
<template #toolbar>
<Authority value="api:yt:convert:js:post">
<a-button type="primary" @click="handleCreate">
{{ t('rule.script.index.add') }}
</a-button>
</Authority>
<Authority value="api:yt:convert:js:delete">
<Popconfirm
:title="t('rule.chain.index.sureBatchDelete')"
:ok-text="t('common.okText')"
:cancel-text="t('common.cancelText')"
@confirm="handleDeleteOrBatchDelete(null)"
>
<a-button color="error" :disabled="hasBatchDelete">
{{ t('rule.script.index.batchDelete') }}
</a-button>
</Popconfirm>
</Authority>
</template>
<template #status="{ record }">
<Authority value="api:yt:convert:js:update">
<Switch
:checked="record.status === 1"
:loading="record.pendingStatus"
:checkedChildren="t('rule.script.index.enable')"
:unCheckedChildren="t('rule.script.index.disable')"
@change="(checked:boolean)=>statusChange(checked,record)"
/>
</Authority>
<Tag
v-if="!hasPermission('api:yt:convert:js:update')"
:color="record.status ? 'green' : 'red'"
>
{{ record.status ? t('rule.script.index.enable') : t('rule.script.index.disable') }}
</Tag>
</template>
<template #action="{ record }">
<TableAction
:actions="[
{
label: t('rule.script.index.singleView'),
auth: 'api:yt:convert:js:get',
icon: 'ant-design:eye-outlined',
onClick: handleView.bind(null, record),
ifShow: record.status == 1,
},
{
label: t('rule.script.index.singleEdit'),
auth: 'api:yt:convert:js:update',
icon: 'clarity:note-edit-line',
onClick: handleEdit.bind(null, record),
ifShow: record.status == 0,
},
{
label: t('rule.script.index.singleDelete'),
auth: 'api:yt:convert:js:delete',
icon: 'ant-design:delete-outlined',
ifShow: record.status == 0,
color: 'error',
popConfirm: {
title: t('rule.script.index.sureDelete'),
confirm: handleDeleteOrBatchDelete.bind(null, record),
},
},
]"
/>
</template>
</BasicTable>
<ScriptDrawer
@register="registerDrawer"
@isStatus="handleIsStatus"
ref="scriptDrawerRef"
@success="handleSuccess"
/>
<TestScript v-show="isStatus === 1" @isStatus="handleCancelStatus" ref="testScriptRef" />
</div>
</template>
<script lang="ts" setup>
import { ref, nextTick } from 'vue';
import { Switch, Popconfirm, Tag } from 'ant-design-vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { columns } from '../config/config.data';
import { getConvertApi, deleteTransformApi } from '/@/api/device/TransformScriptApi';
import { useDrawer } from '/@/components/Drawer/index';
import ScriptDrawer from '../cpns/ScriptDrawer.vue';
import TestScript from '../cpns/TestScript.vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { updateTransformScriptStatusApi } from '/@/api/device/TransformScriptApi';
import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
import { Authority } from '/@/components/Authority';
import { computed, unref } from 'vue';
import { usePermission } from '/@/hooks/web/usePermission';
import { useI18n } from '/@/hooks/web/useI18n';
const { t } = useI18n(); // 加载国际化
const props = defineProps<{ searchInfo: Recordable }>();
const getSearchInfo = computed(() => props.searchInfo);
const { hasPermission } = usePermission();
const handleSuccess = () => {
reload();
};
const [registerTable, { reload, setProps, setSelectedRowKeys }] = useTable({
api: getConvertApi,
title: t('rule.script.index.title'),
columns,
useSearchForm: false,
showTableSetting: true,
bordered: true,
showIndexColumn: false,
clickToRowSelect: false,
rowKey: 'id',
beforeFetch: (params: Recordable) => {
return { ...unref(getSearchInfo), ...params };
},
resizeHeightOffset: 30,
actionColumn: {
width: 180,
title: t('rule.script.index.action'),
dataIndex: 'action',
slots: { customRender: 'action' },
fixed: 'right',
},
});
const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions, resetSelectedRowKeys } =
useBatchDelete(deleteTransformApi, handleSuccess, setProps);
selectionOptions.rowSelection.getCheckboxProps = (record: Recordable) => {
// Demo:status为1的选择框禁用
if (record.status === 1) {
return { disabled: true };
} else {
return { disabled: false };
}
};
nextTick(() => {
setProps(selectionOptions);
});
const [registerDrawer, { openDrawer }] = useDrawer();
const isStatus = ref(0);
const testScriptRef = ref();
const scriptDrawerRef = ref();
const handleIsStatus = ({ status, jsCode }) => {
isStatus.value = status;
testScriptRef.value.aceEditor.setValue(jsCode);
};
const handleCancelStatus = ({ status, emitType }) => {
openDrawer(true);
isStatus.value = status;
if (emitType === 'ok') {
const jsCode = testScriptRef.value.aceEditor.getValue();
scriptDrawerRef.value.aceEditor.setValue(jsCode);
}
};
const handleCreate = () => {
nextTick(() => {
openDrawer(true, {
isUpdate: false,
});
});
};
const handleEdit = (record: Recordable) => {
nextTick(() => {
openDrawer(true, { isUpdate: true, record });
});
};
const statusChange = async (checked, record) => {
setProps({
loading: true,
});
setSelectedRowKeys([]);
resetSelectedRowKeys();
const newStatus = checked ? 1 : 0;
const { createMessage } = useMessage();
try {
await updateTransformScriptStatusApi(newStatus, record.id);
if (newStatus) {
createMessage.success(t('rule.script.index.enableSuccess'));
} else {
createMessage.success(t('rule.script.index.disableSuccess'));
}
} finally {
setProps({
loading: false,
});
reload();
}
};
function handleView(record: Recordable) {
nextTick(() => {
openDrawer(true, {
record,
isUpdate: 'view',
});
});
}
defineExpose({
reload: () => reload(),
});
</script>