contact.js 9.0 KB

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