add.vue
5.85 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
210
211
<template>
<div class="article-add">
<zp-page-edit :back="true" @back="$router.back()">
<div slot="header">
{{ header }}
</div>
<el-form
:model="info"
:rules="rules"
ref="form"
label-width="100px"
class="form"
>
<el-form-item label="博客类型:" prop="type">
<el-select
v-model="info.type"
multiple
clearable
placeholder="请选择博客类型"
class="block"
>
<el-option
v-for="item in labelList"
:key="item.label"
:label="item.label"
:value="item.label"
>
</el-option>
</el-select>
</el-form-item>
<el-form-item label="文章标题:" prop="title">
<el-input type="text" v-model="info.title"></el-input>
</el-form-item>
<el-form-item label="文章描述:" prop="desc">
<el-input type="textarea" v-model="info.desc"></el-input>
</el-form-item>
<el-form-item label="文章封面:" prop="fileCoverImgUrl">
<zp-single-img-upload v-model="info.fileCoverImgUrl" :public="true">
</zp-single-img-upload>
</el-form-item>
<el-form-item label="文章内容:" prop="markdown" class="markdown">
<Markdown v-model="info.markdown"></Markdown>
</el-form-item>
<el-form-item label="级别:" prop="album">
<el-select
v-model="info.level"
placeholder="请选择级别"
class="block"
>
<el-option
v-for="item in [1, 2, 3, 4, 5, 6]"
:key="item"
:label="item"
:value="item"
></el-option>
</el-select>
</el-form-item>
<el-form-item label="来源:" prop="source">
<el-select
v-model="info.source"
placeholder="请选择来源"
class="block"
>
<el-option
v-for="item in sources"
:key="item.id"
:label="item.name"
:value="item.id"
></el-option>
</el-select>
</el-form-item>
<el-form-item label="Github:" prop="github">
<el-input type="text" v-model="info.github"></el-input>
</el-form-item>
<el-form-item label="Auth:" prop="auth">
<el-input type="text" v-model="info.auth"></el-input>
</el-form-item>
<el-form-item label="是否可见:" prop="isVisible" class="left-item">
<el-switch v-model="info.isVisible"></el-switch>
</el-form-item>
<el-form-item>
<el-button
type="primary"
@click="handleSubmit('form')"
:loading="loading"
>立即创建</el-button
>
</el-form-item>
</el-form>
</zp-page-edit>
</div>
</template>
<script>
import { sources } from "src/constant/classify";
import { apiGetLabelList } from "src/api/label";
import { apiAddBlog } from "src/api/blog";
import Markdown from "components/markdown";
export default {
name: "articleAdd",
components: { Markdown },
computed: {},
data() {
return {
sources,
header: "添加文章",
labelList: [],
info: {
type: ["JavaScript"],
title: "",
desc: "",
fileCoverImgUrl: "",
html: "",
markdown: "",
level: 1,
source: 1,
github: "",
auth: "",
isVisible: true,
releaseTime: new Date().getTime() + "",
},
loading: false,
rules: {
type: [
{
required: true,
message: "请选择至少选择一个文章类型",
trigger: "change",
type: "array",
},
],
title: [{ required: true, message: "请填写文章标题", trigger: "blur" }],
desc: [{ required: true, message: "请填写文章描述", trigger: "blur" }],
isVisible: [
{
required: true,
message: "请选择",
trigger: "change",
type: "boolean",
},
],
fileCoverImgUrl: [
{ required: true, message: "请上传文章封面", trigger: "change" },
],
},
};
},
created() {
this.getLabelList();
},
methods: {
handleSubmit(formName) {
this.loading = true;
if (!this.info.markdown) {
this.$message.warning("请填写文章内容");
this.loading = false;
return;
}
this.$refs[formName].validate((valid) => {
if (valid) {
this.info.html = this.info.markdown;
this.handleAddBlog();
} else {
this.loading = false;
return false;
}
});
},
handleAddBlog() {
return apiAddBlog(this.info)
.then((res) => {
this.$message.success("新增文章成功");
this.$router.push("/article/list");
})
.catch((err) => {
console.log(err);
this.$message.info("新增文章失败");
})
.finally(() => {
this.loading = false;
});
},
getLabelList() {
let params = {
pageindex: 1,
pagesize: 50,
};
return apiGetLabelList(params)
.then((res) => {
let { list } = res.data;
this.labelList = list;
})
.catch((err) => {
console.log(err);
})
.finally(() => {
});
},
},
};
</script>
<style lang="less" scoped>
/deep/ .markdown {
.el-form-item__content {
width: 1400px;
}
}
</style>