Skip to content

Commit

Permalink
系统设置增加时区设置
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Feb 26, 2025
1 parent 64fcbff commit af3e358
Show file tree
Hide file tree
Showing 10 changed files with 556 additions and 5 deletions.
18 changes: 18 additions & 0 deletions back/api/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,22 @@ export default (app: Router) => {
}
},
);

route.put(
'/config/timezone',
celebrate({
body: Joi.object({
timezone: Joi.string().allow('').allow(null),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.updateTimezone(req.body);
res.send(result);
} catch (e) {
return next(e);
}
},
);
};
18 changes: 17 additions & 1 deletion back/config/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ export function safeJSONParse(value?: string) {
try {
return JSON.parse(value);
} catch (error) {
Logger.error('[JSON.parse失败]', error);
Logger.error('[safeJSONParse失败]', error);
return {};
}
}
Expand All @@ -542,3 +542,19 @@ export async function rmPath(path: string) {
Logger.error('[rmPath失败]', error);
}
}

export async function setSystemTimezone(timezone: string): Promise<boolean> {
try {
if (!(await fileExist(`/usr/share/zoneinfo/${timezone}`))) {
throw new Error('Invalid timezone');
}

await promiseExec(`ln -sf /usr/share/zoneinfo/${timezone} /etc/localtime`);
await promiseExec(`echo "${timezone}" > /etc/timezone`);

return true;
} catch (error) {
Logger.error('[setSystemTimezone失败]', error);
return false;
}
}
1 change: 1 addition & 0 deletions back/data/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface SystemConfigInfo {
nodeMirror?: string;
pythonMirror?: string;
linuxMirror?: string;
timezone?: string;
}

export interface LoginLogInfo {
Expand Down
23 changes: 22 additions & 1 deletion back/services/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
promiseExec,
readDirs,
rmPath,
setSystemTimezone,
} from '../config/util';
import {
DependenceModel,
Expand Down Expand Up @@ -50,7 +51,10 @@ export default class SystemService {

public async getSystemConfig() {
const doc = await this.getDb({ type: AuthDataType.systemConfig });
return doc;
return {
...doc,
info: { ...doc.info, timezone: doc.info?.timezone || 'Asia/Shanghai' },
};
}

private async updateAuthDb(payload: SystemInfo): Promise<SystemInfo> {
Expand Down Expand Up @@ -471,4 +475,21 @@ export default class SystemService {
await rmPath(path.join(config.systemLogPath, log.title));
}
}

public async updateTimezone(info: SystemModelInfo) {
if (!info.timezone) {
info.timezone = 'Asia/Shanghai';
}
const oDoc = await this.getSystemConfig();
await this.updateAuthDb({
...oDoc,
info: { ...oDoc.info, ...info },
});
const success = await setSystemTimezone(info.timezone);
if (success) {
return { code: 200, data: info };
} else {
return { code: 400, message: '设置时区失败' };
}
}
}
3 changes: 3 additions & 0 deletions shell/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ main() {
resettfa)
eval update_auth_config "\\\"twoFactorActivated\\\":false" "禁用两步验证" $cmd
;;
resetpwd)
eval update_auth_config "\\\"password\\\":\\\"$p2\\\"" "重置密码" $cmd
;;
*)
eval echo -e "命令输入错误...\\\n" $cmd
eval usage $cmd
Expand Down
3 changes: 2 additions & 1 deletion src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -500,5 +500,6 @@
"如果恢复失败,可进入容器执行": "If recovery fails, you can enter the container and execute",
"常规定时": "Normal Timing",
"手动运行": "Manual Run",
"开机运行": "Boot Run"
"开机运行": "Boot Run",
"时区": "Timezone"
}
3 changes: 2 additions & 1 deletion src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@
"如果恢复失败,可进入容器执行": "如果恢复失败,可进入容器执行",
"常规定时": "常规定时",
"手动运行": "手动运行",
"开机运行": "开机运行"
"开机运行": "开机运行",
"时区": "时区"
}

10 changes: 10 additions & 0 deletions src/pages/setting/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@
display: flex;
gap: 40px;
}

.ql-container-wrapper.ql-setting-container {
.ant-tabs-tabpane > div {
padding-left: 2px;
}

.ant-tabs-tabpane > .ant-form {
padding-left: 2px;
}
}
31 changes: 31 additions & 0 deletions src/pages/setting/other.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import Countdown from 'antd/lib/statistic/Countdown';
import useProgress from './progress';
import pick from 'lodash/pick';
import { disableBody } from '@/utils';
import { TIMEZONES } from '@/utils/const';

const dataMap = {
'log-remove-frequency': 'logRemoveFrequency',
'cron-concurrency': 'cronConcurrency',
timezone: 'timezone',
};

const Other = ({
Expand All @@ -37,6 +39,7 @@ const Other = ({
const [systemConfig, setSystemConfig] = useState<{
logRemoveFrequency?: number | null;
cronConcurrency?: number | null;
timezone?: string | null;
}>();
const [form] = Form.useForm();
const [exportLoading, setExportLoading] = useState(false);
Expand Down Expand Up @@ -252,6 +255,34 @@ const Other = ({
</Button>
</Input.Group>
</Form.Item>
<Form.Item label={intl.get('时区')} name="timezone">
<Input.Group compact>
<Select
value={systemConfig?.timezone}
style={{ width: 180 }}
onChange={(value) => {
setSystemConfig({ ...systemConfig, timezone: value });
}}
options={TIMEZONES.map((timezone) => ({
value: timezone,
label: timezone,
}))}
showSearch
filterOption={(input, option) =>
option?.value?.toLowerCase().indexOf(input.toLowerCase()) >= 0
}
/>
<Button
type="primary"
onClick={() => {
updateSystemConfig('timezone');
}}
style={{ width: 84 }}
>
{intl.get('确认')}
</Button>
</Input.Group>
</Form.Item>
<Form.Item label={intl.get('语言')} name="lang">
<Select
defaultValue={localStorage.getItem('lang') || ''}
Expand Down
Loading

0 comments on commit af3e358

Please sign in to comment.