index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <transition name="msgbox-fade">
  3. <div class="pub-wrapper" v-if="visible">
  4. <div class="pub-mask"></div>
  5. <div class="pub-modal invite-modal">
  6. <div class="modal-hd">
  7. <!-- <div class="title">
  8. <i class="el-icon-close" @click="visible = false"></i>
  9. </div>-->
  10. <i class="el-icon-close" @click="visible = false"></i>
  11. </div>
  12. <div class="modal-bd clearfix">
  13. <div class="left">
  14. <div class="search-input">
  15. <input type="text" @input="searchUser($event, checkList)" placeholder="搜索联系人">
  16. <i class="el-icon-search"></i>
  17. </div>
  18. <div class="user-list select-list pub-scroll-box">
  19. <template v-if="!isSearchGroup">
  20. <template v-if="checkList.length>0">
  21. <div
  22. class="user-item"
  23. v-for="(item, key) in checkList"
  24. :key="key"
  25. :class="{'checked': item.isChecked}"
  26. @click="changeState(item.user_id)"
  27. >
  28. <img v-if="item.cover_photo" class="user-avatar" :src="item.cover_photo" alt>
  29. <div
  30. v-else
  31. class="user-avatar"
  32. :class="`avatar_bg${item.user_id % 9}`"
  33. :data-name="item.nick_name.slice(0,2).toUpperCase()"
  34. ></div>
  35. <span class="name">{{item.nick_name}}</span>
  36. <i v-if="item.isChoosed" class="el-icon-circle-check is-choosed"></i>
  37. <div v-else-if="item.is_admin!=2">
  38. <i v-if="item.isChecked" class="el-icon-circle-check"></i>
  39. <i v-else class="el-icon-circle-uncheck"></i>
  40. </div>
  41. </div>
  42. </template>
  43. <div class="no-data" v-else>暂无联系人</div>
  44. </template>
  45. <template v-else>
  46. <template v-if="searchGroupList.length>0">
  47. <div
  48. class="user-item"
  49. v-for="(item, key) in searchGroupList"
  50. :key="key"
  51. :class="{'checked': item.isChecked}"
  52. @click="changeState(item.user_id)"
  53. >
  54. <img v-if="item.cover_photo" class="user-avatar" :src="item.cover_photo" alt>
  55. <div
  56. v-else
  57. class="user-avatar"
  58. :class="`avatar_bg${item.user_id % 9}`"
  59. :data-name="item.nick_name.slice(0,2).toUpperCase()"
  60. ></div>
  61. <span class="name">{{item.nick_name}}</span>
  62. <i v-if="item.isChoosed" class="el-icon-circle-check"></i>
  63. <div v-if="item.is_admin!=2">
  64. <i v-if="item.isChecked" class="el-icon-circle-check"></i>
  65. <i v-else class="el-icon-circle-uncheck"></i>
  66. </div>
  67. </div>
  68. </template>
  69. <div class="no-data" v-else>暂无联系人</div>
  70. </template>
  71. </div>
  72. </div>
  73. <div class="right">
  74. <p class="tips" v-if="this.popInviteType==5">转让群主</p>
  75. <p class="tips" v-else-if="this.checkedNum">已选择了{{checkedNum}}位群成员</p>
  76. <p class="tips" v-else-if="this.popInviteType==3">请勾选需要删除的群成员</p>
  77. <p class="tips" v-else>请勾选需要添加的{{this.popInviteType==4?"管理员":"联系人"}}</p>
  78. <div class="group-name" v-if="this.popInviteType==1">
  79. <input v-focus v-model="groupName" type="text" placeholder="请输入群名称">
  80. </div>
  81. <div class="user-list pub-scroll-box">
  82. <template v-for="(item, key) in resultList">
  83. <div class="user-item" :key="key" v-if="item.isChecked">
  84. <img v-if="item.cover_photo" class="user-avatar" :src="item.cover_photo" alt>
  85. <div
  86. v-else
  87. class="user-avatar"
  88. :class="`avatar_bg${item.user_id % 9}`"
  89. :data-name="item.nick_name.slice(0,2).toUpperCase()"
  90. ></div>
  91. <span class="name">{{item.nick_name}}</span>
  92. <i @click="changeState(item.user_id, false)" class="el-icon-circle-close"></i>
  93. </div>
  94. </template>
  95. </div>
  96. <div class="send-btn">
  97. <el-button @click="optSubmit" type="primary" :disabled="checkedNum<=0">确认</el-button>
  98. <el-button @click="visible = false">取消</el-button>
  99. </div>
  100. </div>
  101. </div>
  102. </div>
  103. </div>
  104. </transition>
  105. </template>
  106. <script>
  107. import { Button, Message, MessageBox } from 'element-ui'
  108. import Vue from 'vue'
  109. import { mapActions, mapState } from 'vuex'
  110. import API from '@/api'
  111. import _ from 'lodash'
  112. import { searchGroupUserMixin } from '@/mixins'
  113. Vue.component(Button.name, Button)
  114. export default {
  115. name: 'invitePopup',
  116. mixins: [searchGroupUserMixin],
  117. data () {
  118. return {
  119. curItemIndex: 0,
  120. checkList: [],
  121. groupName: ''
  122. }
  123. },
  124. computed: {
  125. checkedNum () {
  126. return this.resultList.length
  127. },
  128. ...mapState({
  129. userId: state => state.userId,
  130. popInviteType: state => state.chat.popInviteType,
  131. friendList: state => state.chat.friendList,
  132. groupId: state => state.group.groupId,
  133. members: state => state.group.members,
  134. membersNum: state => state.group.membersNum,
  135. adminList: state => state.group.adminList
  136. }),
  137. inviteList () {
  138. return this.checkList
  139. .filter(v => {
  140. return v.isChecked
  141. })
  142. .map(v => v.user_id)
  143. .join(',')
  144. },
  145. resultList () {
  146. let arr1 = this.checkList.filter(v => {
  147. return v.isChecked
  148. })
  149. let arr2 = this.searchGroupList.filter(v => {
  150. return v.isChecked
  151. })
  152. let arr = [...arr1, ...arr2]
  153. let res = []
  154. for (let i = 0; i < arr.length; i++) {
  155. let flag = true
  156. for (let j = 0; j < res.length; j++) {
  157. if (arr[i].user_id === res[j].user_id) flag = false
  158. }
  159. if (flag) {
  160. res.push(arr[i])
  161. }
  162. }
  163. return res
  164. },
  165. ...mapActions(['getGroupInfo'])
  166. },
  167. methods: {
  168. getItemByUid (uid) {
  169. if (this.isSearchGroup) {
  170. return this.searchGroupList.filter((item, index) => {
  171. if (item.user_id == uid) this.curItemIndex = index
  172. return item.user_id == uid
  173. })[0]
  174. } else {
  175. return this.checkList.filter((item, index) => {
  176. if (item.user_id == uid) this.curItemIndex = index
  177. return item.user_id == uid
  178. })[0]
  179. }
  180. },
  181. changeState (uid, flag = true) {
  182. let item = this.getItemByUid(uid)
  183. if (item.isChoosed) return
  184. // 群管理设限
  185. if (
  186. this.popInviteType == 4 &&
  187. flag &&
  188. this.adminList.length + this.checkedNum > 5
  189. ) {
  190. this.$showTips('管理员最多只能设置5个人哦')
  191. return
  192. }
  193. // 转让群主
  194. if (this.popInviteType == 5) {
  195. this.checkList.forEach(item => {
  196. item.isChecked = false
  197. })
  198. }
  199. item['isChecked'] = flag
  200. if (this.isSearchGroup) {
  201. this.$set(this.searchGroupList, this.curItemIndex, item)
  202. } else this.$set(this.checkList, this.curItemIndex, item)
  203. },
  204. async initList () {
  205. switch (this.popInviteType) {
  206. // 1建群/2邀请好友进群
  207. case 1:
  208. await this.$store.dispatch('getFriendList')
  209. this.checkList = this.friendList
  210. break
  211. case 2:
  212. await this.$store.dispatch('getFriendList')
  213. this.checkList = _.filter(this.friendList, item => {
  214. return !this.members[item.user_id]
  215. })
  216. break
  217. // 3删除群成员/4添加群管理/5转让群主
  218. case 3:
  219. case 4:
  220. case 5:
  221. this.checkList = this.members
  222. break
  223. }
  224. this.checkList = _.filter(this.checkList, item => {
  225. item.isChecked = false
  226. item.isChoosed = false
  227. // 群管理特殊处理
  228. if (this.popInviteType == 4 && item.is_admin == 1) {
  229. item.isChoosed = true
  230. }
  231. return item.user_id != this.userId
  232. })
  233. },
  234. optSubmit () {
  235. switch (this.popInviteType) {
  236. // 建群
  237. case 1:
  238. this.createGroup()
  239. break
  240. // 邀请好友进群
  241. case 2:
  242. this.invitesMember()
  243. break
  244. // 删除群成员
  245. case 3:
  246. this.removesMember()
  247. break
  248. // 添加群管理
  249. case 4:
  250. this.addAdmin()
  251. break
  252. // 群主转让
  253. case 5:
  254. this.changeCreator()
  255. break
  256. }
  257. },
  258. // 建群
  259. createGroup () {
  260. if (this.groupName) {
  261. API.group
  262. .createGroup({
  263. group_title: this.groupName,
  264. user_id_list: this.inviteList
  265. })
  266. .then(({ data }) => {
  267. this.$showTips('创建成功')
  268. this.$store.commit('addSessionItem', data.data)
  269. this.visible = false
  270. })
  271. } else {
  272. Message({
  273. message: '请输入群名',
  274. type: 'warning'
  275. })
  276. }
  277. },
  278. // 邀请成员加入
  279. invitesMember () {
  280. API.group
  281. .invites({
  282. user_ids: this.inviteList,
  283. group_id: this.groupId
  284. })
  285. .then(({ data }) => {
  286. this.visible = false
  287. this.$store.dispatch('getGroupInfo')
  288. })
  289. },
  290. // 删除成员
  291. removesMember () {
  292. API.group
  293. .removes({
  294. user_ids: this.inviteList,
  295. group_id: this.groupId
  296. })
  297. .then(({ data }) => {
  298. this.visible = false
  299. this.$store.dispatch('getGroupInfo')
  300. })
  301. },
  302. // 添加群管理
  303. addAdmin () {
  304. API.group
  305. .addAdmin({
  306. user_ids: this.inviteList,
  307. group_id: this.groupId
  308. })
  309. .then(({ data }) => {
  310. this.visible = false
  311. this.$store.dispatch('getGroupInfo')
  312. })
  313. },
  314. changeCreator () {
  315. if (this.checkedNum <= 0) return
  316. let curMember = this.isSearchGroup
  317. ? this.searchGroupList[this.curItemIndex]
  318. : this.checkList[this.curItemIndex]
  319. let username = curMember.nick_name || curMember.user_name
  320. MessageBox.confirm(`确定要把群主转让给${username}`)
  321. .then(() => {
  322. API.group
  323. .changeCreator({
  324. new_creator: this.inviteList,
  325. group_id: this.groupId
  326. })
  327. .then(({ data }) => {
  328. this.visible = false
  329. Message({
  330. message: `已转让群主给${username}`,
  331. type: 'warning'
  332. })
  333. })
  334. })
  335. .catch(e => {
  336. console.log(e)
  337. })
  338. }
  339. },
  340. created () {
  341. this.initList()
  342. },
  343. directives: {
  344. focus: {
  345. // 指令的定义
  346. inserted: function (el) {
  347. setTimeout(() => {
  348. el.focus()
  349. }, 200)
  350. }
  351. }
  352. }
  353. }
  354. </script>
  355. <style lang="scss" scoped>
  356. @import "./style.scss";
  357. </style>