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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
| <script setup> import { Edit, Delete } from '@element-plus/icons-vue' import {ElMessage, ElMessageBox} from "element-plus" import { getAllArticlesCategoryService, getAllArticleService, addArticleService, updateArticleService, deleteArticleService } from "@/api/article.js" import { ref } from 'vue' import {userTokenStore} from '@/stores/token.js' const tokenStore = userTokenStore().token //文章分类数据模型 const categorys = ref([ { "id": 3, "categoryName": "美食", "categoryAlias": "my", "createTime": "2023-09-02 12:06:59", "updateTime": "2023-09-02 12:06:59" }, { "id": 4, "categoryName": "娱乐", "categoryAlias": "yl", "createTime": "2023-09-02 12:08:16", "updateTime": "2023-09-02 12:08:16" }, { "id": 5, "categoryName": "军事", "categoryAlias": "js", "createTime": "2023-09-02 12:08:33", "updateTime": "2023-09-02 12:08:33" } ]) const show = async ()=>{ let data =await getAllArticlesCategoryService() categorys.value = data.data } show() //用户搜索时选中的分类id const categoryId=ref('')
//用户搜索时选中的发布状态 const state=ref('')
//文章列表数据模型 const articles = ref([ { "id": 5, "title": "陕西旅游攻略", "content": "兵马俑,华清池,法门寺,华山...爱去哪去哪...", "coverImg": "https://big-event-gwd.oss-cn-beijing.aliyuncs.com/9bf1cf5b-1420-4c1b-91ad-e0f4631cbed4.png", "state": "草稿", "categoryId": 2, "createTime": "2023-09-03 11:55:30", "updateTime": "2023-09-03 11:55:30" }, { "id": 5, "title": "陕西旅游攻略", "content": "兵马俑,华清池,法门寺,华山...爱去哪去哪...", "coverImg": "https://big-event-gwd.oss-cn-beijing.aliyuncs.com/9bf1cf5b-1420-4c1b-91ad-e0f4631cbed4.png", "state": "草稿", "categoryId": 2, "createTime": "2023-09-03 11:55:30", "updateTime": "2023-09-03 11:55:30" }, { "id": 5, "title": "陕西旅游攻略", "content": "兵马俑,华清池,法门寺,华山...爱去哪去哪...", "coverImg": "https://big-event-gwd.oss-cn-beijing.aliyuncs.com/9bf1cf5b-1420-4c1b-91ad-e0f4631cbed4.png", "state": "草稿", "categoryId": 2, "createTime": "2023-09-03 11:55:30", "updateTime": "2023-09-03 11:55:30" }, ])
//分页条数据模型 const pageNum = ref(1)//当前页 const total = ref(20)//总条数 const pageSize = ref(5)//每页条数
//当每页条数发生了变化,调用此函数 const onSizeChange = (size) => { pageSize.value = size showMainInfo() } //当前页码发生变化,调用此函数 const onCurrentChange = (num) => { pageNum.value = num showMainInfo() } const showMainInfo =async ()=>{ let params ={ pageSize:pageSize.value, pageNum:pageNum.value, categoryId:categoryId.value?categoryId.value:null, state:state.value?state.value:null } let show =await getAllArticleService(params) articles.value=show.data.items total.value=show.data.total for(let i=0;i<categorys.value.length;i++){ let id =categorys.value[i].id for(let j=0;j<articles.value.length;j++){ if (id === articles.value[j].categoryId){ articles.value[j].categoryName=categorys.value[i].categoryName
} } } console.log(show.data.items) } showMainInfo()
import {Plus} from '@element-plus/icons-vue' //控制抽屉是否显示 const visibleDrawer = ref(false) //添加表单数据模型 const articleModel = ref({ title: '', categoryId: '', coverImg: '', content:'', state:'' }) import { QuillEditor } from '@vueup/vue-quill' import '@vueup/vue-quill/dist/vue-quill.snow.css' const to_upload =(result) =>{ articleModel.value.coverImg = result.data console.log(result) } const uploadArticle= async (state)=>{ articleModel.value.state = state console.log(articleModel.value) let result =await addArticleService(articleModel.value) ElMessage.success("添加成功") visibleDrawer.value = false showMainInfo() console.log(result) } const updateArticle = async (state)=>{ articleModel.value.state = state let result = await updateArticleService(articleModel.value) ElMessage.success("修改成功") visibleDrawer.value = false showMainInfo() console.log(result) } const title = ref('')
const toUpdate = (row)=>{ title.value = "修改文章" visibleDrawer.value=true articleModel.value.title = row.title articleModel.value.coverImg = row.coverImg articleModel.value.categoryId = row.categoryId articleModel.value.categoryName=row.categoryName articleModel.value.content =row.content articleModel.value.id = row.id console.log(row) } const clean =()=>{ articleModel.value.title ='' articleModel.value.coverImg = '' articleModel.value.categoryId = '' articleModel.value.categoryName='' articleModel.value.content ='<p></p>' articleModel.value.state = '' } const deleteArticle=(row)=>{ ElMessageBox.confirm( '你确认删除该分类吗?', '温馨提示', { confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning', } ) .then(async () => { let result = await deleteArticleService(row.id) ElMessage({ type: 'success', message: '删除成功', }) console.log(result) showMainInfo() }) .catch(() => { ElMessage({ type: 'info', message: '取消删除', }) }) } </script> <template> <el-card class="page-container"> <template #header> <div class="header"> <span>文章管理</span> <div class="extra"> <el-button type="primary" @click="visibleDrawer=true,title='添加文章',clean()">添加文章</el-button> </div> </div> </template> <!-- 搜索表单 --> <el-form inline> <el-form-item label="文章分类:"> <el-select placeholder="请选择" v-model="categoryId" style="width: 180px"> <el-option v-for="c in categorys" :key="c.id" :label="c.categoryName" :value="c.id"> </el-option> </el-select> </el-form-item>
<el-form-item label="发布状态:"> <el-select placeholder="请选择" v-model="state" style="width: 180px"> <el-option label="已发布" value="已发布"></el-option> <el-option label="草稿" value="草稿"></el-option> </el-select> </el-form-item> <el-form-item> <el-button type="primary" @click="showMainInfo">搜索</el-button> <el-button @click="categoryId='';state=''">重置</el-button> </el-form-item> </el-form> <!-- 文章列表 --> <el-table :data="articles" style="width: 100%"> <el-table-column label="文章标题" width="400" prop="title"></el-table-column> <el-table-column label="分类" prop="categoryName"></el-table-column> <el-table-column label="发表时间" prop="createTime"> </el-table-column> <el-table-column label="状态" prop="state"></el-table-column> <el-table-column label="操作" width="100"> <template #default="{ row }"> <el-button :icon="Edit" circle plain type="primary" @click="toUpdate(row)"></el-button> <el-button :icon="Delete" circle plain type="danger" @click="deleteArticle(row)"></el-button> </template> </el-table-column> <template #empty> <el-empty description="没有数据" /> </template> </el-table> <!-- 分页条 --> <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]" layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange" @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" /> <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%"> <!-- 添加文章表单 --> <el-form :model="articleModel" label-width="100px" > <el-form-item label="文章标题" > <el-input v-model="articleModel.title" placeholder="请输入标题"></el-input> </el-form-item> <el-form-item label="文章分类"> <el-select placeholder="请选择" v-model="articleModel.categoryId"> <el-option v-for="c in categorys" :key="c.id" :label="c.categoryName" :value="c.id"> </el-option> </el-select> </el-form-item> <el-form-item label="文章封面"> <!-- auto-upload:设置是否自动上传 action:设置服务器接口路径 name:设置上传的文件字段名 headers:设置上传的请求头 on-success:设置上传成功的回调函数
--> <el-upload class="avatar-uploader" :auto-upload="true" action="/api/upload" name="multipartFile" :headers="{'Authorization':tokenStore}" :on-success="to_upload" :show-file-list="false"> <img v-if="articleModel.coverImg" :src="articleModel.coverImg" class="avatar" /> <el-icon v-else class="avatar-uploader-icon"> <Plus /> </el-icon> </el-upload> </el-form-item> <el-form-item label="文章内容"> <div class="editor"> <quill-editor theme="snow" v-model:content="articleModel.content" contentType="html" > </quill-editor> </div> </el-form-item> <el-form-item> <el-button type="primary" @click="title=='添加文章'?uploadArticle('已发布'):updateArticle('已发布')">发布</el-button> <el-button type="info" @click="title=='添加文章'?uploadArticle('草稿'):updateArticle('草稿')">草稿</el-button> </el-form-item> </el-form> </el-drawer> </el-card> </template> <style lang="scss" scoped> .page-container { min-height: 100%; box-sizing: border-box;
.header { display: flex; align-items: center; justify-content: space-between; } }
.avatar-uploader { :deep() { .avatar { width: 178px; height: 178px; display: block; }
.el-upload { border: 1px dashed var(--el-border-color); border-radius: 6px; cursor: pointer; position: relative; overflow: hidden; transition: var(--el-transition-duration-fast); }
.el-upload:hover { border-color: var(--el-color-primary); }
.el-icon.avatar-uploader-icon { font-size: 28px; color: #8c939d; width: 178px; height: 178px; text-align: center; } } } .editor { width: 100%; :deep(.ql-editor) { min-height: 200px; } } </style>
|