| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-24 17:10:35 |
| 4 | * @LastEditors: nevin |
| 5 | * @Description: 账户服务 |
| 6 | */ |
| 7 | import { AppDataSource } from '../../db'; |
| 8 | import { AccountModel } from '../../db/models/account'; |
| 9 | import { Injectable } from '../core/decorators'; |
| 10 | import { FindOptionsWhere, In, Repository } from 'typeorm'; |
| 11 | import { |
| 12 | AccountStatus, |
| 13 | PlatType, |
| 14 | defaultAccountGroupId, |
| 15 | } from '../../../commont/AccountEnum'; |
| 16 | import platController from '../plat/index'; |
| 17 | import { EtEvent } from '../../global/event'; |
| 18 | import { AccountGroupModel } from '../../db/models/accountGroup'; |
| 19 | import { getUserInfo } from '../user/comment'; |
| 20 | |
| 21 | @Injectable() |
| 22 | export class AccountService { |
| 23 | private accountRepository: Repository<AccountModel>; |
| 24 | private accountGroupRepository: Repository<AccountGroupModel>; |
| 25 | |
| 26 | constructor() { |
| 27 | this.accountRepository = AppDataSource.getRepository(AccountModel); |
| 28 | this.accountGroupRepository = |
| 29 | AppDataSource.getRepository(AccountGroupModel); |
| 30 | } |
| 31 | |
| 32 | // 增加用户组数据 |
| 33 | async addAccountGroup(data: Partial<AccountGroupModel>) { |
| 34 | return await this.accountGroupRepository.save({ |
| 35 | ...data, |
| 36 | }); |
| 37 | } |
| 38 | // 获取用户组数据 |
| 39 | async getAccountGroup() { |
| 40 | return await this.accountGroupRepository.find(); |
| 41 | } |
| 42 | // 删除用户组数据 |
| 43 | async deleteAccountGroup(id: number) { |
| 44 | // 将删除的用户组下的账户账户的组id设置为默认组id |
| 45 | const accounts = await this.accountRepository.find({ |
| 46 | where: { groupId: id }, |
| 47 | }); |
| 48 | await this.accountRepository.update( |
| 49 | { id: In(accounts.map((v) => v.id)) }, |
| 50 | { |
| 51 | groupId: defaultAccountGroupId, |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | // 删除 |
| 56 | return await this.accountGroupRepository.delete({ |
| 57 | id: id, |
| 58 | }); |
| 59 | } |
| 60 | // 修改用户组数据 |
| 61 | async editAccountGroup(data: Partial<AccountGroupModel>) { |
| 62 | return await this.accountGroupRepository.update({ id: data.id }, data); |
| 63 | } |
| 64 | |
| 65 | // 单个账号登录状态检测core |
| 66 | async checkAccountLoginCore(pType: PlatType, uid: string) { |
| 67 | const userInfo = getUserInfo(); |
| 68 | |
| 69 | const accountInfo = await this.getAccountInfo({ |
| 70 | type: pType, |
| 71 | userId: userInfo.id, |
| 72 | uid: uid, |
| 73 | }); |
| 74 | if (!accountInfo) return accountInfo; |
| 75 | // 取出cookie |
| 76 | if (!accountInfo.loginCookie) return accountInfo; |
| 77 | |
| 78 | const res = await platController |
| 79 | .platLoginCheck(pType, accountInfo) |
| 80 | .catch(() => ({ |
| 81 | online: false, |
| 82 | account: undefined, |
| 83 | })); |
| 84 | |
| 85 | await this.updateAccountInfo(accountInfo.id, { |
| 86 | status: res.online ? AccountStatus.USABLE : AccountStatus.DISABLE, |
| 87 | ...(res.online && typeof res.account === 'object' ? res.account : {}), |
| 88 | }); |
| 89 | const account = await this.getAccountById(accountInfo!.id!); |
| 90 | |
| 91 | return account || accountInfo; |
| 92 | } |
| 93 | |
| 94 | // 没有就添加有就更新cookie |
| 95 | async addOrUpdateAccount( |
| 96 | query: { |
| 97 | userId: string; |
| 98 | type: PlatType; |
| 99 | uid: string; |
| 100 | }, |
| 101 | account: Partial<AccountModel>, |
| 102 | ): Promise<AccountModel> { |
| 103 | const filter: FindOptionsWhere<AccountModel> = { |
| 104 | userId: query.userId, |
| 105 | type: query.type, |
| 106 | uid: query.uid, |
| 107 | }; |
| 108 | const accountData = await this.accountRepository.findOne({ where: filter }); |
| 109 | account.loginTime = new Date(); |
| 110 | // 添加数据 |
| 111 | if (!accountData) { |
| 112 | const newAccount = await this.accountRepository.save(account); |
| 113 | // 上报账号添加事件 |
| 114 | EtEvent.emit('ET_TRACING_ACCOUNT_ADD', { |
| 115 | id: newAccount.id, |
| 116 | desc: '添加账户' + query.type, |
| 117 | }); |
| 118 | |
| 119 | return newAccount; |
| 120 | } |
| 121 | |
| 122 | // 更新数据 |
| 123 | await this.accountRepository.update(filter, account); |
| 124 | |
| 125 | return { |
| 126 | ...accountData, |
| 127 | ...account, |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | // 获取账户 |
| 132 | async getAccountById(id: number) { |
| 133 | return await this.accountRepository.findOne({ where: { id } }); |
| 134 | } |
| 135 | |
| 136 | // 获取账户信息 |
| 137 | async getAccountInfo(query: { |
| 138 | type: PlatType; |
| 139 | userId: string; |
| 140 | uid: string; |
| 141 | }) { |
| 142 | return await this.accountRepository.findOne({ where: query }); |
| 143 | } |
| 144 | |
| 145 | // 获取所有账户 |
| 146 | async getAccounts(userId?: string) { |
| 147 | if (!userId) { |
| 148 | const userInfo = getUserInfo(); |
| 149 | userId = userInfo.id; |
| 150 | } |
| 151 | return await this.accountRepository.find({ where: { userId } }); |
| 152 | } |
| 153 | |
| 154 | // 根据ID数组ids获取账户列表数组 |
| 155 | async getAccountListByIds(userId: string, ids: number[]) { |
| 156 | return await this.accountRepository.find({ |
| 157 | where: { |
| 158 | userId, |
| 159 | id: In(ids), |
| 160 | }, |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * 获取账户的统计信息 |
| 166 | * @param userId |
| 167 | * @param type |
| 168 | * @returns |
| 169 | */ |
| 170 | async getAccountStatistics( |
| 171 | userId: string, |
| 172 | type?: PlatType, |
| 173 | ): Promise<{ |
| 174 | accountTotal: number; |
| 175 | list: AccountModel[]; |
| 176 | fansCount?: number; |
| 177 | readCount?: number; |
| 178 | likeCount?: number; |
| 179 | collectCount?: number; |
| 180 | commentCount?: number; |
| 181 | income?: number; |
| 182 | }> { |
| 183 | const accountList = await this.accountRepository.find({ |
| 184 | where: { userId, ...(type && { type }) }, |
| 185 | }); |
| 186 | |
| 187 | const res = { |
| 188 | accountTotal: accountList.length, |
| 189 | list: accountList, |
| 190 | fansCount: 0, |
| 191 | }; |
| 192 | |
| 193 | for (const element of accountList) { |
| 194 | const ret = await platController.getStatistics(element).catch((err) => { |
| 195 | console.error(err); |
| 196 | }); |
| 197 | res.fansCount += ret?.fansCount || 0; |
| 198 | } |
| 199 | |
| 200 | return res; |
| 201 | } |
| 202 | |
| 203 | // 获取账户看板数据 |
| 204 | async getAccountDashboard(account: AccountModel, time?: [string, string]) { |
| 205 | return await platController.getDashboard(account, time); |
| 206 | } |
| 207 | |
| 208 | // 获取账户总数 |
| 209 | async getAccountCount(userId: string) { |
| 210 | return await this.accountRepository.count({ where: { userId } }); |
| 211 | } |
| 212 | |
| 213 | // 根据多个账户id查询账户信息 |
| 214 | async getAccountsByIds(ids: number[]) { |
| 215 | return await this.accountRepository.find({ |
| 216 | where: { id: In(ids) }, |
| 217 | }); |
| 218 | } |
| 219 | |
| 220 | // 更新粉丝数量 |
| 221 | async updateFansCount(userId: string, account: string, fansCount: number) { |
| 222 | return await this.accountRepository.update( |
| 223 | { userId, account }, |
| 224 | { fansCount: fansCount }, |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | // 获取用户的所有账户的总粉丝量 |
| 229 | async getUserFansCount(userId: string) { |
| 230 | const accounts = await this.accountRepository.find({ where: { userId } }); |
| 231 | return accounts.reduce((acc, cur) => acc + (cur.fansCount || 0), 0); |
| 232 | } |
| 233 | |
| 234 | // 删除多个账户 |
| 235 | async deleteAccounts(ids: number[], userId: string) { |
| 236 | return await this.accountRepository.delete({ |
| 237 | id: In(ids), |
| 238 | userId: userId, |
| 239 | }); |
| 240 | } |
| 241 | |
| 242 | // 更新用户状态 |
| 243 | async updateAccountStatus(id: number, status: number) { |
| 244 | await this.accountRepository.update(id, { status }); |
| 245 | return await this.accountRepository.findOne({ where: { id } }); |
| 246 | } |
| 247 | |
| 248 | // 更新用户信息 |
| 249 | async updateAccountInfo(id: number, data: Partial<AccountModel>) { |
| 250 | return await this.accountRepository.update(id, data); |
| 251 | } |
| 252 | |
| 253 | // 更新账户的统计信息 |
| 254 | async updateAccountStatistics( |
| 255 | id: number, |
| 256 | fansCount: number, |
| 257 | readCount: number, |
| 258 | likeCount: number, |
| 259 | collectCount: number, |
| 260 | commentCount: number, |
| 261 | income: number, |
| 262 | ) { |
| 263 | return await this.accountRepository.update(id, { |
| 264 | fansCount, |
| 265 | readCount, |
| 266 | likeCount, |
| 267 | collectCount, |
| 268 | commentCount, |
| 269 | income, |
| 270 | }); |
| 271 | } |
| 272 | } |
| 273 |