瀏覽代碼

refactor: update test message content

JustSong 2 年之前
父節點
當前提交
99bf0ca127
共有 3 個文件被更改,包括 23 次插入28 次删除
  1. 2 14
      web/src/components/ChannelsTable.js
  2. 2 14
      web/src/components/PushSetting.js
  3. 19 0
      web/src/helpers/utils.js

+ 2 - 14
web/src/components/ChannelsTable.js

@@ -1,7 +1,7 @@
 import React, { useEffect, useState } from 'react';
 import { Button, Form, Label, Pagination, Table } from 'semantic-ui-react';
 import { Link } from 'react-router-dom';
-import { API, showError, showSuccess } from '../helpers';
+import { API, showError, showSuccess, testChannel } from '../helpers';
 
 import { ITEMS_PER_PAGE } from '../constants';
 import { renderChannel, renderTimestamp } from '../helpers/render';
@@ -154,18 +154,6 @@ const ChannelsTable = () => {
     setLoading(false);
   };
 
-  const test = async (channel) => {
-    let res = await API.get(
-      `/push/${user.username}?token=${user.token}&channel=${channel}&title=消息推送服务&description=配置成功!`
-    );
-    const { success, message } = res.data;
-    if (success) {
-      showSuccess('测试消息已发送');
-    } else {
-      showError(message);
-    }
-  };
-
   return (
     <>
       <Form onSubmit={searchChannels}>
@@ -269,7 +257,7 @@ const ChannelsTable = () => {
                       <Button
                         size={'small'}
                         onClick={() => {
-                          test(channel.name).then();
+                          testChannel(user.username, user.token, channel.name).then();
                         }}
                       >
                         测试

+ 2 - 14
web/src/components/PushSetting.js

@@ -1,6 +1,6 @@
 import React, { useEffect, useState } from 'react';
 import { Button, Form, Grid, Header, Message } from 'semantic-ui-react';
-import { API, showError, showSuccess } from '../helpers';
+import { API, showError, showSuccess, testChannel } from '../helpers';
 
 const PushSetting = () => {
   let [user, setUser] = useState({
@@ -79,18 +79,6 @@ const PushSetting = () => {
     }
   };
 
-  const test = async () => {
-    let res = await API.get(
-      `/push/${user.username}?token=${user.token}&channel=${user.channel}&title=消息推送服务&description=配置成功!`
-    );
-    const { success, message } = res.data;
-    if (success) {
-      showSuccess('测试消息已发送');
-    } else {
-      showError(message);
-    }
-  };
-
   return (
     <Grid columns={1}>
       <Grid.Column>
@@ -118,7 +106,7 @@ const PushSetting = () => {
           <Button onClick={() => submit('general')} loading={loading}>
             保存
           </Button>
-          <Button onClick={() => test('')}>测试</Button>
+          <Button onClick={() => testChannel(user.username, user.token, '')}>测试</Button>
         </Form>
       </Grid.Column>
     </Grid>

+ 19 - 0
web/src/helpers/utils.js

@@ -1,5 +1,6 @@
 import { toast } from 'react-toastify';
 import { toastConstants } from '../constants';
+import { API } from './api';
 
 export function isAdmin() {
   let user = localStorage.getItem('user');
@@ -152,4 +153,22 @@ export function downloadTextAsFile(text, filename) {
   a.href = url;
   a.download = filename;
   a.click();
+}
+
+export async function testChannel(username, token, channel) {
+  let res = await API.post(
+    `/push/${username}/`, {
+      token,
+      channel,
+      title: '消息推送服务',
+      description: channel === "" ? '消息推送通道测试成功' : `消息推送通道 ${channel} 测试成功`,
+      content: '欢迎使用消息推送服务,这是一条测试消息。'
+    }
+  );
+  const { success, message } = res.data;
+  if (success) {
+    showSuccess('测试消息已发送');
+  } else {
+    showError(message);
+  }
 }