group.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <template>
  2. <page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta>
  3. <view>
  4. <cu-custom bgImage='/static/bg.png' :isBack='true'>
  5. <template v-slot:content>
  6. <text class="text-black">{{ group.name }}</text>
  7. </template>
  8. <template v-slot:right v-if='isMaster'>
  9. <text class='cuIcon cuIcon-edit text-black' @tap='editGroup'></text>
  10. </template>
  11. </cu-custom>
  12. <view class='bg-white padding'>
  13. <view class='item_list'>
  14. <view class='item_content' @tap='group.prohibitFriend === DictUtils.YES ? "" : openUser(group.master)'>
  15. <image class="logoImg" :src="master.avatar" mode="scaleToFill"></image>
  16. <!-- <vim-avatar :img="master.avatar" :name="master.name" /> -->
  17. <view class='margin-top-xs text-sm' style="display: flex;flex-direction:row;justify-content: center;">
  18. <text class="text-zblue margin-right-xs"><text class="cuIcon-crown"></text></text>
  19. <text class="text-zblue text-bold user_name">{{ master.name }}</text>
  20. </view>
  21. </view>
  22. <view v-for='(item,index) in userList.slice(0,endIndex)' :key='index' class='item_content'
  23. @tap='group.prohibitFriend === DictUtils.YES ? "" : openUser(item.id)'>
  24. <vim-avatar :img="item.avatar" :name="item.name" />
  25. <view class='margin-top-xs text-sm'>
  26. <text class="text-black text-bold user_name">{{ item.name }}</text>
  27. </view>
  28. </view>
  29. <view class='item_content' v-if="group.openInvite === DictUtils.YES || isMaster">
  30. <view class='cuIcon cuIcon-add cu-avatar group-btn radius' @tap='toAddUsers'></view>
  31. <view class='margin-top-xs text-sm text-black text-bold'>邀请</view>
  32. </view>
  33. <view class='item_content' v-if='isMaster'>
  34. <view class='cuIcon cuIcon-move cu-avatar group-btn radius' @tap='toDelUsers'></view>
  35. <view class='margin-top-xs text-sm text-black text-bold'>移除</view>
  36. </view>
  37. <view class='item_content' v-if='userList.length>endIndex'>
  38. <view class='cuIcon cuIcon-more cu-avatar group-btn radius' @tap='moreUser'></view>
  39. <view class='margin-top-xs text-sm '>更多</view>
  40. </view>
  41. </view>
  42. </view>
  43. <chat-setting :chat-id='group.id'></chat-setting>
  44. <view class='cu-list menu margin-top text-sm'>
  45. <view class='cu-item card-menu' v-if='isMaster' @tap='handleCheck()'>
  46. <view class='content'>
  47. <text class='cuIcon-check text-black text-bold'></text>
  48. <text class='text-black text-bold'>入群审核</text>
  49. </view>
  50. <view class='action text-black text-bold'>
  51. <text class="cuIcon cuIcon-right"></text>
  52. </view>
  53. </view>
  54. <view class='cu-item card-menu' v-if='isMaster' @tap='handleTransference()'>
  55. <view class='content'>
  56. <text class='cuIcon-share text-black text-bold'></text>
  57. <text class='text-black text-bold'>转让群</text>
  58. </view>
  59. <view class='action text-black text-bold'>
  60. <text class="cuIcon cuIcon-right"></text>
  61. </view>
  62. </view>
  63. <view class='cu-item text-black text-bold' @tap='history()'>
  64. <view class='content'>
  65. <text class='cuIcon-message'></text>
  66. <text class="text-black text-bold">聊天记录</text>
  67. </view>
  68. <view class='action'>
  69. <text class='cuIcon-right'></text>
  70. </view>
  71. </view>
  72. <view class='cu-item text-black text-bold'>
  73. <view class='content'>
  74. <text class='cuIcon-notice'></text>
  75. <text>公告</text>
  76. </view>
  77. <view class='action' v-if='isMaster' @tap="handleAnnouncement">
  78. <text class='cuIcon-right'></text>
  79. </view>
  80. </view>
  81. <view v-if="group.announcement " class='cu-item text-gray announcement'>{{ group.announcement }}</view>
  82. </view>
  83. <view class='cu-list menu margin-top text-sm'>
  84. <view class='text-cyan margin-top padding' >
  85. <button class="cu-btn lg bg-zblue block border" @tap='openChat()'>聊天</button>
  86. </view>
  87. <view class='cu-item text-red margin-top' v-if='isMaster' @tap='deleteGroup()'>
  88. <view class='content text-center text-bold'>
  89. <text>解散群组</text>
  90. </view>
  91. </view>
  92. <view class='cu-item text-red margin-top' @tap='exitGroup()' v-if='!isMaster'>
  93. <view class='content text-center text-bold'>
  94. <text>退出群聊</text>
  95. </view>
  96. </view>
  97. </view>
  98. </view>
  99. </template>
  100. <script setup lang='ts'>
  101. import {getCurrentInstance, reactive, ref} from 'vue'
  102. import {onLoad} from '@dcloudio/uni-app'
  103. import {useUserStore} from '@/store/userStore'
  104. import {useChatStore} from '@/store/chatStore'
  105. import CuCustom from '@/colorui/components/cu-custom.vue'
  106. import GroupApi from '@/api/GroupApi'
  107. import {useGroupStore} from '@/store/groupStore'
  108. import ChatType from '@/utils/ChatType'
  109. import type Group from '@/mode/Group'
  110. import type Message from '@/mode/Message'
  111. import UserApi from '@/api/UserApi'
  112. import MessageUtils from '@/utils/MessageUtils'
  113. import MessageType from '@/utils/MessageType'
  114. import type ChatSimple from '@/mode/ChatSimple'
  115. import {storeToRefs} from 'pinia'
  116. import {useWsStore} from '@/store/WsStore'
  117. import ChatSetting from '@/components/ChatSetting.vue'
  118. import DictUtils from "@/utils/DictUtils";
  119. import VimAvatar from "@/components/VimAvatar.vue";
  120. import type UserSimple from "@/mode/UserSimple";
  121. import Auth from "@/api/Auth";
  122. const fontValue=ref(Auth.getfontSize());
  123. //@ts-ignore
  124. const {proxy} = getCurrentInstance()
  125. const groupStore = useGroupStore()
  126. const store = useUserStore()
  127. const chatStore = useChatStore()
  128. const userStore = useUserStore()
  129. //显示的群用户条数
  130. const endIndex = ref(8)
  131. //是否是群主
  132. const isMaster = ref(false)
  133. const master = ref({} as UserSimple)
  134. //群用户
  135. const userList = ref<ChatSimple[]>([])
  136. const group = reactive<Group>({
  137. id: '',
  138. master: '',
  139. name: '',
  140. avatar: '',
  141. openInvite: '0',
  142. inviteCheck: '0',
  143. prohibition: '0',
  144. prohibitFriend: '0',
  145. announcement: ''
  146. })
  147. const handleAnnouncement = () => {
  148. uni.navigateTo({
  149. url: '/pages/group/announcement?id=' + group.id
  150. })
  151. }
  152. onLoad((opt) => {
  153. group.id = opt?.id
  154. uni.showLoading()
  155. GroupApi.get(group.id)
  156. .then((res) => {
  157. //console.log('group',res)
  158. group.id = res.data.id
  159. group.name = res.data.name
  160. group.avatar = res.data.avatar
  161. group.master = res.data.master
  162. group.openInvite = res.data.openInvite
  163. group.inviteCheck = res.data.inviteCheck
  164. group.prohibition = res.data.prohibition
  165. group.prohibitFriend = res.data.prohibitFriend
  166. group.announcement = res.data.announcement
  167. isMaster.value = res.data.master === store.user?.id
  168. loadUser(group.id)
  169. })
  170. groupStore.loadWaitCheckList()
  171. })
  172. const toAddUsers = () => {
  173. uni.navigateTo({
  174. url: '/pages/group/add-users?id=' + group.id
  175. })
  176. }
  177. const toDelUsers = () => {
  178. uni.navigateTo({
  179. url: '/pages/group/del-users?id=' + group.id
  180. })
  181. }
  182. const moreUser = () => {
  183. endIndex.value = userList.value.length + 1
  184. }
  185. const history = () => {
  186. uni.navigateTo({
  187. url: '/pages/chat/history?chatId=' + group.id + '&type=' + ChatType.GROUP
  188. })
  189. }
  190. //加载群用户
  191. const loadUser = (gId: string) => {
  192. userList.value = []
  193. GroupApi.users(gId).then((res) => {
  194. //是否是群用户
  195. //console.log('GroupApi',res)
  196. if (!res.data.some((item: any) => {
  197. return item.id === useUserStore().getUser()!.id
  198. })) {
  199. MessageUtils.message('无权限')
  200. }
  201. res.data.forEach((item: any) => {
  202. if(group.master === item.id){
  203. master.value = {
  204. name: item.name,
  205. avatar: item.avatar
  206. }
  207. }else {
  208. userList.value.push({
  209. id: item.id,
  210. name: item.name,
  211. avatar: item.avatar
  212. })
  213. }
  214. })
  215. })
  216. .finally(() => {
  217. uni.hideLoading()
  218. })
  219. }
  220. const editGroup = () => {
  221. uni.navigateTo({
  222. url: '/pages/group/edit?id=' + group.id
  223. })
  224. }
  225. const handleCheck = () => {
  226. uni.navigateTo({
  227. url: '/pages/group/invite?id=' + group.id
  228. })
  229. }
  230. /**
  231. * 转让群
  232. */
  233. const handleTransference = () => {
  234. uni.navigateTo({
  235. url: '/pages/group/transference?id=' + group.id
  236. })
  237. }
  238. const openUser = (id: string | undefined) => {
  239. //console.log('openUser',id);
  240. uni.navigateTo({
  241. url: '/pages/pub/user?id=' + id
  242. })
  243. }
  244. const deleteGroup = () => {
  245. uni.showModal({
  246. title: '提示',
  247. content: '是否解散此群??',
  248. cancelText: '取消',
  249. confirmText: '确定',
  250. success: res => {
  251. if (res.confirm) {
  252. notify(group.master, group.id)
  253. GroupApi.delete(group.id).then((res) => {
  254. if (res) {
  255. groupStore.loadGroupList()
  256. MessageUtils.message('群已经解散')
  257. uni.reLaunch({
  258. url: '/pages/index/index'
  259. })
  260. }
  261. })
  262. }
  263. }
  264. })
  265. }
  266. //离开群
  267. const exitGroup = () => {
  268. uni.showModal({
  269. title: '提示',
  270. content: '是否退出此群??',
  271. cancelText: '取消',
  272. confirmText: '确定',
  273. success: res => {
  274. if (res.confirm) {
  275. GroupApi.exit(group.id).then((res) => {
  276. if (res) {
  277. chatStore.delChat(group.id)
  278. groupStore.loadGroupList()
  279. notify(group.master, group.id, store.getUser()?.name + ' 已退群')
  280. MessageUtils.message('已退群')
  281. uni.reLaunch({
  282. url: '/pages/index/index'
  283. })
  284. }
  285. })
  286. }
  287. }
  288. })
  289. }
  290. /**
  291. * 通知用户群已经被删除
  292. * @param master 群主id
  293. * @param groupId 群id
  294. * @param content 通知内容
  295. */
  296. const notify = (master: string, groupId: string, content: string = '群已被解散') => {
  297. if (master) {
  298. UserApi.getUser(master).then((res) => {
  299. let user = res.data
  300. let msg: Message = {
  301. chatId: groupId,
  302. messageType: MessageType.text,
  303. fromId: master,
  304. mine: true,
  305. content: content,
  306. timestamp: new Date().getTime(),
  307. type: ChatType.GROUP
  308. }
  309. useWsStore().sendMessage(msg)
  310. })
  311. }
  312. }
  313. /**
  314. * 打开聊天室
  315. */
  316. const openChat = () => {
  317. chatStore.openChat({
  318. id: group.id,
  319. name: group.name,
  320. avatar: group.avatar,
  321. type: ChatType.GROUP,
  322. unreadCount: 0,
  323. isLoading: false,
  324. loaded: true
  325. })
  326. uni.navigateTo({
  327. url: '/pages/chat/chat?id=' + group.id + '&type=' + ChatType.GROUP
  328. })
  329. }
  330. const {waitCheckMap} = storeToRefs(groupStore)
  331. </script>
  332. <style scoped>
  333. .grid > view {
  334. height: 140upx;
  335. margin-top: 10px;
  336. }
  337. .group-btn {
  338. font-size: 2rem;
  339. border: 1px solid #ccc;
  340. width: 100upx;
  341. height: 100upx;
  342. }
  343. .grid > .grid-btn {
  344. display: flex;
  345. align-items: center;
  346. justify-content: center;
  347. }
  348. .logoImg{
  349. width: 100upx;
  350. height: 100upx;
  351. border-radius: 8upx;
  352. }
  353. .group-user {
  354. padding: 5px;
  355. text-align: center;
  356. }
  357. .group-user .text {
  358. white-space: nowrap;
  359. text-overflow: ellipsis;
  360. overflow: hidden;
  361. text-align: center;
  362. margin-top: 10upx;
  363. }
  364. .announcement{
  365. text-indent: 2rem;
  366. word-break: break-all;
  367. line-height: 1.5;
  368. padding: 20px !important;
  369. background: #eee !important;;
  370. }
  371. .item_list{
  372. display: flex;
  373. flex-wrap: wrap;
  374. justify-content: flex-start;
  375. }
  376. .item_content{
  377. display: flex;
  378. flex-direction: column;
  379. align-items: center;
  380. width: 154upx;
  381. height: 154upx;
  382. margin: 5px;
  383. box-sizing: border-box;
  384. }
  385. .user_name{
  386. max-width: 154upx;
  387. text-align: center;
  388. overflow: hidden;
  389. text-overflow: ellipsis;
  390. display: -webkit-box;
  391. -webkit-line-clamp: 1;
  392. -webkit-box-orient: vertical;
  393. }
  394. </style>