CreateForm.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <div>
  3. <a-drawer
  4. :title="title"
  5. placement="right"
  6. :closable="true"
  7. @close="onClose"
  8. :visible="drawerVisible"
  9. :width="950">
  10. <a-form @submit="handleSubmit" :form="menuForm" class="form">
  11. <a-row class="form-row" :gutter="16">
  12. <a-col :lg="24" :md="12" :sm="24">
  13. <a-form-item label="菜单名称">
  14. <a-input
  15. placeholder="请输入菜单名称"
  16. v-decorator="['title', { rules: [{ required: true, message: '请输入菜单名称', whitespace: true }] }]"
  17. />
  18. <a href="https://3x.ant.design/components/icon/"> sakura 主题菜单ICON地址</a>
  19. </a-form-item>
  20. </a-col>
  21. </a-row>
  22. <a-row class="form-row" :gutter="16">
  23. <a-col :lg="24" :md="12" :sm="24">
  24. <a-form-item label="菜单icon">
  25. <a-input
  26. placeholder="请输入菜单名称"
  27. v-decorator="['icon', { rules: [{ required: true, message: '请输入菜单ICON', whitespace: true }] }]"
  28. />
  29. </a-form-item>
  30. </a-col>
  31. </a-row>
  32. <a-row class="form-row" :gutter="16">
  33. <a-col :lg="24" :md="12" :sm="24">
  34. <a-form-item label="跳转链接">
  35. <a-input
  36. placeholder="请输入跳转链接"
  37. v-decorator="['url', { rules: [{ required: true, message: '请输入跳转链接', whitespace: true }] }]"
  38. />
  39. </a-form-item>
  40. </a-col>
  41. </a-row>
  42. <a-row class="form-row" :gutter="16">
  43. <a-col :lg="12" :md="6" :sm="12">
  44. <a-form-item label="排序">
  45. <a-input
  46. placeholder="请输入排序"
  47. v-decorator="['sort']"
  48. />
  49. </a-form-item>
  50. </a-col>
  51. </a-row>
  52. <a-row class="form-row" :gutter="16">
  53. <a-col :lg="12" :md="6" :sm="12">
  54. <a-form-item label="父菜单id">
  55. <a-input
  56. placeholder="请输入父菜单id"
  57. v-decorator="['parentId']"
  58. />
  59. </a-form-item>
  60. </a-col>
  61. </a-row>
  62. <div
  63. :style="{
  64. position: 'absolute',
  65. left: 0,
  66. bottom: 0,
  67. width: '100%',
  68. borderTop: '1px solid #e9e9e9',
  69. padding: '10px 16px',
  70. background: '#fff',
  71. textAlign: 'right',
  72. zIndex: '10'
  73. }"
  74. >
  75. <a-button :style="{marginRight: '8px'}" @click="onClose">取消</a-button>
  76. <a-button type="primary" @click="handleSubmit">提交</a-button>
  77. </div>
  78. </a-form>
  79. </a-drawer>
  80. </div>
  81. </template>
  82. <script>
  83. import { createMenu, fetchMenu, updateMenu } from '../../../api/menu'
  84. import UpLoadImage from '@/components/UpLoadImageAndFillInput/UpLoadImage'
  85. export default {
  86. name: 'CreateMenuForm',
  87. components: {
  88. UpLoadImage
  89. },
  90. props: {
  91. formType: {
  92. type: String,
  93. default: 'create'
  94. },
  95. visible: {
  96. type: Boolean
  97. }
  98. },
  99. data () {
  100. return {
  101. title: '新增菜单',
  102. id: null,
  103. drawerVisible: false,
  104. icon: null,
  105. menuForm: this.$form.createForm(this, { name: 'create_menu' })
  106. }
  107. },
  108. watch: {
  109. visible (val) {
  110. this.drawerVisible = val
  111. },
  112. formType (val) {
  113. this.title = (val === 'create' ? '新增菜单' : '更新菜单')
  114. }
  115. },
  116. beforeCreate () { },
  117. created () { },
  118. methods: {
  119. handleSubmit (e) {
  120. e.preventDefault()
  121. this.menuForm.validateFieldsAndScroll((err, values) => {
  122. if (!err) {
  123. console.log('Received values of form: ', values)
  124. const createParams = { ...values }
  125. if (this.formType === 'create') {
  126. createMenu(createParams)
  127. .then(res => {
  128. this.$notification.success({
  129. message: '新增菜单成功'
  130. })
  131. this.$emit('refreshTable')
  132. })
  133. .catch(err => {
  134. console.log(err)
  135. })
  136. } else {
  137. createParams['id'] = this.id
  138. updateMenu(createParams)
  139. .then(res => {
  140. this.$notification.success({
  141. message: '编辑菜单成功'
  142. })
  143. this.$emit('refreshTable')
  144. })
  145. .catch(err => {
  146. console.log(err)
  147. })
  148. }
  149. this.resetForm()
  150. this.drawerVisible = false
  151. this.$emit('resetData', false)
  152. }
  153. })
  154. },
  155. handleEdit (record) {
  156. this.id = record.id
  157. fetchMenu(record.id)
  158. .then(response => {
  159. const postForm = response.model
  160. this.menuForm.resetFields()
  161. this.menuForm = this.$form.createForm(this, {
  162. onFieldsChange: (_, changedFields) => {},
  163. mapPropsToFields: () => {
  164. return {
  165. parentId: this.$form.createFormField({
  166. value: postForm.parentId
  167. }),
  168. title: this.$form.createFormField({
  169. value: postForm.title
  170. }),
  171. icon: this.$form.createFormField({
  172. value: postForm.icon
  173. }),
  174. url: this.$form.createFormField({
  175. value: postForm.url
  176. }),
  177. sort: this.$form.createFormField({
  178. value: postForm.sort
  179. })
  180. }
  181. },
  182. onValuesChange: (_, values) => {
  183. console.log(values)
  184. }
  185. })
  186. })
  187. .catch(err => {
  188. console.log(err)
  189. })
  190. this.drawerVisible = true
  191. },
  192. onClose () {
  193. this.resetForm()
  194. this.drawerVisible = false
  195. this.$emit('resetData', false)
  196. },
  197. resetForm () {
  198. this.menuForm.resetFields()
  199. },
  200. getIcon (icon) {
  201. this.icon = icon
  202. },
  203. handleSelectChange (value) {
  204. console.log(`Selected: ${value}`)
  205. }
  206. }
  207. }
  208. </script>
  209. <style>
  210. .edit-input {
  211. padding-right: 100px;
  212. }
  213. .cancel-btn {
  214. position: absolute;
  215. right: 15px;
  216. top: 10px;
  217. }
  218. .ant-upload-select-picture-card i {
  219. font-size: 32px;
  220. color: #999;
  221. }
  222. .ant-upload-select-picture-card .ant-upload-text {
  223. margin-top: 8px;
  224. color: #666;
  225. }
  226. </style>