| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- import {
- v4 as uuidv4
- } from "uuid";
- import IMSDK from "openim-uniapp-polyfill";
- const state = {
- friendList: [],
- blackList: [],
- groupList: [],
- recvFriendApplications: [],
- sentFriendApplications: [],
- recvGroupApplications: [],
- sentGroupApplications: [],
- unHandleFriendApplicationNum: 0,
- unHandleGroupApplicationNum: 0,
- };
- const mutations = {
- SET_FRIEND_LIST(state, list) {
- state.friendList = [...list];
- },
- SET_BLACK_LIST(state, list) {
- state.blackList = [...list];
- },
- SET_GROUP_LIST(state, list) {
- state.groupList = [...list];
- },
- SET_RECV_FRIEND_APPLICATIONS(state, list) {
- state.recvFriendApplications = [...list];
- },
- SET_SENT_FRIEND_APPLICATIONS(state, list) {
- state.sentFriendApplications = [...list];
- },
- SET_RECV_GROUP_APPLICATIONS(state, list) {
- state.recvGroupApplications = [...list];
- },
- SET_SENT_GROUP_APPLICATIONS(state, list) {
- state.sentGroupApplications = [...list];
- },
- SET_UNHANDLE_FRIEND_APPLICATION_NUM(state, num) {
- state.unHandleFriendApplicationNum = num;
- },
- SET_UNHANDLE_GROUP_APPLICATION_NUM(state, num) {
- state.unHandleGroupApplicationNum = num;
- },
- };
- const actions = {
- async getFriendList({
- commit
- }) {
- let offset = 0;
- let friendInfoList = [];
- let initialFetch = true;
- let retryCount = 0;
- while (true) {
- try {
- const count = initialFetch ? 1000 : 1000;
- const {
- data
- } = await IMSDK.asyncApi("getFriendListPage", uuidv4(), {
- offset,
- count,
- });
- friendInfoList = [
- ...friendInfoList,
- ...data,
- ];
- offset += count;
- retryCount = 0;
- if (data.length < count) break;
- initialFetch = false;
- } catch (error) {
- console.error("getFriendListPage error", error);
- retryCount++;
- if (retryCount >= 10) break;
- // 指数退避:200ms, 400ms, 800ms ...
- const delay = 200 * Math.pow(2, retryCount - 1);
- await new Promise(resolve => setTimeout(resolve, delay));
- }
- }
- commit("SET_FRIEND_LIST", friendInfoList);
- },
- async getGrouplist({
- commit
- }) {
- let offset = 0;
- let groupList = [];
- let retryCount = 0;
- while (true) {
- try {
- const {
- data
- } = await IMSDK.asyncApi(
- "getJoinedGroupListPage",
- uuidv4(), {
- offset,
- count: 1000,
- }
- );
- groupList = [...groupList, ...data];
- offset += 1000;
- retryCount = 0;
- if (data.length < 1000) break;
- } catch (error) {
- console.error("getGrouplist error", error);
- retryCount++;
- if (retryCount >= 10) break;
- // 指数退避:200ms, 400ms, 800ms ...
- const delay = 200 * Math.pow(2, retryCount - 1);
- await new Promise(resolve => setTimeout(resolve, delay));
- }
- }
- commit("SET_GROUP_LIST", groupList);
- },
- getBlacklist({
- commit
- }) {
- IMSDK.asyncApi(IMSDK.IMMethods.GetBlackList, uuidv4()).then(({
- data
- }) => {
- commit("SET_BLACK_LIST", data);
- });
- },
- getRecvFriendApplications({ commit }) {
- let offset = 0;
- let recvFriendApplications = [];
- async function fetchRecvFriendApplications() {
- try {
- const { data } = await IMSDK.asyncApi(
- IMSDK.IMMethods.GetFriendApplicationListAsRecipient,
- uuidv4(),
- {
- offset,
- count: 200,
- }
- );
- recvFriendApplications = [...recvFriendApplications, ...data];
- offset += 200;
- if (data.length < 200) {
- commit("SET_RECV_FRIEND_APPLICATIONS", recvFriendApplications);
- } else {
- fetchRecvFriendApplications();
- }
- } catch (error) {
- console.error("getRecvFriendApplications error");
- commit("SET_RECV_FRIEND_APPLICATIONS", recvFriendApplications);
- }
- }
- fetchRecvFriendApplications();
-
- // // #ifdef H5 || MP-WEIXIN
- // fetchRecvFriendApplications()
- // // #endif
-
- // // #ifdef APP-PLUS
- // IMSDK.asyncApi(
- // IMSDK.IMMethods.GetFriendApplicationListAsRecipient,
- // uuidv4(),
- // ).then(({ data }) => {
- // commit("SET_RECV_FRIEND_APPLICATIONS", data);
- // });
- // // #endif
- },
- getSentFriendApplications({ commit }) {
- let offset = 0;
- let sentFriendApplications = [];
- async function fetchSentFriendApplications() {
- try {
- const { data } = await IMSDK.asyncApi(
- IMSDK.IMMethods.GetFriendApplicationListAsApplicant,
- uuidv4(),
- {
- offset,
- count: 200,
- }
- );
- sentFriendApplications = [...sentFriendApplications, ...data];
- offset += 200;
- if (data.length < 200) {
- commit("SET_SENT_FRIEND_APPLICATIONS", sentFriendApplications);
- } else {
- fetchSentFriendApplications();
- }
- } catch (error) {
- console.error("getSentFriendApplications error");
- commit("SET_SENT_FRIEND_APPLICATIONS", sentFriendApplications);
- }
- }
- fetchSentFriendApplications();
- // // #ifdef H5 || MP-WEIXIN
- // fetchSentFriendApplications()
- // // #endif
-
- // // #ifdef APP-PLUS
- // IMSDK.asyncApi(
- // IMSDK.IMMethods.GetFriendApplicationListAsApplicant,
- // uuidv4(),
- // ).then(({ data }) => {
- // commit("SET_SENT_FRIEND_APPLICATIONS", data);
- // });
- // // #endif
- },
- getRecvGroupApplications({ commit }) {
- let offset = 0;
- let recvGroupApplications = [];
-
- async function fetchRecvGroupApplications() {
- try {
- const { data } = await IMSDK.asyncApi(
- IMSDK.IMMethods.GetGroupApplicationListAsRecipient,
- uuidv4(),
- {
- offset,
- count: 200,
- }
- );
-
- recvGroupApplications = [...recvGroupApplications, ...data];
- offset += 200;
-
- if (data.length < 200) {
- commit("SET_RECV_GROUP_APPLICATIONS", recvGroupApplications);
- } else {
- fetchRecvGroupApplications();
- }
- } catch (error) {
- console.error("getRecvGroupApplications error");
- commit("SET_RECV_GROUP_APPLICATIONS", recvGroupApplications);
- }
- }
-
- fetchRecvGroupApplications();
-
- // // #ifdef H5 || MP-WEIXIN
- // fetchRecvGroupApplications()
- // // #endif
-
- // // #ifdef APP-PLUS
- // IMSDK.asyncApi(
- // IMSDK.IMMethods.GetGroupApplicationListAsRecipient,
- // uuidv4(),
- // ).then(({ data }) => {
- // commit("SET_RECV_GROUP_APPLICATIONS", data);
- // });
- // // #endif
- },
- getSentGroupApplications({ commit }) {
- let offset = 0;
- let sentGroupApplications = [];
- async function fetchSentGroupApplications() {
- try {
- const { data } = await IMSDK.asyncApi(
- IMSDK.IMMethods.GetGroupApplicationListAsApplicant,
- uuidv4(),
- {
- offset,
- count: 200,
- }
- );
-
- sentGroupApplications = [...sentGroupApplications, ...data];
- offset += 200;
-
- if (data.length < 200) {
- commit("SET_SENT_GROUP_APPLICATIONS", sentGroupApplications);
- } else {
- fetchSentGroupApplications();
- }
- } catch (error) {
- console.error("getSentGroupApplications error");
- commit("SET_SENT_GROUP_APPLICATIONS", sentGroupApplications);
- }
- };
- fetchSentGroupApplications();
-
- // // #ifdef H5 || MP-WEIXIN
- // fetchSentGroupApplications()
- // // #endif
-
- // // #ifdef APP-PLUS
- // IMSDK.asyncApi(
- // IMSDK.IMMethods.GetGroupApplicationListAsApplicant,
- // uuidv4(),
- // ).then(({ data }) => {
- // commit("SET_SENT_GROUP_APPLICATIONS", data);
- // });
- // // #endif
- },
- pushNewFriend({
- commit,
- state
- }, friendInfo) {
- const tmpList = [...state.friendList];
- const idx = tmpList.findIndex((item) => item.userID === friendInfo.userID);
- if (idx === -1) {
- commit("SET_FRIEND_LIST", [...tmpList, friendInfo]);
- }
- },
- updateFriendInfo({
- commit,
- state
- }, {
- friendInfo,
- isRemove = false
- }) {
- const tmpList = [...state.friendList];
- const idx = tmpList.findIndex((item) => item.userID === friendInfo.userID);
- if (idx !== -1) {
- if (isRemove) {
- tmpList.splice(idx, 1);
- } else {
- tmpList[idx] = {
- ...friendInfo,
- };
- }
- commit("SET_FRIEND_LIST", tmpList);
- }
- },
- pushNewBlack({
- commit,
- state
- }, blackInfo) {
- const tmpList = [...state.blackList];
- const idx = tmpList.findIndex((item) => item.userID === blackInfo.userID);
- if (idx === -1) {
- commit("SET_BLACK_LIST", [...tmpList, blackInfo]);
- }
- },
- updateBlackInfo({
- commit,
- state
- }, {
- blackInfo,
- isRemove = false
- }) {
- const tmpList = [...state.blackList];
- const idx = tmpList.findIndex((item) => item.userID === blackInfo.userID);
- if (idx !== -1) {
- if (isRemove) {
- tmpList.splice(idx, 1);
- } else {
- tmpList[idx] = {
- ...blackInfo,
- };
- }
- commit("SET_BLACK_LIST", tmpList);
- }
- },
- pushNewGroup({
- commit,
- state
- }, groupInfo) {
- const tmpList = [...state.groupList];
- const idx = tmpList.findIndex((item) => item.groupID === groupInfo.groupID);
- if (idx === -1) {
- commit("SET_GROUP_LIST", [...tmpList, groupInfo]);
- }
- },
- updateGroupInfo({
- commit,
- state,
- rootState
- }, {
- groupInfo,
- isRemove = false
- }, ) {
- const tmpList = [...state.groupList];
- const idx = tmpList.findIndex((item) => item.groupID === groupInfo.groupID);
- if (rootState.conversation.currentGroup.groupID === groupInfo.groupID) {
- commit("conversation/SET_CURRENT_GROUP", groupInfo, {
- root: true
- });
- }
- if (idx !== -1) {
- if (isRemove) {
- tmpList.splice(idx, 1);
- } else {
- tmpList[idx] = {
- ...groupInfo,
- };
- }
- commit("SET_GROUP_LIST", tmpList);
- }
- },
- pushNewRecvFriendApplition({
- commit,
- state
- }, application) {
- const tmpList = [...state.recvFriendApplications];
- const idx = tmpList.findIndex(
- (item) => item.fromUserID === application.fromUserID,
- );
- if (idx !== -1) {
- tmpList.splice(idx, 1);
- }
- commit("SET_RECV_FRIEND_APPLICATIONS", [...tmpList, application]);
- },
- updateRecvFriendApplition({
- commit,
- state,
- rootState
- }, {
- application,
- isRemove = false
- }, ) {
- const tmpList = [...state.recvFriendApplications];
- const idx = tmpList.findIndex(
- (item) => item.fromUserID === application.fromUserID,
- );
- if (idx !== -1) {
- if (isRemove) {
- tmpList.splice(idx, 1);
- } else {
- tmpList[idx] = {
- ...application,
- };
- }
- commit("SET_RECV_FRIEND_APPLICATIONS", tmpList);
- }
- },
- pushNewSentFriendApplition({
- commit,
- state
- }, application) {
- const tmpList = [...state.sentFriendApplications];
- const idx = tmpList.findIndex(
- (item) => item.toUserID === application.toUserID,
- );
- if (idx !== -1) {
- tmpList.splice(idx, 1);
- }
- commit("SET_SENT_FRIEND_APPLICATIONS", [...tmpList, application]);
- },
- updateSentFriendApplition({
- commit,
- state,
- rootState
- }, {
- application,
- isRemove = false
- }, ) {
- const tmpList = [...state.sentFriendApplications];
- const idx = tmpList.findIndex(
- (item) => item.toUserID === application.toUserID,
- );
- if (idx !== -1) {
- if (isRemove) {
- tmpList.splice(idx, 1);
- } else {
- tmpList[idx] = {
- ...application,
- };
- }
- commit("SET_SENT_FRIEND_APPLICATIONS", tmpList);
- }
- },
- pushNewRecvGroupApplition({
- commit,
- state
- }, application) {
- const tmpList = [...state.recvGroupApplications];
- const idx = tmpList.findIndex((item) => item.userID === application.userID);
- if (idx !== -1) {
- tmpList.splice(idx, 1);
- }
- commit("SET_RECV_GROUP_APPLICATIONS", [...tmpList, application]);
- },
- updateRecvGroupApplition({
- commit,
- state,
- rootState
- }, {
- application,
- isRemove = false
- }, ) {
- const tmpList = [...state.recvGroupApplications];
- const idx = tmpList.findIndex((item) => item.userID === application.userID);
- if (idx !== -1) {
- if (isRemove) {
- tmpList.splice(idx, 1);
- } else {
- tmpList[idx] = {
- ...application,
- };
- }
- commit("SET_RECV_GROUP_APPLICATIONS", tmpList);
- }
- },
- pushNewSentGroupApplition({
- commit,
- state
- }, application) {
- const tmpList = [...state.sentGroupApplications];
- const idx = tmpList.findIndex(
- (item) => item.groupID === application.groupID,
- );
- if (idx !== -1) {
- tmpList.splice(idx, 1);
- }
- commit("SET_SENT_GROUP_APPLICATIONS", [...tmpList, application]);
- },
- updateSentGroupApplition({
- commit,
- state,
- rootState
- }, {
- application,
- isRemove = false
- }, ) {
- const tmpList = [...state.sentGroupApplications];
- const idx = tmpList.findIndex(
- (item) => item.groupID === application.groupID,
- );
- if (idx !== -1) {
- if (isRemove) {
- tmpList.splice(idx, 1);
- } else {
- tmpList[idx] = {
- ...application,
- };
- }
- commit("SET_SENT_GROUP_APPLICATIONS", tmpList);
- }
- },
- };
- export default {
- namespaced: true,
- state,
- mutations,
- actions,
- };
|