This commit is contained in:
oneao committed 2025-12-04 17:31:33 +08:00
1 parent 4dcac7b5a0
commit 59d3be2299
25 files changed
+380 -125

No files matched your search

@@ -7,6 +7,7 @@ import { IconQq, IconWeixin, IconZhiwen } from '@/components/iconfont'
import { Divider } from '@/components/ui/divider'
import useFormValidation from '@/hooks/useFormValidation'
import { loginDefaultApi } from '@/request/api/auth'
import { useMoneyCategoryStore } from '@/store/moneyCategory'
import { useUserStore } from '@/store/user'
import { toast } from '@/utils/toast'
@@ -20,6 +21,7 @@ interface LoginForm {
export default function AuthLogin() {
const router = useRouter()
const userStore = useUserStore()
const moneyCategoryStore = useMoneyCategoryStore()
const [loading, setLoading] = useState<boolean>(false)
const [form, setForm] = useState<LoginForm>({
@@ -60,6 +62,7 @@ export default function AuthLogin() {
refreshToken: res.data.refreshToken,
rememberMe: form.rememberMe,
})
moneyCategoryStore.init()
router.replace('/user')
}
@@ -6,10 +6,12 @@ import { Link, LinkText } from '@/components/ui/link'
interface JoinSpaceProps {
onBack?: () => void
onJoin?: (code: string) => void
}
export default function JoinSpace({
onBack,
onJoin,
}: JoinSpaceProps) {
const formRef = useRef<AFormRef>(null)
const [form, setForm] = useState({
@@ -22,11 +24,32 @@ export default function JoinSpace({
name="username"
label="邀请码"
>
<AInput placeholder="请输入邀请码"></AInput>
<AInput
placeholder="请输入邀请码"
value={form.code}
onChangeText={text => setForm({
...form,
code: text,
})}
>
</AInput>
</AFormItem>
</AForm>
<AButton className="w-full">加入空间</AButton>
<AButton
className="w-full"
onPress={() => {
if (form.code === '') {
//
}
else {
onJoin?.(form.code)
}
}}
>
加入空间
</AButton>
<View
className="flex-row items-center w-full"
style={{
@@ -1,3 +1,4 @@
import type { UserSpace } from '@/types/space/spaceUser'
import { Text, View } from 'react-native'
import { AButton, ADivider, LucideIcon } from '@/components/common/ao'
@@ -1,9 +1,10 @@
import type { SpaceUser, UserSpace } from '@/types/space/spaceUser'
import * as Clipboard from 'expo-clipboard'
import { useState } from 'react'
import { FlatList, Pressable, TouchableOpacity, View } from 'react-native'
import QRCode from 'react-native-qrcode-svg'
import { AButton, ADivider, LucideIcon } from '@/components/common/ao'
import { AButton, ADivider, LucideIcon } from '@/components/common/ao'
import {
Accordion,
AccordionContent,
@@ -25,16 +26,22 @@ import { ChevronDownIcon, ChevronUpIcon } from '@/components/ui/icon'
import { Menu, MenuItem, MenuItemLabel } from '@/components/ui/menu'
import { Text } from '@/components/ui/text'
import { VStack } from '@/components/ui/vstack'
import { useUserStore } from '@/store/user'
import { formatRelativeTime } from '@/utils/time'
import { toast } from '@/utils/toast'
export default function SpaceInviteAndMembers() {
const [accordion, setAccordion] = useState(['a'])
interface SpaceInviteProps {
userSpace: UserSpace | null
spaceUsers: SpaceUser[]
}
const members = [
{ id: 1, name: '张三' },
{ id: 2, name: '李四' },
{ id: 3, name: '王五' },
]
export default function SpaceInvite({
userSpace,
spaceUsers,
}: SpaceInviteProps) {
const userStore = useUserStore()
const [accordion, setAccordion] = useState(['a'])
async function copyInviteCode() {
await Clipboard.setStringAsync('hello world')
@@ -94,7 +101,7 @@ export default function SpaceInviteAndMembers() {
copyInviteCode()
}}
>
<Text className="font-bold text-3xl tracking-[4px] mr-3">123456</Text>
<Text className="font-bold text-3xl tracking-[4px] mr-3">{userSpace?.invite_code}</Text>
<LucideIcon icon="Copy" size={16} color="blue" clickable={false} />
</TouchableOpacity>
@@ -102,7 +109,7 @@ export default function SpaceInviteAndMembers() {
<ADivider direction="horizontal" text="扫描二维码" />
<QRCode
size={120}
value="http://awesome.link.qr"
value={userSpace?.invite_code}
/>
</View>
</AccordionContent>
@@ -112,31 +119,50 @@ export default function SpaceInviteAndMembers() {
<View className="flex flex-row items-center justify-between">
<Text className="font-semibold text-lg">空间成员</Text>
<Badge size="lg" variant="solid" action="muted">
<BadgeText>3 人</BadgeText>
<BadgeText>
{spaceUsers.length}
{' '}
人
</BadgeText>
</Badge>
</View>
<View>
<FlatList
data={members}
keyExtractor={item => item.id.toString()}
data={spaceUsers}
keyExtractor={item => item.user_id.toString()}
renderItem={({ item }) => (
<TouchableOpacity>
<HStack space="md" className="my-2 items-center">
<Avatar className="bg-indigo-600">
<AvatarFallbackText className="text-white text-lg">
{item.name}
<AvatarFallbackText className="text-white text-2xl">
{item.username}
</AvatarFallbackText>
<AvatarBadge />
</Avatar>
<VStack>
<HStack space="sm">
<Heading size="sm">{item.name}</Heading>
<Badge size="sm" variant="solid" action="muted">
<BadgeText>管理员</BadgeText>
</Badge>
<Heading size="sm">{item.username}</Heading>
{[0, 1].includes(item.space_role) && (
<Badge size="sm" variant="solid" action="muted">
<BadgeText>
{item.space_role === 0 ? '创建者' : '管理员'}
</BadgeText>
</Badge>
)}
{
(userStore.userInfo?.id === item.user_id) && (
<Badge size="sm" variant="solid" action='info'>
<BadgeText>
我
</BadgeText>
</Badge>
)
}
</HStack>
<Text size="sm">Marketing Coordinator</Text>
<Text size="sm" className="mt-1">{formatRelativeTime(item.active_time)}</Text>
</VStack>
<View className="ml-auto">
@@ -1,18 +1,87 @@
import type { SpaceUser, UserSpace } from '@/types/space/spaceUser'
import { useEffect, useState } from 'react'
import { View } from 'react-native'
import CustomHeader from '@/components/custom/Header'
import { createSpaceApi } from '@/request/api/space/space'
import { getUserSpaceApi, joinSpaceApi, listSpaceUsersApi } from '@/request/api/space/spaceUser'
import JoinSpace from './components/JoinSpace'
import SpaceHome from './components/SpaceHome'
import SpaceInviteAndMembers from './components/SpaceInviteAndMembers'
import SpaceInviteAndMembers from './components/SpaceInvite'
import StepFade from './components/StepFade'
export default function PagerWithTab() {
const [step, setStep] = useState<'join' | 'invite' | 'home'>('home')
const [userSpace, setUserSpace] = useState<UserSpace | null>(null)
const [spaceUsers, setSpaceUsers] = useState<SpaceUser[]>([])
const fetchData = async () => {
await Promise.all([
getUserSpaceApi({})
.then((res) => {
if (res.isSuccess) {
if (res.data === null) {
setUserSpace(null)
setStep('home')
}
else {
setUserSpace({
space_id: res.data.space_id,
space_name: res.data.space_name,
role: res.data.role,
invite_code: res.data.invite_code,
})
setStep('invite')
}
}
else {
// 提示
}
})
.catch(err => console.error('getUserSpaceApi error:', err)),
listSpaceUsersApi({})
.then((res) => {
if (res.isSuccess) {
setSpaceUsers(res.data.users)
}
else {
// 提示
}
})
.catch(err => console.error('listSpaceUsersApi error:', err)),
])
}
useEffect(() => {
fetchData()
}, [])
function createSpace() {
createSpaceApi({}).then((res) => {
if (res.isSuccess) {
setUserSpace({
space_id: res.data.id,
space_name: '',
role: 0,
invite_code: res.data.invite_code,
})
setStep('invite')
}
}).catch((err) => {
console.log(err)
})
}
function joinSpace(code: string) {
joinSpaceApi({
invite_code: code,
}).then((res) => {
if (res.isSuccess) {
fetchData()
}
}).catch((err) => {
console.log(err)
})
}
return (
@@ -21,9 +90,7 @@ export default function PagerWithTab() {
<View className="flex-1">
<StepFade visible={step === 'home'}>
<SpaceHome
onCreate={() => {
setStep('invite')
}}
onCreate={createSpace}
onJoin={() => {
setStep('join')
}}
@@ -35,11 +102,16 @@ export default function PagerWithTab() {
onBack={() => {
setStep('home')
}}
onJoin={(code) => {
if (code !== '') {
joinSpace(code)
}
}}
/>
</StepFade>
<StepFade visible={step === 'invite'}>
<SpaceInviteAndMembers />
<SpaceInviteAndMembers userSpace={userSpace} spaceUsers={spaceUsers} />
</StepFade>
</View>
</View>
@@ -61,7 +61,7 @@ export default function CustomHeader(props: CustomHeaderProps) {
{...screenProps}
options={{
...screenProps?.options,
headerShown: true,
headerShown: false,
header: () => (
<SafeAreaView
className={`${className}`}
@@ -31,8 +31,8 @@ function isWhiteListed(url: string) {
}
const alovaInstance = createAlova({
baseURL: 'http://192.168.5.6:8086/app',
// baseURL: 'http://192.168.1.136:8086/app',
// baseURL: 'http://192.168.5.6:8086/app',
baseURL: 'http://192.168.1.136:8086/app',
requestAdapter: axiosRequestAdapter(),
timeout: 5000,
cacheFor: null,
@@ -1,3 +1,10 @@
export interface UserSpace {
space_id: number
space_name: string
role: number
invite_code: string
}
// 获取用户的空间
export interface GetUserSpaceParams {
@@ -22,6 +29,7 @@ export interface SpaceUser {
user_id: number
username: string
space_role: number
active_time: string
}
export interface ListSpaceUsersParams {
@@ -68,3 +68,40 @@ export function isInMonth(date: string, month: string): boolean {
// month 形如 "2025-01"
return dayjs(date).format('YYYY-MM') === month
}
export function formatRelativeTime(date: string | Date | number): string {
if (typeof date === 'string' && !date.trim()) {
return '暂无活跃'
}
const d = dayjs(date)
if (!d.isValid())
return '暂无活跃'
const now = dayjs()
const diff = now.diff(d, 'second') // 相差秒数
if (diff < 60) {
return '刚刚'
}
else if (diff < 3600) {
const minutes = Math.floor(diff / 60)
return `${minutes}分钟前`
}
else if (diff < 86400) {
const hours = Math.floor(diff / 3600)
return `${hours}小时前`
}
else if (diff < 2592000) { // 30天以内
const days = Math.floor(diff / 86400)
return `${days}天前`
}
else if (diff < 31536000) { // 1年以内
const months = Math.floor(diff / 2592000)
return `${months}个月前`
}
else {
const years = Math.floor(diff / 31536000)
return `${years}年前`
}
}
@@ -44,8 +44,8 @@ func LoginDefault(ctx context.Context, c *app.RequestContext) {
// 异步更新登录事件
go func(userID int64) {
time, err := db.Queries.UpdateUserLoginLastTime(ctx, userID)
if err != nil || time == 0 {
err = db.Queries.UpdateUserLoginLastTime(ctx, userID)
if err != nil {
logx.CtxError(ctx, "更新用户登录时间失败,UserID=%d, err=%v", loginUser.ID, err)
}
}(loginUser.ID)
@@ -6,8 +6,10 @@ import (
auth "cn/oneao/base-go/biz/model/auth"
"cn/oneao/base-go/db"
"cn/oneao/base-go/db/repo"
"cn/oneao/base-go/utils"
"cn/oneao/base-go/utils/errorx"
"cn/oneao/base-go/utils/idgen"
"cn/oneao/base-go/utils/pgtypex"
"cn/oneao/base-go/utils/response"
"context"
@@ -64,11 +66,18 @@ func RegisterDefault(ctx context.Context, c *app.RequestContext) {
}
}()
generateCode, err := utils.GenerateCode(8)
if err != nil {
errorx.AddError(c, err)
return
}
// 插入用户
params := repo.InsertUserParams{
ID: idgen.NextId(),
Account: account,
Password: password,
Username: pgtypex.StringToText("a_" + generateCode),
}
insert, err := db.Queries.WithTx(tx).InsertUser(ctx, params)
@@ -9,6 +9,7 @@ import (
"cn/oneao/base-go/db/repo"
"cn/oneao/base-go/utils/errorx"
"cn/oneao/base-go/utils/idgen"
"cn/oneao/base-go/utils/pgtypex"
"cn/oneao/base-go/utils/response"
"context"
@@ -78,9 +79,10 @@ func ListSpaceUsers(ctx context.Context, c *app.RequestContext) {
// 填充用户列表
for _, u := range users {
resp.Users = append(resp.Users, &space.SpaceUser{
UserId: u.UserID,
Username: u.Username.String,
SpaceRole: u.Role,
UserId: u.UserID,
Username: u.Username.String,
SpaceRole: u.Role,
ActiveTime: pgtypex.TimestampToDateTimeString(u.ActiveTime),
})
}
@@ -3,7 +3,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.0
// protoc v6.33.1
// protoc v4.25.3
// source: api.proto
package api
@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.0
// protoc v6.33.1
// protoc v4.25.3
// source: space.proto
package space
@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.0
// protoc v6.33.1
// protoc v4.25.3
// source: space/space_types.proto
package space
@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.0
// protoc v6.33.1
// protoc v4.25.3
// source: space/space_user_types.proto
package space
@@ -65,9 +65,10 @@ type SpaceUser struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
UserId int64 `protobuf:"varint,1,opt,name=UserId,proto3" form:"user_id" json:"user_id"`
Username string `protobuf:"bytes,2,opt,name=Username,proto3" form:"username" json:"username"`
SpaceRole int32 `protobuf:"varint,4,opt,name=SpaceRole,proto3" form:"space_role" json:"space_role"`
UserId int64 `protobuf:"varint,1,opt,name=UserId,proto3" form:"user_id" json:"user_id"`
Username string `protobuf:"bytes,2,opt,name=Username,proto3" form:"username" json:"username"`
SpaceRole int32 `protobuf:"varint,3,opt,name=SpaceRole,proto3" form:"space_role" json:"space_role"`
ActiveTime string `protobuf:"bytes,4,opt,name=ActiveTime,proto3" form:"active_time" json:"active_time"`
}
func (x *SpaceUser) Reset() {
@@ -123,6 +124,13 @@ func (x *SpaceUser) GetSpaceRole() int32 {
return 0
}
func (x *SpaceUser) GetActiveTime() string {
if x != nil {
return x.ActiveTime
}
return ""
}
type ListSpaceUsersResult struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@@ -646,66 +654,69 @@ var file_space_space_user_types_proto_rawDesc = []byte{
0x65, 0x72, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05,
0x73, 0x70, 0x61, 0x63, 0x65, 0x1a, 0x09, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x22, 0x16, 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x70, 0x61, 0x63, 0x65, 0x55, 0x73, 0x65,
0x72, 0x73, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x22, 0x88, 0x01, 0x0a, 0x09, 0x53, 0x70, 0x61,
0x72, 0x73, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x22, 0xb9, 0x01, 0x0a, 0x09, 0x53, 0x70, 0x61,
0x63, 0x65, 0x55, 0x73, 0x65, 0x72, 0x12, 0x23, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64,
0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x42, 0x0b, 0xca, 0xbb, 0x18, 0x07, 0x75, 0x73, 0x65, 0x72,
0x5f, 0x69, 0x64, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x28, 0x0a, 0x08, 0x55,
0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x42, 0x0c, 0xca,
0xbb, 0x18, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x52, 0x08, 0x55, 0x73, 0x65,
0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x2c, 0x0a, 0x09, 0x53, 0x70, 0x61, 0x63, 0x65, 0x52, 0x6f,
0x6c, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x42, 0x0e, 0xca, 0xbb, 0x18, 0x0a, 0x73, 0x70,
0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x42, 0x0e, 0xca, 0xbb, 0x18, 0x0a, 0x73, 0x70,
0x61, 0x63, 0x65, 0x5f, 0x72, 0x6f, 0x6c, 0x65, 0x52, 0x09, 0x53, 0x70, 0x61, 0x63, 0x65, 0x52,
0x6f, 0x6c, 0x65, 0x22, 0x3e, 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x70, 0x61, 0x63, 0x65,
0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x26, 0x0a, 0x05, 0x75,
0x73, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x73, 0x70, 0x61,
0x63, 0x65, 0x2e, 0x53, 0x70, 0x61, 0x63, 0x65, 0x55, 0x73, 0x65, 0x72, 0x52, 0x05, 0x75, 0x73,
0x65, 0x72, 0x73, 0x22, 0x4d, 0x0a, 0x0f, 0x4a, 0x6f, 0x69, 0x6e, 0x53, 0x70, 0x61, 0x63, 0x65,
0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x3a, 0x0a, 0x0a, 0x49, 0x6e, 0x76, 0x69, 0x74, 0x65,
0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x1a, 0xca, 0xbb, 0x18, 0x0b,
0x69, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0xda, 0xbb, 0x18, 0x07, 0x24,
0x20, 0x21, 0x3d, 0x20, 0x27, 0x27, 0x52, 0x0a, 0x49, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x43, 0x6f,
0x64, 0x65, 0x22, 0x11, 0x0a, 0x0f, 0x4a, 0x6f, 0x69, 0x6e, 0x53, 0x70, 0x61, 0x63, 0x65, 0x52,
0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x11, 0x0a, 0x0f, 0x45, 0x78, 0x69, 0x74, 0x53, 0x70, 0x61,
0x63, 0x65, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x22, 0x11, 0x0a, 0x0f, 0x45, 0x78, 0x69, 0x74,
0x53, 0x70, 0x61, 0x63, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x78, 0x0a, 0x15, 0x52,
0x65, 0x6d, 0x6f, 0x76, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x55, 0x73, 0x65, 0x72, 0x50, 0x61,
0x72, 0x61, 0x6d, 0x73, 0x12, 0x30, 0x0a, 0x07, 0x53, 0x70, 0x61, 0x63, 0x65, 0x49, 0x64, 0x18,
0x01, 0x20, 0x01, 0x28, 0x03, 0x42, 0x16, 0xca, 0xbb, 0x18, 0x08, 0x73, 0x70, 0x61, 0x63, 0x65,
0x5f, 0x69, 0x64, 0xda, 0xbb, 0x18, 0x06, 0x24, 0x20, 0x21, 0x3d, 0x20, 0x30, 0x52, 0x07, 0x53,
0x70, 0x61, 0x63, 0x65, 0x49, 0x64, 0x12, 0x2d, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64,
0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x42, 0x15, 0xca, 0xbb, 0x18, 0x07, 0x75, 0x73, 0x65, 0x72,
0x5f, 0x69, 0x64, 0xda, 0xbb, 0x18, 0x06, 0x24, 0x20, 0x21, 0x3d, 0x20, 0x30, 0x52, 0x06, 0x55,
0x73, 0x65, 0x72, 0x49, 0x64, 0x22, 0x17, 0x0a, 0x15, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x53,
0x70, 0x61, 0x63, 0x65, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0xae,
0x01, 0x0a, 0x19, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x55, 0x73,
0x65, 0x72, 0x52, 0x6f, 0x6c, 0x65, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x30, 0x0a, 0x07,
0x53, 0x70, 0x61, 0x63, 0x65, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x42, 0x16, 0xca,
0xbb, 0x18, 0x08, 0x73, 0x70, 0x61, 0x63, 0x65, 0x5f, 0x69, 0x64, 0xda, 0xbb, 0x18, 0x06, 0x24,
0x20, 0x21, 0x3d, 0x20, 0x30, 0x52, 0x07, 0x53, 0x70, 0x61, 0x63, 0x65, 0x49, 0x64, 0x12, 0x2d,
0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x42, 0x15,
0xca, 0xbb, 0x18, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0xda, 0xbb, 0x18, 0x06, 0x24,
0x20, 0x21, 0x3d, 0x20, 0x30, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x30, 0x0a,
0x04, 0x52, 0x6f, 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x42, 0x1c, 0xca, 0xbb, 0x18,
0x04, 0x72, 0x6f, 0x6c, 0x65, 0xda, 0xbb, 0x18, 0x10, 0x24, 0x20, 0x3d, 0x3d, 0x20, 0x31, 0x20,
0x7c, 0x7c, 0x20, 0x24, 0x20, 0x3d, 0x3d, 0x20, 0x32, 0x52, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x22,
0x1b, 0x0a, 0x19, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x55, 0x73,
0x65, 0x72, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x14, 0x0a, 0x12,
0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x53, 0x70, 0x61, 0x63, 0x65, 0x50, 0x61, 0x72, 0x61,
0x6d, 0x73, 0x22, 0xb9, 0x01, 0x0a, 0x12, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x53, 0x70,
0x61, 0x63, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x26, 0x0a, 0x07, 0x53, 0x70, 0x61,
0x63, 0x65, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x42, 0x0c, 0xca, 0xbb, 0x18, 0x08,
0x73, 0x70, 0x61, 0x63, 0x65, 0x5f, 0x69, 0x64, 0x52, 0x07, 0x53, 0x70, 0x61, 0x63, 0x65, 0x49,
0x64, 0x12, 0x2c, 0x0a, 0x09, 0x53, 0x70, 0x61, 0x63, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x42, 0x0e, 0xca, 0xbb, 0x18, 0x0a, 0x73, 0x70, 0x61, 0x63, 0x65, 0x5f,
0x6e, 0x61, 0x6d, 0x65, 0x52, 0x09, 0x53, 0x70, 0x61, 0x63, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x12,
0x2f, 0x0a, 0x0a, 0x49, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x03, 0x20,
0x01, 0x28, 0x09, 0x42, 0x0f, 0xca, 0xbb, 0x18, 0x0b, 0x69, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x5f,
0x63, 0x6f, 0x64, 0x65, 0x52, 0x0a, 0x49, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x43, 0x6f, 0x64, 0x65,
0x12, 0x1c, 0x0a, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x42, 0x08,
0xca, 0xbb, 0x18, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x42, 0x22,
0x5a, 0x20, 0x63, 0x6e, 0x2f, 0x6f, 0x6e, 0x65, 0x61, 0x6f, 0x2f, 0x62, 0x61, 0x73, 0x65, 0x2d,
0x67, 0x6f, 0x2f, 0x62, 0x69, 0x7a, 0x2f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x2f, 0x73, 0x70, 0x61,
0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x6f, 0x6c, 0x65, 0x12, 0x2f, 0x0a, 0x0a, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65, 0x54, 0x69, 0x6d,
0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x42, 0x0f, 0xca, 0xbb, 0x18, 0x0b, 0x61, 0x63, 0x74,
0x69, 0x76, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x52, 0x0a, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65,
0x54, 0x69, 0x6d, 0x65, 0x22, 0x3e, 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x53, 0x70, 0x61, 0x63,
0x65, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x26, 0x0a, 0x05,
0x75, 0x73, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x73, 0x70,
0x61, 0x63, 0x65, 0x2e, 0x53, 0x70, 0x61, 0x63, 0x65, 0x55, 0x73, 0x65, 0x72, 0x52, 0x05, 0x75,
0x73, 0x65, 0x72, 0x73, 0x22, 0x4d, 0x0a, 0x0f, 0x4a, 0x6f, 0x69, 0x6e, 0x53, 0x70, 0x61, 0x63,
0x65, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x3a, 0x0a, 0x0a, 0x49, 0x6e, 0x76, 0x69, 0x74,
0x65, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x1a, 0xca, 0xbb, 0x18,
0x0b, 0x69, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0xda, 0xbb, 0x18, 0x07,
0x24, 0x20, 0x21, 0x3d, 0x20, 0x27, 0x27, 0x52, 0x0a, 0x49, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x43,
0x6f, 0x64, 0x65, 0x22, 0x11, 0x0a, 0x0f, 0x4a, 0x6f, 0x69, 0x6e, 0x53, 0x70, 0x61, 0x63, 0x65,
0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x11, 0x0a, 0x0f, 0x45, 0x78, 0x69, 0x74, 0x53, 0x70,
0x61, 0x63, 0x65, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x22, 0x11, 0x0a, 0x0f, 0x45, 0x78, 0x69,
0x74, 0x53, 0x70, 0x61, 0x63, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x78, 0x0a, 0x15,
0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x55, 0x73, 0x65, 0x72, 0x50,
0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x30, 0x0a, 0x07, 0x53, 0x70, 0x61, 0x63, 0x65, 0x49, 0x64,
0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x42, 0x16, 0xca, 0xbb, 0x18, 0x08, 0x73, 0x70, 0x61, 0x63,
0x65, 0x5f, 0x69, 0x64, 0xda, 0xbb, 0x18, 0x06, 0x24, 0x20, 0x21, 0x3d, 0x20, 0x30, 0x52, 0x07,
0x53, 0x70, 0x61, 0x63, 0x65, 0x49, 0x64, 0x12, 0x2d, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49,
0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x42, 0x15, 0xca, 0xbb, 0x18, 0x07, 0x75, 0x73, 0x65,
0x72, 0x5f, 0x69, 0x64, 0xda, 0xbb, 0x18, 0x06, 0x24, 0x20, 0x21, 0x3d, 0x20, 0x30, 0x52, 0x06,
0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x22, 0x17, 0x0a, 0x15, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65,
0x53, 0x70, 0x61, 0x63, 0x65, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22,
0xae, 0x01, 0x0a, 0x19, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x55,
0x73, 0x65, 0x72, 0x52, 0x6f, 0x6c, 0x65, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x30, 0x0a,
0x07, 0x53, 0x70, 0x61, 0x63, 0x65, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x42, 0x16,
0xca, 0xbb, 0x18, 0x08, 0x73, 0x70, 0x61, 0x63, 0x65, 0x5f, 0x69, 0x64, 0xda, 0xbb, 0x18, 0x06,
0x24, 0x20, 0x21, 0x3d, 0x20, 0x30, 0x52, 0x07, 0x53, 0x70, 0x61, 0x63, 0x65, 0x49, 0x64, 0x12,
0x2d, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x42,
0x15, 0xca, 0xbb, 0x18, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0xda, 0xbb, 0x18, 0x06,
0x24, 0x20, 0x21, 0x3d, 0x20, 0x30, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x30,
0x0a, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x42, 0x1c, 0xca, 0xbb,
0x18, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0xda, 0xbb, 0x18, 0x10, 0x24, 0x20, 0x3d, 0x3d, 0x20, 0x31,
0x20, 0x7c, 0x7c, 0x20, 0x24, 0x20, 0x3d, 0x3d, 0x20, 0x32, 0x52, 0x04, 0x52, 0x6f, 0x6c, 0x65,
0x22, 0x1b, 0x0a, 0x19, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x55,
0x73, 0x65, 0x72, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x14, 0x0a,
0x12, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x53, 0x70, 0x61, 0x63, 0x65, 0x50, 0x61, 0x72,
0x61, 0x6d, 0x73, 0x22, 0xb9, 0x01, 0x0a, 0x12, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x53,
0x70, 0x61, 0x63, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x26, 0x0a, 0x07, 0x53, 0x70,
0x61, 0x63, 0x65, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x42, 0x0c, 0xca, 0xbb, 0x18,
0x08, 0x73, 0x70, 0x61, 0x63, 0x65, 0x5f, 0x69, 0x64, 0x52, 0x07, 0x53, 0x70, 0x61, 0x63, 0x65,
0x49, 0x64, 0x12, 0x2c, 0x0a, 0x09, 0x53, 0x70, 0x61, 0x63, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x18,
0x02, 0x20, 0x01, 0x28, 0x09, 0x42, 0x0e, 0xca, 0xbb, 0x18, 0x0a, 0x73, 0x70, 0x61, 0x63, 0x65,
0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x52, 0x09, 0x53, 0x70, 0x61, 0x63, 0x65, 0x4e, 0x61, 0x6d, 0x65,
0x12, 0x2f, 0x0a, 0x0a, 0x49, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x03,
0x20, 0x01, 0x28, 0x09, 0x42, 0x0f, 0xca, 0xbb, 0x18, 0x0b, 0x69, 0x6e, 0x76, 0x69, 0x74, 0x65,
0x5f, 0x63, 0x6f, 0x64, 0x65, 0x52, 0x0a, 0x49, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x43, 0x6f, 0x64,
0x65, 0x12, 0x1c, 0x0a, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x42,
0x08, 0xca, 0xbb, 0x18, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x42,
0x22, 0x5a, 0x20, 0x63, 0x6e, 0x2f, 0x6f, 0x6e, 0x65, 0x61, 0x6f, 0x2f, 0x62, 0x61, 0x73, 0x65,
0x2d, 0x67, 0x6f, 0x2f, 0x62, 0x69, 0x7a, 0x2f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x2f, 0x73, 0x70,
0x61, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -0,0 +1,27 @@
package middleware
import (
"cn/oneao/base-go/db"
"cn/oneao/base-go/logx"
"context"
"github.com/cloudwego/hertz/pkg/app"
)
func RefreshActive() app.HandlerFunc {
return func(c context.Context, ctx *app.RequestContext) {
// 先执行后续 handler
ctx.Next(c)
userID := GetUserID(c)
if userID == 0 {
return
}
// 异步刷新活跃时间
go func() {
if err := db.Queries.UpdateUserActiveTime(c, userID); err != nil {
logx.CtxError(c, "刷新用户活跃时间失败, userID=%d, err=%v", userID, err)
}
}()
}
}
@@ -18,19 +18,21 @@ SET role = $1
WHERE user_id = $2
AND space_id = $3;
-- 获取空间下的所有用户信息
-- 获取空间下的所有用户信息(按 role, active_time, username 排序)
-- name: ListSpaceUsers :many
SELECT
u.id as user_id,
u.id AS user_id,
u.username,
u.active_time,
su.role,
su.join_time
FROM space_user su
JOIN space s ON su.space_id = s.id
JOIN user_info u ON su.user_id = u.id
WHERE s.id = $1
ORDER BY su.join_time ASC;
ORDER BY su.role ASC,
u.active_time DESC,
u.username ASC;
-- name: GetSpaceUserByUserId :one
SELECT * FROM space_user WHERE user_id = $1;
@@ -1,7 +1,7 @@
-- 插入用户并返回刚创建的记录
-- name: InsertUser :execrows
INSERT INTO user_info (id, account, password)
VALUES ($1, $2, $3);
INSERT INTO user_info (id, account, password,username)
VALUES ($1, $2, $3,$4);
-- 根据 ID 查询单条用户记录
-- name: GetUserById :one
@@ -23,8 +23,13 @@ WHERE account = $1
AND password = $2;
-- 更新用户最后登录时间,并返回更新后的记录
-- name: UpdateUserLoginLastTime :execrows
-- name: UpdateUserLoginLastTime :exec
UPDATE user_info
SET last_login_time = now()
WHERE id = $1;
-- 更新用户活跃时间
-- name: UpdateUserActiveTime :exec
UPDATE user_info
SET active_time = now()
WHERE id = $1;
@@ -76,5 +76,6 @@ type UserInfo struct {
Gender pgtype.Int4
CreateTime pgtype.Timestamp
UpdateTime pgtype.Timestamp
ActiveTime pgtype.Timestamp
LastLoginTime pgtype.Timestamp
}
@@ -109,25 +109,29 @@ func (q *Queries) InsertSpaceUser(ctx context.Context, arg InsertSpaceUserParams
const listSpaceUsers = `-- name: ListSpaceUsers :many
SELECT
u.id as user_id,
u.id AS user_id,
u.username,
u.active_time,
su.role,
su.join_time
FROM space_user su
JOIN space s ON su.space_id = s.id
JOIN user_info u ON su.user_id = u.id
WHERE s.id = $1
ORDER BY su.join_time ASC
ORDER BY su.role ASC,
u.active_time DESC,
u.username ASC
`
type ListSpaceUsersRow struct {
UserID int64
Username pgtype.Text
Role int32
JoinTime pgtype.Timestamp
UserID int64
Username pgtype.Text
ActiveTime pgtype.Timestamp
Role int32
JoinTime pgtype.Timestamp
}
// 获取空间下的所有用户信息
// 获取空间下的所有用户信息(按 role, active_time, username 排序)
func (q *Queries) ListSpaceUsers(ctx context.Context, id int64) ([]ListSpaceUsersRow, error) {
rows, err := q.db.Query(ctx, listSpaceUsers, id)
if err != nil {
@@ -140,6 +144,7 @@ func (q *Queries) ListSpaceUsers(ctx context.Context, id int64) ([]ListSpaceUser
if err := rows.Scan(
&i.UserID,
&i.Username,
&i.ActiveTime,
&i.Role,
&i.JoinTime,
); err != nil {
@@ -7,10 +7,12 @@ package repo
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getUserByAccount = `-- name: GetUserByAccount :one
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, last_login_time
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, active_time, last_login_time
FROM user_info
WHERE account = $1
`
@@ -29,13 +31,14 @@ func (q *Queries) GetUserByAccount(ctx context.Context, account string) (UserInf
&i.Gender,
&i.CreateTime,
&i.UpdateTime,
&i.ActiveTime,
&i.LastLoginTime,
)
return i, err
}
const getUserById = `-- name: GetUserById :one
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, last_login_time
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, active_time, last_login_time
FROM user_info
WHERE id = $1
`
@@ -54,13 +57,14 @@ func (q *Queries) GetUserById(ctx context.Context, id int64) (UserInfo, error) {
&i.Gender,
&i.CreateTime,
&i.UpdateTime,
&i.ActiveTime,
&i.LastLoginTime,
)
return i, err
}
const getUserForLogin = `-- name: GetUserForLogin :one
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, last_login_time
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, active_time, last_login_time
FROM user_info
WHERE account = $1
AND password = $2
@@ -85,42 +89,58 @@ func (q *Queries) GetUserForLogin(ctx context.Context, arg GetUserForLoginParams
&i.Gender,
&i.CreateTime,
&i.UpdateTime,
&i.ActiveTime,
&i.LastLoginTime,
)
return i, err
}
const insertUser = `-- name: InsertUser :execrows
INSERT INTO user_info (id, account, password)
VALUES ($1, $2, $3)
INSERT INTO user_info (id, account, password,username)
VALUES ($1, $2, $3,$4)
`
type InsertUserParams struct {
ID int64
Account string
Password string
Username pgtype.Text
}
// 插入用户并返回刚创建的记录
func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (int64, error) {
result, err := q.db.Exec(ctx, insertUser, arg.ID, arg.Account, arg.Password)
result, err := q.db.Exec(ctx, insertUser,
arg.ID,
arg.Account,
arg.Password,
arg.Username,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const updateUserLoginLastTime = `-- name: UpdateUserLoginLastTime :execrows
const updateUserActiveTime = `-- name: UpdateUserActiveTime :exec
UPDATE user_info
SET active_time = now()
WHERE id = $1
`
// 更新用户活跃时间
func (q *Queries) UpdateUserActiveTime(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, updateUserActiveTime, id)
return err
}
const updateUserLoginLastTime = `-- name: UpdateUserLoginLastTime :exec
UPDATE user_info
SET last_login_time = now()
WHERE id = $1
`
// 更新用户最后登录时间,并返回更新后的记录
func (q *Queries) UpdateUserLoginLastTime(ctx context.Context, id int64) (int64, error) {
result, err := q.db.Exec(ctx, updateUserLoginLastTime, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
func (q *Queries) UpdateUserLoginLastTime(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, updateUserLoginLastTime, id)
return err
}
@@ -8,6 +8,7 @@ CREATE TABLE user_info (
gender INT DEFAULT 0,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
active_time TIMESTAMP,
last_login_time TIMESTAMP
);
@@ -12,7 +12,8 @@ message ListSpaceUsersParams {
message SpaceUser {
int64 UserId = 1 [(api.body)="user_id"];
string Username = 2 [(api.body)="username"];
int32 SpaceRole = 4 [(api.body)="space_role"];
int32 SpaceRole = 3 [(api.body)="space_role"];
string ActiveTime = 4 [(api.body)="active_time"];
}
message ListSpaceUsersResult {
+1
View File
@@ -34,6 +34,7 @@ func main() {
h.Use(middleware.Log())
h.Use(middleware.Error())
h.Use(middleware.Auth())
h.Use(middleware.RefreshActive())
register(h)
h.Spin()