group.js 14 KB

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