Commit 70b9e0d4 by Sweet Zhang

按类型封装api

parent b09264c7
VITE_API_BASE_URL='http://139.224.145.34:9002/email/api'
\ No newline at end of file
VITE_API_BASE_URL='/email/api'
\ No newline at end of file
interface resPage {
page: number
pageSize: number
total: number
hasNextPage: boolean
pageTotal: number
}
interface itfRes {
code: number
msg: string | undefined
data: T
reason?: string
page?: resPage
}
import * as axios from 'axios'
declare module 'axios'
{interface AxiosResponse extends itfRes}
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<object, object, unknown>
export default component
}
// 分页类型
export interface Pagination<T> {
records?: T[]
total?: number
pageSize?: number
sortField?: string
sortOrder?: string
pageNo?: number
}
// 联系人类型
export interface Contact {
export interface Contact extends Pagination<Contact> {
contactBizId?: string
name?: string
type?: string
......@@ -11,17 +21,19 @@ export interface Contact {
}
// 发件人类型
export interface Sender {
export interface Sender extends Pagination<Sender> {
senderBizId?: string
email?: string
password?: string
displayName?: string
providerBizId?: string
active?: number
emailSenderConfigName?: string
emailSenderConfigEmail?: string
}
// 变量类型
export interface Variable {
export interface Variable extends Pagination<Variable> {
id?: string
variableBizId?: string
variableNameCn?: string
......@@ -30,7 +42,7 @@ export interface Variable {
}
// 变量模板类型
export interface VariableTemplate {
export interface VariableTemplate extends Pagination<VariableTemplate> {
variableGroupBizId?: string
groupName?: string
description?: string
......@@ -39,7 +51,7 @@ export interface VariableTemplate {
}
// 邮件类型
export interface Email {
export interface Email extends Pagination<Email> {
id?: string
sender?: string
to?: string[]
......@@ -52,7 +64,7 @@ export interface Email {
}
// 邮件表单类型
export interface EmailForm {
export interface EmailForm extends Pagination<EmailForm> {
to: string
cc: string
subject: string
......@@ -84,12 +96,27 @@ export interface ContactSessionId {
}
// 编辑-邮件联系人导入信息
export interface EditContactImport {
export interface EditContactImport extends Pagination<EditContactImport> {
receiveEmail?: string
sessionId?: string
source?: string
pageNo?: number
pageSize?: number
sortField?: string
sortOrder?: string
}
// 发送邮件
export interface SendEmail {
senderBizId?: string
recipientEmailList?: string[]
ccEmailList?: string[]
bccEmailList?: string[]
subject?: string
body?: string
}
//邮件服务商类型
export interface EmailProvider extends Pagination<EmailProvider> {
providerBizId?: string
providerName?: string
smtpHost?: string
smtpPort?: string
sslEnabled?: number
active?: number
description?: string
}
import axios, { AxiosError } from 'axios'
import axios, { type AxiosError, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios'
// 定义统一的响应类型
export interface ApiResponse<T = object> {
code: number
message?: string
data: T
success: boolean
msg?: string
}
// 创建axios实例
const request = axios.create({
......@@ -14,7 +23,7 @@ const request = axios.create({
// 请求拦截器 - 添加Authorization头
request.interceptors.request.use(
(config) => {
(config: InternalAxiosRequestConfig) => {
// 从本地存储获取token
const token = localStorage.getItem('authToken')
......@@ -33,7 +42,7 @@ request.interceptors.request.use(
// 响应拦截器 - 处理常见错误
request.interceptors.response.use(
(response) => {
(response: AxiosResponse) => {
// 直接返回响应数据
return response.data
},
......
......@@ -7,7 +7,7 @@
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
>
<option v-for="sender in senders" :key="sender.senderBizId" :value="sender">
{{ sender.email }}
{{ sender.email }} - {{ sender.displayName }}
</option>
</select>
</div>
......@@ -43,6 +43,7 @@
type="text"
class="flex-1 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
placeholder="输入收件人邮箱,多个邮箱用逗号分隔"
:disabled="!!selectedVariableTemplate"
/>
<!-- 当选择模版有值时,不显示选择联系人按钮 -->
<button
......@@ -69,6 +70,7 @@
type="text"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
placeholder="输入抄送人邮箱,多个邮箱用逗号分隔"
:disabled="!!selectedVariableTemplate"
/>
</div>
......@@ -261,12 +263,7 @@ import type {
ImportRecord,
} from '../types'
// 引入api接口,获取联系人列表、发件人列表、变量模版列表
import {
getContactList,
getEmailSenderConfigList,
getEmailVariableGroupList,
getEmailVariableGroupDetail,
} from '../api/api'
import { senderApi, variableGroupApi, contactApi, getContactList } from '../api/api'
// 引入弹窗组件
import CommonModal from '@/components/CommonModal.vue'
// 弹窗提示信息对象
......@@ -301,28 +298,34 @@ const handleCancel = (triggerKey: string) => {
}
// 初始化数据
const getSenders = () => {
getEmailSenderConfigList({
pageNo: 1,
pageSize: 100,
}).then((res) => {
senders.value = res.data?.records || []
})
senderApi
.getEmailSenderConfigList({
pageNo: 1,
pageSize: 100,
})
.then((res) => {
senders.value = res.data?.records || []
})
}
const getContacts = () => {
getContactList({
pageNo: 1,
pageSize: 100,
}).then((res) => {
contacts.value = res.data?.records || []
})
contactApi
.getContactList({
pageNo: 1,
pageSize: 100,
})
.then((res) => {
contacts.value = res.data?.records || []
})
}
const getGroups = () => {
getEmailVariableGroupList({
pageNo: 1,
pageSize: 100,
}).then((res) => {
groups.value = res.data?.records || []
})
variableGroupApi
.getEmailVariableGroupList({
pageNo: 1,
pageSize: 100,
})
.then((res) => {
groups.value = res.data?.records || []
})
}
const senders = ref<Sender[]>([])
......@@ -388,9 +391,11 @@ const removeAttachment = (index: number) => {
const applyVariableTemplate = () => {
if (!selectedVariableTemplate.value) return
if (selectedVariableTemplate.value.variableGroupBizId) {
getEmailVariableGroupDetail(selectedVariableTemplate.value.variableGroupBizId).then((res) => {
variables.value = res.data?.emailVariableDtoList || []
})
variableGroupApi
.getEmailVariableGroupDetail(selectedVariableTemplate.value.variableGroupBizId)
.then((res) => {
variables.value = res.data?.emailVariableDtoList || []
})
}
}
......
......@@ -319,14 +319,7 @@
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
// 引入我们创建的api拦截器
import {
addContact,
editContact,
deleteContact,
getContactDetail,
getContactList,
getEmailSenderConfigList,
} from '@/api/api'
import { contactApi } from '@/api/api'
import type { Contact } from '@/types/index'
// 引入弹窗组件
import CommonModal from '@/components/CommonModal.vue'
......@@ -355,7 +348,7 @@ const handleConfirm = (triggerKey: string) => {
modalVisible.value = false
console.log('用户确认操作', triggerKey)
if (triggerKey === 'deleteContactModal' && form.value.contactBizId) {
deleteContact(form.value.contactBizId as string).then((res) => {
contactApi.deleteContact(form.value.contactBizId as string).then((res) => {
if (res.code === 200) {
fetchContacts()
openModal({
......@@ -476,7 +469,7 @@ const totalPages = computed(() => {
const fetchContacts = async () => {
try {
isLoading.value = true
const data = await getContactList({
const data = await contactApi.getContactList({
pageNo: currentPage.value,
pageSize: pageSize.value,
sortField: sortBy.value,
......@@ -614,7 +607,7 @@ const saveContact = async () => {
if (isEditing.value && form.value.contactBizId) {
// 更新现有联系人
editContact({ ...contactData }).then((res) => {
contactApi.updateContact({ ...contactData }).then((res) => {
if (res.code === 200) {
openModal({
title: '更新联系人成功',
......@@ -631,7 +624,7 @@ const saveContact = async () => {
})
} else {
// 添加新联系人
await addContact(contactData).then((res) => {
await contactApi.addContact(contactData).then((res) => {
if (res.code === 200) {
openModal({
title: '添加联系人成功',
......
......@@ -102,8 +102,8 @@
<td class="px-6 py-4 whitespace-nowrap">{{ sender.email }}</td>
<td class="px-6 py-4 whitespace-nowrap">
{{
providers.find((provider) => provider.providerBizId === sender.providerBizId)
?.providerName || '未知'
providers.find((i) => i.providerBizId === sender.providerBizId)?.providerName ||
'未知'
}}
</td>
<td class="px-6 py-4 whitespace-nowrap">{{ sender.displayName }}</td>
......@@ -149,13 +149,7 @@
<script setup lang="ts">
import { ref, defineProps, defineEmits, onMounted } from 'vue'
import {
getEmailSenderConfigList,
getEmailProviderList,
deleteEmailSenderConfig,
addEmailSenderConfig,
editEmailSenderConfig,
} from '@/api/api'
import { emailProviderApi, senderApi, getEmailSenderConfigList } from '@/api/api'
import type { Sender } from '@/types/index'
// 引入弹窗组件
import CommonModal from '@/components/CommonModal.vue'
......@@ -184,7 +178,7 @@ const handleConfirm = (triggerKey: string) => {
modalVisible.value = false
console.log('用户确认操作', triggerKey)
if (triggerKey === 'deleteModal') {
deleteEmailSenderConfig(editingSenderId.value).then((res) => {
senderApi.deleteEmailSenderConfig(editingSenderId.value).then((res) => {
if (res.code === 200) {
openModal({
title: '删除确认',
......@@ -235,22 +229,24 @@ const resetForm = () => {
}
}
const getProviders = () => {
getEmailProviderList({
providerName: '',
pageNo: 1,
pageSize: 100,
}).then((res) => {
providers.value = res.data?.records || []
})
emailProviderApi
.getEmailProviderList({
providerName: '',
})
.then((res) => {
providers.value = res.data?.records || []
})
}
const getSenders = () => {
getEmailSenderConfigList({
emailSenderConfigName: '',
pageNo: 1,
pageSize: 100,
}).then((res) => {
senders.value = res.data?.records || []
})
senderApi
.getEmailSenderConfigList({
emailSenderConfigName: '',
pageNo: 1,
pageSize: 100,
})
.then((res) => {
senders.value = res.data?.records || []
})
}
const saveSender = () => {
console.log(formData.value)
......@@ -264,23 +260,25 @@ const saveSender = () => {
if (editingSenderId.value) {
// 更新现有发件人
editEmailSenderConfig({
senderBizId: editingSenderId.value,
...formData.value,
}).then((res) => {
if (res.code === 200) {
getSenders()
openModal({
title: '更新确认',
message: '发件人更新成功',
})
} else {
openModal({
title: '更新失败',
message: res.msg || '更新失败',
})
}
})
senderApi
.editEmailSenderConfig({
senderBizId: editingSenderId.value,
...formData.value,
})
.then((res) => {
if (res.code === 200) {
getSenders()
openModal({
title: '更新确认',
message: '发件人更新成功',
})
} else {
openModal({
title: '更新失败',
message: res.msg || '更新失败',
})
}
})
} else {
// 添加新发件人
const newSender: Sender = {
......@@ -290,7 +288,7 @@ const saveSender = () => {
displayName: formData.value.displayName || '',
active: formData.value.active ?? 1,
}
addEmailSenderConfig(newSender).then((res) => {
senderApi.addEmailSenderConfig(newSender).then((res) => {
// 补充异常处理
if (res.code === 200) {
getSenders()
......
......@@ -290,17 +290,7 @@
import { ref, defineProps, defineEmits, onMounted } from 'vue'
import type { Variable, VariableTemplate } from '../types'
import {
getEmailVariableList,
addEmailVariable,
editEmailVariable,
deleteEmailVariable,
addEmailVariableGroup,
editEmailVariableGroup,
deleteEmailVariableGroup,
getEmailVariableGroupList,
getEmailVariableGroupDetail,
} from '@/api/api'
import { variableApi, variableGroupApi } from '@/api/api'
// 引入弹窗组件
import CommonModal from '@/components/CommonModal.vue'
......@@ -386,26 +376,29 @@ const saveVariable = () => {
if (editingVariableId.value) {
// 更新接口
editEmailVariable({
variableBizId: variableForm.value.variableBizId || '',
variableNameCn: variableForm.value.variableNameCn,
variableNameEn: variableForm.value.variableNameEn,
description: variableForm.value.description,
}).then(() => {
// 更新本地变量列表
fetchVariables()
openModal({
title: '成功',
message: '变量更新成功',
variableApi
.editEmailVariable({
variableBizId: variableForm.value.variableBizId || '',
variableNameCn: variableForm.value.variableNameCn,
variableNameEn: variableForm.value.variableNameEn,
description: variableForm.value.description,
})
.then(() => {
// 更新本地变量列表
fetchVariables()
openModal({
title: '成功',
message: '变量更新成功',
})
})
})
} else {
// 创建接口
addEmailVariable({
variableNameCn: variableForm.value.variableNameCn,
variableNameEn: variableForm.value.variableNameEn,
description: variableForm.value.description,
})
variableApi
.addEmailVariable({
variableNameCn: variableForm.value.variableNameCn,
variableNameEn: variableForm.value.variableNameEn,
description: variableForm.value.description,
})
.then((res) => {
// 刷新变量列表
fetchVariables()
......@@ -441,7 +434,8 @@ const editVariable = (variable: Variable) => {
const deleteVariable = (id: string, type?: string) => {
editingVariableId.value = id
if (id && type === 'confirmDelete') {
deleteEmailVariable(id)
variableApi
.deleteEmailVariable(id)
.then(() => {
// 刷新变量列表
fetchVariables()
......@@ -483,7 +477,8 @@ const fetchVariables = () => {
pageNo: 1,
pageSize: 10,
}
getEmailVariableList(params)
variableApi
.getEmailVariableList(params)
.then((res) => {
variables.value = res.data.records || []
})
......@@ -508,7 +503,8 @@ const fetchVariableTemplates = () => {
pageNo: 1,
pageSize: 10,
}
getEmailVariableGroupList(params)
variableGroupApi
.getEmailVariableGroupList(params)
.then((res) => {
variableTemplates.value = res.data.records || []
})
......@@ -581,12 +577,13 @@ const saveVariableTemplate = () => {
if (editingTemplateId.value) {
console.log('更新变量模版', templateForm.value)
// 更新现有模板
editEmailVariableGroup({
variableGroupBizId: editingTemplateId.value,
groupName: templateForm.value.groupName || '',
description: templateForm.value.description || '',
variableBizIdList: templateForm.value.variableBizIdList || [],
})
variableGroupApi
.editEmailVariableGroup({
variableGroupBizId: editingTemplateId.value,
groupName: templateForm.value.groupName || '',
description: templateForm.value.description || '',
variableBizIdList: templateForm.value.variableBizIdList || [],
})
.then(() => {
// 刷新变量模版列表
fetchVariableTemplates()
......@@ -607,11 +604,12 @@ const saveVariableTemplate = () => {
})
} else {
// 调用变量组保存接口
addEmailVariableGroup({
groupName: templateForm.value.groupName || '',
description: templateForm.value.description || '',
variableBizIdList: templateForm.value.variableBizIdList || [],
})
variableGroupApi
.addEmailVariableGroup({
groupName: templateForm.value.groupName || '',
description: templateForm.value.description || '',
variableBizIdList: templateForm.value.variableBizIdList || [],
})
.then(() => {
// 刷新变量模版列表
fetchVariableTemplates()
......@@ -638,7 +636,8 @@ const saveVariableTemplate = () => {
const deleteVariableTemplate = (id: string, type?: string) => {
editingTemplateId.value = id
if (type === 'confirmDelete') {
deleteEmailVariableGroup(editingTemplateId.value)
variableGroupApi
.deleteEmailVariableGroup(editingTemplateId.value)
.then(() => {
// 刷新变量模版列表
fetchVariableTemplates()
......
{
"files": [],
"references": [
{
"path": "./tsconfig.node.json"
},
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.vitest.json"
}
],
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"strict": false
}
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"composite": true,
"declaration": true,
"declarationMap": true,
"baseUrl": "/yd-email",
"paths": {
"@/*": ["src/*"],
"@/components/*": ["src/components/*"],
"@/views/*": ["src/views/*"],
"@/utils/*": ["src/utils/*"],
"@/api/*": ["src/api/*"],
"@/types/*": ["src/types/*"],
"@/stores/*": ["src/stores/*"],
"@/assets/*": ["src/assets/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "src/**/*.d.ts", "src/shims-vue.d.ts"],
"exclude": ["node_modules", "dist"]
}
......@@ -3,7 +3,7 @@ import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
/**设置server转发 */
import { resolve } from 'path'
export default defineConfig({
// 关键配置:设置基础路径为子目录 yd-email
......@@ -12,6 +12,7 @@ export default defineConfig({
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@/components': resolve(__dirname, 'src/components'),
},
},
// 添加CSS配置
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment