group.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. import API from '@/api'
  2. import { mapState, mapActions, mapMutations } from 'vuex'
  3. import { confirmPopup, getMeechatType, getAvatarBgColor } from '@/util/util'
  4. import { Message, MessageBox } from 'element-ui'
  5. import { searchGroupUserMixin } from '@/mixins'
  6. import _ from 'lodash'
  7. export const groupSetMixins = {
  8. computed: {
  9. ...mapState({
  10. myId: state => state.userId,
  11. group: state => state.group,
  12. curGroupId: state => state.curSession,
  13. members: state => state.group.members,
  14. sessionInfo: state => state.group.sessionInfo,
  15. shareName: state => state.group.shareName,
  16. adminList: state => state.group.adminList,
  17. membersArray: state => state.group.membersArray
  18. }),
  19. isAdmin () {
  20. return this.members && this.members[this.myId] && this.members[this.myId].is_admin == 1
  21. },
  22. isCreator () {
  23. return this.group.creator == this.myId
  24. },
  25. isPrivate () {
  26. return this.$store.getters.isPrivate
  27. }
  28. },
  29. mounted () {
  30. let groupId = this.$route.params.id
  31. this.changeSessionId(groupId)
  32. if (!this.isPrivate && !this.group.groupId) {
  33. this.initGroup({
  34. userId: this.myId,
  35. groupId: groupId,
  36. useCache: false
  37. })
  38. this.getGroupInfo()
  39. }
  40. document.addEventListener('click', () => {
  41. if (!this.$refs.chatSet) return
  42. let classNames = this.$refs.chatSet.getAttribute('class')
  43. if (classNames.indexOf('move-left') > -1) {
  44. this.$emit('showCharSet', 0)
  45. }
  46. })
  47. },
  48. data () {
  49. return {
  50. msgPush: false, // 消失免打扰
  51. msgTop: false, // 置顶聊天
  52. limitHeight: true, // 展开全部
  53. isEdit: false, // 编辑群名
  54. newGroupName: '', // 群名
  55. newLink: '', // 新连接
  56. newNotice: '', // 新群名
  57. isEditLink: false, // 编辑链接
  58. editNotice: false,
  59. editShareName: '',
  60. sharePath: `${location.origin}/s/`,
  61. isSearch: false,
  62. searchList: [], // 搜索结果列表
  63. searchTxt: '', // 搜索内容
  64. meechatType: getMeechatType()// meechat版本
  65. }
  66. },
  67. methods: {
  68. ...mapMutations(['initGroup', 'changeSessionId', 'changeHotGroupStatus', 'removeSessionListById']),
  69. ...mapActions([
  70. 'updateSessionItem',
  71. 'getGroupInfo'
  72. ]),
  73. // 群标题
  74. handleTitleBlur () {
  75. this.isEdit = false
  76. if (this.newGroupName.length) {
  77. API.group.changeTitle({
  78. group_id: this.curGroupId,
  79. title: this.newGroupName
  80. }).then(({ data }) => {
  81. // 更新侧边栏
  82. this.updateSessionItem({
  83. sessionId: this.curGroupId,
  84. data: {
  85. name: this.newGroupName
  86. }
  87. })
  88. // 更新群组数据
  89. this.$store.commit('updateGroup', {
  90. key: 'groupName',
  91. data: this.newGroupName
  92. })
  93. })
  94. }
  95. },
  96. handleTitleFocus () {
  97. this.isEdit = true
  98. this.newGroupName = this.group.groupName
  99. },
  100. handleShowAll () {
  101. if (!this.limitHeight) {
  102. this.$refs.scrollWrap.scrollTop = 0
  103. }
  104. this.limitHeight = !this.limitHeight
  105. },
  106. // 群链接
  107. async handleLinkBlur () {
  108. this.isEditLink = false
  109. if (this.editShareName.match(/^\d{1,}$/)) {
  110. this.$showTips('分享链接不能是纯数字')
  111. return
  112. }
  113. if (!this.editShareName.match(/^(\w|.){1,}$/)) {
  114. this.$showTips('分享链接只能是英文字母,数字和 . 的组合')
  115. return
  116. }
  117. await confirmPopup(`分享链接只能被修改一次,确认修改为:${this.sharePath + this.editShareName}`, '提示')
  118. .catch(e => {})
  119. API.group.changeName({
  120. group_id: this.curGroupId,
  121. name: this.editShareName
  122. }).then(() => {
  123. this.$store.commit('updateGroup', {
  124. key: 'shareName',
  125. data: this.editShareName
  126. })
  127. })
  128. },
  129. handleLinkFocus () {
  130. this.isEditLink = true
  131. this.editShareName = this.shareName
  132. // this.newLink = this.group.inviteUrl
  133. },
  134. // 群公告
  135. handleNotice () {
  136. API.group.changeNotice({
  137. group_id: this.curGroupId,
  138. notice: this.newNotice
  139. }).then(() => {
  140. this.$showTips('修改成功')
  141. this.editNotice = false
  142. this.$store.commit('updateGroup', {
  143. key: 'groupNotice',
  144. data: this.newNotice
  145. })
  146. })
  147. },
  148. // 群管理
  149. handleGroudMgr () {
  150. this.$emit('handleShowGroudMgr', 1)
  151. },
  152. handleOpenNotice () {
  153. this.editNotice = true
  154. this.newNotice = this.group.groupNotice
  155. },
  156. copyLink (link) {
  157. let url = document.location.protocol + link
  158. this.$copyText(url).then((e) => {
  159. this.$showTips('复制成功')
  160. })
  161. },
  162. /**
  163. * @des 置顶聊天相关操作
  164. * @param {String} val 置顶操作Flag开关{1: 置顶, 0: 取消置顶}
  165. */
  166. async changePin (val) {
  167. let opt = val == 1 ? 'setPin' : 'cancelPin'
  168. let optRes = val == 1 ? 'updateSessionListByPin' : 'cancelSessionListByPin'
  169. let res = await API.session[opt]({
  170. session_id: this.curGroupId
  171. })
  172. let data = res.data.data
  173. this.$store.commit('updatePin', val)
  174. // 更新侧边栏的顺序
  175. this.$store.commit(optRes, {
  176. session_id: this.curGroupId,
  177. is_pin: data.is_pin,
  178. pin_time_int: data.pin_time_int
  179. })
  180. },
  181. /**
  182. * @des 消息免打扰
  183. * @param {String} val 消息免打扰Flag开关{1: 免打扰, 0: 取消免打扰}
  184. */
  185. async changeMute (val) {
  186. let opt = val == 1 ? 'setMute' : 'cancelMute'
  187. let optRes = val == 1 ? 'updateSessionListByMute' : 'cancelSessionListByMute'
  188. await API.session[opt]({
  189. session_id: this.curGroupId
  190. })
  191. this.$store.commit('updateMute', val)
  192. // 更新侧边栏的顺序
  193. this.$store.commit(optRes, this.curGroupId)
  194. },
  195. // 退出群组
  196. leaveGroup () {
  197. confirmPopup(this.$t('group.leaveComfirm')).then(() => {
  198. this.$emit('showLoadingRoom', true)
  199. API.group.leaveGroup({
  200. group_id: this.curGroupId
  201. }).then(() => {
  202. this.changeHotGroupStatus({
  203. groupId: this.curGroupId,
  204. isJoin: 0
  205. })
  206. this.removeSessionListById(this.curGroupId)
  207. this.$showTips(this.$t('group.leaveTip'))
  208. this.$emit('showLoadingRoom', false)
  209. this.$store.commit('updateJoin', false)
  210. if (this.meechatType == 'h5') this.$router.go(-2)
  211. }).catch(() => {
  212. this.$emit('showLoadingRoom', false)
  213. })
  214. })
  215. },
  216. searchUser () {
  217. if (!this.searchTxt.trim()) return
  218. if (this.membersArray.length < this.group.membersNum) {
  219. API.group.searchMember({
  220. group_id: this.group.groupId,
  221. keyword: this.searchTxt
  222. }).then(({ data }) => {
  223. this.searchList = data.data
  224. })
  225. } else {
  226. let val = this.searchTxt
  227. this.searchList = this.membersArray.filter(item => {
  228. let inName, inNick
  229. inName = item.user_name ? item.user_name.indexOf(val) > -1 : false
  230. inNick = item.nick_name ? item.nick_name.indexOf(val) > -1 : false
  231. return inName || inNick
  232. })
  233. }
  234. }
  235. }
  236. }
  237. export const groupInviteMixins = {
  238. mixins: [searchGroupUserMixin],
  239. data () {
  240. return {
  241. curItemIndex: 0,
  242. checkList: [],
  243. groupName: '',
  244. isLoading: false,
  245. showNum: 0,
  246. meechatType: getMeechatType()// meechat版本
  247. }
  248. },
  249. computed: {
  250. checkedNum () {
  251. return (this.checkList.filter(v => {
  252. return v.isChecked
  253. })).length
  254. },
  255. ...mapState({
  256. userId: state => state.userId,
  257. friendList: state => state.chat.friendList,
  258. groupId: state => state.group.groupId,
  259. group: state => state.group,
  260. members: state => state.group.members,
  261. membersArray: state => state.group.membersArray,
  262. membersNum: state => state.group.membersNum,
  263. adminList: state => state.group.adminList
  264. }),
  265. inviteList () {
  266. return this.checkList.filter(v => {
  267. return v.isChecked
  268. }).map(v => v.user_id).join(',')
  269. },
  270. resultList () {
  271. return (this.checkList && this.checkList.filter(v => {
  272. return v.isChecked
  273. })) || []
  274. }
  275. },
  276. methods: {
  277. ...mapActions(['getGroupInfo']),
  278. ...mapMutations([
  279. 'initGroup',
  280. 'changeSessionId'
  281. ]),
  282. getItemByUid (uid) {
  283. return this.checkList.filter((item, index) => {
  284. if (item.user_id == uid) this.curItemIndex = index
  285. return item.user_id == uid
  286. })[0]
  287. },
  288. changeState (uid, flag = true) {
  289. let item = this.getItemByUid(uid)
  290. if (item.isChoosed) return
  291. // 群管理设限
  292. if (
  293. this.inviteType == 4 &&
  294. flag &&
  295. this.adminList.length + this.checkedNum > 5
  296. ) {
  297. this.$showTips(this.$t('group.groupMgrMostTips'))
  298. return
  299. }
  300. // 转让群主
  301. if (this.inviteType == 5) {
  302. this.checkList.forEach(item => {
  303. item.isChecked = false
  304. })
  305. }
  306. item['isChecked'] = flag
  307. this.$set(this.checkList, this.curItemIndex, item)
  308. },
  309. changeStateForH5 (uid) {
  310. let item = this.getItemByUid(uid)
  311. let flag = !item.isChecked
  312. // 群管理设限
  313. if (this.inviteType == 4 && flag && this.adminList.length + this.checkedNum > 5) {
  314. this.$showTips(this.$t('group.groupMgrMostTips'))
  315. return
  316. }
  317. // 转让群主
  318. if (this.inviteType == 5) {
  319. this.checkList.forEach(item => {
  320. item.isChecked = false
  321. })
  322. }
  323. item['isChecked'] = flag
  324. this.$set(this.checkList, this.curItemIndex, item)
  325. },
  326. async initList () {
  327. switch (this.inviteType) {
  328. // 1建群/2邀请好友进群
  329. case 1:
  330. case 2:
  331. await this.$store.dispatch('getFriendList')
  332. this.checkList = this.friendList
  333. break
  334. // 3删除群成员/4添加群管理/5转让群主
  335. case 3:
  336. case 4:
  337. case 5:
  338. this.checkList = this.membersArray
  339. break
  340. }
  341. this.checkList = _.filter(this.checkList, item => {
  342. item.isChecked = false
  343. item.isChoosed = false
  344. item.isShow = true
  345. // 邀请好友进群-特殊处理
  346. if (this.inviteType == 2) {
  347. // 判断是否已经在群
  348. let isInGroup = this.membersArray.some((n) => {
  349. return n.user_id == item.user_id
  350. })
  351. return !isInGroup
  352. }
  353. // 群管理-特殊处理
  354. if (this.inviteType == 4 && item.is_admin == 1) {
  355. item.isChoosed = true
  356. }
  357. return item.user_id != this.userId
  358. })
  359. this.showNum = this.checkList.length
  360. },
  361. optSubmit () {
  362. switch (this.inviteType) {
  363. // 建群
  364. case 1:
  365. this.createGroup()
  366. break
  367. // 邀请好友进群
  368. case 2:
  369. this.invitesMember()
  370. break
  371. // 删除群成员
  372. case 3:
  373. this.removesMember()
  374. break
  375. // 添加群管理
  376. case 4:
  377. this.addAdmin()
  378. break
  379. // 群主转让
  380. case 5:
  381. this.changeCreator()
  382. break
  383. // 创建新群并且同步telegram
  384. case 6:
  385. this.createGroup()
  386. break
  387. }
  388. },
  389. // 建群
  390. createGroup () {
  391. if (this.groupName.length > 16) {
  392. this.$showTips(this.$t('group.groupNameLengthTips'))
  393. return
  394. }
  395. if (this.groupName) {
  396. this.isLoading = true
  397. API.group
  398. .createGroup({
  399. group_title: this.groupName,
  400. user_id_list: this.inviteList
  401. })
  402. .then(({ data }) => {
  403. let groupId = data.data.session_id
  404. this.$showTips(this.$t('group.createSuccess'))
  405. this.$store.commit('addSessionItem', data.data)
  406. if (this.ext && this.ext.tgGroupId) {
  407. // 同步tele
  408. this.doSyncTelegram(groupId)
  409. } else {
  410. this.$router.push(`/group/${groupId}`)
  411. }
  412. if (this.meechatType != 'h5') this.hidePopup()
  413. })
  414. } else {
  415. Message({
  416. message: this.$t('group.groupNameTips'),
  417. type: 'warning'
  418. })
  419. }
  420. },
  421. // 同步tele
  422. doSyncTelegram (groupId) {
  423. API.tg.doSync({
  424. group_id: groupId,
  425. tg_group_id: this.ext.tgGroupId
  426. }).then(() => {
  427. this.$router.replace(`/group/${groupId}`)
  428. }).catch(error => {
  429. console.log(error)
  430. if (this.meechatType == 'h5') this.$router.replace('/')
  431. })
  432. },
  433. // 邀请成员加入
  434. async invitesMember () {
  435. this.isLoading = true
  436. await API.group.invites({
  437. user_ids: this.inviteList,
  438. group_id: this.groupId
  439. })
  440. await this.getGroupInfo()
  441. if (this.meechatType == 'h5') this.$router.go(-1)
  442. else this.hidePopup()
  443. },
  444. // 删除成员
  445. async removesMember () {
  446. this.isLoading = true
  447. await API.group.removes({
  448. user_ids: this.inviteList,
  449. group_id: this.groupId
  450. })
  451. await this.getGroupInfo()
  452. if (this.meechatType == 'h5') this.$router.go(-1)
  453. else this.hidePopup()
  454. },
  455. // 添加群管理
  456. addAdmin () {
  457. this.isLoading = true
  458. API.group
  459. .addAdmin({
  460. user_ids: this.inviteList,
  461. group_id: this.groupId
  462. })
  463. .then(({ data }) => {
  464. this.getGroupInfo()
  465. if (this.meechatType == 'h5') this.$router.go(-1)
  466. else this.hidePopup()
  467. })
  468. },
  469. changeCreator () {
  470. if (this.checkedNum <= 0) return
  471. let curMember = this.checkList[this.curItemIndex]
  472. let username = curMember.nick_name || curMember.user_name
  473. MessageBox.confirm(`${this.$t('group.groupMgrComfime')} ${username}`)
  474. .then(() => {
  475. this.isLoading = true
  476. API.group
  477. .changeCreator({
  478. new_creator: this.inviteList,
  479. group_id: this.groupId
  480. })
  481. .then(({ data }) => {
  482. if (this.meechatType == 'h5') this.$router.go(-1)
  483. else this.hidePopup()
  484. this.$store.dispatch('getGroupInfo')
  485. Message({
  486. message: `${this.$t('group.groupMgrResult')} ${username}`,
  487. type: 'warning'
  488. })
  489. })
  490. })
  491. .catch(e => {
  492. console.log(e)
  493. })
  494. }
  495. },
  496. mounted () {
  497. this.initList()
  498. }
  499. }
  500. export const groupHotMixins = {
  501. props: {
  502. searchTxt: String
  503. },
  504. data () {
  505. return {
  506. }
  507. },
  508. components: {},
  509. computed: {
  510. ...mapState(['hotList']),
  511. showHotList () {
  512. let val = this.searchTxt && this.searchTxt.trim()
  513. if (!val) return this.hotList
  514. let arr = this.hotList.filter((item) => {
  515. let title = (item.group_title && item.group_title.toLocaleLowerCase()) || ''
  516. return title.match(val)
  517. })
  518. return arr
  519. }
  520. },
  521. methods: {
  522. ...mapMutations(['changeHotGroupStatus']),
  523. bgColorNum (str) {
  524. return getAvatarBgColor(str, this.userId)
  525. },
  526. /**
  527. * @des 加入群聊
  528. * @param {type} 1已加入0未加入
  529. * */
  530. joinGroup (groupId, type) {
  531. if (type == 1) {
  532. this.$router.push(`/group/${groupId}`)
  533. } else {
  534. API.group.joinGroup({
  535. group_id: groupId
  536. }).then(() => {
  537. this.changeHotGroupStatus({
  538. groupId: groupId,
  539. isJoin: 1
  540. })
  541. this.$router.push(`/group/${groupId}`)
  542. })
  543. }
  544. }
  545. },
  546. mounted () {
  547. this.$store.commit('changeSessionId', '0')
  548. this.$store.dispatch('getHotList')
  549. }
  550. }