| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-02-08 11:40:45 |
| 4 | * @LastEditTime: 2025-03-24 23:35:03 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: 微信视频号 |
| 7 | */ |
| 8 | import { PlatformBase } from '../../PlatformBase'; |
| 9 | import { |
| 10 | AccountInfoTypeRV, |
| 11 | CommentData, |
| 12 | CookiesType, |
| 13 | DashboardData, |
| 14 | IAccountInfoParams, |
| 15 | IGetLocationDataParams, |
| 16 | IGetTopicsParams, |
| 17 | IGetTopicsResponse, |
| 18 | IGetUsersParams, |
| 19 | ResponsePageInfo, |
| 20 | VideoCallbackType, |
| 21 | WorkData, |
| 22 | } from '../../plat.type'; |
| 23 | import { PublishVideoResult } from '../../module'; |
| 24 | import { shipinhaoService } from '../../../../plat/shipinhao'; |
| 25 | import { PlatType } from '../../../../../commont/AccountEnum'; |
| 26 | import { AccountModel } from '../../../../db/models/account'; |
| 27 | import { CommentInfo } from '../../../../plat/shipinhao/wxShp.type'; |
| 28 | import { IRequestNetResult } from '../../../../plat/requestNet'; |
| 29 | import { VideoModel } from '../../../../db/models/video'; |
| 30 | |
| 31 | export class WxSph extends PlatformBase { |
| 32 | constructor() { |
| 33 | super(PlatType.WxSph); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * 登录 |
| 38 | * @returns |
| 39 | */ |
| 40 | async login() { |
| 41 | try { |
| 42 | const { success, data, error } = |
| 43 | await shipinhaoService.loginOrView('login'); |
| 44 | if (!success || !data) { |
| 45 | console.log('Login process failed:', error); |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | const userInfo = await shipinhaoService.getUserInfo(data.cookie); |
| 50 | |
| 51 | const loginCookie = |
| 52 | typeof data.cookie === 'string' |
| 53 | ? data.cookie |
| 54 | : JSON.stringify(data.cookie); |
| 55 | |
| 56 | return { |
| 57 | loginCookie, |
| 58 | loginTime: new Date(), |
| 59 | type: this.type, |
| 60 | uid: userInfo.authorId, |
| 61 | account: userInfo.authorId, |
| 62 | avatar: userInfo.avatar, |
| 63 | nickname: userInfo.nickname, |
| 64 | }; |
| 65 | } catch (error) { |
| 66 | console.error('Login process failed:', error); |
| 67 | return null; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | async loginCheck(account: AccountModel) { |
| 72 | const online = await shipinhaoService.checkLoginStatus(account.loginCookie); |
| 73 | return { |
| 74 | online, |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | async getAccountInfo(params: IAccountInfoParams): Promise<AccountInfoTypeRV> { |
| 79 | const res = await shipinhaoService.getUserInfo(params.cookies); |
| 80 | |
| 81 | return { |
| 82 | type: this.type, |
| 83 | uid: res.authorId, |
| 84 | account: res.authorId, |
| 85 | avatar: res.avatar, |
| 86 | nickname: res.nickname, |
| 87 | fansCount: res.fansCount, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | async getStatistics(account: AccountModel) { |
| 92 | const cookie: CookiesType = JSON.parse(account.loginCookie); |
| 93 | const accountInfo = await shipinhaoService.getUserInfo(cookie); |
| 94 | |
| 95 | return { |
| 96 | fansCount: accountInfo.fansCount, |
| 97 | workCount: 0, // TODO: 作品数量 |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | async getDashboard(account: AccountModel, time: string[] = []) { |
| 102 | const res: DashboardData[] = []; |
| 103 | try { |
| 104 | const cookie: CookiesType = JSON.parse(account.loginCookie); |
| 105 | console.log('time@.@:', time); |
| 106 | const ret = await shipinhaoService.getDashboardFunc( |
| 107 | cookie, |
| 108 | time[0], |
| 109 | time[1], |
| 110 | ); |
| 111 | if (!ret.success) throw new Error('获取三方平台数据失败'); |
| 112 | for (const item of ret.data) { |
| 113 | res.push({ |
| 114 | fans: item.zhangfen, |
| 115 | read: item.bofang, |
| 116 | comment: item.pinglun, |
| 117 | like: item.dianzan, |
| 118 | forward: item.fenxiang, |
| 119 | collect: 0, // TODO: 获取收藏数据 |
| 120 | }); |
| 121 | } |
| 122 | } catch (error) { |
| 123 | console.log('------ getDashboard wxSph ---', error); |
| 124 | } |
| 125 | |
| 126 | return res; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * 获取作品列表 |
| 131 | * @param pageInfo |
| 132 | * @returns |
| 133 | */ |
| 134 | async getWorkList(account: AccountModel, pcursor?: string) { |
| 135 | const cookie: CookiesType = JSON.parse(account.loginCookie); |
| 136 | const pageNo = pcursor ? Number.parseInt(pcursor) : 1; |
| 137 | const res = await shipinhaoService.getPostList(cookie, { |
| 138 | pageNo: pageNo, |
| 139 | pageSize: 20, |
| 140 | }); |
| 141 | |
| 142 | const listData: WorkData[] = res.list.map((item) => { |
| 143 | return { |
| 144 | dataId: item.objectId, |
| 145 | commentCount: item.commentCount, |
| 146 | title: item.desc.shortTitle[0]?.shortTitle || '', |
| 147 | desc: item.desc.description, |
| 148 | coverUrl: item.desc.media[0]?.coverUrl || '', |
| 149 | videoUrl: item.desc.media[0]?.url || '', |
| 150 | }; |
| 151 | }); |
| 152 | |
| 153 | return { |
| 154 | list: listData, |
| 155 | pageInfo: { |
| 156 | count: res.totalCount, |
| 157 | hasMore: res.totalCount > res.list.length * pageNo, |
| 158 | pcursor: |
| 159 | res.totalCount > res.list.length * pageNo ? pageNo + 1 + '' : '', |
| 160 | }, |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * TODO: 未实现 |
| 166 | * @returns |
| 167 | * @param dataId |
| 168 | */ |
| 169 | async getWorkData(dataId: string) { |
| 170 | return { |
| 171 | dataId: '', |
| 172 | }; |
| 173 | } |
| 174 | |
| 175 | async getCommentList( |
| 176 | account: AccountModel, |
| 177 | data: WorkData, |
| 178 | pcursor?: string, |
| 179 | ) { |
| 180 | const cookie: CookiesType = JSON.parse(account.loginCookie); |
| 181 | const res = await shipinhaoService.getCommentList(cookie, data.dataId); |
| 182 | |
| 183 | const dataList: CommentData[] = []; |
| 184 | |
| 185 | for (const item of res.comment) { |
| 186 | const subDataList: CommentData[] = []; |
| 187 | for (const subItem of item.levelTwoComment) { |
| 188 | subDataList.push({ |
| 189 | userId: subItem.commentNickname, |
| 190 | dataId: subItem.commentId, |
| 191 | commentId: subItem.commentId, |
| 192 | parentCommentId: item.commentId, |
| 193 | content: subItem.commentContent, |
| 194 | nikeName: subItem.commentNickname, |
| 195 | headUrl: subItem.commentHeadurl, |
| 196 | data: subItem, |
| 197 | subCommentList: [], |
| 198 | }); |
| 199 | } |
| 200 | |
| 201 | dataList.push({ |
| 202 | userId: item.commentNickname, |
| 203 | dataId: item.commentId, |
| 204 | commentId: item.commentId, |
| 205 | content: item.commentContent, |
| 206 | nikeName: item.commentNickname, |
| 207 | headUrl: item.commentHeadurl, |
| 208 | data: item, |
| 209 | subCommentList: subDataList, |
| 210 | }); |
| 211 | } |
| 212 | |
| 213 | const pcursorNum = +(pcursor || 0); |
| 214 | |
| 215 | return { |
| 216 | list: dataList, |
| 217 | pageInfo: { |
| 218 | count: res.commentCount, |
| 219 | hasMore: res.commentCount > res.comment.length * pcursorNum, |
| 220 | pcursor: |
| 221 | res.commentCount > res.comment.length * pcursorNum |
| 222 | ? pcursorNum + 1 + '' |
| 223 | : '', |
| 224 | }, |
| 225 | }; |
| 226 | } |
| 227 | |
| 228 | async getCreatorCommentListByOther( |
| 229 | account: AccountModel, |
| 230 | data: WorkData, |
| 231 | pcursor?: string, |
| 232 | ) { |
| 233 | return { |
| 234 | list: [], |
| 235 | pageInfo: { |
| 236 | count: 0, |
| 237 | pcursor: '', |
| 238 | hasMore: false, |
| 239 | }, |
| 240 | }; |
| 241 | } |
| 242 | |
| 243 | getCreatorSecondCommentListByOther( |
| 244 | account: AccountModel, |
| 245 | data: WorkData, |
| 246 | root_comment_id: string, |
| 247 | pcursor?: string, |
| 248 | ): Promise<any> { |
| 249 | return new Promise((resolve, reject) => {}); |
| 250 | } |
| 251 | |
| 252 | async createCommentByOther( |
| 253 | account: AccountModel, |
| 254 | dataId: string, // 作品ID |
| 255 | content: string, |
| 256 | ) { |
| 257 | return null; |
| 258 | } |
| 259 | |
| 260 | async replyCommentByOther( |
| 261 | account: AccountModel, |
| 262 | commentId: string, |
| 263 | content: string, |
| 264 | option: { |
| 265 | dataId?: string; // 作品ID |
| 266 | comment: any; // 辅助数据,原数据 |
| 267 | }, |
| 268 | ) { |
| 269 | return null; |
| 270 | } |
| 271 | |
| 272 | async createComment( |
| 273 | account: AccountModel, |
| 274 | dataId: string, // 作品ID |
| 275 | content: string, |
| 276 | ) { |
| 277 | const cookie: CookiesType = JSON.parse(account.loginCookie); |
| 278 | const res = await shipinhaoService.createComment(cookie, dataId, content); |
| 279 | return (res.status === 200 || res.status === 201) && res.data.errCode === 0; |
| 280 | } |
| 281 | |
| 282 | async replyComment( |
| 283 | account: AccountModel, |
| 284 | commentId: string, |
| 285 | content: string, |
| 286 | option: { |
| 287 | dataId: string; // 作品ID |
| 288 | comment: CommentInfo; // 辅助数据,原数据 |
| 289 | }, |
| 290 | ) { |
| 291 | const cookie: CookiesType = JSON.parse(account.loginCookie); |
| 292 | const res = await shipinhaoService.createComment( |
| 293 | cookie, |
| 294 | option.dataId, |
| 295 | content, |
| 296 | option.comment, |
| 297 | ); |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | getCode(res: IRequestNetResult<any>) { |
| 302 | return res.data.errCode === 300334 || res.data.errCode === 300333 |
| 303 | ? 401 |
| 304 | : res.status; |
| 305 | } |
| 306 | |
| 307 | async getUsers(params: IGetUsersParams) { |
| 308 | const usersRes = await shipinhaoService.getUsers( |
| 309 | JSON.parse(params.account.loginCookie), |
| 310 | params.keyword, |
| 311 | params.page, |
| 312 | ); |
| 313 | |
| 314 | return { |
| 315 | status: this.getCode(usersRes), |
| 316 | data: usersRes?.data?.data?.list?.map((v) => { |
| 317 | return { |
| 318 | image: v.headImgUrl, |
| 319 | id: v.username, |
| 320 | name: v.nickName, |
| 321 | }; |
| 322 | }), |
| 323 | }; |
| 324 | } |
| 325 | |
| 326 | async getMixList(cookie: CookiesType) { |
| 327 | const mixRes = await shipinhaoService.getMixList(cookie); |
| 328 | return { |
| 329 | status: this.getCode(mixRes), |
| 330 | data: mixRes?.data?.data?.collectionList?.map((v) => { |
| 331 | return { |
| 332 | id: v.id, |
| 333 | name: v.name, |
| 334 | coverImg: v.coverImgUrl || '', |
| 335 | feedCount: v.feedCount, |
| 336 | }; |
| 337 | }), |
| 338 | }; |
| 339 | } |
| 340 | |
| 341 | async videoPublish( |
| 342 | params: VideoModel, |
| 343 | callback: VideoCallbackType, |
| 344 | ): Promise<PublishVideoResult> { |
| 345 | return new Promise(async (resolve) => { |
| 346 | const wxSphParams = params.diffParams![PlatType.WxSph]!; |
| 347 | const result = await shipinhaoService |
| 348 | .publishVideoWorkApi( |
| 349 | params.cookies!, |
| 350 | params.videoPath!, |
| 351 | { |
| 352 | proxy: params.proxyIp || '', |
| 353 | mixInfo: params.mixInfo |
| 354 | ? { |
| 355 | mixId: `${params.mixInfo.value}`, |
| 356 | mixName: params.mixInfo.label, |
| 357 | } |
| 358 | : undefined, |
| 359 | postFlag: wxSphParams.isOriginal ? 1 : 0, |
| 360 | cover: params.coverPath!, |
| 361 | title: params.desc, |
| 362 | topics: params.topics, |
| 363 | des: params.desc, |
| 364 | timingTime: params.timingTime?.getTime(), |
| 365 | // 位置 |
| 366 | ...(params.location |
| 367 | ? { |
| 368 | poiInfo: { |
| 369 | latitude: params.location.latitude, |
| 370 | longitude: params.location.longitude, |
| 371 | poiCity: params.location.city, |
| 372 | poiName: params.location.name, |
| 373 | poiAddress: params.location.simpleAddress, |
| 374 | poiId: params.location.id, |
| 375 | }, |
| 376 | } |
| 377 | : {}), |
| 378 | // @用户 |
| 379 | mentionedUserInfo: params.mentionedUserInfo |
| 380 | ? params.mentionedUserInfo.map((v) => { |
| 381 | return { |
| 382 | nickName: v.label, |
| 383 | }; |
| 384 | }) |
| 385 | : undefined, |
| 386 | // 活动 |
| 387 | event: wxSphParams.activity, |
| 388 | }, |
| 389 | callback, |
| 390 | ) |
| 391 | .catch((e) => { |
| 392 | resolve({ |
| 393 | code: 0, |
| 394 | msg: e, |
| 395 | }); |
| 396 | }); |
| 397 | if (!result || !result.publishId) |
| 398 | return resolve({ |
| 399 | code: 0, |
| 400 | msg: '网络繁忙,请稍后重试', |
| 401 | }); |
| 402 | |
| 403 | return resolve({ |
| 404 | code: 1, |
| 405 | msg: '成功!', |
| 406 | dataId: result.publishId, |
| 407 | }); |
| 408 | }); |
| 409 | } |
| 410 | |
| 411 | async getTopics({}: IGetTopicsParams): Promise<IGetTopicsResponse> { |
| 412 | return Promise.resolve({ |
| 413 | data: [], |
| 414 | status: 400, |
| 415 | }); |
| 416 | } |
| 417 | |
| 418 | async getLocationData(params: IGetLocationDataParams) { |
| 419 | const locationRes = await shipinhaoService.getLocation({ |
| 420 | ...params, |
| 421 | query: params.keywords, |
| 422 | cookie: params.cookie!, |
| 423 | }); |
| 424 | return { |
| 425 | status: this.getCode(locationRes), |
| 426 | data: locationRes?.data?.data?.list?.map((v) => { |
| 427 | return { |
| 428 | name: v.name, |
| 429 | simpleAddress: v.fullAddress, |
| 430 | id: v.uid, |
| 431 | latitude: v.latitude, |
| 432 | longitude: v.longitude, |
| 433 | city: v.city, |
| 434 | }; |
| 435 | }), |
| 436 | }; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * 点赞 |
| 441 | */ |
| 442 | dianzanDyOther(account: AccountModel, pcursor?: string): Promise<any> { |
| 443 | return new Promise((resolve, reject) => {}); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * 收藏 |
| 448 | */ |
| 449 | shoucangDyOther(account: AccountModel, pcursor?: string): Promise<any> { |
| 450 | return new Promise((resolve, reject) => {}); |
| 451 | } |
| 452 | |
| 453 | getsearchNodeList( |
| 454 | account: AccountModel, |
| 455 | pcursor?: string, |
| 456 | ): Promise<{ |
| 457 | list: WorkData[]; |
| 458 | pageInfo: ResponsePageInfo; |
| 459 | }> { |
| 460 | throw '无此方法'; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | const wxSph = new WxSph(); |
| 465 | export default wxSph; |
| 466 |