edit.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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">{{ title }}</text>
  7. </template>
  8. </cu-custom>
  9. <view class="padding">
  10. <view class="padding-xl text-center">
  11. <image :src="group.avatar" class="cu-avatar round xl logo-xxl" mode="aspectFill" @tap="chooseImage"></image>
  12. </view>
  13. <uni-forms ref="groupForm" :model="group" :label-width="130" :rules="rules">
  14. <uni-forms-item class="text-black text-bold" label="群名称" name="name">
  15. <uni-easyinput type="text" v-model="group.name"/>
  16. </uni-forms-item>
  17. <uni-forms-item class="text-black text-bold" label="开启邀请">
  18. <switch @change="openInviteSwitch" :class="openInvite?'checked':''" :checked="openInvite"/>
  19. </uni-forms-item>
  20. <uni-forms-item class="text-black text-bold" label="邀请审核" v-if="showInviteCheck">
  21. <switch @change="inviteCheckSwitch" :class="inviteCheck?'checked':''" :checked="inviteCheck"/>
  22. </uni-forms-item>
  23. <uni-forms-item class="text-black text-bold" label="全体禁言">
  24. <switch @change="prohibitionSwitch" :class="prohibition?'checked':''" :checked="prohibition"/>
  25. </uni-forms-item>
  26. <uni-forms-item class="text-black text-bold" label="禁加好友">
  27. <switch @change="prohibitFriendSwitch" :class="prohibitFriend?'checked':''" :checked="prohibitFriend"/>
  28. </uni-forms-item>
  29. <view class="cu-bar bg-white">
  30. <button class="cu-btn bg-zblue lg margin-top border" style="width: 100%;" @tap="save">保存</button>
  31. </view>
  32. </uni-forms>
  33. </view>
  34. </view>
  35. </template>
  36. <script setup lang="ts">
  37. import {getCurrentInstance, reactive, ref} from "vue";
  38. import CuCustom from '@/colorui/components/cu-custom.vue'
  39. import GroupApi from "@/api/GroupApi";
  40. import MessageUtils from "@/utils/MessageUtils";
  41. import Auth from "@/api/Auth";
  42. import FetchRequest from "@/api/FetchRequest";
  43. import {onLoad} from '@dcloudio/uni-app';
  44. import ChatType from "@/utils/ChatType";
  45. import {useChatStore} from "@/store/chatStore";
  46. import type Group from "@/mode/Group";
  47. import {useGroupStore} from "@/store/groupStore";
  48. import UniForms from "@/uni_modules/uni-forms/components/uni-forms/uni-forms.vue";
  49. import UniFormsItem from "@/uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.vue";
  50. import UniEasyinput from "@/uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue";
  51. import VimConfig from "@/config/VimConfig";
  52. const fontValue=ref(Auth.getfontSize());
  53. const groupStore = useGroupStore();
  54. const chatStore = useChatStore();
  55. //@ts-ignore
  56. const {proxy} = getCurrentInstance();
  57. const map = ref(new Map());
  58. const hidden = ref(true);
  59. const title = ref('新建群组');
  60. const rules = {
  61. name: {
  62. rules: [
  63. {
  64. required: true,
  65. errorMessage: '请输入群名称',
  66. },
  67. {
  68. minLength: 2,
  69. maxLength: 8,
  70. errorMessage: '群名称长度在 {minLength} 到 {maxLength} 个字符',
  71. }
  72. ]
  73. }
  74. }
  75. const group = reactive<Group>({
  76. id: "",
  77. master: "",
  78. name: '',
  79. avatar: '',
  80. openInvite: '0',
  81. inviteCheck: '0',
  82. prohibition: '0',
  83. prohibitFriend: '0',
  84. announcement: ''
  85. })
  86. const groupForm = ref();
  87. const openInvite = ref(false);
  88. const showInviteCheck = ref(false);
  89. const inviteCheck = ref(false);
  90. const prohibition = ref(false);
  91. const prohibitFriend = ref(false);
  92. const openInviteSwitch = (e: any) => {
  93. openInvite.value = e.detail.value
  94. openInviteChange();
  95. }
  96. const inviteCheckSwitch = (e: any) => {
  97. inviteCheck.value = e.detail.value
  98. }
  99. const prohibitionSwitch = (e: any) => {
  100. prohibition.value = e.detail.value
  101. }
  102. const prohibitFriendSwitch = (e: any) => {
  103. prohibitFriend.value = e.detail.value
  104. }
  105. /**
  106. * 开启邀请改变
  107. */
  108. const openInviteChange = () => {
  109. if (openInvite.value) {
  110. showInviteCheck.value = true
  111. } else {
  112. showInviteCheck.value = false
  113. }
  114. }
  115. /**
  116. * 初始化
  117. */
  118. onLoad((opt) => {
  119. if (opt?.id) {
  120. group.id = opt.id;
  121. GroupApi.get(group.id)
  122. .then((res) => {
  123. group.id = res.data.id;
  124. group.name = res.data.name;
  125. group.avatar = res.data.avatar;
  126. group.announcement = res.data.announcement;
  127. group.openInvite = res.data.openInvite;
  128. group.inviteCheck = res.data.inviteCheck;
  129. group.prohibition = res.data.prohibition;
  130. group.prohibitFriend = res.data.prohibitFriend;
  131. openInvite.value = res.data.openInvite === '1';
  132. openInviteChange();
  133. inviteCheck.value = res.data.inviteCheck === '1';
  134. prohibition.value = res.data.prohibition === '1';
  135. prohibitFriend.value = res.data.prohibitFriend === '1';
  136. title.value='编辑群组'
  137. })
  138. }
  139. })
  140. const chooseImage = () => {
  141. uni.chooseImage({
  142. count: 1, //默认9
  143. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  144. sourceType: ['album'], //从相册选择
  145. success: (res) => {
  146. uni.uploadFile({
  147. url: `${FetchRequest.getHost()}/${VimConfig.uploadType}/upload`, //仅为示例,非真实的接口地址
  148. filePath: res.tempFilePaths[0],
  149. name: 'file',
  150. header: {
  151. "Access-Control-Allow-Origin": "*",
  152. "Authorization": "Bearer " + Auth.getToken(),
  153. },
  154. formData: {
  155. 'type': 'image'
  156. },
  157. success: (res) => {
  158. group.avatar = JSON.parse(res.data).url
  159. },
  160. fail: () => {
  161. MessageUtils.error('上传失败')
  162. },
  163. });
  164. }
  165. });
  166. }
  167. const save = () => {
  168. groupForm.value.validate().then(() => {
  169. // //新增
  170. if (!group.id) {
  171. GroupApi.save(group.name, group.avatar, openInvite.value ? '1' : '0', inviteCheck.value ? '1' : '0',prohibition.value ? '1' : '0',prohibitFriend.value ? '1' : '0', group.announcement)
  172. .then(() => {
  173. MessageUtils.message('保存成功')
  174. groupStore.loadGroupList();
  175. uni.navigateBack()
  176. })
  177. .catch((err) => {
  178. MessageUtils.error(err)
  179. })
  180. } else {
  181. GroupApi.update(group.id, group.name, group.avatar, openInvite.value ? '1' : '0', inviteCheck.value ? '1' : '0',prohibition.value ? '1' : '0',prohibitFriend.value ? '1' : '0', group.announcement)
  182. .then(() => {
  183. MessageUtils.message('更新成功')
  184. chatStore.updateChat(group.id, group.name, group.avatar)
  185. groupStore.loadGroupList();
  186. uni.navigateTo({
  187. url: '/pages/group/group?id=' + group.id
  188. })
  189. })
  190. .catch((err) => {
  191. MessageUtils.error(err)
  192. })
  193. }
  194. }).catch((err: any) => {
  195. console.log('表单错误信息:', err);
  196. })
  197. }
  198. </script>
  199. <style scoped>
  200. .title {
  201. color: #666666;
  202. text-align: left;
  203. width: 150upx;
  204. }
  205. .border {
  206. border: 1px solid #ccc;
  207. padding: 10upx;
  208. min-height: 70upx;
  209. }
  210. </style>