| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-20 22:02:54 |
| 4 | * @LastEditTime: 2025-02-19 22:08:57 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import { Controller, Et, Icp, Inject } from '../core/decorators'; |
| 9 | import { AccountService } from './service'; |
| 10 | import { getUserInfo } from '../user/comment'; |
| 11 | import platController from '../plat'; |
| 12 | import type { ICreateBrowserWindowParams } from './BrowserWindow/browserWindow'; |
| 13 | import { browserWindowController } from './BrowserWindow'; |
| 14 | import { AccountStatus, PlatType } from '../../../commont/AccountEnum'; |
| 15 | import { AccountModel } from '../../db/models/account'; |
| 16 | import windowOperate from '../../util/windowOperate'; |
| 17 | import { SendChannelEnum } from '../../../commont/UtilsEnum'; |
| 18 | import { AccountGroupModel } from '../../db/models/accountGroup'; |
| 19 | import { proxyCheck } from '../../plat/coomont'; |
| 20 | |
| 21 | @Controller() |
| 22 | export class AccountController { |
| 23 | @Inject(AccountService) |
| 24 | private readonly accountService!: AccountService; |
| 25 | |
| 26 | // 创建浏览器视图 |
| 27 | @Icp('ICP_ACCOUNT_CREATE_BROWSER_VIEW') |
| 28 | async createBrowserView( |
| 29 | event: Electron.IpcMainInvokeEvent, |
| 30 | data: ICreateBrowserWindowParams, |
| 31 | ): Promise<void> { |
| 32 | await browserWindowController.createBrowserWindow(data); |
| 33 | } |
| 34 | |
| 35 | // 销毁浏览器视图 |
| 36 | @Icp('ICP_ACCOUNT_DESTROY_BROWSER_VIEW') |
| 37 | async destroyBrowserView( |
| 38 | event: Electron.IpcMainInvokeEvent, |
| 39 | webViewId: number, |
| 40 | ): Promise<void> { |
| 41 | browserWindowController.destroyBrowserWindow(webViewId); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * 登录三方平台 |
| 46 | */ |
| 47 | @Icp('ICP_ACCOUNT_LOGIN') |
| 48 | async accountLogin( |
| 49 | event: Electron.IpcMainInvokeEvent, |
| 50 | pType: PlatType, |
| 51 | ): Promise<any> { |
| 52 | const userInfo = getUserInfo(); |
| 53 | |
| 54 | const accountInfo = await platController.platlogin(pType); |
| 55 | if (!accountInfo) return null; |
| 56 | |
| 57 | accountInfo.status = AccountStatus.USABLE; |
| 58 | accountInfo.userId = userInfo.id; |
| 59 | |
| 60 | const account = await this.accountService.addOrUpdateAccount( |
| 61 | { |
| 62 | userId: userInfo.id, |
| 63 | type: pType, |
| 64 | uid: accountInfo?.uid || '', |
| 65 | }, |
| 66 | accountInfo, |
| 67 | ); |
| 68 | windowOperate.sendRenderMsg(SendChannelEnum.AccountLoginFinish, account); |
| 69 | // 保存账户信息 |
| 70 | return account; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * 账户登录检测-单个 |
| 75 | */ |
| 76 | @Icp('ICP_ACCOUNT_LOGIN_CHECK') |
| 77 | async checkAccountLogin( |
| 78 | event: Electron.IpcMainInvokeEvent, |
| 79 | pType: PlatType, |
| 80 | uid: string, |
| 81 | isSendEvent: boolean = true, |
| 82 | ): Promise<AccountModel | null> { |
| 83 | const account = await this.accountService.checkAccountLoginCore(pType, uid); |
| 84 | if (isSendEvent) { |
| 85 | windowOperate.sendRenderMsg(SendChannelEnum.AccountLoginFinish, account); |
| 86 | } |
| 87 | return account; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * 账户登录检测-多个 |
| 92 | */ |
| 93 | @Icp('ICP_ACCOUNT_LOGIN_CHECK_MULTI') |
| 94 | async checkAccountLoginMulti( |
| 95 | event: Electron.IpcMainInvokeEvent, |
| 96 | checkAccounts: { |
| 97 | pType: PlatType; |
| 98 | uid: string; |
| 99 | }[], |
| 100 | ): Promise<(AccountModel | null)[]> { |
| 101 | const tasks: Promise<AccountModel | null>[] = []; |
| 102 | |
| 103 | for (const { pType, uid } of checkAccounts) { |
| 104 | tasks.push(this.accountService.checkAccountLoginCore(pType, uid)); |
| 105 | } |
| 106 | const accounts = await Promise.all(tasks); |
| 107 | windowOperate.sendRenderMsg( |
| 108 | SendChannelEnum.AccountLoginFinish, |
| 109 | accounts[0], |
| 110 | accounts, |
| 111 | ); |
| 112 | return accounts; |
| 113 | } |
| 114 | |
| 115 | // 更新用户状态 |
| 116 | @Icp('ICP_ACCOUNT_UPDATE_STATUS') |
| 117 | async updateAccountStatus( |
| 118 | event: Electron.IpcMainInvokeEvent, |
| 119 | // 账户ID |
| 120 | id: number, |
| 121 | status: AccountStatus, |
| 122 | ) { |
| 123 | return this.accountService.updateAccountStatus(id, status); |
| 124 | } |
| 125 | |
| 126 | // 获取账户信息 |
| 127 | @Icp('ICP_ACCOUNT_GET_INFO') |
| 128 | async getAccountInfo( |
| 129 | event: Electron.IpcMainInvokeEvent, |
| 130 | data: { type: PlatType; uid: string }, |
| 131 | ): Promise<any> { |
| 132 | const userInfo = getUserInfo(); |
| 133 | |
| 134 | const { type, uid } = data; |
| 135 | |
| 136 | const accountInfo = await this.accountService.getAccountInfo({ |
| 137 | type, |
| 138 | userId: userInfo.id, |
| 139 | uid, |
| 140 | }); |
| 141 | |
| 142 | // console.log('userInfouserInfo@@@:', accountInfo); |
| 143 | |
| 144 | return accountInfo; |
| 145 | } |
| 146 | |
| 147 | // 获取账户列表 |
| 148 | @Icp('ICP_ACCOUNT_GET_LIST') |
| 149 | async getAccountList(event: Electron.IpcMainInvokeEvent): Promise<any> { |
| 150 | const userInfo = getUserInfo(); |
| 151 | return this.accountService.getAccounts(userInfo.id); |
| 152 | } |
| 153 | |
| 154 | // 获取账户列表(ids) |
| 155 | @Icp('ICP_ACCOUNT_GET_LIST_BY_IDS') |
| 156 | async getAccountListByIdsTcp( |
| 157 | event: Electron.IpcMainInvokeEvent, |
| 158 | ids: number[], |
| 159 | ): Promise<any> { |
| 160 | return this.getAccountListByIds(ids); |
| 161 | } |
| 162 | // 获取账户列表(ids) |
| 163 | @Et('ET_ACCOUNT_GET_LIST_BY_IDS') |
| 164 | async getAccountListByIdsEt( |
| 165 | ids: number[], |
| 166 | callback: (p: AccountModel[]) => void, |
| 167 | ): Promise<any> { |
| 168 | const accounts = await this.getAccountListByIds(ids); |
| 169 | callback(accounts); |
| 170 | } |
| 171 | async getAccountListByIds(ids: number[]) { |
| 172 | const userInfo = getUserInfo(); |
| 173 | if (!userInfo?.id) return []; |
| 174 | return this.accountService.getAccountListByIds(userInfo.id, ids); |
| 175 | } |
| 176 | |
| 177 | // 获取账户总数 |
| 178 | @Icp('ICP_ACCOUNT_GET_COUNT') |
| 179 | async getAccountCount(event: Electron.IpcMainInvokeEvent): Promise<any> { |
| 180 | const userInfo = getUserInfo(); |
| 181 | return this.accountService.getAccountCount(userInfo.id); |
| 182 | } |
| 183 | |
| 184 | // 获取账户统计 |
| 185 | @Icp('ICP_ACCOUNT_STATISTICS') |
| 186 | async getAccountStatistics( |
| 187 | event: Electron.IpcMainInvokeEvent, |
| 188 | type?: PlatType, |
| 189 | ): Promise<any> { |
| 190 | const userInfo = getUserInfo(); |
| 191 | return this.accountService.getAccountStatistics(userInfo.id, type); |
| 192 | } |
| 193 | |
| 194 | // 获取账户的看板数据 |
| 195 | @Icp('ICP_ACCOUNT_DASHBOARD') |
| 196 | async getDashboard( |
| 197 | event: Electron.IpcMainInvokeEvent, |
| 198 | id: number, |
| 199 | time?: any, |
| 200 | ) { |
| 201 | if (!id) return null; |
| 202 | |
| 203 | const account = await this.accountService.getAccountById(id); |
| 204 | if (!account) return null; |
| 205 | return this.accountService.getAccountDashboard(account, time); |
| 206 | } |
| 207 | |
| 208 | // 删除账户 |
| 209 | @Icp('ICP_ACCOUNTS_DELETE') |
| 210 | async deleteAccount( |
| 211 | event: Electron.IpcMainInvokeEvent, |
| 212 | ids: number[], |
| 213 | ): Promise<any> { |
| 214 | const userInfo = getUserInfo(); |
| 215 | return this.accountService.deleteAccounts(ids, userInfo.id); |
| 216 | } |
| 217 | |
| 218 | // 修改账户的账户组 |
| 219 | @Icp('ICP_ACCOUNTS_EDIT_GROUP') |
| 220 | async accountEditGroup( |
| 221 | event: Electron.IpcMainInvokeEvent, |
| 222 | id: number, |
| 223 | groupId: number, |
| 224 | ): Promise<any> { |
| 225 | return this.accountService.updateAccountInfo(id, { |
| 226 | groupId, |
| 227 | }); |
| 228 | } |
| 229 | |
| 230 | // 添加用户组数据 |
| 231 | @Icp('ICP_ACCOUNTS_GROUP_ADD') |
| 232 | async addAccountGroup( |
| 233 | event: Electron.IpcMainInvokeEvent, |
| 234 | data: Partial<AccountGroupModel>, |
| 235 | ): Promise<any> { |
| 236 | return this.accountService.addAccountGroup(data); |
| 237 | } |
| 238 | // 获取用户组数据 |
| 239 | @Icp('ICP_ACCOUNTS_GROUP_GET') |
| 240 | async getAccountGroup(event: Electron.IpcMainInvokeEvent): Promise<any> { |
| 241 | return this.accountService.getAccountGroup(); |
| 242 | } |
| 243 | // 删除用户组数据 |
| 244 | @Icp('ICP_ACCOUNTS_GROUP_DELETE') |
| 245 | async deleteAccountGroup( |
| 246 | event: Electron.IpcMainInvokeEvent, |
| 247 | id: number, |
| 248 | ): Promise<any> { |
| 249 | return this.accountService.deleteAccountGroup(id); |
| 250 | } |
| 251 | // 编辑用户组数据 |
| 252 | @Icp('ICP_ACCOUNTS_GROUP_EDIT') |
| 253 | async editAccountGroup( |
| 254 | event: Electron.IpcMainInvokeEvent, |
| 255 | data: Partial<AccountGroupModel>, |
| 256 | ): Promise<any> { |
| 257 | return this.accountService.editAccountGroup(data); |
| 258 | } |
| 259 | |
| 260 | // 代理地址有效性检测 |
| 261 | @Icp('ICP_ACCOUNTS_PROXY_CHECK') |
| 262 | async proxyCheck( |
| 263 | event: Electron.IpcMainInvokeEvent, |
| 264 | proxy: string, |
| 265 | ): Promise<any> { |
| 266 | return await proxyCheck(proxy); |
| 267 | } |
| 268 | |
| 269 | @Et('ET_UP_ALL_ACCOUNT_STATISTICS') // 更新所有的账户的统计信息 |
| 270 | async updateAllAccountStatistics(id: number, status: number) { |
| 271 | const userInfo = getUserInfo(); |
| 272 | |
| 273 | const allAccountList = await this.accountService.getAccounts(userInfo.id); |
| 274 | for (const element of allAccountList) { |
| 275 | this.accountService.updateAccountStatistics( |
| 276 | element.id!, |
| 277 | element.fansCount, |
| 278 | element.readCount, |
| 279 | element.likeCount, |
| 280 | element.collectCount, |
| 281 | element.commentCount, |
| 282 | element.income!, |
| 283 | ); |
| 284 | } |
| 285 | await this.accountService.updateAccountStatus(id, status); |
| 286 | } |
| 287 | } |
| 288 |