CreateForm.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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="socialFrom" class="form">
  11. <a-row class="form-row" :gutter="16">
  12. <a-col :lg="24" :md="12" :sm="24">
  13. <a-form-item label="code">
  14. <a-row class="form-row" :gutter="16" justify="space-between">
  15. <a-col :lg="22" :md="12" :sm="24">
  16. <a-select placeholder="请选择code" v-show="!isAddCode" v-decorator="['code', { rules: [{ required: true, message: '请输入code', whitespace: true }] }]" >
  17. <a-select-option value="byteblogs">ByteBlogs</a-select-option>
  18. <a-select-option value="QQ">QQ</a-select-option>
  19. <a-select-option value="CSDN">CSDN</a-select-option>
  20. <a-select-option value="reward">打赏</a-select-option>
  21. <a-select-option value="qrCode">二维码</a-select-option>
  22. <a-select-option value="email">邮箱</a-select-option>
  23. <a-select-option value="sifou">思否</a-select-option>
  24. <a-select-option value="oschina">开源中国</a-select-option>
  25. </a-select>
  26. <a-input
  27. placeholder="请输入code"
  28. v-show="isAddCode"
  29. v-decorator="['code', { rules: [{ required: true, message: '请输入code', whitespace: true }] }]"
  30. />
  31. </a-col>
  32. <a-col :lg="2" :md="12" :sm="24">
  33. <a-button
  34. type="primary"
  35. :icon="isAddCode?'select':'plus-circle'"
  36. size="small"
  37. shape="circle"
  38. @click="handlerAddCode"></a-button>
  39. </a-col>
  40. </a-row>
  41. </a-form-item>
  42. </a-col>
  43. </a-row>
  44. <a-row class="form-row" :gutter="16">
  45. <a-col :lg="8" :md="12" :sm="24">
  46. <a-form-item label="展示类型">
  47. <a-radio-group v-decorator="['showType', { initialValue: 1 }]" @change="changeShowType">
  48. <a-radio :value="1">图片</a-radio>
  49. <a-radio :value="2">文本信息</a-radio>
  50. <a-radio :value="3">跳转链接</a-radio>
  51. </a-radio-group>
  52. </a-form-item>
  53. </a-col>
  54. </a-row>
  55. <a-row class="form-row" :gutter="16">
  56. <a-col :lg="8" :md="12" :sm="24">
  57. <a-form-item label="是否启用">
  58. <a-radio-group v-decorator="['isEnabled', { initialValue: 0 }]">
  59. <a-radio :value="0">否</a-radio>
  60. <a-radio :value="1">是</a-radio>
  61. </a-radio-group>
  62. </a-form-item>
  63. </a-col>
  64. <a-col :lg="8" :md="12" :sm="24">
  65. <a-form-item label="是否主页社交信息">
  66. <a-radio-group v-decorator="['isHome', { initialValue: 0 }]">
  67. <a-radio :value="0">否</a-radio>
  68. <a-radio :value="1">是</a-radio>
  69. </a-radio-group>
  70. </a-form-item>
  71. </a-col>
  72. </a-row>
  73. <a-row class="form-row" :gutter="16">
  74. <a-col :lg="24" :md="12" :sm="24">
  75. <a-form-item label="图标">
  76. <UpLoadImage
  77. @getImageUrl="getIcon"
  78. :placeholder="`请选择图标`"
  79. ref="handlerIconRef"
  80. :imageUrl="this.icon"
  81. ></UpLoadImage>
  82. </a-form-item>
  83. </a-col>
  84. </a-row>
  85. <a-row class="form-row" :gutter="16">
  86. <a-col :lg="24" :md="12" :sm="24">
  87. <a-form-item label="备注">
  88. <a-input
  89. placeholder="请输入备注"
  90. v-decorator="['remark']"
  91. />
  92. </a-form-item>
  93. </a-col>
  94. </a-row>
  95. <a-row class="form-row" :gutter="16">
  96. <a-col :lg="24" :md="12" :sm="24">
  97. <a-form-item label="社交内容">
  98. <a-input v-show="!show" placeholder="请输入社交内容" v-decorator="['content']"/>
  99. <UpLoadImage
  100. v-show="show"
  101. @getImageUrl="getContent"
  102. ref="handlerContentRef"
  103. :placeholder="`请输入社交内容`"
  104. :imageUrl="this.content"
  105. :style="show?'':'display: none'"></UpLoadImage>
  106. </a-form-item>
  107. </a-col>
  108. </a-row>
  109. <div
  110. :style="{
  111. position: 'absolute',
  112. left: 0,
  113. bottom: 0,
  114. width: '100%',
  115. borderTop: '1px solid #e9e9e9',
  116. padding: '10px 16px',
  117. background: '#fff',
  118. textAlign: 'right',
  119. zIndex: '10'
  120. }"
  121. >
  122. <a-button :style="{marginRight: '8px'}" @click="onClose">取消</a-button>
  123. <a-button type="primary" @click="handleSubmit">提交</a-button>
  124. </div>
  125. </a-form>
  126. </a-drawer>
  127. </div>
  128. </template>
  129. <script>
  130. import { fetchSocial, updateSocial } from '@/api/social'
  131. import { createSocial } from '../../../api/social'
  132. import UpLoadImage from '@/components/UpLoadImageAndFillInput/UpLoadImage'
  133. export default {
  134. name: 'CreateSocialForm',
  135. components: {
  136. UpLoadImage
  137. },
  138. props: {
  139. formType: {
  140. type: String,
  141. default: 'create'
  142. },
  143. visible: {
  144. type: Boolean
  145. }
  146. },
  147. data () {
  148. return {
  149. title: '新增社交信息',
  150. id: null,
  151. drawerVisible: false,
  152. socialFrom: this.$form.createForm(this, { name: 'create_social' }),
  153. content: null,
  154. icon: null,
  155. show: true,
  156. iconShow: true,
  157. isAddCode: false
  158. }
  159. },
  160. watch: {
  161. visible (val) {
  162. this.drawerVisible = val
  163. },
  164. formType (val) {
  165. this.title = (val === 'create' ? '新增社交信息' : '更新社交信息')
  166. }
  167. },
  168. beforeCreate () {},
  169. created () {},
  170. methods: {
  171. handleSubmit (e) {
  172. e.preventDefault()
  173. this.socialFrom.validateFieldsAndScroll((err, values) => {
  174. if (!err) {
  175. console.log('Received values of form: ', values)
  176. const createParams = { ...values }
  177. if (createParams['showType'] === 1) {
  178. createParams['content'] = this.content
  179. }
  180. createParams['icon'] = this.icon
  181. if (this.formType === 'create') {
  182. createSocial(createParams)
  183. .then(res => {
  184. this.$notification.success({
  185. message: '新增社交信息成功'
  186. })
  187. this.$emit('refreshTable')
  188. })
  189. .catch(err => {
  190. console.log(err)
  191. })
  192. } else {
  193. createParams['id'] = this.id
  194. updateSocial(createParams)
  195. .then(res => {
  196. this.$notification.success({
  197. message: '编辑社交信息成功'
  198. })
  199. this.$emit('refreshTable')
  200. })
  201. .catch(err => {
  202. })
  203. }
  204. this.resetForm()
  205. this.drawerVisible = false
  206. this.$emit('resetData', false)
  207. }
  208. })
  209. },
  210. handleEdit (record) {
  211. this.id = record.id
  212. fetchSocial(record.id)
  213. .then(response => {
  214. const postForm = response.model
  215. if (postForm.showType === 1) {
  216. this.show = true
  217. this.$refs.handlerContentRef.handleUrl(postForm.content)
  218. } else {
  219. this.show = false
  220. }
  221. this.$refs.handlerIconRef.handleUrl(postForm.icon)
  222. this.socialFrom.resetFields()
  223. this.socialFrom = this.$form.createForm(this, {
  224. onFieldsChange: (_, changedFields) => {},
  225. mapPropsToFields: () => {
  226. return {
  227. content: this.$form.createFormField({
  228. value: postForm.content
  229. }),
  230. showType: this.$form.createFormField({
  231. value: postForm.showType
  232. }),
  233. remark: this.$form.createFormField({
  234. value: postForm.remark
  235. }),
  236. icon: this.$form.createFormField({
  237. value: postForm.icon
  238. }),
  239. isEnabled: this.$form.createFormField({
  240. value: postForm.isEnabled
  241. }),
  242. isHome: this.$form.createFormField({
  243. value: postForm.isHome
  244. }),
  245. code: this.$form.createFormField({
  246. value: postForm.code
  247. })
  248. }
  249. },
  250. onValuesChange: (_, values) => {
  251. console.log(values)
  252. }
  253. })
  254. })
  255. .catch(err => {
  256. console.log(err)
  257. })
  258. this.drawerVisible = true
  259. },
  260. getContent (content) {
  261. this.content = content
  262. },
  263. getIcon (icon) {
  264. this.icon = icon
  265. },
  266. onClose () {
  267. this.resetForm()
  268. this.drawerVisible = false
  269. this.$emit('resetData', false)
  270. },
  271. resetForm () {
  272. this.show = true
  273. this.socialFrom.resetFields()
  274. this.$refs.handlerContentRef.handleUrl(null)
  275. this.$refs.handlerIconRef.handleUrl(null)
  276. },
  277. handleSelectChange (value) {
  278. console.log(`Selected: ${value}`)
  279. },
  280. changeShowType (e) {
  281. if (e.target.value === 1) {
  282. this.show = true
  283. } else {
  284. this.show = false
  285. }
  286. },
  287. handlerAddCode () {
  288. this.isAddCode = !this.isAddCode
  289. }
  290. }
  291. }
  292. </script>
  293. <style>
  294. .edit-input {
  295. padding-right: 100px;
  296. }
  297. .cancel-btn {
  298. position: absolute;
  299. right: 15px;
  300. top: 10px;
  301. }
  302. .ant-upload-select-picture-card i {
  303. font-size: 32px;
  304. color: #999;
  305. }
  306. .ant-upload-select-picture-card .ant-upload-text {
  307. margin-top: 8px;
  308. color: #666;
  309. }
  310. </style>