add-users.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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">{{ groupName }}</text>
  7. </template>
  8. </cu-custom>
  9. <index-bar :indexMap="map">
  10. <checkbox-group class="block" @change="checkboxChange">
  11. <view v-for="(item,key) in map.keys()" :key="key" :class="'indexItem-' + item" :id="'indexes-' + item"
  12. :data-index="item">
  13. <view class="padding">{{ item }}</view>
  14. <view class="cu-list menu-avatar no-padding">
  15. <view class="cu-item" v-for="(user,sub) in map.get(item)" :key="sub">
  16. <checkbox class="checkbox" :value="user.id" :disabled="checkedIds.indexOf(user.id)>-1"/>
  17. <image class="cu-avatar radius lg" :src="user.avatar"></image>
  18. <view class="content">
  19. <view class="text-black text-bold">{{ user.name }}</view>
  20. <view class="text-black text-sm">
  21. {{ user.name }}
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. <view style="height: 140rpx;"></view>
  28. </checkbox-group>
  29. </index-bar>
  30. <view class="cu-form-group margin-top text-center" style="bottom:0;position: fixed;width: 100%;">
  31. <button class="cu-btn bg-cyan lg margin-top" style="width: 100%;margin-bottom: 30rpx;" @tap="save">提交</button>
  32. </view>
  33. </view>
  34. </template>
  35. <script setup lang="ts">
  36. import {getCurrentInstance, ref} from "vue";
  37. import FriendApi from "@/api/FriendApi";
  38. import Tools from "@/utils/Tools";
  39. import CuCustom from '@/colorui/components/cu-custom.vue'
  40. import GroupApi from "@/api/GroupApi";
  41. import MessageUtils from "@/utils/MessageUtils";
  42. import {onLoad} from '@dcloudio/uni-app';
  43. import type User from "@/mode/User";
  44. import IndexBar from '@/components/IndexBar.vue'
  45. import ChatType from "@/utils/ChatType";
  46. import MessageType from "@/utils/MessageType";
  47. import SendCode from "@/utils/SendCode";
  48. import {useWsStore} from "@/store/WsStore";
  49. import {useUserStore} from "@/store/userStore";
  50. import VimAvatar from "@/components/VimAvatar.vue";
  51. import Auth from "@/api/Auth";
  52. const fontValue=ref(Auth.getfontSize());
  53. //@ts-ignore
  54. const {proxy} = getCurrentInstance();
  55. const userStore = useUserStore();
  56. const map = ref(new Map());
  57. const hidden = ref(true);
  58. const avatar = ref('');
  59. const groupId = ref('');
  60. const groupName = ref('');
  61. const master = ref('');
  62. //选中的用户id
  63. const ids = ref([]);
  64. const checkedIds = ref([]);
  65. const user = userStore.getUser()
  66. const checkboxChange = (e: any) => {
  67. ids.value = e.detail.value;
  68. }
  69. onLoad((opt) => {
  70. const gId = opt?.id;
  71. if (gId) {
  72. groupId.value = gId;
  73. GroupApi.get(groupId.value)
  74. .then((res) => {
  75. groupName.value = res.data.name;
  76. master.value = res.data.master;
  77. avatar.value = res.data.avatar;
  78. return GroupApi.users(groupId.value)
  79. })
  80. .then((res) => {
  81. checkedIds.value = res.data.map((item: User) => {
  82. return item.id;
  83. });
  84. })
  85. }
  86. })
  87. const loadFriends = () => {
  88. FriendApi.friends().then(res => {
  89. map.value = Tools.listToMap(res.data);
  90. })
  91. }
  92. const loadUsers = () => {
  93. GroupApi.users(groupId.value)
  94. .then((res) => {
  95. checkedIds.value = res.data.map((item: User) => {
  96. return item.id;
  97. });
  98. uni.navigateTo({
  99. url: '/pages/group/group?id=' + groupId.value
  100. })
  101. })
  102. .catch((err) => {
  103. MessageUtils.error(err)
  104. })
  105. }
  106. const save = () => {
  107. if (ids.value.length > 0) {
  108. GroupApi.addUsers(groupId.value, ids.value)
  109. .then((res) => {
  110. if (res.data.length > 0) {
  111. loadUsers();
  112. } else {
  113. MessageUtils.message('请等待审核');
  114. if (user) {
  115. notifyMaster(master.value, user.id)
  116. }
  117. }
  118. })
  119. }
  120. }
  121. /**
  122. * 通知群主
  123. * @param masterId 群主id
  124. * @param fromId 发送人
  125. */
  126. const notifyMaster = (masterId: string, fromId: string) => {
  127. if (masterId) {
  128. const sendInfo = {
  129. code: SendCode.GROUP_VALIDATE,
  130. message: {
  131. mine: true,
  132. fromId: fromId,
  133. chatId: masterId,
  134. type: ChatType.FRIEND,
  135. messageType: MessageType.text,
  136. content: '',
  137. timestamp: new Date().getTime()
  138. }
  139. }
  140. useWsStore().send(JSON.stringify(sendInfo))
  141. }
  142. }
  143. loadFriends();
  144. </script>
  145. <style scoped>
  146. .cu-list.menu-avatar > .cu-item > .cu-avatar {
  147. left: 3.9375rem;
  148. }
  149. .cu-list.menu-avatar > .cu-item .content {
  150. left: 7.5625rem;
  151. }
  152. .cu-list.menu-avatar > .cu-item .checkbox {
  153. left: 1rem;
  154. position: absolute;
  155. }
  156. </style>