group.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. await this.$store.dispatch('getFriendList')
  331. this.checkList = this.friendList
  332. break
  333. case 2:
  334. await this.$store.dispatch('getFriendList')
  335. this.checkList = _.filter(this.friendList, item => {
  336. return !this.members[item.user_id]
  337. })
  338. break
  339. // 3删除群成员/4添加群管理/5转让群主
  340. case 3:
  341. case 4:
  342. case 5:
  343. this.checkList = this.membersArray
  344. break
  345. }
  346. this.checkList = _.filter(this.checkList, item => {
  347. item.isChecked = false
  348. item.isChoosed = false
  349. item.isShow = true
  350. // 群管理特殊处理
  351. if (this.inviteType == 4 && item.is_admin == 1) {
  352. item.isChoosed = true
  353. }
  354. return item.user_id != this.userId
  355. })
  356. this.showNum = this.checkList.length
  357. },
  358. optSubmit () {
  359. switch (this.inviteType) {
  360. // 建群
  361. case 1:
  362. this.createGroup()
  363. break
  364. // 邀请好友进群
  365. case 2:
  366. this.invitesMember()
  367. break
  368. // 删除群成员
  369. case 3:
  370. this.removesMember()
  371. break
  372. // 添加群管理
  373. case 4:
  374. this.addAdmin()
  375. break
  376. // 群主转让
  377. case 5:
  378. this.changeCreator()
  379. break
  380. // 创建新群并且同步telegram
  381. case 6:
  382. this.createGroup()
  383. break
  384. }
  385. },
  386. // 建群
  387. createGroup () {
  388. if (this.groupName.length > 16) {
  389. this.$showTips(this.$t('group.groupNameLengthTips'))
  390. return
  391. }
  392. if (this.groupName) {
  393. this.isLoading = true
  394. API.group
  395. .createGroup({
  396. group_title: this.groupName,
  397. user_id_list: this.inviteList
  398. })
  399. .then(({ data }) => {
  400. let groupId = data.data.session_id
  401. this.$showTips(this.$t('group.createSuccess'))
  402. this.$store.commit('addSessionItem', data.data)
  403. if (this.ext && this.ext.tgGroupId) {
  404. // 同步tele
  405. this.doSyncTelegram(groupId)
  406. } else {
  407. this.$router.push(`/group/${groupId}`)
  408. }
  409. if (this.meechatType != 'h5') this.hidePopup()
  410. })
  411. } else {
  412. Message({
  413. message: this.$t('group.groupNameTips'),
  414. type: 'warning'
  415. })
  416. }
  417. },
  418. // 同步tele
  419. doSyncTelegram (groupId) {
  420. API.tg.doSync({
  421. group_id: groupId,
  422. tg_group_id: this.ext.tgGroupId
  423. }).then(() => {
  424. this.$router.replace(`/group/${groupId}`)
  425. }).catch(error => {
  426. console.log(error)
  427. if (this.meechatType == 'h5') this.$router.replace('/')
  428. })
  429. },
  430. // 邀请成员加入
  431. async invitesMember () {
  432. this.isLoading = true
  433. await API.group.invites({
  434. user_ids: this.inviteList,
  435. group_id: this.groupId
  436. })
  437. await this.getGroupInfo()
  438. if (this.meechatType == 'h5') this.$router.go(-1)
  439. else this.hidePopup()
  440. },
  441. // 删除成员
  442. async removesMember () {
  443. this.isLoading = true
  444. await API.group.removes({
  445. user_ids: this.inviteList,
  446. group_id: this.groupId
  447. })
  448. await this.getGroupInfo()
  449. if (this.meechatType == 'h5') this.$router.go(-1)
  450. else this.hidePopup()
  451. },
  452. // 添加群管理
  453. addAdmin () {
  454. this.isLoading = true
  455. API.group
  456. .addAdmin({
  457. user_ids: this.inviteList,
  458. group_id: this.groupId
  459. })
  460. .then(({ data }) => {
  461. this.getGroupInfo()
  462. if (this.meechatType == 'h5') this.$router.go(-1)
  463. else this.hidePopup()
  464. })
  465. },
  466. changeCreator () {
  467. if (this.checkedNum <= 0) return
  468. let curMember = this.checkList[this.curItemIndex]
  469. let username = curMember.nick_name || curMember.user_name
  470. MessageBox.confirm(`${this.$t('group.groupMgrComfime')} ${username}`)
  471. .then(() => {
  472. this.isLoading = true
  473. API.group
  474. .changeCreator({
  475. new_creator: this.inviteList,
  476. group_id: this.groupId
  477. })
  478. .then(({ data }) => {
  479. if (this.meechatType == 'h5') this.$router.go(-1)
  480. else this.hidePopup()
  481. this.$store.dispatch('getGroupInfo')
  482. Message({
  483. message: `${this.$t('group.groupMgrResult')} ${username}`,
  484. type: 'warning'
  485. })
  486. })
  487. })
  488. .catch(e => {
  489. console.log(e)
  490. })
  491. }
  492. },
  493. mounted () {
  494. this.initList()
  495. }
  496. }
  497. export const groupHotMixins = {
  498. props: {
  499. searchTxt: String
  500. },
  501. data () {
  502. return {
  503. }
  504. },
  505. components: {},
  506. computed: {
  507. ...mapState(['hotList']),
  508. ...mapActions(['getHotList']),
  509. showHotList () {
  510. let val = this.searchTxt && this.searchTxt.trim()
  511. if (!val) return this.hotList
  512. let arr = this.hotList.filter((item) => {
  513. let title = (item.group_title && item.group_title.toLocaleLowerCase()) || ''
  514. return title.match(val)
  515. })
  516. return arr
  517. }
  518. },
  519. methods: {
  520. ...mapMutations(['changeHotGroupStatus']),
  521. bgColorNum (str) {
  522. return getAvatarBgColor(str, this.userId)
  523. },
  524. /**
  525. * @des 加入群聊
  526. * @param {type} 1已加入0未加入
  527. * */
  528. joinGroup (groupId, type) {
  529. if (type == 1) {
  530. this.$router.push(`/group/${groupId}`)
  531. } else {
  532. API.group.joinGroup({
  533. group_id: groupId
  534. }).then(() => {
  535. this.changeHotGroupStatus({
  536. groupId: groupId,
  537. isJoin: 1
  538. })
  539. this.$router.push(`/group/${groupId}`)
  540. })
  541. }
  542. }
  543. },
  544. mounted () {
  545. this.$store.commit('changeSessionId', '0')
  546. this.$store.dispatch('getHotList')
  547. }
  548. }