group.js 15 KB

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