index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view> </view>
  3. </template>
  4. <script>
  5. import { EMClient } from '@/EaseIM';
  6. import { MESSAGE_TYPE } from '@/EaseIM/constant';
  7. import { emMessages } from '@/EaseIM/emApis';
  8. const { sendDisplayMessages } = emMessages();
  9. const apiUrl = EMClient.apiUrl;
  10. const orgName = EMClient.orgName;
  11. const appName = EMClient.appName;
  12. const uploadTargetUrl = `${apiUrl}/${orgName}/${appName}/chatfiles`;
  13. const token = EMClient.token;
  14. export default {
  15. data() {
  16. return {
  17. platForm: '',
  18. };
  19. },
  20. computed: {
  21. chattingId() {
  22. return this.$store.getters.chattingId;
  23. },
  24. chattingChatType() {
  25. return this.$store.getters.chattingChatType;
  26. },
  27. },
  28. mounted() {
  29. const res = uni.getSystemInfoSync();
  30. this.platForm = res?.uniPlatform;
  31. },
  32. methods: {
  33. //打开图库
  34. openPhotoAlbum() {
  35. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-LARK || MP-JD
  36. uni.chooseMedia({
  37. count: 1,
  38. mediaType: ['image'],
  39. sourceType: ['album'],
  40. camera: 'back',
  41. success: (res) => {
  42. console.log('拍照选取的图片', res);
  43. this.upLoadImage(res);
  44. },
  45. fail: (err) => {
  46. uni.showToast({
  47. title: err.errMsg,
  48. icon: 'none',
  49. });
  50. },
  51. });
  52. // #endif
  53. uni.chooseImage({
  54. count: 1,
  55. sizeType: ['original', 'compressed'],
  56. sourceType: ['album'],
  57. success: (res) => {
  58. this.upLoadImage(res);
  59. },
  60. fail: (err) => {
  61. uni.showToast({
  62. title: err.errMsg,
  63. icon: 'none',
  64. });
  65. },
  66. });
  67. },
  68. //打开相机
  69. openCamera() {
  70. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-LARK || MP-JD
  71. uni.chooseMedia({
  72. count: 1,
  73. mediaType: ['image'],
  74. sourceType: ['camera'],
  75. camera: 'back',
  76. success: (res) => {
  77. console.log('拍照选取的图片', res);
  78. this.upLoadImage(res);
  79. },
  80. fail: (err) => {
  81. uni.showToast({
  82. title: err.errMsg,
  83. icon: 'none',
  84. });
  85. },
  86. });
  87. // #endif
  88. uni.chooseImage({
  89. count: 1,
  90. sizeType: ['original', 'compressed'],
  91. sourceType: ['camera'],
  92. success: (res) => {
  93. this.upLoadImage(res);
  94. },
  95. fail: (err) => {
  96. uni.showToast({
  97. title: err.errMsg,
  98. icon: 'none',
  99. });
  100. },
  101. });
  102. },
  103. async upLoadImage(res) {
  104. const tempFile = res?.tempFiles[0];
  105. const tempFilePath =
  106. (res && res.tempFilePaths && res.tempFilePaths[0]) ||
  107. (res && res.tempFiles && res.tempFiles[0].tempFilePath);
  108. console.log('tempFilePath', tempFilePath);
  109. const imageName = tempFile?.name || 'image' + Date.now();
  110. const imageType = tempFile?.type?.split('/')[1] || '';
  111. let width = '';
  112. let height = '';
  113. uni.getImageInfo({
  114. src: tempFilePath,
  115. success: (res) => {
  116. console.log('>>>>获取图片信息成功', res);
  117. width = res.width;
  118. height = res.height;
  119. },
  120. fail: (err) => {
  121. console.log('>>>>>>获取图片信息失败', err);
  122. },
  123. });
  124. uni.uploadFile({
  125. url: uploadTargetUrl,
  126. filePath: tempFilePath,
  127. fileType: 'image',
  128. name: 'file',
  129. header: {
  130. Authorization: 'Bearer ' + token,
  131. },
  132. success: (res) => {
  133. console.log('上传图片成功', res);
  134. if (res.statusCode === 400) {
  135. // 图片上传阿里云检验不合法
  136. var errData = JSON.parse(res.data);
  137. if (errData.error === 'content improper') {
  138. uni.showToast({ title: '图片不合法', icon: 'none' });
  139. return false;
  140. }
  141. }
  142. this.sendImagesMessage(res, { imageName, width, height, imageType });
  143. console.log(
  144. '>>>>>>>执行发送图片信息',
  145. imageName,
  146. width,
  147. height,
  148. imageType
  149. );
  150. uni.showToast({ title: '图片已上传', icon: 'none' });
  151. },
  152. fail: (e) => {
  153. console.log('>>>>>上传失败', e);
  154. uni.showToast({ title: '图片上传失败', icon: 'none' });
  155. },
  156. });
  157. },
  158. async sendImagesMessage(res, { imageName, width, height, imageType }) {
  159. console.log('>>>>>开始执行发送图片消息', res);
  160. const dataObj = JSON.parse(res.data); // 接收消息对象
  161. var params = {
  162. type: MESSAGE_TYPE.IMAGE,
  163. width: width,
  164. height: height,
  165. url: dataObj.uri + '/' + dataObj.entities[0].uuid,
  166. // 消息接收方:单聊为对方用户 ID,群聊和聊天室分别为群组 ID 和聊天室 ID。
  167. to: this.chattingId,
  168. // 会话类型:单聊、群聊和聊天室分别为 `singleChat`、`groupChat` 和 `chatRoom`。
  169. chatType: this.chattingChatType,
  170. ext: {
  171. imageName: imageName,
  172. imageType: imageType,
  173. },
  174. };
  175. try {
  176. const res = await sendDisplayMessages({ ...params });
  177. } catch (error) {
  178. console.log('>>>>>图片消息发送失败', error);
  179. uni.showToast({
  180. title: '消息发送失败',
  181. icon: 'none',
  182. });
  183. } finally {
  184. this.$emit('onCloseAllShowContainer');
  185. }
  186. },
  187. },
  188. };
  189. </script>
  190. <style scoped></style>