index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <view class="create_group_container">
  3. <custom-nav-bar title="发起群聊" />
  4. <u-toast ref="uToast"></u-toast>
  5. <view class="main">
  6. <view class="group_base_info">
  7. <my-avatar
  8. @click="chooseImage"
  9. :isGroup="true"
  10. :src="groupFaceUrl"
  11. size="44"/>
  12. <u--input
  13. placeholder="取个群名称方便后续搜索"
  14. border="none"
  15. maxlength="16"
  16. v-model="groupName"></u--input>
  17. </view>
  18. <view class="member_row" @click="toChooseMember">
  19. <view class="desc_title">
  20. <text>群成员</text>
  21. <text>{{ `${checkedMemberList.length}人` }}</text>
  22. </view>
  23. <view class="member_list">
  24. <view
  25. v-for="member in checkedMemberList.slice(0, 5)"
  26. :key="member.userID"
  27. class="member_item">
  28. <my-avatar :src="member.faceURL" :desc="member.nickname" size="42" />
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="action_bar">
  34. <u-button
  35. :loading="createLoading"
  36. :disabled="disabledCreate"
  37. @click="complateCreate"
  38. type="primary"
  39. text="完成创建">
  40. </u-button>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import { ContactChooseTypes } from "../../../constant";
  46. import IMSDK, {
  47. GroupType,
  48. IMMethods,
  49. SessionType,
  50. } from "openim-uniapp-polyfill";
  51. import CustomNavBar from "../../../components/CustomNavBar/index.vue";
  52. import MyAvatar from "../../../components/MyAvatar/index.vue";
  53. import { navigateToDesignatedConversation } from "../../../util/imCommon";
  54. import { getPurePath, toastWithCallback } from "../../../util/common";
  55. export default {
  56. components: {
  57. CustomNavBar,
  58. MyAvatar,
  59. },
  60. data() {
  61. return {
  62. groupName: "",
  63. groupFaceUrl: "",
  64. checkedMemberList: [],
  65. fileList: [],
  66. createLoading: false,
  67. };
  68. },
  69. computed: {
  70. disabledCreate() {
  71. return !this.groupName || this.checkedMemberList.length === 0;
  72. },
  73. },
  74. onLoad(options) {
  75. const { checkedMemberList } = options;
  76. this.checkedMemberList = checkedMemberList
  77. ? JSON.parse(checkedMemberList)
  78. : [];
  79. },
  80. methods: {
  81. toChooseMember() {
  82. uni.navigateTo({
  83. url: `/pages_im/pages/common/contactChoose/index?type=${
  84. ContactChooseTypes.GetList
  85. }&checkedUserInfoList=${JSON.stringify(this.checkedMemberList)}`,
  86. });
  87. },
  88. complateCreate() {
  89. this.createLoading = true;
  90. const options = {
  91. adminUserIDs: [],
  92. memberUserIDs: this.checkedMemberList.map((member) => member.userID),
  93. groupInfo: {
  94. groupType: GroupType.WorkingGroup,
  95. groupName: this.groupName,
  96. faceURL: this.groupFaceUrl,
  97. },
  98. };
  99. IMSDK.asyncApi(IMSDK.IMMethods.CreateGroup, IMSDK.uuid(), options)
  100. .then(({ data }) => {
  101. toastWithCallback("创建成功", () =>
  102. navigateToDesignatedConversation(
  103. data.groupID,
  104. SessionType.WorkingGroup,
  105. true,
  106. ),
  107. );
  108. })
  109. .catch((err) => {
  110. console.log(err);
  111. uni.$u.toast("创建失败");
  112. })
  113. .finally(() => (this.createLoading = false));
  114. },
  115. getCheckedUsers(list) {
  116. this.checkedMemberList = [...list];
  117. },
  118. chooseImage() {
  119. uni.chooseImage({
  120. count: 1,
  121. sizeType: ["compressed"],
  122. success: async ({ tempFilePaths }) => {
  123. const path = tempFilePaths[0];
  124. const nameIdx = path.lastIndexOf("/") + 1;
  125. const typeIdx = path.lastIndexOf(".") + 1;
  126. const fileName = path.slice(nameIdx);
  127. const fileType = path.slice(typeIdx);
  128. try {
  129. const {
  130. data: { url },
  131. } = await IMSDK.asyncApi(IMMethods.UploadFile, IMSDK.uuid(), {
  132. filepath: getPurePath(tempFilePaths[0]),
  133. name: fileName,
  134. contentType: fileType,
  135. uuid: IMSDK.uuid(),
  136. });
  137. this.groupFaceUrl = url;
  138. } catch (error) {
  139. uni.$u.toast("上传失败");
  140. }
  141. },
  142. fail: function (err) {
  143. console.log(err)
  144. if(err.errMsg === 'chooseImage:fail cancel') return
  145. uni.$u.toast("上传失败");
  146. },
  147. });
  148. },
  149. },
  150. };
  151. </script>
  152. <style lang="scss">
  153. .create_group_container {
  154. @include colBox(false);
  155. height: 100vh;
  156. background-color: #f6f6f6;
  157. .main {
  158. display: flex;
  159. flex-direction: column;
  160. flex: 1;
  161. }
  162. .group_base_info {
  163. @include vCenterBox();
  164. padding: 44rpx;
  165. background-color: #fff;
  166. margin: 36rpx 0;
  167. .u-input {
  168. margin-left: 48rpx;
  169. }
  170. }
  171. .member_row {
  172. padding: 44rpx;
  173. background-color: #fff;
  174. color: #999;
  175. .desc_title {
  176. @include vCenterBox();
  177. justify-content: space-between;
  178. }
  179. .member_list {
  180. display: flex;
  181. flex-wrap: wrap;
  182. margin-top: 24rpx;
  183. .member_item {
  184. @include colBox(false);
  185. align-items: center;
  186. margin-right: 12rpx;
  187. .member_name {
  188. @include nomalEllipsis();
  189. max-width: 42px;
  190. margin-top: 12rpx;
  191. }
  192. }
  193. }
  194. }
  195. .action_bar {
  196. background-color: #fff;
  197. padding: 44rpx 44rpx;
  198. }
  199. }
  200. </style>