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.userID" :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. console.log(options);
  100. IMSDK.asyncApi(IMSDK.IMMethods.CreateGroup, IMSDK.uuid(), options)
  101. .then(({ data }) => {
  102. toastWithCallback("创建成功", () =>
  103. navigateToDesignatedConversation(
  104. data.groupID,
  105. SessionType.WorkingGroup,
  106. true,
  107. ),
  108. );
  109. })
  110. .catch((err) => {
  111. console.log(err);
  112. uni.$u.toast("创建失败");
  113. })
  114. .finally(() => (this.createLoading = false));
  115. },
  116. getCheckedUsers(list) {
  117. this.checkedMemberList = [...list];
  118. },
  119. chooseImage() {
  120. uni.chooseImage({
  121. count: 1,
  122. sizeType: ["compressed"],
  123. success: async ({ tempFilePaths }) => {
  124. const path = tempFilePaths[0];
  125. const nameIdx = path.lastIndexOf("/") + 1;
  126. const typeIdx = path.lastIndexOf(".") + 1;
  127. const fileName = path.slice(nameIdx);
  128. const fileType = path.slice(typeIdx);
  129. try {
  130. const {
  131. data: { url },
  132. } = await IMSDK.asyncApi(IMMethods.UploadFile, IMSDK.uuid(), {
  133. filepath: getPurePath(tempFilePaths[0]),
  134. name: fileName,
  135. contentType: fileType,
  136. uuid: IMSDK.uuid(),
  137. });
  138. this.groupFaceUrl = url;
  139. } catch (error) {
  140. uni.$u.toast("上传失败");
  141. }
  142. },
  143. fail: function (err) {
  144. console.log(err)
  145. if(err.errMsg === 'chooseImage:fail cancel') return
  146. uni.$u.toast("上传失败");
  147. },
  148. });
  149. },
  150. },
  151. };
  152. </script>
  153. <style lang="scss">
  154. .create_group_container {
  155. @include colBox(false);
  156. height: 100vh;
  157. background-color: #f6f6f6;
  158. .main {
  159. display: flex;
  160. flex-direction: column;
  161. flex: 1;
  162. }
  163. .group_base_info {
  164. @include vCenterBox();
  165. padding: 44rpx;
  166. background-color: #fff;
  167. margin: 36rpx 0;
  168. .u-input {
  169. margin-left: 48rpx;
  170. }
  171. }
  172. .member_row {
  173. padding: 44rpx;
  174. background-color: #fff;
  175. color: #999;
  176. .desc_title {
  177. @include vCenterBox();
  178. justify-content: space-between;
  179. }
  180. .member_list {
  181. display: flex;
  182. flex-wrap: wrap;
  183. margin-top: 24rpx;
  184. .member_item {
  185. @include colBox(false);
  186. align-items: center;
  187. margin-right: 12rpx;
  188. .member_name {
  189. @include nomalEllipsis();
  190. max-width: 42px;
  191. margin-top: 12rpx;
  192. }
  193. }
  194. }
  195. }
  196. .action_bar {
  197. background-color: #fff;
  198. padding: 44rpx 44rpx;
  199. }
  200. }
  201. </style>