group.js 15 KB

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