menu-add-or-update.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <el-dialog
  3. :title="!dataForm.id ? '新增' : '修改'"
  4. :close-on-click-modal="false"
  5. :visible.sync="visible"
  6. width="600px">
  7. <el-form
  8. :model="dataForm"
  9. :rules="dataRule"
  10. ref="dataForm"
  11. @keyup.enter.native="dataFormSubmit()"
  12. label-width="80px"
  13. style="margin-left: 20px;width: 500px"
  14. size="mini">
  15. <el-form-item label="类型" prop="type">
  16. <el-radio-group v-model="dataForm.type">
  17. <el-radio v-for="(type, index) in dataForm.typeList" :label="index" :key="index">{{ type }}</el-radio>
  18. </el-radio-group>
  19. </el-form-item>
  20. <el-form-item :label="dataForm.typeList[dataForm.type] + '名称'" prop="name">
  21. <el-input v-model="dataForm.name" :placeholder="dataForm.typeList[dataForm.type] + '名称'"></el-input>
  22. </el-form-item>
  23. <el-form-item label="上级菜单" prop="parentName">
  24. <el-popover
  25. ref="menuListPopover"
  26. placement="bottom-start"
  27. trigger="click">
  28. <el-tree
  29. :data="menuList"
  30. :props="menuListTreeProps"
  31. node-key="menuId"
  32. ref="menuListTree"
  33. @current-change="menuListTreeCurrentChangeHandle"
  34. :default-expand-all="true"
  35. :highlight-current="true"
  36. :expand-on-click-node="false">
  37. </el-tree>
  38. </el-popover>
  39. <el-input v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true" placeholder="点击选择上级菜单" class="menu-list__input"></el-input>
  40. </el-form-item>
  41. <el-form-item v-if="dataForm.type === 1" label="菜单路由" prop="url">
  42. <el-input v-model="dataForm.url" placeholder="菜单路由"></el-input>
  43. </el-form-item>
  44. <el-form-item v-if="dataForm.type !== 0" label="授权标识" prop="perms">
  45. <el-input v-model="dataForm.perms" placeholder="多个用逗号分隔, 如: user:list,user:create"></el-input>
  46. </el-form-item>
  47. <el-form-item v-if="dataForm.type !== 2" label="排序号" prop="orderNum">
  48. <el-input-number
  49. v-model="dataForm.orderNum"
  50. controls-position="right"
  51. :min="0"
  52. label="排序号"
  53. style="width: 100%">
  54. </el-input-number>
  55. </el-form-item>
  56. <el-form-item v-if="dataForm.type !== 2" label="菜单图标" prop="icon">
  57. <el-row>
  58. <el-col :span="22">
  59. <el-popover
  60. ref="iconListPopover"
  61. placement="bottom-start"
  62. trigger="click"
  63. popper-class="mod-menu__icon-popover">
  64. <div class="mod-menu__icon-inner">
  65. <div class="mod-menu__icon-list">
  66. <el-button
  67. v-for="(item, index) in iconList"
  68. :key="index"
  69. @click="iconActiveHandle(item)"
  70. :class="{ 'is-active': item === dataForm.icon }">
  71. <icon-svg :name="item"></icon-svg>
  72. </el-button>
  73. </div>
  74. </div>
  75. </el-popover>
  76. <el-input v-model="dataForm.icon" v-popover:iconListPopover :readonly="true" placeholder="菜单图标名称" class="icon-list__input"></el-input>
  77. </el-col>
  78. <el-col :span="2" class="icon-list__tips">
  79. <el-tooltip placement="top" effect="light">
  80. <div slot="content">全站推荐使用SVG Sprite, 详细请参考:<a href="//github.com/daxiongYang/renren-fast-vue/blob/master/src/icons/index.js" target="_blank">icons/index.js</a>描述</div>
  81. <i class="el-icon-warning"></i>
  82. </el-tooltip>
  83. </el-col>
  84. </el-row>
  85. </el-form-item>
  86. </el-form>
  87. <span slot="footer" class="dialog-footer">
  88. <el-button size="mini" @click="visible = false">关闭</el-button>
  89. <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
  90. </span>
  91. </el-dialog>
  92. </template>
  93. <script>
  94. import { treeDataTranslate } from '@/utils'
  95. import Icon from '@/icons'
  96. export default {
  97. data () {
  98. var validateUrl = (rule, value, callback) => {
  99. if (this.dataForm.type === 1 && !/\S/.test(value)) {
  100. callback(new Error('菜单URL不能为空'))
  101. } else {
  102. callback()
  103. }
  104. }
  105. return {
  106. visible: false,
  107. dataForm: {
  108. id: 0,
  109. type: 1,
  110. typeList: ['目录', '菜单', '按钮'],
  111. name: '',
  112. parentId: 0,
  113. parentName: '',
  114. url: '',
  115. perms: '',
  116. orderNum: 0,
  117. icon: '',
  118. iconList: []
  119. },
  120. dataRule: {
  121. name: [
  122. { required: true, message: '菜单名称不能为空', trigger: 'blur' }
  123. ],
  124. parentName: [
  125. { required: true, message: '上级菜单不能为空', trigger: 'change' }
  126. ],
  127. url: [
  128. { validator: validateUrl, trigger: 'blur' }
  129. ]
  130. },
  131. menuList: [],
  132. menuListTreeProps: {
  133. label: 'name',
  134. children: 'children'
  135. }
  136. }
  137. },
  138. created () {
  139. this.iconList = Icon.getNameList()
  140. },
  141. methods: {
  142. init (id) {
  143. this.dataForm.id = id || 0
  144. this.$http({
  145. url: this.$http.adornUrl('/sys/menu/select'),
  146. method: 'get',
  147. params: this.$http.adornParams()
  148. }).then(({data}) => {
  149. this.menuList = treeDataTranslate(data.menuList, 'menuId')
  150. }).then(() => {
  151. this.visible = true
  152. this.$nextTick(() => {
  153. this.$refs['dataForm'].resetFields()
  154. })
  155. }).then(() => {
  156. if (!this.dataForm.id) {
  157. // 新增
  158. this.menuListTreeSetCurrentNode()
  159. } else {
  160. // 修改
  161. this.$http({
  162. url: this.$http.adornUrl(`/sys/menu/info/${this.dataForm.id}`),
  163. method: 'get',
  164. params: this.$http.adornParams()
  165. }).then(({data}) => {
  166. this.dataForm.id = data.menu.menuId
  167. this.dataForm.type = data.menu.type
  168. this.dataForm.name = data.menu.name
  169. this.dataForm.parentId = data.menu.parentId
  170. this.dataForm.url = data.menu.url
  171. this.dataForm.perms = data.menu.perms
  172. this.dataForm.orderNum = data.menu.orderNum
  173. this.dataForm.icon = data.menu.icon
  174. this.menuListTreeSetCurrentNode()
  175. })
  176. }
  177. })
  178. },
  179. // 菜单树选中
  180. menuListTreeCurrentChangeHandle (data, node) {
  181. this.dataForm.parentId = data.menuId
  182. this.dataForm.parentName = data.name
  183. },
  184. // 菜单树设置当前选中节点
  185. menuListTreeSetCurrentNode () {
  186. this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
  187. this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']
  188. },
  189. // 图标选中
  190. iconActiveHandle (iconName) {
  191. this.dataForm.icon = iconName
  192. },
  193. // 表单提交
  194. dataFormSubmit () {
  195. this.$refs['dataForm'].validate((valid) => {
  196. if (valid) {
  197. this.$http({
  198. url: this.$http.adornUrl(`/sys/menu/${!this.dataForm.id ? 'save' : 'update'}`),
  199. method: 'post',
  200. data: this.$http.adornData({
  201. 'menuId': this.dataForm.id || undefined,
  202. 'type': this.dataForm.type,
  203. 'name': this.dataForm.name,
  204. 'parentId': this.dataForm.parentId,
  205. 'url': this.dataForm.url,
  206. 'perms': this.dataForm.perms,
  207. 'orderNum': this.dataForm.orderNum,
  208. 'icon': this.dataForm.icon
  209. })
  210. }).then(({data}) => {
  211. if (data && data.code === 0) {
  212. this.visible = false
  213. this.$emit('refreshDataList')
  214. this.$message({
  215. message: '操作成功',
  216. type: 'success',
  217. duration: 1500,
  218. // onClose: () => {
  219. // this.visible = false
  220. // this.$emit('refreshDataList')
  221. // }
  222. })
  223. } else {
  224. this.$message.error(data.msg)
  225. }
  226. })
  227. }
  228. })
  229. }
  230. }
  231. }
  232. </script>
  233. <style lang="scss">
  234. .mod-menu {
  235. .menu-list__input,
  236. .icon-list__input {
  237. > .el-input__inner {
  238. cursor: pointer;
  239. }
  240. }
  241. &__icon-popover {
  242. width: 458px;
  243. overflow: hidden;
  244. }
  245. &__icon-inner {
  246. width: 478px;
  247. max-height: 258px;
  248. overflow-x: hidden;
  249. overflow-y: auto;
  250. }
  251. &__icon-list {
  252. width: 458px;
  253. padding: 0;
  254. margin: -8px 0 0 -8px;
  255. > .el-button {
  256. padding: 8px;
  257. margin: 8px 0 0 8px;
  258. > span {
  259. display: inline-block;
  260. vertical-align: middle;
  261. width: 18px;
  262. height: 18px;
  263. font-size: 18px;
  264. }
  265. }
  266. }
  267. .icon-list__tips {
  268. font-size: 18px;
  269. text-align: center;
  270. color: #e6a23c;
  271. cursor: pointer;
  272. }
  273. }
  274. </style>