From 9307be249a162671bef3bf6416740af2e9657843 Mon Sep 17 00:00:00 2001 From: JustSong Date: Sun, 16 Apr 2023 19:01:38 +0800 Subject: [PATCH] chore: update time format --- common/constants.go | 5 ++++ controller/user.go | 11 ++++++--- web/src/components/MessagesTable.js | 8 +++--- web/src/helpers/utils.js | 38 +++++++++++++++++++++++++++++ web/src/pages/Home/index.js | 5 ++-- 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/common/constants.go b/common/constants.go index ddff2f7..c80f233 100644 --- a/common/constants.go +++ b/common/constants.go @@ -93,6 +93,11 @@ const ( SendEmailToOthersDisallowed = 2 ) +const ( + SaveMessageToDatabaseAllowed = 1 + SaveMessageToDatabaseDisallowed = 2 +) + const ( MessageSendStatusUnknown = 0 MessageSendStatusPending = 1 diff --git a/controller/user.go b/controller/user.go index 1e06955..4b38c05 100644 --- a/controller/user.go +++ b/controller/user.go @@ -635,6 +635,10 @@ func ManageUser(c *gin.Context) { user.SendEmailToOthers = common.SendEmailToOthersAllowed case "disallow_send_email_to_others": user.SendEmailToOthers = common.SendEmailToOthersDisallowed + case "allow_save_message_to_database": + user.SaveMessageToDatabase = common.SaveMessageToDatabaseAllowed + case "disallow_save_message_to_database": + user.SaveMessageToDatabase = common.SaveMessageToDatabaseDisallowed } if err := user.Update(false); err != nil { @@ -645,9 +649,10 @@ func ManageUser(c *gin.Context) { return } clearUser := model.User{ - Role: user.Role, - Status: user.Status, - SendEmailToOthers: user.SendEmailToOthers, + Role: user.Role, + Status: user.Status, + SendEmailToOthers: user.SendEmailToOthers, + SaveMessageToDatabase: user.SaveMessageToDatabase, } c.JSON(http.StatusOK, gin.H{ "success": true, diff --git a/web/src/components/MessagesTable.js b/web/src/components/MessagesTable.js index 998b9e7..d6b981d 100644 --- a/web/src/components/MessagesTable.js +++ b/web/src/components/MessagesTable.js @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react'; import { Button, Form, Label, Modal, Pagination, Table } from 'semantic-ui-react'; -import { API, openPage, showError, showSuccess, showWarning } from '../helpers'; +import { API, openPage, showError, showSuccess, showWarning, timestamp2string } from '../helpers'; import { ITEMS_PER_PAGE } from '../constants'; @@ -70,13 +70,11 @@ function renderChannel(channel) { } function renderTimestamp(timestamp) { - const date = new Date(timestamp * 1000); return ( <> - {date.getFullYear()}-{date.getMonth() + 1}-{date.getDate()}{' '} - {date.getHours()}:{date.getMinutes()}:{date.getSeconds()} + {timestamp2string(timestamp)} - ); + ) } function renderStatus(status) { diff --git a/web/src/helpers/utils.js b/web/src/helpers/utils.js index 9b1047a..37464ae 100644 --- a/web/src/helpers/utils.js +++ b/web/src/helpers/utils.js @@ -102,3 +102,41 @@ export function removeTrailingSlash(url) { return url; } } + +export function timestamp2string(timestamp) { + let date = new Date(timestamp * 1000); + let year = date.getFullYear().toString(); + let month = (date.getMonth() + 1).toString(); + let day = date.getDate().toString(); + let hour = date.getHours().toString(); + let minute = date.getMinutes().toString(); + let second = date.getSeconds().toString(); + if (month.length === 1) { + month = '0' + month; + } + if (day.length === 1) { + day = '0' + day; + } + if (hour.length === 1) { + hour = '0' + hour; + } + if (minute.length === 1) { + minute = '0' + minute; + } + if (second.length === 1) { + second = '0' + second; + } + return ( + year + + '-' + + month + + '-' + + day + + ' ' + + hour + + ':' + + minute + + ':' + + second + ); +} \ No newline at end of file diff --git a/web/src/pages/Home/index.js b/web/src/pages/Home/index.js index baf2471..bcea564 100644 --- a/web/src/pages/Home/index.js +++ b/web/src/pages/Home/index.js @@ -1,6 +1,6 @@ import React, { useContext, useEffect } from 'react'; import { Card, Grid, Header, Segment } from 'semantic-ui-react'; -import { API, showError, showNotice } from '../../helpers'; +import { API, showError, showNotice, timestamp2string } from '../../helpers'; import { StatusContext } from '../../context/Status'; const Home = () => { @@ -22,8 +22,7 @@ const Home = () => { const getStartTimeString = () => { const timestamp = statusState?.status?.start_time; - const date = new Date(timestamp * 1000); - return date.toLocaleString(); + return timestamp2string(timestamp); }; useEffect(() => {