group.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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('确认退出该群聊?').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('已退出该群聊')
  208. this.$emit('showLoadingRoom', false)
  209. if (this.meechatType == 'h5') this.$router.go(-2)
  210. }).catch(() => {
  211. this.$emit('showLoadingRoom', false)
  212. })
  213. })
  214. },
  215. searchUser () {
  216. if (!this.searchTxt.trim()) return
  217. if (this.membersArray.length < this.group.membersNum) {
  218. API.group.searchMember({
  219. group_id: this.group.groupId,
  220. keyword: this.searchTxt
  221. }).then(({ data }) => {
  222. this.searchList = data.data
  223. })
  224. } else {
  225. let val = this.searchTxt
  226. this.searchList = this.membersArray.filter(item => {
  227. let inName, inNick
  228. inName = item.user_name ? item.user_name.indexOf(val) > -1 : false
  229. inNick = item.nick_name ? item.nick_name.indexOf(val) > -1 : false
  230. return inName || inNick
  231. })
  232. }
  233. }
  234. }
  235. }
  236. export const groupInviteMixins = {
  237. mixins: [searchGroupUserMixin],
  238. data () {
  239. return {
  240. curItemIndex: 0,
  241. checkList: [],
  242. groupName: '',
  243. isLoading: false,
  244. showNum: 0,
  245. meechatType: getMeechatType()// meechat版本
  246. }
  247. },
  248. computed: {
  249. checkedNum () {
  250. return (this.checkList.filter(v => {
  251. return v.isChecked
  252. })).length
  253. },
  254. ...mapState({
  255. userId: state => state.userId,
  256. friendList: state => state.chat.friendList,
  257. groupId: state => state.group.groupId,
  258. group: state => state.group,
  259. members: state => state.group.members,
  260. membersArray: state => state.group.membersArray,
  261. membersNum: state => state.group.membersNum,
  262. adminList: state => state.group.adminList
  263. }),
  264. inviteList () {
  265. return this.checkList.filter(v => {
  266. return v.isChecked
  267. }).map(v => v.user_id).join(',')
  268. },
  269. resultList () {
  270. return (this.checkList && this.checkList.filter(v => {
  271. return v.isChecked
  272. })) || []
  273. }
  274. },
  275. methods: {
  276. ...mapActions(['getGroupInfo']),
  277. ...mapMutations([
  278. 'initGroup',
  279. 'changeSessionId'
  280. ]),
  281. getItemByUid (uid) {
  282. return this.checkList.filter((item, index) => {
  283. if (item.user_id == uid) this.curItemIndex = index
  284. return item.user_id == uid
  285. })[0]
  286. },
  287. changeState (uid, flag = true) {
  288. let item = this.getItemByUid(uid)
  289. if (item.isChoosed) return
  290. // 群管理设限
  291. if (
  292. this.inviteType == 4 &&
  293. flag &&
  294. this.adminList.length + this.checkedNum > 5
  295. ) {
  296. this.$showTips(this.$t('group.groupMgrMostTips'))
  297. return
  298. }
  299. // 转让群主
  300. if (this.inviteType == 5) {
  301. this.checkList.forEach(item => {
  302. item.isChecked = false
  303. })
  304. }
  305. item['isChecked'] = flag
  306. this.$set(this.checkList, this.curItemIndex, item)
  307. },
  308. changeStateForH5 (uid) {
  309. let item = this.getItemByUid(uid)
  310. let flag = !item.isChecked
  311. // 群管理设限
  312. if (this.inviteType == 4 && flag && this.adminList.length + this.checkedNum > 5) {
  313. this.$showTips(this.$t('group.groupMgrMostTips'))
  314. return
  315. }
  316. // 转让群主
  317. if (this.inviteType == 5) {
  318. this.checkList.forEach(item => {
  319. item.isChecked = false
  320. })
  321. }
  322. item['isChecked'] = flag
  323. this.$set(this.checkList, this.curItemIndex, item)
  324. },
  325. async initList () {
  326. switch (this.inviteType) {
  327. // 1建群/2邀请好友进群
  328. case 1:
  329. await this.$store.dispatch('getFriendList')
  330. this.checkList = this.friendList
  331. break
  332. case 2:
  333. await this.$store.dispatch('getFriendList')
  334. this.checkList = _.filter(this.friendList, item => {
  335. return !this.members[item.user_id]
  336. })
  337. break
  338. // 3删除群成员/4添加群管理/5转让群主
  339. case 3:
  340. case 4:
  341. case 5:
  342. this.checkList = this.membersArray
  343. break
  344. }
  345. this.checkList = _.filter(this.checkList, item => {
  346. item.isChecked = false
  347. item.isChoosed = false
  348. item.isShow = true
  349. // 群管理特殊处理
  350. if (this.inviteType == 4 && item.is_admin == 1) {
  351. item.isChoosed = true
  352. }
  353. return item.user_id != this.userId
  354. })
  355. this.showNum = this.checkList.length
  356. },
  357. optSubmit () {
  358. switch (this.inviteType) {
  359. // 建群
  360. case 1:
  361. this.createGroup()
  362. break
  363. // 邀请好友进群
  364. case 2:
  365. this.invitesMember()
  366. break
  367. // 删除群成员
  368. case 3:
  369. this.removesMember()
  370. break
  371. // 添加群管理
  372. case 4:
  373. this.addAdmin()
  374. break
  375. // 群主转让
  376. case 5:
  377. this.changeCreator()
  378. break
  379. }
  380. },
  381. // 建群
  382. createGroup () {
  383. if (this.groupName.length > 16) {
  384. this.$showTips(this.$t('group.groupNameLengthTips'))
  385. return
  386. }
  387. if (this.groupName) {
  388. this.isLoading = true
  389. API.group
  390. .createGroup({
  391. group_title: this.groupName,
  392. user_id_list: this.inviteList
  393. })
  394. .then(({ data }) => {
  395. this.$showTips('')
  396. this.$store.commit('addSessionItem', data.data)
  397. this.$router.push(`/group/${data.data.session_id}`)
  398. if (this.meechatType != 'h5') this.hidePopup()
  399. })
  400. } else {
  401. Message({
  402. message: this.$t('group.groupNameTips'),
  403. type: 'warning'
  404. })
  405. }
  406. },
  407. // 邀请成员加入
  408. async invitesMember () {
  409. this.isLoading = true
  410. await API.group.invites({
  411. user_ids: this.inviteList,
  412. group_id: this.groupId
  413. })
  414. await this.getGroupInfo()
  415. if (this.meechatType == 'h5') this.$router.go(-1)
  416. else this.hidePopup()
  417. },
  418. // 删除成员
  419. async removesMember () {
  420. this.isLoading = true
  421. await API.group.removes({
  422. user_ids: this.inviteList,
  423. group_id: this.groupId
  424. })
  425. await this.getGroupInfo()
  426. if (this.meechatType == 'h5') this.$router.go(-1)
  427. else this.hidePopup()
  428. },
  429. // 添加群管理
  430. addAdmin () {
  431. this.isLoading = true
  432. API.group
  433. .addAdmin({
  434. user_ids: this.inviteList,
  435. group_id: this.groupId
  436. })
  437. .then(({ data }) => {
  438. this.getGroupInfo()
  439. if (this.meechatType == 'h5') this.$router.go(-1)
  440. else this.hidePopup()
  441. })
  442. },
  443. changeCreator () {
  444. if (this.checkedNum <= 0) return
  445. let curMember = this.checkList[this.curItemIndex]
  446. let username = curMember.nick_name || curMember.user_name
  447. MessageBox.confirm(`${this.$t('group.groupMgrComfime')} ${username}`)
  448. .then(() => {
  449. this.isLoading = true
  450. API.group
  451. .changeCreator({
  452. new_creator: this.inviteList,
  453. group_id: this.groupId
  454. })
  455. .then(({ data }) => {
  456. if (this.meechatType == 'h5') this.$router.go(-1)
  457. else this.hidePopup()
  458. this.$store.dispatch('getGroupInfo')
  459. Message({
  460. message: `${this.$t('group.groupMgrResult')} ${username}`,
  461. type: 'warning'
  462. })
  463. })
  464. })
  465. .catch(e => {
  466. console.log(e)
  467. })
  468. }
  469. },
  470. mounted () {
  471. this.initList()
  472. }
  473. }
  474. export const groupHotMixins = {
  475. props: {
  476. searchTxt: String
  477. },
  478. data () {
  479. return {
  480. }
  481. },
  482. components: {},
  483. computed: {
  484. ...mapState(['hotList']),
  485. ...mapActions(['getHotList']),
  486. showHotList () {
  487. let val = this.searchTxt && this.searchTxt.trim()
  488. if (!val) return this.hotList
  489. let arr = this.hotList.filter((item) => {
  490. let title = (item.group_title && item.group_title.toLocaleLowerCase()) || ''
  491. return title.match(val)
  492. })
  493. return arr
  494. }
  495. },
  496. methods: {
  497. ...mapMutations(['changeHotGroupStatus']),
  498. bgColorNum (str) {
  499. return getAvatarBgColor(str, this.userId)
  500. },
  501. /**
  502. * @des 加入群聊
  503. * @param {type} 1已加入0未加入
  504. * */
  505. joinGroup (groupId, type) {
  506. if (type == 1) {
  507. this.$router.push(`/group/${groupId}`)
  508. } else {
  509. API.group.joinGroup({
  510. group_id: groupId
  511. }).then(() => {
  512. this.changeHotGroupStatus({
  513. groupId: groupId,
  514. isJoin: 1
  515. })
  516. this.$router.push(`/group/${groupId}`)
  517. })
  518. }
  519. }
  520. },
  521. mounted () {
  522. this.$store.commit('changeSessionId', '0')
  523. this.$store.dispatch('getHotList')
  524. }
  525. }