index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <div class="container">
  3. <van-loading class="load-box" size="24px" v-if="loading">加载中...</van-loading>
  4. <template v-else>
  5. <van-index-bar class="list">
  6. <template v-for="(group, groupIndex) in memberGroups" :key="group.index">
  7. <van-index-anchor :index="group.index" />
  8. <van-checkbox-group v-model="checked">
  9. <van-cell-group inset>
  10. <van-cell
  11. v-for="(item, index) in group.members"
  12. :key="item.id"
  13. clickable
  14. @click="toggle(groupIndex, index, item)"
  15. >
  16. <template #icon>
  17. <van-checkbox checked-color="#4765DD"
  18. :name="item"
  19. :ref="el => setCheckboxRef(groupIndex, index, el)"
  20. :disabled="joinedIds.has(item.uuid)"
  21. @click.stop
  22. />
  23. </template>
  24. <template #title>
  25. <div class="cell-item">
  26. <van-image class="img-icon" round :src="item.avatar" />
  27. <div>{{ item.nickname }}</div>
  28. <span v-if="joinedIds.has(item.uuid)" class="joined-tag">已在群</span>
  29. </div>
  30. </template>
  31. </van-cell>
  32. </van-cell-group>
  33. </van-checkbox-group>
  34. </template>
  35. </van-index-bar>
  36. <div class="footer-box">
  37. <div class="avatar-list">
  38. <van-image
  39. v-for="item in checked"
  40. :key="item.id"
  41. class="img-icon"
  42. round
  43. :src="item.avatar"
  44. />
  45. </div>
  46. <div class="btn" :class="checked.length == 0?'':'active-btn'" @click="submit">完成({{ checked.length }})</div>
  47. </div>
  48. </template>
  49. </div>
  50. </template>
  51. <script setup>
  52. import { userFriendList,createSetGroup,groupJoin } from "@/api/path/im.api";
  53. import {getFirstLetter} from "@/utils/utils";
  54. import { useWalletStore } from "@/stores/modules/walletStore";
  55. import { useWebSocketStore } from "@/stores/modules/webSocketStore.js";
  56. import { showToast } from "vant";
  57. import * as MsgType from "@/common/constant/msgType";
  58. import { useSystemStore } from "@/stores/modules/systemStore";
  59. const IM_PATH = import.meta.env.VITE_IM_PATH_FIlE;
  60. const router = useRouter();
  61. const route = useRoute();
  62. const walletStore = useWalletStore();
  63. const wsStore = useWebSocketStore();
  64. const systemStore = useSystemStore();
  65. const checked = ref([]);
  66. const memberGroups = ref([]);
  67. const loading = ref(false);
  68. // 已加入群的成员数组
  69. const dataArr = route.query.arr ? JSON.parse(route.query.arr) : [];
  70. // 转成 Set,快速判断
  71. const joinedIds = new Set(dataArr.map(m => m.uuid));
  72. const getuserList = async () => {
  73. loading.value = true;
  74. try {
  75. const res = await userFriendList({ uuid: walletStore.account });
  76. const groupedMap = {};
  77. res.data?.forEach(user => {
  78. const name = user.nickname || '';
  79. user.avatar = user.avatar ? IM_PATH + user.avatar : user.avatar
  80. const firstLetter = getFirstLetter(name); // 获取首字母
  81. if (!groupedMap[firstLetter]) {
  82. groupedMap[firstLetter] = [];
  83. }
  84. groupedMap[firstLetter].push({...user});
  85. });
  86. // 转为数组并按 index 排序
  87. const groupedList = Object.keys(groupedMap)
  88. .sort()
  89. .map(letter => ({
  90. index: letter,
  91. members: groupedMap[letter]
  92. }));
  93. memberGroups.value = groupedList;
  94. } finally {
  95. loading.value = false;
  96. }
  97. };
  98. const checkboxRefs = ref([]);
  99. onBeforeUpdate(() => {
  100. checkboxRefs.value = [];
  101. });
  102. const setCheckboxRef = (groupIndex, index, el) => {
  103. if (!checkboxRefs.value[groupIndex]) {
  104. checkboxRefs.value[groupIndex] = [];
  105. }
  106. checkboxRefs.value[groupIndex][index] = el;
  107. };
  108. // 切换选中
  109. const toggle = (groupIndex, index, item) => {
  110. if (joinedIds.has(item.uuid)) {
  111. return; // 已在群的不触发切换
  112. }
  113. checkboxRefs.value[groupIndex]?.[index]?.toggle();
  114. };
  115. const submit = async () => {
  116. if(checked.value.length == 0) return;
  117. const ids = checked.value.map(item => item.id);//创建群需要id
  118. const uname = checked.value.map(item => item.nickname).join('、');
  119. const uuids = checked.value.map(item => item.uuid);//添加成员需要uuid
  120. if(route.name == 'addMember'){
  121. // 邀请添加成员加入群组
  122. try {
  123. const res = await groupJoin(uuids,wsStore.toUserInfo.uuid);
  124. if(res.code == 200){
  125. showToast('加入成功');
  126. const message = {
  127. content: `${uname}加入了群聊`,
  128. contentType: MsgType.MSG_TYPE.TEXT,
  129. messageType: MsgType.INVITATION_GROUP,
  130. };
  131. wsStore.sendMessage(message);
  132. // systemStore.needRefreshIm = true;
  133. // router.push('im')
  134. // router.push({
  135. // path: 'detail',
  136. // query:{ status:wsStore.toUserInfo.type == 'user'?1:2 }
  137. // }) // 1:单聊 2:群聊
  138. router.push({
  139. path: 'chat',
  140. query:{ uuid:wsStore.toUserInfo.uuid }
  141. })
  142. }else{
  143. showToast('加入失败')
  144. }
  145. } catch (error) {
  146. showToast('加入失败')
  147. }
  148. }else{
  149. // 创建群
  150. try {
  151. const res = await createSetGroup({
  152. uuid:walletStore.account,
  153. userIds:ids
  154. });
  155. wsStore.toUserInfo = res.data;
  156. const message = {
  157. content: `${walletStore.username}创建了群聊`,
  158. contentType: MsgType.MSG_TYPE.TEXT,
  159. messageType: MsgType.CREATE_GROUP
  160. };
  161. wsStore.sendMessage(message);
  162. if(res.code == 200){
  163. showToast('创建成功')
  164. // systemStore.needRefreshIm = true;
  165. router.push('im')
  166. }else{
  167. showToast('创建失败')
  168. }
  169. } catch (error) {
  170. showToast('创建失败')
  171. }
  172. }
  173. };
  174. onMounted(() => {
  175. getuserList();
  176. });
  177. </script>
  178. <style lang="less" scoped>
  179. .load-box{
  180. text-align: center !important;
  181. margin-top: 50px !important;
  182. }
  183. .container{
  184. display: flex;
  185. flex-direction: column;
  186. height: 100%;
  187. }
  188. .list{
  189. flex: 1;
  190. overflow: auto;
  191. :deep(.van-cell-group--inset){
  192. border-radius:12px !important;
  193. }
  194. :deep(.van-cell){
  195. padding: 12px 16px !important;
  196. }
  197. :deep(.van-cell:after){
  198. left: 90px;
  199. }
  200. :deep(.van-index-anchor--sticky){
  201. background: #F7F8FA !important;
  202. color: #4765DD !important;
  203. }
  204. :deep(.van-index-bar__index){
  205. font-weight: 400 !important;
  206. font-size: 10px !important;
  207. color: #1D2129 !important;
  208. margin-bottom: 8px !important;
  209. }
  210. :deep(.van-index-bar__index--active){
  211. color: #4765DD !important;
  212. }
  213. .cell-item{
  214. display: flex;
  215. align-items: center;
  216. margin-left: 12px;
  217. font-family: PingFang SC, PingFang SC;
  218. font-weight: 500;
  219. font-size: 15px;
  220. color: #000000;
  221. .img-icon{
  222. width: 32px;
  223. height: 32px;
  224. flex-shrink: 0;
  225. margin-right: 12px;
  226. }
  227. .joined-tag {
  228. margin-left: 8px;
  229. font-size: 12px;
  230. color: #999;
  231. }
  232. }
  233. }
  234. .list::-webkit-scrollbar{
  235. width: 0;
  236. }
  237. .footer-box {
  238. display: flex;
  239. justify-content: space-between;
  240. align-items: center;
  241. margin-top: 16px;
  242. height: 54px;
  243. background: #ffffff;
  244. padding: 0 16px;
  245. box-sizing: border-box;
  246. font-family: PingFang SC, PingFang SC;
  247. font-weight: 400;
  248. font-size: 15px;
  249. color: #8d8d8d;
  250. .avatar-list {
  251. display: flex;
  252. overflow-x: auto;
  253. flex: 1;
  254. .img-icon {
  255. width: 32px;
  256. height: 32px;
  257. flex-shrink: 0;
  258. margin-right: 4px;
  259. }
  260. }
  261. .avatar-list::-webkit-scrollbar{
  262. height: 0;
  263. }
  264. .btn{
  265. margin-left: 16px;
  266. height: 25px;
  267. line-height: 25px;
  268. padding: 0 6px;
  269. background: #F2F2F2;
  270. border-radius: 4px;
  271. box-sizing: border-box;
  272. font-family: PingFang SC, PingFang SC;
  273. font-weight: 400;
  274. font-size: 12px;
  275. color: #8D8D8D;
  276. }
  277. .active-btn{
  278. background: #4765DD;
  279. color: #FFFFFF;
  280. }
  281. }
  282. </style>