CreateForm.vue 7.2 KB

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