Group.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <template>
  2. <el-container class="group-page">
  3. <el-aside width="260px" class="aside">
  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%">
  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="$store.state.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 userInfo = this.$store.state.userStore.userInfo;
  139. let data = {
  140. name: o.value
  141. }
  142. this.$http({
  143. url: `/group/create?groupName=${o.value}`,
  144. method: 'post',
  145. data: data
  146. }).then((group) => {
  147. this.$store.commit("addGroup", group);
  148. })
  149. })
  150. },
  151. onActiveItem(group) {
  152. this.showMaxIdx = 150;
  153. // store数据不能直接修改,所以深拷贝一份内存
  154. this.activeGroup = JSON.parse(JSON.stringify(group));
  155. // 重新加载群成员
  156. this.groupMembers = [];
  157. this.loadGroupMembers();
  158. },
  159. onInvite() {
  160. this.$refs.addGroupMember.open();
  161. },
  162. onRemove() {
  163. // 群主不显示
  164. let hideIds = [this.activeGroup.ownerId];
  165. this.$refs.removeSelector.open(50, [], [], hideIds);
  166. },
  167. onRemoveComplete(members) {
  168. let userIds = members.map(m => m.userId);
  169. let data = {
  170. groupId: this.activeGroup.id,
  171. userIds: userIds
  172. }
  173. this.$http({
  174. url: "/group/members/remove",
  175. method: 'delete',
  176. data: data
  177. }).then(() => {
  178. this.loadGroupMembers();
  179. this.$message.success(`您移除了${userIds.length}位成员`);
  180. })
  181. },
  182. onUploadSuccess(data) {
  183. this.activeGroup.headImage = data.originUrl;
  184. this.activeGroup.headImageThumb = data.thumbUrl;
  185. },
  186. onSaveGroup() {
  187. this.$refs['groupForm'].validate((valid) => {
  188. if (valid) {
  189. let vo = this.activeGroup;
  190. this.$http({
  191. url: "/group/modify",
  192. method: "put",
  193. data: vo
  194. }).then((group) => {
  195. this.$store.commit("updateGroup", group);
  196. this.$message.success("修改成功");
  197. })
  198. }
  199. });
  200. },
  201. onDissolve() {
  202. this.$confirm(`确认要解散'${this.activeGroup.name}'吗?`, '确认解散?', {
  203. confirmButtonText: '确定',
  204. cancelButtonText: '取消',
  205. type: 'warning'
  206. }).then(() => {
  207. this.$http({
  208. url: `/group/delete/${this.activeGroup.id}`,
  209. method: 'delete'
  210. }).then(() => {
  211. this.$message.success(`群聊'${this.activeGroup.name}'已解散`);
  212. this.$store.commit("removeGroup", this.activeGroup.id);
  213. this.reset();
  214. });
  215. })
  216. },
  217. onQuit() {
  218. this.$confirm(`确认退出'${this.activeGroup.showGroupName}',并清空聊天记录吗?`, '确认退出?', {
  219. confirmButtonText: '确定',
  220. cancelButtonText: '取消',
  221. type: 'warning'
  222. }).then(() => {
  223. this.$http({
  224. url: `/group/quit/${this.activeGroup.id}`,
  225. method: 'delete'
  226. }).then(() => {
  227. this.$message.success(`您已退出'${this.activeGroup.name}'`);
  228. this.$store.commit("removeGroup", this.activeGroup.id);
  229. this.$store.commit("removeGroupChat", this.activeGroup.id);
  230. this.reset();
  231. });
  232. })
  233. },
  234. onSendMessage() {
  235. let chat = {
  236. type: 'GROUP',
  237. targetId: this.activeGroup.id,
  238. showName: this.activeGroup.showGroupName,
  239. headImage: this.activeGroup.headImage,
  240. };
  241. this.$store.commit("openChat", chat);
  242. this.$store.commit("activeChat", 0);
  243. this.$router.push("/home/chat");
  244. },
  245. onScroll(e) {
  246. const scrollbar = e.target;
  247. // 滚到底部
  248. if (scrollbar.scrollTop + scrollbar.clientHeight >= scrollbar.scrollHeight - 30) {
  249. if (this.showMaxIdx < this.showMembers.length) {
  250. this.showMaxIdx += 50;
  251. }
  252. }
  253. },
  254. loadGroupMembers() {
  255. this.$http({
  256. url: `/group/members/${this.activeGroup.id}`,
  257. method: "get"
  258. }).then((members) => {
  259. this.groupMembers = members;
  260. })
  261. },
  262. reset() {
  263. this.activeGroup = {};
  264. this.groupMembers = [];
  265. },
  266. firstLetter(strText) {
  267. // 使用pinyin-pro库将中文转换为拼音
  268. let pinyinOptions = {
  269. toneType: 'none', // 无声调
  270. type: 'normal' // 普通拼音
  271. };
  272. let pyText = pinyin(strText, pinyinOptions);
  273. return pyText[0];
  274. },
  275. isEnglish(character) {
  276. return /^[A-Za-z]+$/.test(character);
  277. }
  278. },
  279. computed: {
  280. groupStore() {
  281. return this.$store.state.groupStore;
  282. },
  283. ownerName() {
  284. let member = this.groupMembers.find((m) => m.userId == this.activeGroup.ownerId);
  285. return member && member.showNickName;
  286. },
  287. isOwner() {
  288. return this.activeGroup.ownerId == this.$store.state.userStore.userInfo.id;
  289. },
  290. imageAction() {
  291. return `/image/upload`;
  292. },
  293. groupMap() {
  294. // 按首字母分组
  295. let map = new Map();
  296. this.groupStore.groups.forEach((g) => {
  297. if (g.quit || (this.searchText && !g.showGroupName.includes(this.searchText))) {
  298. return;
  299. }
  300. let letter = this.firstLetter(g.showGroupName).toUpperCase();
  301. // 非英文一律为#组
  302. if (!this.isEnglish(letter)) {
  303. letter = "#"
  304. }
  305. if (map.has(letter)) {
  306. map.get(letter).push(g);
  307. } else {
  308. map.set(letter, [g]);
  309. }
  310. })
  311. // 排序
  312. let arrayObj = Array.from(map);
  313. arrayObj.sort((a, b) => {
  314. // #组在最后面
  315. if (a[0] == '#' || b[0] == '#') {
  316. return b[0].localeCompare(a[0])
  317. }
  318. return a[0].localeCompare(b[0])
  319. })
  320. map = new Map(arrayObj.map(i => [i[0], i[1]]));
  321. return map;
  322. },
  323. groupKeys() {
  324. return Array.from(this.groupMap.keys());
  325. },
  326. groupValues() {
  327. return Array.from(this.groupMap.values());
  328. },
  329. showMembers() {
  330. return this.groupMembers.filter((m) => !m.quit)
  331. },
  332. scrollHeight() {
  333. return Math.min(300, 80 + this.showMembers.length / 10 * 80);
  334. }
  335. },
  336. mounted() {
  337. let scrollWrap = this.$refs.scrollbar.$el.querySelector('.el-scrollbar__wrap');
  338. scrollWrap.addEventListener('scroll', this.onScroll);
  339. }
  340. }
  341. </script>
  342. <style lang="scss" scoped>
  343. .group-page {
  344. .aside {
  345. display: flex;
  346. flex-direction: column;
  347. background: var(--im-background);
  348. .header {
  349. height: 50px;
  350. display: flex;
  351. align-items: center;
  352. padding: 0 8px;
  353. .add-btn {
  354. padding: 5px !important;
  355. margin: 5px;
  356. font-size: 16px;
  357. border-radius: 50%;
  358. }
  359. }
  360. .group-items {
  361. flex: 1;
  362. .letter {
  363. text-align: left;
  364. font-size: var(--im-larger-size-larger);
  365. padding: 5px 15px;
  366. color: var(--im-text-color-light);
  367. }
  368. }
  369. }
  370. .container {
  371. display: flex;
  372. flex-direction: column;
  373. .header {
  374. display: flex;
  375. justify-content: space-between;
  376. padding: 0 12px;
  377. line-height: 50px;
  378. font-size: var(--im-font-size-larger);
  379. border-bottom: var(--im-border);
  380. }
  381. .el-divider--horizontal {
  382. margin: 16px 0;
  383. }
  384. .container-box {
  385. overflow: auto;
  386. padding: 20px;
  387. flex: 1;
  388. .group-info {
  389. display: flex;
  390. padding: 5px 20px;
  391. .form {
  392. flex: 1;
  393. padding-left: 40px;
  394. max-width: 700px;
  395. }
  396. .avatar-uploader {
  397. --width: 160px;
  398. text-align: left;
  399. .el-upload {
  400. border: 1px dashed #d9d9d9 !important;
  401. border-radius: 6px;
  402. cursor: pointer;
  403. position: relative;
  404. overflow: hidden;
  405. }
  406. .el-upload:hover {
  407. border-color: #409EFF;
  408. }
  409. .avatar-uploader-icon {
  410. font-size: 28px;
  411. color: #8c939d;
  412. width: var(--width);
  413. height: var(--width);
  414. line-height: var(--width);
  415. text-align: center;
  416. }
  417. .avatar {
  418. width: var(--width);
  419. height: var(--width);
  420. display: block;
  421. }
  422. }
  423. .send-btn {
  424. margin-top: 12px;
  425. }
  426. }
  427. .member-items {
  428. padding: 0 12px;
  429. display: flex;
  430. align-items: center;
  431. flex-wrap: wrap;
  432. text-align: center;
  433. .member-item {
  434. margin-right: 5px;
  435. }
  436. .member-tools {
  437. display: flex;
  438. flex-direction: column;
  439. align-items: center;
  440. width: 60px;
  441. .tool-btn {
  442. width: 38px;
  443. height: 38px;
  444. line-height: 38px;
  445. border: var(--im-border);
  446. font-size: 14px;
  447. cursor: pointer;
  448. box-sizing: border-box;
  449. &:hover {
  450. border: #aaaaaa solid 1px;
  451. }
  452. }
  453. .tool-text {
  454. font-size: var(--im-font-size-smaller);
  455. text-align: center;
  456. width: 100%;
  457. height: 30px;
  458. line-height: 30px;
  459. white-space: nowrap;
  460. text-overflow: ellipsis;
  461. overflow: hidden
  462. }
  463. }
  464. }
  465. }
  466. }
  467. }
  468. </style>