contact.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. import {
  2. v4 as uuidv4
  3. } from "uuid";
  4. import IMSDK from "openim-uniapp-polyfill";
  5. const state = {
  6. friendList: [],
  7. blackList: [],
  8. groupList: [],
  9. recvFriendApplications: [],
  10. sentFriendApplications: [],
  11. recvGroupApplications: [],
  12. sentGroupApplications: [],
  13. unHandleFriendApplicationNum: 0,
  14. unHandleGroupApplicationNum: 0,
  15. };
  16. const mutations = {
  17. SET_FRIEND_LIST(state, list) {
  18. state.friendList = [...list];
  19. },
  20. SET_BLACK_LIST(state, list) {
  21. state.blackList = [...list];
  22. },
  23. SET_GROUP_LIST(state, list) {
  24. state.groupList = [...list];
  25. },
  26. SET_RECV_FRIEND_APPLICATIONS(state, list) {
  27. state.recvFriendApplications = [...list];
  28. },
  29. SET_SENT_FRIEND_APPLICATIONS(state, list) {
  30. state.sentFriendApplications = [...list];
  31. },
  32. SET_RECV_GROUP_APPLICATIONS(state, list) {
  33. state.recvGroupApplications = [...list];
  34. },
  35. SET_SENT_GROUP_APPLICATIONS(state, list) {
  36. state.sentGroupApplications = [...list];
  37. },
  38. SET_UNHANDLE_FRIEND_APPLICATION_NUM(state, num) {
  39. state.unHandleFriendApplicationNum = num;
  40. },
  41. SET_UNHANDLE_GROUP_APPLICATION_NUM(state, num) {
  42. state.unHandleGroupApplicationNum = num;
  43. },
  44. };
  45. const actions = {
  46. async getFriendList({
  47. commit
  48. }) {
  49. let offset = 0;
  50. let friendInfoList = [];
  51. let initialFetch = true;
  52. let retryCount = 0;
  53. while (true) {
  54. try {
  55. const count = initialFetch ? 1000 : 1000;
  56. const {
  57. data
  58. } = await IMSDK.asyncApi("getFriendListPage", uuidv4(), {
  59. offset,
  60. count,
  61. });
  62. friendInfoList = [
  63. ...friendInfoList,
  64. ...data,
  65. ];
  66. offset += count;
  67. retryCount = 0;
  68. if (data.length < count) break;
  69. initialFetch = false;
  70. } catch (error) {
  71. console.error("getFriendListPage error", error);
  72. retryCount++;
  73. if (retryCount >= 10) break;
  74. // 指数退避:200ms, 400ms, 800ms ...
  75. const delay = 200 * Math.pow(2, retryCount - 1);
  76. await new Promise(resolve => setTimeout(resolve, delay));
  77. }
  78. }
  79. commit("SET_FRIEND_LIST", friendInfoList);
  80. },
  81. async getGrouplist({
  82. commit
  83. }) {
  84. let offset = 0;
  85. let groupList = [];
  86. let retryCount = 0;
  87. while (true) {
  88. try {
  89. const {
  90. data
  91. } = await IMSDK.asyncApi(
  92. "getJoinedGroupListPage",
  93. uuidv4(), {
  94. offset,
  95. count: 1000,
  96. }
  97. );
  98. groupList = [...groupList, ...data];
  99. offset += 1000;
  100. retryCount = 0;
  101. if (data.length < 1000) break;
  102. } catch (error) {
  103. console.error("getGrouplist error", error);
  104. retryCount++;
  105. if (retryCount >= 10) break;
  106. // 指数退避:200ms, 400ms, 800ms ...
  107. const delay = 200 * Math.pow(2, retryCount - 1);
  108. await new Promise(resolve => setTimeout(resolve, delay));
  109. }
  110. }
  111. commit("SET_GROUP_LIST", groupList);
  112. },
  113. getBlacklist({
  114. commit
  115. }) {
  116. IMSDK.asyncApi(IMSDK.IMMethods.GetBlackList, uuidv4()).then(({
  117. data
  118. }) => {
  119. commit("SET_BLACK_LIST", data);
  120. });
  121. },
  122. getRecvFriendApplications({ commit }) {
  123. let offset = 0;
  124. let recvFriendApplications = [];
  125. async function fetchRecvFriendApplications() {
  126. try {
  127. const { data } = await IMSDK.asyncApi(
  128. IMSDK.IMMethods.GetFriendApplicationListAsRecipient,
  129. uuidv4(),
  130. {
  131. offset,
  132. count: 200,
  133. }
  134. );
  135. recvFriendApplications = [...recvFriendApplications, ...data];
  136. offset += 200;
  137. if (data.length < 200) {
  138. commit("SET_RECV_FRIEND_APPLICATIONS", recvFriendApplications);
  139. } else {
  140. fetchRecvFriendApplications();
  141. }
  142. } catch (error) {
  143. console.error("getRecvFriendApplications error");
  144. commit("SET_RECV_FRIEND_APPLICATIONS", recvFriendApplications);
  145. }
  146. }
  147. fetchRecvFriendApplications();
  148. // // #ifdef H5 || MP-WEIXIN
  149. // fetchRecvFriendApplications()
  150. // // #endif
  151. // // #ifdef APP-PLUS
  152. // IMSDK.asyncApi(
  153. // IMSDK.IMMethods.GetFriendApplicationListAsRecipient,
  154. // uuidv4(),
  155. // ).then(({ data }) => {
  156. // commit("SET_RECV_FRIEND_APPLICATIONS", data);
  157. // });
  158. // // #endif
  159. },
  160. getSentFriendApplications({ commit }) {
  161. let offset = 0;
  162. let sentFriendApplications = [];
  163. async function fetchSentFriendApplications() {
  164. try {
  165. const { data } = await IMSDK.asyncApi(
  166. IMSDK.IMMethods.GetFriendApplicationListAsApplicant,
  167. uuidv4(),
  168. {
  169. offset,
  170. count: 200,
  171. }
  172. );
  173. sentFriendApplications = [...sentFriendApplications, ...data];
  174. offset += 200;
  175. if (data.length < 200) {
  176. commit("SET_SENT_FRIEND_APPLICATIONS", sentFriendApplications);
  177. } else {
  178. fetchSentFriendApplications();
  179. }
  180. } catch (error) {
  181. console.error("getSentFriendApplications error");
  182. commit("SET_SENT_FRIEND_APPLICATIONS", sentFriendApplications);
  183. }
  184. }
  185. fetchSentFriendApplications();
  186. // // #ifdef H5 || MP-WEIXIN
  187. // fetchSentFriendApplications()
  188. // // #endif
  189. // // #ifdef APP-PLUS
  190. // IMSDK.asyncApi(
  191. // IMSDK.IMMethods.GetFriendApplicationListAsApplicant,
  192. // uuidv4(),
  193. // ).then(({ data }) => {
  194. // commit("SET_SENT_FRIEND_APPLICATIONS", data);
  195. // });
  196. // // #endif
  197. },
  198. getRecvGroupApplications({ commit }) {
  199. let offset = 0;
  200. let recvGroupApplications = [];
  201. async function fetchRecvGroupApplications() {
  202. try {
  203. const { data } = await IMSDK.asyncApi(
  204. IMSDK.IMMethods.GetGroupApplicationListAsRecipient,
  205. uuidv4(),
  206. {
  207. offset,
  208. count: 200,
  209. }
  210. );
  211. recvGroupApplications = [...recvGroupApplications, ...data];
  212. offset += 200;
  213. if (data.length < 200) {
  214. commit("SET_RECV_GROUP_APPLICATIONS", recvGroupApplications);
  215. } else {
  216. fetchRecvGroupApplications();
  217. }
  218. } catch (error) {
  219. console.error("getRecvGroupApplications error");
  220. commit("SET_RECV_GROUP_APPLICATIONS", recvGroupApplications);
  221. }
  222. }
  223. fetchRecvGroupApplications();
  224. // // #ifdef H5 || MP-WEIXIN
  225. // fetchRecvGroupApplications()
  226. // // #endif
  227. // // #ifdef APP-PLUS
  228. // IMSDK.asyncApi(
  229. // IMSDK.IMMethods.GetGroupApplicationListAsRecipient,
  230. // uuidv4(),
  231. // ).then(({ data }) => {
  232. // commit("SET_RECV_GROUP_APPLICATIONS", data);
  233. // });
  234. // // #endif
  235. },
  236. getSentGroupApplications({ commit }) {
  237. let offset = 0;
  238. let sentGroupApplications = [];
  239. async function fetchSentGroupApplications() {
  240. try {
  241. const { data } = await IMSDK.asyncApi(
  242. IMSDK.IMMethods.GetGroupApplicationListAsApplicant,
  243. uuidv4(),
  244. {
  245. offset,
  246. count: 200,
  247. }
  248. );
  249. sentGroupApplications = [...sentGroupApplications, ...data];
  250. offset += 200;
  251. if (data.length < 200) {
  252. commit("SET_SENT_GROUP_APPLICATIONS", sentGroupApplications);
  253. } else {
  254. fetchSentGroupApplications();
  255. }
  256. } catch (error) {
  257. console.error("getSentGroupApplications error");
  258. commit("SET_SENT_GROUP_APPLICATIONS", sentGroupApplications);
  259. }
  260. };
  261. fetchSentGroupApplications();
  262. // // #ifdef H5 || MP-WEIXIN
  263. // fetchSentGroupApplications()
  264. // // #endif
  265. // // #ifdef APP-PLUS
  266. // IMSDK.asyncApi(
  267. // IMSDK.IMMethods.GetGroupApplicationListAsApplicant,
  268. // uuidv4(),
  269. // ).then(({ data }) => {
  270. // commit("SET_SENT_GROUP_APPLICATIONS", data);
  271. // });
  272. // // #endif
  273. },
  274. pushNewFriend({
  275. commit,
  276. state
  277. }, friendInfo) {
  278. const tmpList = [...state.friendList];
  279. const idx = tmpList.findIndex((item) => item.userID === friendInfo.userID);
  280. if (idx === -1) {
  281. commit("SET_FRIEND_LIST", [...tmpList, friendInfo]);
  282. }
  283. },
  284. updateFriendInfo({
  285. commit,
  286. state
  287. }, {
  288. friendInfo,
  289. isRemove = false
  290. }) {
  291. const tmpList = [...state.friendList];
  292. const idx = tmpList.findIndex((item) => item.userID === friendInfo.userID);
  293. if (idx !== -1) {
  294. if (isRemove) {
  295. tmpList.splice(idx, 1);
  296. } else {
  297. tmpList[idx] = {
  298. ...friendInfo,
  299. };
  300. }
  301. commit("SET_FRIEND_LIST", tmpList);
  302. }
  303. },
  304. pushNewBlack({
  305. commit,
  306. state
  307. }, blackInfo) {
  308. const tmpList = [...state.blackList];
  309. const idx = tmpList.findIndex((item) => item.userID === blackInfo.userID);
  310. if (idx === -1) {
  311. commit("SET_BLACK_LIST", [...tmpList, blackInfo]);
  312. }
  313. },
  314. updateBlackInfo({
  315. commit,
  316. state
  317. }, {
  318. blackInfo,
  319. isRemove = false
  320. }) {
  321. const tmpList = [...state.blackList];
  322. const idx = tmpList.findIndex((item) => item.userID === blackInfo.userID);
  323. if (idx !== -1) {
  324. if (isRemove) {
  325. tmpList.splice(idx, 1);
  326. } else {
  327. tmpList[idx] = {
  328. ...blackInfo,
  329. };
  330. }
  331. commit("SET_BLACK_LIST", tmpList);
  332. }
  333. },
  334. pushNewGroup({
  335. commit,
  336. state
  337. }, groupInfo) {
  338. const tmpList = [...state.groupList];
  339. const idx = tmpList.findIndex((item) => item.groupID === groupInfo.groupID);
  340. if (idx === -1) {
  341. commit("SET_GROUP_LIST", [...tmpList, groupInfo]);
  342. }
  343. },
  344. updateGroupInfo({
  345. commit,
  346. state,
  347. rootState
  348. }, {
  349. groupInfo,
  350. isRemove = false
  351. }, ) {
  352. const tmpList = [...state.groupList];
  353. const idx = tmpList.findIndex((item) => item.groupID === groupInfo.groupID);
  354. if (rootState.conversation.currentGroup.groupID === groupInfo.groupID) {
  355. commit("conversation/SET_CURRENT_GROUP", groupInfo, {
  356. root: true
  357. });
  358. }
  359. if (idx !== -1) {
  360. if (isRemove) {
  361. tmpList.splice(idx, 1);
  362. } else {
  363. tmpList[idx] = {
  364. ...groupInfo,
  365. };
  366. }
  367. commit("SET_GROUP_LIST", tmpList);
  368. }
  369. },
  370. pushNewRecvFriendApplition({
  371. commit,
  372. state
  373. }, application) {
  374. const tmpList = [...state.recvFriendApplications];
  375. const idx = tmpList.findIndex(
  376. (item) => item.fromUserID === application.fromUserID,
  377. );
  378. if (idx !== -1) {
  379. tmpList.splice(idx, 1);
  380. }
  381. commit("SET_RECV_FRIEND_APPLICATIONS", [...tmpList, application]);
  382. },
  383. updateRecvFriendApplition({
  384. commit,
  385. state,
  386. rootState
  387. }, {
  388. application,
  389. isRemove = false
  390. }, ) {
  391. const tmpList = [...state.recvFriendApplications];
  392. const idx = tmpList.findIndex(
  393. (item) => item.fromUserID === application.fromUserID,
  394. );
  395. if (idx !== -1) {
  396. if (isRemove) {
  397. tmpList.splice(idx, 1);
  398. } else {
  399. tmpList[idx] = {
  400. ...application,
  401. };
  402. }
  403. commit("SET_RECV_FRIEND_APPLICATIONS", tmpList);
  404. }
  405. },
  406. pushNewSentFriendApplition({
  407. commit,
  408. state
  409. }, application) {
  410. const tmpList = [...state.sentFriendApplications];
  411. const idx = tmpList.findIndex(
  412. (item) => item.toUserID === application.toUserID,
  413. );
  414. if (idx !== -1) {
  415. tmpList.splice(idx, 1);
  416. }
  417. commit("SET_SENT_FRIEND_APPLICATIONS", [...tmpList, application]);
  418. },
  419. updateSentFriendApplition({
  420. commit,
  421. state,
  422. rootState
  423. }, {
  424. application,
  425. isRemove = false
  426. }, ) {
  427. const tmpList = [...state.sentFriendApplications];
  428. const idx = tmpList.findIndex(
  429. (item) => item.toUserID === application.toUserID,
  430. );
  431. if (idx !== -1) {
  432. if (isRemove) {
  433. tmpList.splice(idx, 1);
  434. } else {
  435. tmpList[idx] = {
  436. ...application,
  437. };
  438. }
  439. commit("SET_SENT_FRIEND_APPLICATIONS", tmpList);
  440. }
  441. },
  442. pushNewRecvGroupApplition({
  443. commit,
  444. state
  445. }, application) {
  446. const tmpList = [...state.recvGroupApplications];
  447. const idx = tmpList.findIndex((item) => item.userID === application.userID);
  448. if (idx !== -1) {
  449. tmpList.splice(idx, 1);
  450. }
  451. commit("SET_RECV_GROUP_APPLICATIONS", [...tmpList, application]);
  452. },
  453. updateRecvGroupApplition({
  454. commit,
  455. state,
  456. rootState
  457. }, {
  458. application,
  459. isRemove = false
  460. }, ) {
  461. const tmpList = [...state.recvGroupApplications];
  462. const idx = tmpList.findIndex((item) => item.userID === application.userID);
  463. if (idx !== -1) {
  464. if (isRemove) {
  465. tmpList.splice(idx, 1);
  466. } else {
  467. tmpList[idx] = {
  468. ...application,
  469. };
  470. }
  471. commit("SET_RECV_GROUP_APPLICATIONS", tmpList);
  472. }
  473. },
  474. pushNewSentGroupApplition({
  475. commit,
  476. state
  477. }, application) {
  478. const tmpList = [...state.sentGroupApplications];
  479. const idx = tmpList.findIndex(
  480. (item) => item.groupID === application.groupID,
  481. );
  482. if (idx !== -1) {
  483. tmpList.splice(idx, 1);
  484. }
  485. commit("SET_SENT_GROUP_APPLICATIONS", [...tmpList, application]);
  486. },
  487. updateSentGroupApplition({
  488. commit,
  489. state,
  490. rootState
  491. }, {
  492. application,
  493. isRemove = false
  494. }, ) {
  495. const tmpList = [...state.sentGroupApplications];
  496. const idx = tmpList.findIndex(
  497. (item) => item.groupID === application.groupID,
  498. );
  499. if (idx !== -1) {
  500. if (isRemove) {
  501. tmpList.splice(idx, 1);
  502. } else {
  503. tmpList[idx] = {
  504. ...application,
  505. };
  506. }
  507. commit("SET_SENT_GROUP_APPLICATIONS", tmpList);
  508. }
  509. },
  510. };
  511. export default {
  512. namespaced: true,
  513. state,
  514. mutations,
  515. actions,
  516. };