Group.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <template>
  2. <el-container class="group-page">
  3. <el-aside width="260px" class="aside" :class="{ fullscreen: configStore.fullScreen }">
  4. <div class="header">
  5. <el-input class="search-text" size="small" placeholder="搜索" v-model="searchText">
  6. <i class="el-icon-search el-input__icon" slot="prefix"> </i>
  7. </el-input>
  8. <el-button plain class="add-btn" icon="el-icon-plus" title="创建群聊" @click="onCreateGroup()"></el-button>
  9. </div>
  10. <el-scrollbar class="group-items">
  11. <div v-for="(groups, i) in groupValues" :key="i">
  12. <div class="letter">{{ groupKeys[i] }}</div>
  13. <div v-for="group in groups" :key="group.id">
  14. <group-item :group="group" :active="group.id == activeGroup.id"
  15. @click.native="onActiveItem(group)">
  16. </group-item>
  17. </div>
  18. <div v-if="i < groupValues.length - 1" class="divider"></div>
  19. </div>
  20. </el-scrollbar>
  21. </el-aside>
  22. <el-container class="container">
  23. <div class="header" v-show="activeGroup.id">{{ activeGroup.showGroupName }}({{ showMembers.length }})</div>
  24. <div class="container-box" v-show="activeGroup.id">
  25. <div class="group-info">
  26. <div>
  27. <file-upload v-show="isOwner" class="avatar-uploader" :action="imageAction" :showLoading="true"
  28. :maxSize="maxSize" @success="onUploadSuccess"
  29. :fileTypes="['image/jpeg', 'image/png', 'image/jpg', 'image/webp']">
  30. <img v-if="activeGroup.headImage" :src="activeGroup.headImage" class="avatar">
  31. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  32. </file-upload>
  33. <head-image v-show="!isOwner" class="avatar" :size="160" :url="activeGroup.headImage"
  34. :name="activeGroup.showGroupName" radius="10%" @click.native="showFullImage()">
  35. </head-image>
  36. <el-button class="send-btn" icon="el-icon-position" type="primary" @click="onSendMessage()">发消息
  37. </el-button>
  38. </div>
  39. <el-form class="form" label-width="130px" :model="activeGroup" :rules="rules" size="small"
  40. ref="groupForm">
  41. <el-form-item label="群聊名称" prop="name">
  42. <el-input v-model="activeGroup.name" :disabled="!isOwner" maxlength="20"></el-input>
  43. </el-form-item>
  44. <el-form-item label="群主">
  45. <el-input :value="ownerName" disabled></el-input>
  46. </el-form-item>
  47. <el-form-item label="群名备注">
  48. <el-input v-model="activeGroup.remarkGroupName" :placeholder="activeGroup.name"
  49. maxlength="20"></el-input>
  50. </el-form-item>
  51. <el-form-item label="我在本群的昵称">
  52. <el-input v-model="activeGroup.remarkNickName" maxlength="20"
  53. :placeholder="userStore.userInfo.nickName"></el-input>
  54. </el-form-item>
  55. <el-form-item label="群公告">
  56. <el-input v-model="activeGroup.notice" :disabled="!isOwner" type="textarea" :rows="3"
  57. maxlength="1024" placeholder="群主未设置"></el-input>
  58. </el-form-item>
  59. <div>
  60. <el-button type="warning" @click="onInvite()">邀请</el-button>
  61. <el-button type="success" @click="onSaveGroup()">保存</el-button>
  62. <el-button type="danger" v-show="!isOwner" @click="onQuit()">退出</el-button>
  63. <el-button type="danger" v-show="isOwner" @click="onDissolve()">解散</el-button>
  64. </div>
  65. </el-form>
  66. </div>
  67. <el-divider content-position="center"></el-divider>
  68. <el-scrollbar ref="scrollbar" :style="'height: ' + scrollHeight + 'px'">
  69. <div class="member-items">
  70. <div class="member-tools">
  71. <div class="tool-btn" title="邀请好友进群聊" @click="onInvite()">
  72. <i class="el-icon-plus"></i>
  73. </div>
  74. <div class="tool-text">邀请</div>
  75. <add-group-member ref="addGroupMember" :groupId="activeGroup.id" :members="groupMembers"
  76. @reload="loadGroupMembers"></add-group-member>
  77. </div>
  78. <div class="member-tools" v-if="isOwner">
  79. <div class="tool-btn" title="选择成员移出群聊" @click="onRemove()">
  80. <i class="el-icon-minus"></i>
  81. </div>
  82. <div class="tool-text">移除</div>
  83. <group-member-selector ref="removeSelector" title="选择成员进行移除" :group="activeGroup"
  84. @complete="onRemoveComplete"></group-member-selector>
  85. </div>
  86. <div v-for="(member, idx) in showMembers" :key="member.id">
  87. <group-member v-if="idx < showMaxIdx" class="member-item" :member="member"></group-member>
  88. </div>
  89. </div>
  90. </el-scrollbar>
  91. </div>
  92. </el-container>
  93. </el-container>
  94. </template>
  95. <script>
  96. import GroupItem from '../components/group/GroupItem';
  97. import FileUpload from '../components/common/FileUpload';
  98. import GroupMember from '../components/group/GroupMember.vue';
  99. import AddGroupMember from '../components/group/AddGroupMember.vue';
  100. import GroupMemberSelector from '../components/group/GroupMemberSelector.vue';
  101. import HeadImage from '../components/common/HeadImage.vue';
  102. import { pinyin } from 'pinyin-pro';
  103. export default {
  104. name: "group",
  105. components: {
  106. GroupItem,
  107. GroupMember,
  108. FileUpload,
  109. AddGroupMember,
  110. GroupMemberSelector,
  111. HeadImage
  112. },
  113. data() {
  114. return {
  115. searchText: "",
  116. maxSize: 5 * 1024 * 1024,
  117. activeGroup: {},
  118. groupMembers: [],
  119. showAddGroupMember: false,
  120. showMaxIdx: 150,
  121. rules: {
  122. name: [{
  123. required: true,
  124. message: '请输入群聊名称',
  125. trigger: 'blur'
  126. }]
  127. }
  128. };
  129. },
  130. methods: {
  131. onCreateGroup() {
  132. this.$prompt('请输入群聊名称', '创建群聊', {
  133. confirmButtonText: '确定',
  134. cancelButtonText: '取消',
  135. inputPattern: /\S/,
  136. inputErrorMessage: '请输入群聊名称'
  137. }).then(o => {
  138. let data = {
  139. name: o.value
  140. }
  141. this.$http({
  142. url: `/group/create?groupName=${o.value}`,
  143. method: 'post',
  144. data: data
  145. }).then((group) => {
  146. this.groupStore.addGroup(group);
  147. this.onActiveItem(group)
  148. this.$message.success('创建成功');
  149. })
  150. })
  151. },
  152. onActiveItem(group) {
  153. this.showMaxIdx = 150;
  154. // store数据不能直接修改,所以深拷贝一份内存
  155. this.activeGroup = JSON.parse(JSON.stringify(group));
  156. // 重新加载群成员
  157. this.groupMembers = [];
  158. this.loadGroupMembers();
  159. },
  160. onInvite() {
  161. this.$refs.addGroupMember.open();
  162. },
  163. onRemove() {
  164. // 群主不显示
  165. let hideIds = [this.activeGroup.ownerId];
  166. this.$refs.removeSelector.open(50, [], [], hideIds);
  167. },
  168. onRemoveComplete(members) {
  169. let userIds = members.map(m => m.userId);
  170. let data = {
  171. groupId: this.activeGroup.id,
  172. userIds: userIds
  173. }
  174. this.$http({
  175. url: "/group/members/remove",
  176. method: 'delete',
  177. data: data
  178. }).then(() => {
  179. this.loadGroupMembers();
  180. this.$message.success(`您移除了${userIds.length}位成员`);
  181. })
  182. },
  183. onUploadSuccess(data) {
  184. this.activeGroup.headImage = data.originUrl;
  185. this.activeGroup.headImageThumb = data.thumbUrl;
  186. },
  187. onSaveGroup() {
  188. this.$refs['groupForm'].validate((valid) => {
  189. if (valid) {
  190. let vo = this.activeGroup;
  191. this.$http({
  192. url: "/group/modify",
  193. method: "put",
  194. data: vo
  195. }).then((group) => {
  196. this.groupStore.updateGroup(group);
  197. this.$message.success("修改成功");
  198. })
  199. }
  200. });
  201. },
  202. onDissolve() {
  203. this.$confirm(`确认要解散'${this.activeGroup.name}'吗?`, '确认解散?', {
  204. confirmButtonText: '确定',
  205. cancelButtonText: '取消',
  206. type: 'warning'
  207. }).then(() => {
  208. this.$http({
  209. url: `/group/delete/${this.activeGroup.id}`,
  210. method: 'delete'
  211. }).then(() => {
  212. this.$message.success(`群聊'${this.activeGroup.name}'已解散`);
  213. this.groupStore.removeGroup(this.activeGroup.id);
  214. this.reset();
  215. });
  216. })
  217. },
  218. onQuit() {
  219. this.$confirm(`确认退出'${this.activeGroup.showGroupName}',并清空聊天记录吗?`, '确认退出?', {
  220. confirmButtonText: '确定',
  221. cancelButtonText: '取消',
  222. type: 'warning'
  223. }).then(() => {
  224. this.$http({
  225. url: `/group/quit/${this.activeGroup.id}`,
  226. method: 'delete'
  227. }).then(() => {
  228. this.$message.success(`您已退出'${this.activeGroup.name}'`);
  229. this.groupStore.removeGroup(this.activeGroup.id);
  230. this.chatStore.removeGroupChat(this.activeGroup.id);
  231. this.reset();
  232. });
  233. })
  234. },
  235. onSendMessage() {
  236. let chat = {
  237. type: 'GROUP',
  238. targetId: this.activeGroup.id,
  239. showName: this.activeGroup.showGroupName,
  240. headImage: this.activeGroup.headImageThumb,
  241. isDnd: this.activeGroup.isDnd
  242. };
  243. this.chatStore.openChat(chat);
  244. this.chatStore.setActiveChat(0);
  245. this.$router.push("/home/chat");
  246. },
  247. onScroll(e) {
  248. const scrollbar = e.target;
  249. // 滚到底部
  250. if (scrollbar.scrollTop + scrollbar.clientHeight >= scrollbar.scrollHeight - 30) {
  251. if (this.showMaxIdx < this.showMembers.length) {
  252. this.showMaxIdx += 50;
  253. }
  254. }
  255. },
  256. showFullImage() {
  257. if (this.activeGroup.headImage) {
  258. this.$eventBus.$emit("openFullImage", this.activeGroup.headImage);
  259. }
  260. },
  261. loadGroupMembers() {
  262. this.$http({
  263. url: `/group/members/${this.activeGroup.id}`,
  264. method: "get"
  265. }).then((members) => {
  266. this.groupMembers = members;
  267. })
  268. },
  269. reset() {
  270. this.activeGroup = {};
  271. this.groupMembers = [];
  272. },
  273. firstLetter(strText) {
  274. // 使用pinyin-pro库将中文转换为拼音
  275. let pinyinOptions = {
  276. toneType: 'none', // 无声调
  277. type: 'normal' // 普通拼音
  278. };
  279. let pyText = pinyin(strText, pinyinOptions);
  280. return pyText[0];
  281. },
  282. isEnglish(character) {
  283. return /^[A-Za-z]+$/.test(character);
  284. }
  285. },
  286. computed: {
  287. ownerName() {
  288. let member = this.groupMembers.find(m => m.userId == this.activeGroup.ownerId);
  289. return member && member.showNickName;
  290. },
  291. isOwner() {
  292. return this.activeGroup.ownerId == this.userStore.userInfo.id;
  293. },
  294. imageAction() {
  295. return `/image/upload?thumbSize=20`;
  296. },
  297. groupMap() {
  298. // 按首字母分组
  299. let map = new Map();
  300. this.groupStore.groups.forEach((g) => {
  301. if (g.quit || (this.searchText && !g.showGroupName.includes(this.searchText))) {
  302. return;
  303. }
  304. let letter = this.firstLetter(g.showGroupName).toUpperCase();
  305. // 非英文一律为#组
  306. if (!this.isEnglish(letter)) {
  307. letter = "#"
  308. }
  309. if (map.has(letter)) {
  310. map.get(letter).push(g);
  311. } else {
  312. map.set(letter, [g]);
  313. }
  314. })
  315. // 排序
  316. let arrayObj = Array.from(map);
  317. arrayObj.sort((a, b) => {
  318. // #组在最后面
  319. if (a[0] == '#' || b[0] == '#') {
  320. return b[0].localeCompare(a[0])
  321. }
  322. return a[0].localeCompare(b[0])
  323. })
  324. map = new Map(arrayObj.map(i => [i[0], i[1]]));
  325. return map;
  326. },
  327. groupKeys() {
  328. return Array.from(this.groupMap.keys());
  329. },
  330. groupValues() {
  331. return Array.from(this.groupMap.values());
  332. },
  333. showMembers() {
  334. return this.groupMembers.filter((m) => !m.quit)
  335. },
  336. scrollHeight() {
  337. return Math.min(300, 80 + this.showMembers.length / 10 * 80);
  338. }
  339. },
  340. mounted() {
  341. let scrollWrap = this.$refs.scrollbar.$el.querySelector('.el-scrollbar__wrap');
  342. scrollWrap.addEventListener('scroll', this.onScroll);
  343. }
  344. }
  345. </script>
  346. <style lang="scss" scoped>
  347. .group-page {
  348. .aside {
  349. display: flex;
  350. flex-direction: column;
  351. background: var(--im-background);
  352. border-right: 1px solid #eee;
  353. &.fullscreen {
  354. width: 260px !important;
  355. @media (min-width: 1200px) {
  356. width: 290px !important;
  357. }
  358. }
  359. .header {
  360. height: 50px;
  361. display: flex;
  362. align-items: center;
  363. padding: 0 8px;
  364. .add-btn {
  365. padding: 5px !important;
  366. margin: 5px;
  367. font-size: 16px;
  368. border-radius: 50%;
  369. }
  370. }
  371. .group-items {
  372. flex: 1;
  373. .letter {
  374. text-align: left;
  375. font-size: var(--im-larger-size-larger);
  376. padding: 5px 15px;
  377. color: var(--im-text-color-light);
  378. }
  379. }
  380. }
  381. .container {
  382. display: flex;
  383. flex-direction: column;
  384. .header {
  385. display: flex;
  386. justify-content: space-between;
  387. padding: 0 12px;
  388. line-height: 50px;
  389. font-size: var(--im-font-size-larger);
  390. border-bottom: var(--im-border);
  391. }
  392. .el-divider--horizontal {
  393. margin: 16px 0;
  394. }
  395. .container-box {
  396. overflow: auto;
  397. padding: 20px;
  398. flex: 1;
  399. .group-info {
  400. display: flex;
  401. padding: 5px 20px;
  402. .form {
  403. flex: 1;
  404. padding-left: 40px;
  405. max-width: 700px;
  406. }
  407. .avatar-uploader {
  408. --width: 160px;
  409. text-align: left;
  410. .el-upload {
  411. border: 1px dashed #d9d9d9 !important;
  412. border-radius: 6px;
  413. cursor: pointer;
  414. position: relative;
  415. overflow: hidden;
  416. }
  417. .el-upload:hover {
  418. border-color: #409EFF;
  419. }
  420. .avatar-uploader-icon {
  421. font-size: 28px;
  422. color: #8c939d;
  423. width: var(--width);
  424. height: var(--width);
  425. line-height: var(--width);
  426. text-align: center;
  427. }
  428. .avatar {
  429. width: var(--width);
  430. height: var(--width);
  431. display: block;
  432. }
  433. }
  434. .send-btn {
  435. margin-top: 12px;
  436. }
  437. }
  438. .member-items {
  439. padding: 0 12px;
  440. display: flex;
  441. align-items: center;
  442. flex-wrap: wrap;
  443. text-align: center;
  444. .member-item {
  445. margin-right: 5px;
  446. }
  447. .member-tools {
  448. display: flex;
  449. flex-direction: column;
  450. align-items: center;
  451. width: 60px;
  452. .tool-btn {
  453. width: 38px;
  454. height: 38px;
  455. line-height: 38px;
  456. border: var(--im-border);
  457. font-size: 14px;
  458. cursor: pointer;
  459. box-sizing: border-box;
  460. &:hover {
  461. border: #aaaaaa solid 1px;
  462. }
  463. }
  464. .tool-text {
  465. font-size: var(--im-font-size-smaller);
  466. text-align: center;
  467. width: 100%;
  468. height: 30px;
  469. line-height: 30px;
  470. white-space: nowrap;
  471. text-overflow: ellipsis;
  472. overflow: hidden
  473. }
  474. }
  475. }
  476. }
  477. }
  478. }
  479. </style>