You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.6 KiB

12 months ago
  1. import api from "@/api"
  2. import MyTable from "@/components/MyTable"
  3. import { RootState } from "@/store"
  4. import { getTime } from "@/utils"
  5. import { getBaseUrl } from "@/utils/axios"
  6. import { InboxOutlined } from "@ant-design/icons"
  7. import { Button, Drawer, Form, Input, Popconfirm, Tooltip, notification } from "antd"
  8. import TextArea from "antd/lib/input/TextArea"
  9. import Dragger from "antd/lib/upload/Dragger"
  10. import { title } from "process"
  11. import React, { useRef, useState } from "react"
  12. import { useSelector } from "react-redux"
  13. const BUZZUPNews = () => {
  14. const { userInfo } = useSelector((state: RootState) => state.storeData)
  15. const [visible, setVisible] = useState(false)
  16. const [form] = Form.useForm()
  17. const currentType = useRef('' as 'create' | 'update')
  18. const currentItem = useRef({} as any)
  19. const tableRef = useRef(null)
  20. const columns = [
  21. {
  22. title: '图片',
  23. dataIndex: 'image',
  24. width: 300,
  25. render: (logo) => {
  26. return (
  27. <Tooltip title={
  28. <img src={logo} alt="" style={{ width: 240, height: 240, objectFit: 'contain' }} />
  29. }>
  30. <img src={logo} className="tp" alt="" style={{ width: 50, height: 50, objectFit: 'contain' }} />
  31. </Tooltip>
  32. )
  33. }
  34. },
  35. {
  36. title: '标题',
  37. width: 300,
  38. dataIndex: "title",
  39. render: (title) => {
  40. return (
  41. <Tooltip overlay={false} title={
  42. <div dangerouslySetInnerHTML={{ __html: title.replace(/\\n/g, '<br/>') }}></div>
  43. }>
  44. <div className="text-overflow tp" dangerouslySetInnerHTML={{ __html: title.replace(/\\n/g, '<br/>') }}></div>
  45. </Tooltip>
  46. )
  47. }
  48. },
  49. {
  50. title: '链接',
  51. dataIndex: 'url',
  52. width: 300
  53. },
  54. {
  55. title: '创建时间',
  56. dataIndex: 'time',
  57. render: (time) => (
  58. <div>{getTime(time * 1000)}</div>
  59. )
  60. },
  61. {
  62. title: 'options',
  63. fixed: 'right',
  64. width: 100,
  65. render: (item) => (
  66. <div>
  67. <Button type="primary" style={{ marginTop: 10 }} onClick={() => {
  68. currentItem.current = item
  69. currentType.current = 'update'
  70. form.setFieldsValue(item)
  71. setVisible(true)
  72. }}></Button>
  73. <Popconfirm title={`确认删除“${item.title}”吗?`} onConfirm={() => deleteSystemMsg(item)}>
  74. <Button type="primary" style={{ marginTop: 10, backgroundColor: 'red', borderColor: 'red' }}></Button>
  75. </Popconfirm>
  76. </div>
  77. )
  78. }
  79. ]
  80. const deleteSystemMsg = async (item) => {
  81. const res: any = await api.delete_new_message({
  82. id: item.id
  83. })
  84. if (res.code === 0) {
  85. tableRef.current.update()
  86. notification.success({
  87. message: '删除成功'
  88. })
  89. }
  90. }
  91. const onClose = () => setVisible(false)
  92. const onFinish = async (values) => {
  93. const parmas = {
  94. ...values
  95. }
  96. console.log(parmas);
  97. if (currentType.current === 'update') {
  98. parmas.id = currentItem.current.id
  99. }
  100. let res: any;
  101. if (currentType.current === 'create') {
  102. res = await api.add_news_message(parmas)
  103. } else {
  104. res = await api.updata_news_message(parmas)
  105. }
  106. if (res.code === 0) {
  107. setVisible(false)
  108. tableRef.current.update()
  109. form.resetFields()
  110. notification.success({
  111. message: currentType.current === 'create' ? '添加成功' : '修改成功'
  112. })
  113. }
  114. }
  115. const uploadProps = {
  116. name: 'img',
  117. action: `${getBaseUrl()}/admin/uploadImg`,
  118. headers: {
  119. Token: userInfo.token,
  120. },
  121. }
  122. const onChange = (info: any) => {
  123. if (info.file.status === 'removed') {
  124. form.setFieldsValue({
  125. image: ''
  126. })
  127. }
  128. if (info.file.status === 'done') {
  129. if (info.file.response && info.file.response.code === 0) {
  130. form.setFieldsValue({
  131. image: info.file.response.data.url
  132. })
  133. notification.success({
  134. message: `图片上传成功`
  135. })
  136. }
  137. } else if (info.file.status === 'error') {
  138. notification.error({
  139. message: `图片上传失败`
  140. });
  141. }
  142. }
  143. return (
  144. <div>
  145. <MyTable
  146. apiFun={api.get_news_message}
  147. ref={tableRef}
  148. columns={columns}
  149. header={
  150. <div style={{ width: '100%', display: 'flex', justifyContent: 'flex-end', marginBottom: 20 }}>
  151. <Button type="primary" onClick={() => {
  152. currentType.current = 'create'
  153. form.resetFields()
  154. setVisible(true)
  155. }}>BUZZUPNews</Button>
  156. </div>
  157. }
  158. />
  159. <Drawer title="添加系统通知" onClose={onClose} visible={visible} width="40%">
  160. <Form form={form} onFinish={onFinish}>
  161. <Form.Item
  162. name="url"
  163. label="链接"
  164. rules={[{ required: true, message: '请输入链接' }]}
  165. >
  166. <Input placeholder="请输入链接" />
  167. </Form.Item>
  168. <Form.Item
  169. name="title"
  170. label="标题"
  171. rules={[{ required: true, message: '请输入标题' }]}
  172. >
  173. <TextArea placeholder="请输入标题" style={{ minHeight: 200 }} />
  174. </Form.Item>
  175. <Form.Item label="模版" shouldUpdate={(prevValues, currentValues) => prevValues.title !== currentValues.title}>
  176. {({ getFieldValue }) => (
  177. <div dangerouslySetInnerHTML={{ __html: (getFieldValue('title') || '').replace(/\\n/g, '<br/>') }}></div>
  178. )}
  179. </Form.Item>
  180. <Form.Item
  181. label="图片"
  182. name="image"
  183. rules={[{ required: true, message: 'Please Select Logo Image!' }]}
  184. >
  185. <Dragger
  186. {...uploadProps}
  187. onChange={onChange}
  188. >
  189. <p className="ant-upload-drag-icon" style={{ display: 'flex', justifyContent: 'center', paddingTop: 10 }}>
  190. <InboxOutlined />
  191. </p>
  192. <p className="ant-upload-text" style={{ textAlign: 'center' }}></p>
  193. <p className="ant-upload-hint" style={{ textAlign: 'center', paddingBottom: 10 }}>
  194. Support for a single or bulk upload. Strictly prohibited from uploading company data or other
  195. banned files.
  196. </p>
  197. </Dragger>
  198. </Form.Item>
  199. <Form.Item label="">
  200. <div style={{ display: 'flex', justifyContent: 'center' }}>
  201. <Button type="primary" htmlType="submit"></Button>
  202. </div>
  203. </Form.Item>
  204. </Form>
  205. </Drawer>
  206. </div>
  207. )
  208. }
  209. export default BUZZUPNews