contact.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { v4 as uuidv4 } from "uuid";
  2. import IMSDK from "openim-uniapp-polyfill";
  3. const state = {
  4. friendList: [],
  5. blackList: [],
  6. groupList: [],
  7. recvFriendApplications: [],
  8. sentFriendApplications: [],
  9. recvGroupApplications: [],
  10. sentGroupApplications: [],
  11. unHandleFriendApplicationNum: 0,
  12. unHandleGroupApplicationNum: 0,
  13. };
  14. const mutations = {
  15. SET_FRIEND_LIST(state, list) {
  16. state.friendList = [...list];
  17. },
  18. SET_BLACK_LIST(state, list) {
  19. state.blackList = [...list];
  20. },
  21. SET_GROUP_LIST(state, list) {
  22. state.groupList = [...list];
  23. },
  24. SET_RECV_FRIEND_APPLICATIONS(state, list) {
  25. state.recvFriendApplications = [...list];
  26. },
  27. SET_SENT_FRIEND_APPLICATIONS(state, list) {
  28. state.sentFriendApplications = [...list];
  29. },
  30. SET_RECV_GROUP_APPLICATIONS(state, list) {
  31. state.recvGroupApplications = [...list];
  32. },
  33. SET_SENT_GROUP_APPLICATIONS(state, list) {
  34. state.sentGroupApplications = [...list];
  35. },
  36. SET_UNHANDLE_FRIEND_APPLICATION_NUM(state, num) {
  37. state.unHandleFriendApplicationNum = num;
  38. },
  39. SET_UNHANDLE_GROUP_APPLICATION_NUM(state, num) {
  40. state.unHandleGroupApplicationNum = num;
  41. },
  42. };
  43. const actions = {
  44. async getFriendList({ commit }) {
  45. let offset = 0;
  46. let friendInfoList = [];
  47. let initialFetch = true;
  48. while (true) {
  49. try {
  50. const count = initialFetch ? 10000 : 1000;
  51. const { data } = await IMSDK.asyncApi("getFriendListPage", uuidv4(), {
  52. offset,
  53. count,
  54. });
  55. friendInfoList = [
  56. ...friendInfoList,
  57. ...data,
  58. ];
  59. offset += count;
  60. if (data.length < count) break;
  61. initialFetch = false;
  62. } catch (error) {
  63. console.error("getFriendListPage error");
  64. }
  65. }
  66. commit("SET_FRIEND_LIST", friendInfoList);
  67. },
  68. async getGrouplist({ commit }) {
  69. let offset = 0;
  70. let groupList = [];
  71. while (true) {
  72. try {
  73. const { data } = await IMSDK.asyncApi(
  74. "getJoinedGroupListPage",
  75. uuidv4(),
  76. {
  77. offset,
  78. count: 1000,
  79. }
  80. );
  81. groupList = [...groupList, ...data];
  82. offset += 1000;
  83. if (data.length < 1000) break;
  84. } catch (error) {
  85. console.error("getGrouplist error");
  86. }
  87. }
  88. commit("SET_GROUP_LIST", groupList);
  89. },
  90. getBlacklist({ commit }) {
  91. IMSDK.asyncApi(IMSDK.IMMethods.GetBlackList, uuidv4()).then(({ data }) => {
  92. commit("SET_BLACK_LIST", data);
  93. });
  94. },
  95. getRecvFriendApplications({ commit }) {
  96. IMSDK.asyncApi(
  97. IMSDK.IMMethods.GetFriendApplicationListAsRecipient,
  98. uuidv4(),
  99. ).then(({ data }) => {
  100. commit("SET_RECV_FRIEND_APPLICATIONS", data);
  101. });
  102. },
  103. getSentFriendApplications({ commit }) {
  104. IMSDK.asyncApi(
  105. IMSDK.IMMethods.GetFriendApplicationListAsApplicant,
  106. uuidv4(),
  107. ).then(({ data }) => {
  108. commit("SET_SENT_FRIEND_APPLICATIONS", data);
  109. });
  110. },
  111. getRecvGroupApplications({ commit }) {
  112. IMSDK.asyncApi(
  113. IMSDK.IMMethods.GetGroupApplicationListAsRecipient,
  114. uuidv4(),
  115. ).then(({ data }) => {
  116. commit("SET_RECV_GROUP_APPLICATIONS", data);
  117. });
  118. },
  119. getSentGroupApplications({ commit }) {
  120. IMSDK.asyncApi(
  121. IMSDK.IMMethods.GetGroupApplicationListAsApplicant,
  122. uuidv4(),
  123. ).then(({ data }) => {
  124. commit("SET_SENT_GROUP_APPLICATIONS", data);
  125. });
  126. },
  127. pushNewFriend({ commit, state }, friendInfo) {
  128. const tmpList = [...state.friendList];
  129. const idx = tmpList.findIndex((item) => item.userID === friendInfo.userID);
  130. if (idx === -1) {
  131. commit("SET_FRIEND_LIST", [...tmpList, friendInfo]);
  132. }
  133. },
  134. updateFriendInfo({ commit, state }, { friendInfo, isRemove = false }) {
  135. const tmpList = [...state.friendList];
  136. const idx = tmpList.findIndex((item) => item.userID === friendInfo.userID);
  137. if (idx !== -1) {
  138. if (isRemove) {
  139. tmpList.splice(idx, 1);
  140. } else {
  141. tmpList[idx] = {
  142. ...friendInfo,
  143. };
  144. }
  145. commit("SET_FRIEND_LIST", tmpList);
  146. }
  147. },
  148. pushNewBlack({ commit, state }, blackInfo) {
  149. const tmpList = [...state.blackList];
  150. const idx = tmpList.findIndex((item) => item.userID === blackInfo.userID);
  151. if (idx === -1) {
  152. commit("SET_BLACK_LIST", [...tmpList, blackInfo]);
  153. }
  154. },
  155. updateBlackInfo({ commit, state }, { blackInfo, isRemove = false }) {
  156. const tmpList = [...state.blackList];
  157. const idx = tmpList.findIndex((item) => item.userID === blackInfo.userID);
  158. if (idx !== -1) {
  159. if (isRemove) {
  160. tmpList.splice(idx, 1);
  161. } else {
  162. tmpList[idx] = {
  163. ...blackInfo,
  164. };
  165. }
  166. commit("SET_BLACK_LIST", tmpList);
  167. }
  168. },
  169. pushNewGroup({ commit, state }, groupInfo) {
  170. const tmpList = [...state.groupList];
  171. const idx = tmpList.findIndex((item) => item.groupID === groupInfo.groupID);
  172. if (idx === -1) {
  173. commit("SET_GROUP_LIST", [...tmpList, groupInfo]);
  174. }
  175. },
  176. updateGroupInfo(
  177. { commit, state, rootState },
  178. { groupInfo, isRemove = false },
  179. ) {
  180. const tmpList = [...state.groupList];
  181. const idx = tmpList.findIndex((item) => item.groupID === groupInfo.groupID);
  182. if (rootState.conversation.currentGroup.groupID === groupInfo.groupID) {
  183. commit("conversation/SET_CURRENT_GROUP", groupInfo, { root: true });
  184. }
  185. if (idx !== -1) {
  186. if (isRemove) {
  187. tmpList.splice(idx, 1);
  188. } else {
  189. tmpList[idx] = {
  190. ...groupInfo,
  191. };
  192. }
  193. commit("SET_GROUP_LIST", tmpList);
  194. }
  195. },
  196. pushNewRecvFriendApplition({ commit, state }, application) {
  197. const tmpList = [...state.recvFriendApplications];
  198. const idx = tmpList.findIndex(
  199. (item) => item.fromUserID === application.fromUserID,
  200. );
  201. if (idx !== -1) {
  202. tmpList.splice(idx, 1);
  203. }
  204. commit("SET_RECV_FRIEND_APPLICATIONS", [...tmpList, application]);
  205. },
  206. updateRecvFriendApplition(
  207. { commit, state, rootState },
  208. { application, isRemove = false },
  209. ) {
  210. const tmpList = [...state.recvFriendApplications];
  211. const idx = tmpList.findIndex(
  212. (item) => item.fromUserID === application.fromUserID,
  213. );
  214. if (idx !== -1) {
  215. if (isRemove) {
  216. tmpList.splice(idx, 1);
  217. } else {
  218. tmpList[idx] = {
  219. ...application,
  220. };
  221. }
  222. commit("SET_RECV_FRIEND_APPLICATIONS", tmpList);
  223. }
  224. },
  225. pushNewSentFriendApplition({ commit, state }, application) {
  226. const tmpList = [...state.sentFriendApplications];
  227. const idx = tmpList.findIndex(
  228. (item) => item.toUserID === application.toUserID,
  229. );
  230. if (idx !== -1) {
  231. tmpList.splice(idx, 1);
  232. }
  233. commit("SET_SENT_FRIEND_APPLICATIONS", [...tmpList, application]);
  234. },
  235. updateSentFriendApplition(
  236. { commit, state, rootState },
  237. { application, isRemove = false },
  238. ) {
  239. const tmpList = [...state.sentFriendApplications];
  240. const idx = tmpList.findIndex(
  241. (item) => item.toUserID === application.toUserID,
  242. );
  243. if (idx !== -1) {
  244. if (isRemove) {
  245. tmpList.splice(idx, 1);
  246. } else {
  247. tmpList[idx] = {
  248. ...application,
  249. };
  250. }
  251. commit("SET_SENT_FRIEND_APPLICATIONS", tmpList);
  252. }
  253. },
  254. pushNewRecvGroupApplition({ commit, state }, application) {
  255. const tmpList = [...state.recvGroupApplications];
  256. const idx = tmpList.findIndex((item) => item.userID === application.userID);
  257. if (idx !== -1) {
  258. tmpList.splice(idx, 1);
  259. }
  260. commit("SET_RECV_GROUP_APPLICATIONS", [...tmpList, application]);
  261. },
  262. updateRecvGroupApplition(
  263. { commit, state, rootState },
  264. { application, isRemove = false },
  265. ) {
  266. const tmpList = [...state.recvGroupApplications];
  267. const idx = tmpList.findIndex((item) => item.userID === application.userID);
  268. if (idx !== -1) {
  269. if (isRemove) {
  270. tmpList.splice(idx, 1);
  271. } else {
  272. tmpList[idx] = {
  273. ...application,
  274. };
  275. }
  276. commit("SET_RECV_GROUP_APPLICATIONS", tmpList);
  277. }
  278. },
  279. pushNewSentGroupApplition({ commit, state }, application) {
  280. const tmpList = [...state.sentGroupApplications];
  281. const idx = tmpList.findIndex(
  282. (item) => item.groupID === application.groupID,
  283. );
  284. if (idx !== -1) {
  285. tmpList.splice(idx, 1);
  286. }
  287. commit("SET_SENT_GROUP_APPLICATIONS", [...tmpList, application]);
  288. },
  289. updateSentGroupApplition(
  290. { commit, state, rootState },
  291. { application, isRemove = false },
  292. ) {
  293. const tmpList = [...state.sentGroupApplications];
  294. const idx = tmpList.findIndex(
  295. (item) => item.groupID === application.groupID,
  296. );
  297. if (idx !== -1) {
  298. if (isRemove) {
  299. tmpList.splice(idx, 1);
  300. } else {
  301. tmpList[idx] = {
  302. ...application,
  303. };
  304. }
  305. commit("SET_SENT_GROUP_APPLICATIONS", tmpList);
  306. }
  307. },
  308. };
  309. export default {
  310. namespaced: true,
  311. state,
  312. mutations,
  313. actions,
  314. };