Skip to content

Commit

Permalink
add user management feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Nov 8, 2023
1 parent 719380e commit ea8331b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 11 deletions.
25 changes: 24 additions & 1 deletion admin/user.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package admin

import (
"chat/utils"
"database/sql"
"math"
"time"
)

func GetUserPagination(db *sql.DB, page int64, search string) PaginationForm {
Expand Down Expand Up @@ -41,12 +43,33 @@ func GetUserPagination(db *sql.DB, page int64, search string) PaginationForm {

for rows.Next() {
var user UserData
if err := rows.Scan(&user.Id, &user.Username, &user.IsAdmin); err != nil {
var (
expired []uint8
quota sql.NullFloat64
usedQuota sql.NullFloat64
totalMonth sql.NullInt64
isEnterprise sql.NullBool
)
if err := rows.Scan(&user.Id, &user.Username, &user.IsAdmin, &quota, &usedQuota, &expired, &totalMonth, &isEnterprise); err != nil {
return PaginationForm{
Status: false,
Message: err.Error(),
}
}
if quota.Valid {
user.Quota = float32(quota.Float64)
}
if usedQuota.Valid {
user.UsedQuota = float32(usedQuota.Float64)
}
if totalMonth.Valid {
user.TotalMonth = totalMonth.Int64
}
stamp := utils.ConvertTime(expired)
if stamp != nil {
user.IsSubscribed = stamp.After(time.Now())
}
user.Enterprise = isEnterprise.Valid && isEnterprise.Bool
users = append(users, user)
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/assets/admin/management.less
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
user-select: none;
text-align: center;
font-size: 14px;
margin: 4rem 0 2rem;
color: hsl(var(--text-secondary));
}

Expand All @@ -41,6 +42,7 @@
}
}

.user-row,
.invitation-row {
display: flex;
flex-direction: row;
Expand All @@ -52,6 +54,7 @@
margin: 1rem 0;
}

.user-action,
.invitation-action {
display: flex;
margin-top: 1rem;
Expand Down
43 changes: 33 additions & 10 deletions app/src/components/admin/UserTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTranslation } from "react-i18next";
import { useToast } from "@/components/ui/use-toast.ts";
import { useState } from "react";
import {useEffect, useRef, useState} from "react";
import { UserForm, UserResponse } from "@/admin/types.ts";
import { getUserList } from "@/admin/api.ts";
import { useEffectAsync } from "@/utils/hook.ts";
Expand All @@ -13,7 +13,8 @@ import {
TableRow,
} from "@/components/ui/table.tsx";
import { Button } from "@/components/ui/button.tsx";
import { ChevronLeft, ChevronRight, RotateCw } from "lucide-react";
import {ChevronLeft, ChevronRight, MoreHorizontal, RotateCw, Search} from "lucide-react";
import {Input} from "@/components/ui/input.tsx";

function UserTable() {
const { t } = useTranslation();
Expand All @@ -38,26 +39,48 @@ function UserTable() {

return (
<div className={`user-table`}>
<div className={`flex flex-row mb-6`}>
<Input
className={`search`}
placeholder={t("admin.search-username")}
value={search} onChange={(e) => setSearch(e.target.value)}
/>
<Button size={`icon`} className={`flex-shrink-0 ml-2`} onClick={update}>
<Search className={`h-4 w-4`} />
</Button>
</div>
{(data.data && data.data.length > 0) || page > 0 ? (
<>
<Table>
<TableHeader>
<TableRow className={`select-none whitespace-nowrap`}>
<TableHead>{t("admin.code")}</TableHead>
<TableHead>ID</TableHead>
<TableHead>{t("admin.username")}</TableHead>
<TableHead>{t("admin.quota")}</TableHead>
<TableHead>{t("admin.type")}</TableHead>
<TableHead>{t("admin.used")}</TableHead>
<TableHead>{t("admin.updated-at")}</TableHead>
<TableHead>{t("admin.used-quota")}</TableHead>
<TableHead>{t("admin.is-subscribed")}</TableHead>
<TableHead>{t("admin.total-month")}</TableHead>
<TableHead>{t("admin.enterprise")}</TableHead>
<TableHead>{t("admin.is-admin")}</TableHead>
<TableHead>{t("admin.action")}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{data.data.map((user, idx) => (
<TableRow key={idx}>
<TableCell>{user.code}</TableCell>
<TableCell>{user.id}</TableCell>
<TableCell>{user.username}</TableCell>
<TableCell>{user.quota}</TableCell>
<TableCell>{user.type}</TableCell>
<TableCell>{t(`admin.used-${user.used}`)}</TableCell>
<TableCell>{user.updated_at}</TableCell>
<TableCell>{user.used_quota}</TableCell>
<TableCell>{t(user.is_subscribed.toString())}</TableCell>
<TableCell>{user.total_month}</TableCell>
<TableCell>{t(user.enterprise.toString())}</TableCell>
<TableCell>{t(user.is_admin.toString())}</TableCell>
<TableCell>
<Button variant={`outline`} size={`icon`}>
<MoreHorizontal className={`h-4 w-4`} />
</Button>
</TableCell>
</TableRow>
))}
</TableBody>
Expand Down
54 changes: 54 additions & 0 deletions app/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const resources = {
close: "关闭",
edit: "编辑",
pricing: "模型定价表",
true: "是",
false: "否",
conversation: {
title: "对话",
empty: "空空如也",
Expand Down Expand Up @@ -264,6 +266,22 @@ const resources = {
type: "类型",
used: "状态",
number: "数量",
username: "用户名",
"is-admin": "管理员",
"used-quota": "已用点数",
"is-subscribed": "是否订阅",
"total-month": "总计订阅月数",
enterprise: "企业版",
action: "操作",
"search-username": "搜索用户名",
"quota-action": "点数变更",
"quota-action-desc": "请输入点数变更值(正数为增加,负数为减少)",
"subscription-action": "赠送订阅",
"subscription-action-desc": "请输入赠送的订阅月数",
"operate-success": "操作成功",
"operate-success-prompt": "您的操作已成功执行。",
"operate-failed": "操作失败",
"operate-failed-prompt": "操作失败,原因:{{reason}}",
"updated-at": "领取时间",
"used-true": "已使用",
"used-false": "未使用",
Expand Down Expand Up @@ -297,6 +315,8 @@ const resources = {
close: "Close",
edit: "Edit",
pricing: "Model Pricing",
true: "Yes",
false: "No",
conversation: {
title: "Conversation",
empty: "Empty",
Expand Down Expand Up @@ -546,6 +566,22 @@ const resources = {
type: "Type",
used: "Status",
number: "Number",
username: "Username",
"is-admin": "Admin",
"used-quota": "Used Quota",
"is-subscribed": "Subscribed",
"total-month": "Total Month",
enterprise: "Enterprise",
action: "Action",
"search-username": "Search Username",
"quota-action": "Quota Change",
"quota-action-desc": "Please enter the quota change value (positive for increase, negative for decrease)",
"subscription-action": "Subscription Gift",
"subscription-action-desc": "Please enter the gift subscription months",
"operate-success": "Operate Success",
"operate-success-prompt": "Your operation has been successfully executed.",
"operate-failed": "Operate Failed",
"operate-failed-prompt": "Operation failed, reason: {{reason}}",
"updated-at": "Updated At",
"used-true": "Used",
"used-false": "Unused",
Expand Down Expand Up @@ -579,6 +615,8 @@ const resources = {
close: "Закрыть",
edit: "Редактировать",
pricing: "Тарифы моделей",
true: "Да",
false: "Нет",
conversation: {
title: "Разговор",
empty: "Пусто",
Expand Down Expand Up @@ -829,6 +867,22 @@ const resources = {
type: "Тип",
used: "Статус",
number: "Количество",
username: "Имя пользователя",
"is-admin": "Админ",
"used-quota": "Использовано",
"is-subscribed": "Подписан",
"total-month": "Всего месяцев",
enterprise: "Корпоративный",
action: "Действие",
"search-username": "Поиск по имени пользователя",
"quota-action": "Изменение квоты",
"quota-action-desc": "Пожалуйста, введите значение изменения квоты (положительное для увеличения, отрицательное для уменьшения)",
"subscription-action": "Подарок подписки",
"subscription-action-desc": "Пожалуйста, введите количество месяцев подарочной подписки",
"operate-success": "Успешно",
"operate-success-prompt": "Ваша операция была успешно выполнена.",
"operate-failed": "Не удалось",
"operate-failed-prompt": "Не удалось выполнить операцию, причина: {{reason}}",
"updated-at": "Обновлено",
"used-true": "Использовано",
"used-false": "Не использовано",
Expand Down

0 comments on commit ea8331b

Please sign in to comment.