add.vue
7.91 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
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
<template>
<div>
<el-dialog
class="user-unfreeze-dialog"
:title="dialog.title + '菜单'"
:type="dialog.type"
:visible.sync="dialog.isVisible"
:before-close="handleClose"
>
<div class="unfreeze-form">
<el-form
ref="ruleForm"
:rules="formRules"
:model="userData"
label-width="100px"
class="demo-ruleForm"
>
<el-form-item label="名称:" prop="name">
<el-input
v-model="userData.name"
placeholder="请输入"
autocomplete="off"
minlength="1"
maxlength="6"
/>
</el-form-item>
<el-form-item label="路由URL:" prop="path">
<el-input
v-model="userData.path"
placeholder="请输入"
autocomplete="off"
minlength="1"
maxlength="50"
@keyup.native="menuPath"
/>
{{ menuComponent }}
</el-form-item>
<el-form-item label="上级类目:" prop="parentId">
<select-tree
ref="tree"
:props="props"
:options="optionData"
:parentname="parentname"
:parentid="parentid"
:editdata="editdata"
:clearable="isClearable"
:accordion="isAccordion"
@get-value="getValue($event)"
/>
</el-form-item>
<el-form-item label="图标:" prop="icon" class="incoSelete">
<el-input
v-model="userData.icon"
class="iconInfo"
placeholder="请选择图标"
readonly
@focus="handleIcon"
>
<el-button
slot="append"
icon="el-icon-brush"
@click="clearIcon"
/>
</el-input>
</el-form-item>
<el-form-item label="状态:">
<el-radio-group v-model="userData.isEnable">
<el-radio :label="true">正常</el-radio>
<el-radio :label="false">停用</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="排序:" prop="sortValue">
<el-input-number v-model="userData.sortValue" :min="0" />
</el-form-item>
<el-form-item class="right">
<el-button @click="handleClose"> 取 消 </el-button>
<el-button
type="primary"
:disabled="isDisable"
@click="handleSubmit"
>
确 定
</el-button>
</el-form-item>
</el-form>
</div>
</el-dialog>
<icons
:is-visible="isIconVisible"
@close="handleCloseIcon"
@getIcon="getIcon"
/>
</div>
</template>
<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { ElForm } from 'element-ui/types/form'
// 公用组件
// 组织树
import SelectTree from '@/components/SelectTree/org-tree.vue'
import Icons from './components/icons.vue'
// api
import { addMenu, editMenu } from '@/pages/menu/api'
@Component({
name: 'AddDialog',
components: {
SelectTree,
Icons
}
})
export default class extends Vue {
@Prop() private dialog!: any
@Prop() private menuTreeData!: []
@Prop() private editdata!: {}
@Prop() private parentname!: string
private isDisable: boolean = false
private parentid = ''
private el: any = this.$refs
private values = ''
private styles = {
width: '300px'
}
private userData = {
parentId: '',
name: '',
path: '',
isEnable: true,
sortValue: '0',
icon: '',
component: ''
} as any
private isClearable = true // 可清空(可选)
private isAccordion = true // 可收起(可选)
private valueId = ''
private parentName = ''
private props = {
// 配置项(必选)
value: 'id',
label: 'name',
children: 'children'
// disabled:true
}
private isIconVisible = false
private formRules = {
parentId: [
{ required: true, message: '请选择上级类目', trigger: 'change' }
],
name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
path: [{ required: true, message: '请输入URL', trigger: 'blur' }]
}
get optionData() {
let cloneData = JSON.parse(JSON.stringify(this.menuTreeData)) // 对源数据深度克隆
return cloneData
}
get menuComponent() {
let comp = ''
if (this.userData.path && this.userData.path !== '/') {
const isUrl = this.isUrl(this.userData.path)
if (isUrl) {
comp = `${this.userData.path}`
} else {
comp = `${this.userData.path.substr(1)}/index.vue`
}
} else {
comp = `/index.vue`
}
return comp
}
@Watch('editdata')
getUserData(val: any) {
if (this.dialog.type !== 'add' && val.id !== undefined) {
this.userData = val
this.parentid = val.parentId
}
}
@Watch('parentid')
getParentId(val: string) {
this.userData.parentId = val
}
@Watch('userData.path')
getmenu(val: any) {
this.computedComponent()
}
isUrl(path: any) {
const urls = [
'http://',
'/http://',
'https://',
'/https://',
'www.',
'/www.'
]
const urlIndex = urls.findIndex(item => {
return path.startsWith(item)
})
return urlIndex >= 0
}
menuPath() {
const isUrl = this.isUrl(this.userData.path)
if (!isUrl && !this.userData.path.startsWith('/')) {
this.userData.path = '/' + this.userData.path
} else if (isUrl) {
if (this.userData.path.startsWith('/')) {
this.userData.path = this.userData.path.substr(1)
}
}
}
computedComponent() {
const isUrl = this.isUrl(this.userData.path)
if (isUrl) {
this.userData.component = 'Layout'
} else {
if (this.userData.path) {
this.userData.component = `${this.userData.path}/index`
} else {
this.userData.component = `index`
}
}
}
// 功能函数
// 添加
private async addSave() {
const { data } = await addMenu(this.userData)
if (data.isSuccess) {
this.$message({
message: '操作成功!',
type: 'success'
})
this.$store.commit('updatedMenuTreeId', data.data.id) // 当前菜单数据存储到数据池
this.handleClose()
}
}
// 编辑
private async updateSave() {
delete (this.userData as any).createTime
delete (this.userData as any).updateTime
const { data } = await editMenu(this.userData)
if (data.isSuccess) {
this.$message({
message: '操作成功!',
type: 'success'
})
this.$store.commit('updatedMenuTreeId', data.data.id) // 当前菜单数据存储到数据池
this.handleClose()
}
}
// 表单提交
private handleSubmit() {
this.isDisable = true
setTimeout(() => {
this.isDisable = false // 点击一次时隔两秒后才能再次点击
}, 2000)
;(this.$refs.ruleForm as ElForm).validate(async(valid: boolean) => {
if (valid) {
if (this.dialog.type === 'add') {
await this.addSave()
} else {
await this.updateSave()
}
this.$emit('getMenuList')
} else {
return false
}
})
}
// ui
handleNodeClick(data: Object) {}
// 获取图标
getIcon(val: string) {
this.userData.icon = val
}
clearIcon() {
this.userData.icon = ''
}
handleIcon() {
this.isIconVisible = true
}
// 获取组织树id
getValue(value: string) {
(this.userData as any).parentId = value
}
handleCloseIcon() {
this.isIconVisible = false
}
// 表单取消
private handleClose() {
(this.$refs.ruleForm as ElForm).resetFields()
this.$emit('cleardata')
this.$emit('close')
this.parentid = ''
;(this.$refs as any).tree.clearHandle()
}
}
</script>
<style lang="scss"></style>