group-edit.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <view v-if="$store.state.userStore.userInfo.type == 1" class="page group-edit">
  3. <uni-forms ref="form" :modelValue="group" :rules="rules" validate-trigger="bind" label-position="top"
  4. label-width="100%">
  5. <uni-forms-item label="群聊头像:" name="headImage">
  6. <image-upload :disabled="!isOwner" :onSuccess="onUnloadImageSuccess">
  7. <image :src="group.headImage" class="head-image"></image>
  8. </image-upload>
  9. </uni-forms-item>
  10. <uni-forms-item label="群聊名称:" name="name" :required="true">
  11. <uni-easyinput type="text" v-model="group.name" :disabled="!isOwner" placeholder="请输入群聊名称" />
  12. </uni-forms-item>
  13. <uni-forms-item label="群聊备注:" name="remark">
  14. <uni-easyinput v-model="group.remark" type="text" placeholder="请输入群聊备注" />
  15. </uni-forms-item>
  16. <uni-forms-item label="我在本群的昵称:" name="email">
  17. <uni-easyinput v-model="group.aliasName" type="text" placeholder="请输入群聊昵称" />
  18. </uni-forms-item>
  19. <uni-forms-item label="群公告:" name="notice">
  20. <uni-easyinput type="textarea" v-model="group.notice" :disabled="!isOwner" placeholder="请输入群公告" />
  21. </uni-forms-item>
  22. </uni-forms>
  23. <button type="primary" @click="submit()">提交</button>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. group: {},
  31. rules: {
  32. name: {
  33. rules: [{
  34. required: true,
  35. errorMessage: '请输入群聊名称',
  36. }]
  37. }
  38. }
  39. }
  40. },
  41. methods: {
  42. submit() {
  43. if (this.group.id) {
  44. this.modifyGroup();
  45. } else {
  46. this.createNewGroup();
  47. }
  48. },
  49. onUnloadImageSuccess(file, res) {
  50. this.group.headImage = res.data.originUrl;
  51. this.group.headImageThumb = res.data.thumbUrl;
  52. },
  53. modifyGroup() {
  54. this.$http({
  55. url: "/group/modify",
  56. method: "PUT",
  57. data: this.group
  58. }).then((group) => {
  59. this.$store.commit("updateGroup", group);
  60. uni.showToast({
  61. title: "修改群聊信息成功",
  62. icon: 'none'
  63. });
  64. setTimeout(() => {
  65. let pages = getCurrentPages();
  66. let prevPage = pages[pages.length - 2];
  67. prevPage.$vm.loadGroupInfo();
  68. uni.navigateBack();
  69. }, 1000);
  70. })
  71. },
  72. createNewGroup() {
  73. this.$http({
  74. url: "/group/create",
  75. method: 'POST',
  76. data: this.group
  77. }).then((group) => {
  78. this.$store.commit("addGroup", group);
  79. uni.showToast({
  80. title: `群聊创建成功,快邀请小伙伴进群吧`,
  81. icon: 'none',
  82. duration: 1500
  83. });
  84. setTimeout(() => {
  85. uni.navigateTo({
  86. url: "/pages/group/group-info?id=" + group.id
  87. });
  88. }, 1500)
  89. })
  90. },
  91. loadGroupInfo(id) {
  92. this.$http({
  93. url: `/group/find/${id}`,
  94. method: 'GET'
  95. }).then((group) => {
  96. this.group = group;
  97. // 更新聊天页面的群聊信息
  98. this.$store.commit("updateChatFromGroup", group);
  99. // 更新聊天列表的群聊信息
  100. this.$store.commit("updateGroup", group);
  101. });
  102. },
  103. initNewGroup() {
  104. let userInfo = this.$store.state.userStore.userInfo;
  105. this.group = {
  106. name: `${userInfo.userName}创建的群聊`,
  107. headImage: userInfo.headImage,
  108. headImageThumb: userInfo.headImageThumb,
  109. aliasName: userInfo.nickName,
  110. ownerId: this.$store.state.userStore.userInfo.id
  111. }
  112. }
  113. },
  114. computed: {
  115. isOwner() {
  116. return this.$store.state.userStore.userInfo.id == this.group.ownerId
  117. }
  118. },
  119. onLoad(options) {
  120. if (options.id) {
  121. // 修改群聊
  122. this.loadGroupInfo(options.id);
  123. } else {
  124. // 创建群聊
  125. this.initNewGroup();
  126. }
  127. }
  128. }
  129. </script>
  130. <style lang="scss" scoped>
  131. .group-edit {
  132. padding: 20rpx;
  133. .head-image {
  134. width: 200rpx;
  135. height: 200rpx;
  136. }
  137. }
  138. </style>