gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MpAutoReplyApi } from '#/api/mp/autoReply';
 
import { computed, nextTick, ref } from 'vue';
 
import { DocAlert, Page, useVbenModal } from '..\..\..\packages\effects\common-ui\src';
import { AutoReplyMsgType } from '..\..\..\packages\constants\src';
import { IconifyIcon } from '..\..\..\packages\icons\src';
 
import { message, Row, Tabs } from 'ant-design-vue';
 
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  deleteAutoReply,
  getAutoReply,
  getAutoReplyPage,
} from '#/api/mp/autoReply';
import { $t } from '#/locales';
import { WxAccountSelect } from '#/views/mp/components';
 
import { useGridColumns, useGridFormSchema } from './data';
import ReplyContent from './modules/content.vue';
import Form from './modules/form.vue';
 
defineOptions({ name: 'MpAutoReply' });
 
const msgType = ref<string>(String(AutoReplyMsgType.Keyword)); // 消息类型
 
const showCreateButton = computed(() => {
  if (Number(msgType.value) !== AutoReplyMsgType.Follow) {
    return true;
  }
  try {
    const tableData = gridApi.grid?.getTableData();
    return (tableData?.tableData?.length || 0) <= 0;
  } catch {
    return true;
  }
}); // 计算是否显示新增按钮:关注时回复类型只有在没有数据时才显示
 
/** 刷新表格 */
function handleRefresh() {
  gridApi.query();
}
 
/** 公众号变化时查询数据 */
function handleAccountChange(accountId: number) {
  gridApi.formApi.setValues({ accountId });
  gridApi.formApi.submitForm();
}
 
/** 切换回复类型 */
async function onTabChange(tabName: string) {
  msgType.value = tabName;
  await nextTick();
  // 更新 columns
  const columns = useGridColumns(Number(msgType.value) as AutoReplyMsgType);
  if (columns) {
    // 使用 setGridOptions 更新列配置
    gridApi.setGridOptions({ columns });
    // 等待列配置更新完成
    await nextTick();
  }
  // 查询数据
  await gridApi.query();
}
 
/** 新增自动回复 */
async function handleCreate() {
  const formValues = await gridApi.formApi.getValues();
  formModalApi
    .setData({
      msgType: Number(msgType.value) as AutoReplyMsgType,
      accountId: formValues.accountId,
    })
    .open();
}
 
/** 修改自动回复 */
async function handleEdit(row: MpAutoReplyApi.AutoReply) {
  const data = await getAutoReply(row.id!);
  formModalApi
    .setData({
      msgType: Number(msgType.value) as AutoReplyMsgType,
      accountId: row.accountId,
      row: data,
    })
    .open();
}
 
/** 删除自动回复 */
async function handleDelete(row: MpAutoReplyApi.AutoReply) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', ['自动回复']),
    duration: 0,
  });
  try {
    await deleteAutoReply(row.id!);
    message.success($t('ui.actionMessage.deleteSuccess'));
    handleRefresh();
  } finally {
    hideLoading();
  }
}
 
const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: Form,
  destroyOnClose: true,
});
 
const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useGridColumns(Number(msgType.value) as AutoReplyMsgType),
    height: 'auto',
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getAutoReplyPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            type: Number(msgType.value) as AutoReplyMsgType,
            ...formValues,
          });
        },
      },
      autoLoad: false,
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<MpAutoReplyApi.AutoReply>,
});
</script>
 
<template>
  <Page auto-content-height>
    <template #doc>
      <DocAlert title="自动回复" url="https://doc.iocoder.cn/mp/auto-reply/" />
    </template>
 
    <FormModal @success="handleRefresh" />
    <Grid>
      <template #form-accountId>
        <WxAccountSelect @change="handleAccountChange" />
      </template>
      <template #toolbar-actions>
        <Tabs
          v-model:active-key="msgType"
          class="w-full"
          @change="(activeKey) => onTabChange(activeKey as string)"
        >
          <Tabs.TabPane :key="String(AutoReplyMsgType.Follow)">
            <template #tab>
              <Row align="middle">
                <IconifyIcon icon="lucide:star" class="mr-2px" /> 关注时回复
              </Row>
            </template>
          </Tabs.TabPane>
          <Tabs.TabPane :key="String(AutoReplyMsgType.Message)">
            <template #tab>
              <Row align="middle">
                <IconifyIcon icon="lucide:message-circle-more" class="mr-2px" />
                消息回复
              </Row>
            </template>
          </Tabs.TabPane>
          <Tabs.TabPane :key="String(AutoReplyMsgType.Keyword)">
            <template #tab>
              <Row align="middle">
                <IconifyIcon icon="lucide:newspaper" class="mr-2px" />
                关键词回复
              </Row>
            </template>
          </Tabs.TabPane>
        </Tabs>
      </template>
      <template #toolbar-tools>
        <TableAction
          v-if="showCreateButton"
          :actions="[
            {
              label: $t('ui.actionTitle.create', ['自动回复']),
              type: 'primary',
              icon: ACTION_ICON.ADD,
              auth: ['mp:auto-reply:create'],
              onClick: handleCreate,
            },
          ]"
        />
      </template>
      <template #replyContent="{ row }">
        <ReplyContent :row="row" />
      </template>
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: $t('common.edit'),
              type: 'link',
              icon: ACTION_ICON.EDIT,
              auth: ['mp:auto-reply:update'],
              onClick: handleEdit.bind(null, row),
            },
            {
              label: $t('common.delete'),
              type: 'link',
              danger: true,
              icon: ACTION_ICON.DELETE,
              auth: ['mp:auto-reply:delete'],
              popConfirm: {
                title: '是否确认删除此数据?',
                confirm: handleDelete.bind(null, row),
              },
            },
          ]"
        />
      </template>
    </Grid>
  </Page>
</template>