From 48bae77b278f7ef9ecd88241c03e205e3b385376 Mon Sep 17 00:00:00 2001 From: wanglihui <1769794040@qq.com> Date: Sat, 9 Mar 2024 17:59:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E5=A5=BD=E5=8F=8B?= =?UTF-8?q?=E7=94=B3=E8=AF=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/src/App/index.tsx | 126 +++++++++------ packages/tsdaodaobase/src/App.tsx | 75 +++++---- .../tsdaodaocontacts/src/NewFriend/index.tsx | 136 ++++++++++------ .../tsdaodaocontacts/src/NewFriend/vm.tsx | 96 +++++++++--- packages/tsdaodaocontacts/src/module.tsx | 146 ++++++++++++------ 5 files changed, 379 insertions(+), 200 deletions(-) diff --git a/apps/web/src/App/index.tsx b/apps/web/src/App/index.tsx index 08db5b6..8c3e67d 100644 --- a/apps/web/src/App/index.tsx +++ b/apps/web/src/App/index.tsx @@ -1,60 +1,100 @@ -import { ChatPage, EndpointCategory, WKApp, Menus } from '@tsdaodao/base'; -import { ContactsList } from '@tsdaodao/contacts'; -import React from 'react'; -import './index.css'; -import AppLayout from '../Layout'; -import { WKSDK } from 'wukongimjssdk'; +import { ChatPage, EndpointCategory, WKApp, Menus } from "@tsdaodao/base"; +import { ContactsList } from "@tsdaodao/contacts"; +import React from "react"; +import "./index.css"; +import AppLayout from "../Layout"; +import { WKSDK } from "wukongimjssdk"; function App() { - registerMenus() - return ( - - ); + registerMenus(); + return ; } function registerMenus() { - WKSDK.shared().conversationManager.addConversationListener(() => { - WKApp.menus.refresh() - }) + WKApp.menus.refresh(); + }); - WKApp.endpointManager.setMethod("menus.friendapply.change", () => { - WKApp.menus.refresh() - }, { - category: EndpointCategory.friendApplyDataChange, - }) - - WKApp.menus.register("chat", (context) => { - const m = new Menus("chat", "/", "会话", - 会话, - 会话) - let badge = 0 - for (const conversation of WKSDK.shared().conversationManager.conversations) { - if (!conversation.channelInfo?.mute) { - badge += conversation.unread - } + WKApp.endpointManager.setMethod( + "menus.friendapply.change", + () => { + WKApp.menus.refresh(); + }, + { + category: EndpointCategory.friendApplyDataChange, } - m.badge = badge - return m - },1000) + ); - WKApp.menus.register("contacts", (param) => { - const m = new Menus("contacts", "/contacts", "通讯录", - 通讯录, - 通讯录) + WKApp.menus.register( + "chat", + (context) => { + const m = new Menus( + "chat", + "/", + "会话", + 会话, + ( + 会话 + ) + ); + let badge = 0; + for (const conversation of WKSDK.shared().conversationManager + .conversations) { + if (!conversation.channelInfo?.mute) { + badge += conversation.unread; + } + } + m.badge = badge; + return m; + }, + 1000 + ); - m.badge = WKApp.shared.getFriendApplysUnreadCount() - return m - },2000) + // 获取好友未申请添加数量 + let unreadCount = 0; + if (WKApp.loginInfo.isLogined()) { + WKApp.apiClient.get(`/user/reddot/friendApply`).then((res) => { + unreadCount = res.count; + WKApp.menus.refresh(); + }); + } + WKApp.menus.register( + "contacts", + (param) => { + const m = new Menus( + "contacts", + "/contacts", + "通讯录", + ( + 通讯录 + ), + ( + 通讯录 + ) + ); + + m.badge = unreadCount; + return m; + }, + 2000 + ); WKApp.route.register("/", () => { - return - }) + return ; + }); WKApp.route.register("/contacts", () => { - return - }) + return ; + }); } export default App; - diff --git a/packages/tsdaodaobase/src/App.tsx b/packages/tsdaodaobase/src/App.tsx index f994d0b..52c0b3b 100644 --- a/packages/tsdaodaobase/src/App.tsx +++ b/packages/tsdaodaobase/src/App.tsx @@ -457,9 +457,9 @@ export default class WKApp extends ProviderListener { for (const friendApplyObj of friendApplyObjs) { const f = new FriendApply() f.uid = friendApplyObj.uid - f.name = friendApplyObj.name + f.to_name = friendApplyObj.to_name f.remark = friendApplyObj.remark - f.state = friendApplyObj.state + f.status = friendApplyObj.status f.token = friendApplyObj.token f.unread = friendApplyObj.unread f.createdAt = friendApplyObj.createdAt @@ -472,35 +472,41 @@ export default class WKApp extends ProviderListener { return friendApplys } - public getFriendApplysUnreadCount() { - const friendApplys = this.getFriendApplys() + public async getFriendApplysUnreadCount() { + // const friendApplys = this.getFriendApplys() let unreadCount = 0 - if (friendApplys && friendApplys.length > 0) { - for (const friendApply of friendApplys) { - if (friendApply.unread) { - unreadCount++ - } - } - } + // if (friendApplys && friendApplys.length > 0) { + // for (const friendApply of friendApplys) { + // if (friendApply.unread) { + // unreadCount++ + // } + // } + // } + if (!WKApp.loginInfo.isLogined()) { + return unreadCount; + } + const res = await WKApp.apiClient.get(`/user/reddot/friendApply`); + unreadCount = res.count; return unreadCount } - public friendApplyMarkAllReaded() { - let friendApplys = this.getFriendApplys() - if (!friendApplys) { - friendApplys = new Array() - } - var change = false - for (const friendApply of friendApplys) { - if (friendApply.unread) { - friendApply.unread = false - change = true - } - } - if (change) { - WKApp.loginInfo.setStorageItem(this.getFriendApplysKey(), JSON.stringify(friendApplys)) - WKApp.endpointManager.invokes(EndpointCategory.friendApplyDataChange) - } + public async friendApplyMarkAllReaded(): Promise { + // let friendApplys = this.getFriendApplys() + // if (!friendApplys) { + // friendApplys = new Array() + // } + // var change = false + // for (const friendApply of friendApplys) { + // if (friendApply.unread) { + // friendApply.unread = false + // change = true + // } + // } + // if (change) { + // WKApp.loginInfo.setStorageItem(this.getFriendApplysKey(), JSON.stringify(friendApplys)) + // WKApp.endpointManager.invokes(EndpointCategory.friendApplyDataChange) + // } + await WKApp.apiClient.delete(`/user/reddot/friendApply`); } public addFriendApply(friendApply: FriendApply) { @@ -572,13 +578,14 @@ export enum FriendApplyState { } // 好友申请 export class FriendApply { - uid!: string - name!: string - remark?: string - token?: string - state!: FriendApplyState - unread: boolean = false // 是否未读 - createdAt!: number // 创建时间 + uid!: string; + to_uid!: string; + to_name!: string; + remark?: string; + token?: string; + status!: FriendApplyState; + unread: boolean = false; // 是否未读 + createdAt!: number; // 创建时间 } diff --git a/packages/tsdaodaocontacts/src/NewFriend/index.tsx b/packages/tsdaodaocontacts/src/NewFriend/index.tsx index 7d273bb..6d45cb8 100644 --- a/packages/tsdaodaocontacts/src/NewFriend/index.tsx +++ b/packages/tsdaodaocontacts/src/NewFriend/index.tsx @@ -1,58 +1,92 @@ -import { FriendApplyState, WKApp, WKViewQueueHeader, Provider } from "@tsdaodao/base"; +import { + FriendApplyState, + WKApp, + WKViewQueueHeader, + Provider, +} from "@tsdaodao/base"; import React from "react"; import { Component, ReactNode } from "react"; -import { Button } from '@douyinfe/semi-ui'; -import "./index.css" +import { Button } from "@douyinfe/semi-ui"; +import "./index.css"; import { NewFriendVM } from "./vm"; -import "./index.css" +import "./index.css"; import { FriendAdd } from "../FriendAdd"; export class NewFriend extends Component { - - render(): ReactNode { - return { - return new NewFriendVM() - }} render={(vm: NewFriendVM) => { - - return
- { - WKApp.routeLeft.pop() - }} action={
- -
}>
-
-
    - { - vm.friendApplys.map((f) => { - return
  • -
    - -
    -
    -
    - {f.name} -
    -
    - {f.remark} -
    -
    -
    - -
    -
  • - }) - } -
-
+ render(): ReactNode { + return ( + { + return new NewFriendVM(); + }} + render={(vm: NewFriendVM) => { + return ( +
+ { + WKApp.routeLeft.pop(); + }} + action={ +
+ +
+ } + >
+
+
    + {vm.friendApplys.map((f) => { + return ( +
  • +
    + +
    +
    +
    + {f.to_name} +
    +
    + {f.remark} +
    +
    +
    + +
    +
  • + ); + })} +
+
- }}> - -
- } -} \ No newline at end of file + ); + }} + > + ); + } +} diff --git a/packages/tsdaodaocontacts/src/NewFriend/vm.tsx b/packages/tsdaodaocontacts/src/NewFriend/vm.tsx index 76f0354..06b2503 100644 --- a/packages/tsdaodaocontacts/src/NewFriend/vm.tsx +++ b/packages/tsdaodaocontacts/src/NewFriend/vm.tsx @@ -1,33 +1,83 @@ +import { WKSDK, Message, CMDContent } from "wukongimjssdk"; import { FriendApplyState, WKApp, ProviderListener } from "@tsdaodao/base"; import { FriendApply } from "@tsdaodao/base"; - export class NewFriendVM extends ProviderListener { - friendApplys: FriendApply[] = [] - sureLoading:boolean = false - currentFriendApply?:FriendApply + friendApplys: FriendApply[] = []; + sureLoading: boolean = false; + currentFriendApply?: FriendApply; - didMount(): void { + async didMount(): Promise { + WKApp.shared.friendApplyMarkAllReaded(); - WKApp.shared.friendApplyMarkAllReaded() - - this.friendApplys = WKApp.shared.getFriendApplys() - this.notifyListener() + this.friendApplys = await this.getFriendApply(); + if (this.friendApplys.length === 0) { + this.clearFriendApply(); } + this.notifyListener(); + // 监听好友申请 + WKSDK.shared().chatManager.addCMDListener(this.friendRequestCMDListener); + } - friendSure(apply: FriendApply) { - this.sureLoading = true - this.currentFriendApply = apply - this.notifyListener() + didUnMount(): void { + // 监听好友申请 + WKSDK.shared().chatManager.removeCMDListener(this.friendRequestCMDListener); + } - WKApp.dataSource.commonDataSource.friendSure(apply.token || "").then(() => { - apply.state = FriendApplyState.accepted - WKApp.shared.updateFriendApply(apply) - this.sureLoading = false - this.notifyListener() - }).catch(() => { - this.sureLoading = false - this.notifyListener() - }) + friendSure(apply: FriendApply) { + this.sureLoading = true; + this.currentFriendApply = apply; + this.notifyListener(); + + WKApp.dataSource.commonDataSource + .friendSure(apply.token || "") + .then(() => { + apply.status = FriendApplyState.accepted; + WKApp.shared.updateFriendApply(apply); + this.sureLoading = false; + this.notifyListener(); + }) + .catch(() => { + this.sureLoading = false; + this.notifyListener(); + }); + } + + async getFriendApply(): Promise { + const fromData = { + page_index: 1, + page_size: 999, + }; + const res = await WKApp.apiClient.get("/friend/apply", { + param: fromData, + }); + + return res; + } + + async delFriendApply(apply: FriendApply): Promise { + WKApp.apiClient + .delete(`/friend/apply/${apply.to_uid}`) + .then(async () => { + this.friendApplys = await this.getFriendApply(); + this.sureLoading = false; + this.notifyListener(); + }) + .catch(() => { + this.sureLoading = false; + this.notifyListener(); + }); + } + + async clearFriendApply(): Promise { + await WKApp.apiClient.delete(`/user/reddot/friendApply`); + } + + public async friendRequestCMDListener(message: Message) { + console.log("收到CMD->", message); + const cmdContent = message.content as CMDContent; + if (cmdContent.cmd === "friendRequest") { + this.friendApplys = await this.getFriendApply(); } -} \ No newline at end of file + } +} diff --git a/packages/tsdaodaocontacts/src/module.tsx b/packages/tsdaodaocontacts/src/module.tsx index a047af0..38944c2 100644 --- a/packages/tsdaodaocontacts/src/module.tsx +++ b/packages/tsdaodaocontacts/src/module.tsx @@ -1,57 +1,105 @@ -import { EndpointCategory, IconListItem, IModule, WKApp, ThemeMode } from "@tsdaodao/base" -import React from "react" -import Blacklist from "./Blacklist" -import { FriendAdd } from "./FriendAdd" -import GroupSave from "./GroupSave" -import { NewFriend } from "./NewFriend" -import { ContactsListManager } from "./Service/ContactsListManager" - +import { + EndpointCategory, + IconListItem, + IModule, + WKApp, + ThemeMode, +} from "@tsdaodao/base"; +import React from "react"; +import Blacklist from "./Blacklist"; +import { FriendAdd } from "./FriendAdd"; +import GroupSave from "./GroupSave"; +import { NewFriend } from "./NewFriend"; +import { ContactsListManager } from "./Service/ContactsListManager"; export default class ContactsModule implements IModule { + id(): string { + return "ContactsModule"; + } + init(): void { + console.log("【ContactsModule】初始化"); - id(): string { - return "ContactsModule" + WKApp.endpointManager.setMethod( + "contacts.friendapply.change", + () => { + ContactsListManager.shared.refreshList(); + }, + { + category: EndpointCategory.friendApplyDataChange, + } + ); + + // 获取好友未申请添加数量 + let unreadCount = 0; + if (WKApp.loginInfo.isLogined()) { + WKApp.apiClient.get(`/user/reddot/friendApply`).then((res) => { + unreadCount = res.count; + WKApp.menus.refresh(); + }); } - init(): void { - console.log("【ContactsModule】初始化") + WKApp.endpoints.registerContactsHeader("friends.new", (param: any) => { + return ( + { + WKApp.routeLeft.push(); + }} + > + ); + }); - WKApp.endpointManager.setMethod("contacts.friendapply.change", () => { - ContactsListManager.shared.refreshList() - }, { - category: EndpointCategory.friendApplyDataChange, - }) + WKApp.endpoints.registerContactsHeader("groups.save", (param: any) => { + return ( + { + WKApp.routeLeft.push(); + }} + > + ); + }); - WKApp.endpoints.registerContactsHeader("friends.new", (param: any) => { - return { - WKApp.routeLeft.push() - }} > - }) + WKApp.endpoints.registerContactsHeader( + "contacts.blacklist", + (param: any) => { + return ( + { + WKApp.routeLeft.push(); + }} + > + ); + } + ); - WKApp.endpoints.registerContactsHeader("groups.save", (param: any) => { - return { - WKApp.routeLeft.push() - }}> - }) - - WKApp.endpoints.registerContactsHeader("contacts.blacklist", (param: any) => { - return { - WKApp.routeLeft.push() - }}> - }) - - WKApp.shared.chatMenusRegister("chatmenus.addfriend",(param)=>{ - const isDark = WKApp.config.themeMode === ThemeMode.dark - return { - title: "添加朋友", - icon: require(`${isDark?"./assets/popmenus_friendadd_dark.png":"./assets/popmenus_friendadd.png"}`), - onClick:()=>{ - WKApp.routeLeft.push({ - WKApp.routeLeft.pop() - }}>) - } - } - }) - - } -} \ No newline at end of file + WKApp.shared.chatMenusRegister("chatmenus.addfriend", (param) => { + const isDark = WKApp.config.themeMode === ThemeMode.dark; + return { + title: "添加朋友", + icon: require(`${ + isDark + ? "./assets/popmenus_friendadd_dark.png" + : "./assets/popmenus_friendadd.png" + }`), + onClick: () => { + WKApp.routeLeft.push( + { + WKApp.routeLeft.pop(); + }} + > + ); + }, + }; + }); + } +}