index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <template>
  2. <div class="p-2">
  3. <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
  4. <div v-show="showSearch" class="mb-[10px]">
  5. <el-card shadow="hover">
  6. <el-form ref="queryFormRef" :model="queryParams" :inline="true">
  7. <el-form-item label="分类名称" prop="name">
  8. <el-input v-model="queryParams.name" placeholder="请输入分类名称" clearable @keyup.enter="handleQuery" />
  9. </el-form-item>
  10. <el-form-item label="唯一编码" prop="code">
  11. <el-input v-model="queryParams.code" placeholder="请输入唯一编码" clearable @keyup.enter="handleQuery" />
  12. </el-form-item>
  13. <el-form-item>
  14. <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
  15. <el-button icon="Refresh" @click="resetQuery">重置</el-button>
  16. </el-form-item>
  17. </el-form>
  18. </el-card>
  19. </div>
  20. </transition>
  21. <el-card shadow="never">
  22. <template #header>
  23. <el-row :gutter="10" class="mb8">
  24. <el-col :span="1.5">
  25. <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['business:category:add']">新增</el-button>
  26. </el-col>
  27. <el-col :span="1.5">
  28. <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['business:category:edit']"
  29. >修改</el-button
  30. >
  31. </el-col>
  32. <el-col :span="1.5">
  33. <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['business:category:remove']"
  34. >删除</el-button
  35. >
  36. </el-col>
  37. <!-- <el-col :span="1.5">
  38. <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:category:export']">导出</el-button>
  39. </el-col>-->
  40. <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
  41. </el-row>
  42. </template>
  43. <el-table v-loading="loading" border :data="categoryList" @selection-change="handleSelectionChange">
  44. <el-table-column type="selection" width="55" align="center" />
  45. <el-table-column label="" align="center" prop="id" v-if="false" />
  46. <el-table-column label="分类名称" align="center" prop="name" />
  47. <el-table-column label="唯一编码" align="center" prop="code" />
  48. <el-table-column label="排序" align="center" prop="sort" />
  49. <el-table-column label="图标" align="center" prop="iconUrl" width="120">
  50. <template #default="scope">
  51. <img v-if="scope.row.iconUrl" :src="scope.row.iconUrl" alt="" style="width: 40px; height: 40px; object-fit: cover; border-radius: 4px" />
  52. <span v-else>无</span>
  53. </template>
  54. </el-table-column>
  55. <el-table-column label="是否显示" align="center" prop="isShow" />
  56. <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
  57. <template #default="scope">
  58. <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
  59. </template>
  60. </el-table-column>
  61. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  62. <template #default="scope">
  63. <el-tooltip content="修改" placement="top">
  64. <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:category:edit']"></el-button>
  65. </el-tooltip>
  66. <el-tooltip content="删除" placement="top">
  67. <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:category:remove']"></el-button>
  68. </el-tooltip>
  69. </template>
  70. </el-table-column>
  71. </el-table>
  72. <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  73. </el-card>
  74. <!-- 添加或修改新闻分类对话框 -->
  75. <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
  76. <el-form ref="categoryFormRef" :model="form" :rules="rules" label-width="80px">
  77. <el-form-item label="分类名称" prop="name">
  78. <el-input v-model="form.name" placeholder="请输入分类名称" />
  79. </el-form-item>
  80. <el-form-item label="唯一编码" prop="code">
  81. <el-input v-model="form.code" placeholder="请输入唯一编码" />
  82. </el-form-item>
  83. <el-form-item label="排序" prop="sort">
  84. <el-input v-model="form.sort" placeholder="请输入排序,越大越靠前" />
  85. </el-form-item>
  86. <el-form-item label="图标" prop="iconUrl">
  87. <div class="upload-container">
  88. <el-upload
  89. class="upload-icon"
  90. action="#"
  91. :on-change="handleIconChange"
  92. :on-remove="handleIconRemove"
  93. :file-list="fileList"
  94. :auto-upload="false"
  95. :limit="1"
  96. accept="image/*"
  97. >
  98. <template #trigger>
  99. <el-button type="primary">点击选择图标</el-button>
  100. </template>
  101. <!-- 预览图区域 -->
  102. <template #default>
  103. <div class="preview-area" @click="handlePreviewClick">
  104. <img
  105. v-if="iconPreviewUrl || form.iconUrl"
  106. :src="iconPreviewUrl || form.iconUrl"
  107. alt="预览图"
  108. style="max-width: 100px; max-height: 100px; margin-top: 10px; cursor: pointer"
  109. />
  110. <div v-else style="margin-top: 10px; color: #999">无</div>
  111. </div>
  112. </template>
  113. <template #tip>
  114. <div class="el-upload__tip">
  115. <span v-if="fileList.length > 0">当前已选文件:{{ fileList[0].name }}</span>
  116. </div>
  117. </template>
  118. </el-upload>
  119. </div>
  120. </el-form-item>
  121. <el-form-item label="是否显示" prop="isShow">
  122. <el-radio-group v-model="form.isShow">
  123. <el-radio :label="1">是</el-radio>
  124. <el-radio :label="0">否</el-radio>
  125. </el-radio-group>
  126. </el-form-item>
  127. </el-form>
  128. <template #footer>
  129. <div class="dialog-footer">
  130. <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
  131. <el-button @click="cancel">取 消</el-button>
  132. </div>
  133. </template>
  134. </el-dialog>
  135. </div>
  136. </template>
  137. <script setup name="Category" lang="ts">
  138. import { listCategory, getCategory, delCategory, addCategory, updateCategory } from '@/api/system/business/category';
  139. import { CategoryVO, CategoryQuery, CategoryForm } from '@/api/system/business/category/types';
  140. import { uploadTournament } from '@/api/system/business/tournaments';
  141. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  142. const categoryList = ref<CategoryVO[]>([]);
  143. const buttonLoading = ref(false);
  144. const loading = ref(true);
  145. const showSearch = ref(true);
  146. const ids = ref<Array<string | number>>([]);
  147. const single = ref(true);
  148. const multiple = ref(true);
  149. const total = ref(0);
  150. const queryFormRef = ref<ElFormInstance>();
  151. const categoryFormRef = ref<ElFormInstance>();
  152. const dialog = reactive<DialogOption>({
  153. visible: false,
  154. title: ''
  155. });
  156. const initFormData: CategoryForm = {
  157. id: undefined,
  158. name: undefined,
  159. code: undefined,
  160. sort: undefined,
  161. isShow: 1,
  162. createdAt: undefined,
  163. updatedAt: undefined
  164. };
  165. const data = reactive<PageData<CategoryForm, CategoryQuery>>({
  166. form: { ...initFormData },
  167. queryParams: {
  168. pageNum: 1,
  169. pageSize: 10,
  170. name: undefined,
  171. code: undefined,
  172. sort: undefined,
  173. isShow: undefined,
  174. createdAt: undefined,
  175. updatedAt: undefined,
  176. params: {}
  177. },
  178. rules: {
  179. id: [{ required: true, message: '不能为空', trigger: 'blur' }],
  180. name: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
  181. code: [{ required: true, message: '唯一编码,用于前端请求,如 hot/sports不能为空', trigger: 'blur' }],
  182. sort: [
  183. { required: true, message: '排序不能为空', trigger: 'blur' },
  184. { pattern: /^[0-9]*$/, message: '只能输入数字', trigger: 'blur' }
  185. ]
  186. }
  187. });
  188. const { queryParams, form, rules } = toRefs(data);
  189. /** 查询新闻分类列表 */
  190. const getList = async () => {
  191. loading.value = true;
  192. const res = await listCategory(queryParams.value);
  193. categoryList.value = res.rows;
  194. total.value = res.total;
  195. loading.value = false;
  196. };
  197. /** 取消按钮 */
  198. const cancel = () => {
  199. reset();
  200. dialog.visible = false;
  201. };
  202. /** 表单重置 */
  203. const reset = () => {
  204. form.value = { ...initFormData };
  205. categoryFormRef.value?.resetFields();
  206. // 清空上传相关状态
  207. fileList.value = [];
  208. iconPreviewUrl.value = '';
  209. form.value.iconUrl = '';
  210. };
  211. /** 搜索按钮操作 */
  212. const handleQuery = () => {
  213. queryParams.value.pageNum = 1;
  214. getList();
  215. };
  216. /** 重置按钮操作 */
  217. const resetQuery = () => {
  218. queryFormRef.value?.resetFields();
  219. handleQuery();
  220. };
  221. /** 多选框选中数据 */
  222. const handleSelectionChange = (selection: CategoryVO[]) => {
  223. ids.value = selection.map((item) => item.id);
  224. single.value = selection.length != 1;
  225. multiple.value = !selection.length;
  226. };
  227. /** 新增按钮操作 */
  228. const handleAdd = () => {
  229. reset();
  230. dialog.visible = true;
  231. dialog.title = '添加新闻分类';
  232. // 清空上传相关状态
  233. fileList.value = [];
  234. iconPreviewUrl.value = '';
  235. form.value.iconUrl = '';
  236. };
  237. /** 修改按钮操作 */
  238. const handleUpdate = async (row?: CategoryVO) => {
  239. reset();
  240. const _id = row?.id || ids.value[0];
  241. const res = await getCategory(_id);
  242. Object.assign(form.value, res.data);
  243. // 设置预览图
  244. if (form.value.iconUrl) {
  245. iconPreviewUrl.value = form.value.iconUrl;
  246. }
  247. dialog.visible = true;
  248. dialog.title = '修改新闻分类';
  249. };
  250. /** 提交按钮 */
  251. const submitForm = () => {
  252. categoryFormRef.value?.validate(async (valid: boolean) => {
  253. if (valid) {
  254. buttonLoading.value = true;
  255. if (form.value.id) {
  256. await updateCategory(form.value).finally(() => (buttonLoading.value = false));
  257. } else {
  258. await addCategory(form.value).finally(() => (buttonLoading.value = false));
  259. }
  260. proxy?.$modal.msgSuccess('操作成功');
  261. dialog.visible = false;
  262. await getList();
  263. }
  264. });
  265. };
  266. /** 删除按钮操作 */
  267. const handleDelete = async (row?: CategoryVO) => {
  268. const _ids = row?.id || ids.value;
  269. await proxy?.$modal.confirm('是否确认删除新闻分类编号为"' + _ids + '"的数据项?').finally(() => (loading.value = false));
  270. await delCategory(_ids);
  271. proxy?.$modal.msgSuccess('删除成功');
  272. await getList();
  273. };
  274. /** 导出按钮操作 */
  275. const handleExport = () => {
  276. proxy?.download(
  277. 'business/category/export',
  278. {
  279. ...queryParams.value
  280. },
  281. `category_${new Date().getTime()}.xlsx`
  282. );
  283. };
  284. onMounted(() => {
  285. getList();
  286. });
  287. // 添加响应式变量(在其他ref声明附近添加)
  288. const iconPreviewUrl = ref('');
  289. const fileList = ref([]);
  290. const dialogVisible = ref(false);
  291. const previewSrc = ref('');
  292. /** 处理图标上传 */
  293. const handleIconChange = async (file) => {
  294. const index = fileList.value.findIndex((f) => f.uid === file.uid);
  295. fileList.value = [];
  296. if (file.raw) {
  297. iconPreviewUrl.value = URL.createObjectURL(file.raw);
  298. }
  299. if (index === -1) {
  300. fileList.value.push(file);
  301. }
  302. try {
  303. const rawFile = file.raw;
  304. const res = await uploadTournament(rawFile);
  305. if (res.code === 200) {
  306. fileList.value[index] = {
  307. ...file,
  308. status: 'success',
  309. response: res.data.url
  310. };
  311. form.value.iconUrl = fileList.value[index].response;
  312. iconPreviewUrl.value = fileList.value[index].response;
  313. proxy?.$modal.msgSuccess('上传成功');
  314. } else {
  315. throw new Error(res.msg);
  316. }
  317. } catch (error) {
  318. fileList.value[index] = {
  319. ...file,
  320. status: 'fail',
  321. error: '上传失败'
  322. };
  323. proxy?.$modal.msgError('上传失败,请重试');
  324. }
  325. };
  326. /** 处理图标删除 */
  327. const handleIconRemove = (file, updatedFileList) => {
  328. fileList.value = updatedFileList;
  329. iconPreviewUrl.value = '';
  330. form.value.iconUrl = '';
  331. };
  332. /** 点击预览图触发放大 */
  333. const handlePreviewClick = () => {
  334. const currentSrc = iconPreviewUrl.value || form.value.iconUrl;
  335. if (currentSrc) {
  336. previewSrc.value = currentSrc;
  337. dialogVisible.value = true;
  338. }
  339. };
  340. </script>