group-edit.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view 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. console.log(res)
  51. this.group.headImage = res.data.originUrl;
  52. this.group.headImageThumb = res.data.thumbUrl;
  53. },
  54. modifyGroup() {
  55. this.$http({
  56. url: "/group/modify",
  57. method: "PUT",
  58. data: this.group
  59. }).then((group) => {
  60. this.$store.commit("updateGroup", group);
  61. uni.showToast({
  62. title: "修改群聊信息成功",
  63. icon: 'none'
  64. });
  65. setTimeout(()=>{
  66. uni.navigateBack();
  67. },1000);
  68. })
  69. },
  70. createNewGroup() {
  71. this.$http({
  72. url: "/group/create",
  73. method: 'POST',
  74. data: this.group
  75. }).then((group) => {
  76. this.$store.commit("addGroup", group);
  77. uni.showToast({
  78. title: `群聊创建成功,快邀请小伙伴进群吧`,
  79. icon: 'none',
  80. duration: 2000
  81. });
  82. setTimeout(()=>{
  83. uni.navigateTo({
  84. url: "/pages/group/group-info?id="+group.id
  85. });
  86. },2000)
  87. })
  88. },
  89. loadGroupInfo(id) {
  90. this.$http({
  91. url: `/group/find/${id}`,
  92. method: 'GET'
  93. }).then((group) => {
  94. this.group = group;
  95. // 更新聊天页面的群聊信息
  96. this.$store.commit("updateChatFromGroup", group);
  97. // 更新聊天列表的群聊信息
  98. this.$store.commit("updateGroup", group);
  99. });
  100. },
  101. initNewGroup() {
  102. let userInfo = this.$store.state.userStore.userInfo;
  103. this.group = {
  104. name: `${userInfo.userName}创建的群聊`,
  105. headImage: userInfo.headImage,
  106. headImageThumb: userInfo.headImageThumb,
  107. aliasName: userInfo.nickName,
  108. ownerId: this.$store.state.userStore.userInfo.id
  109. }
  110. }
  111. },
  112. computed: {
  113. isOwner() {
  114. return this.$store.state.userStore.userInfo.id == this.group.ownerId
  115. }
  116. },
  117. onLoad(options) {
  118. if (options.id) {
  119. // 修改群聊
  120. this.loadGroupInfo(options.id);
  121. } else {
  122. // 创建群聊
  123. this.initNewGroup();
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="scss" scoped>
  129. .group-edit {
  130. padding: 20rpx;
  131. .head-image {
  132. width: 200rpx;
  133. height: 200rpx;
  134. }
  135. }
  136. </style>