image.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. let WebIM = require("../../../../../utils/WebIM")["default"];
  6. let msgType = require("../../../msgtype");
  7. let disp = require("../../../../../utils/broadcast");
  8. let msgStorage = require("../../../msgstorage");
  9. export default {
  10. data() {
  11. return {};
  12. },
  13. components: {},
  14. props: {
  15. username: {
  16. type: Object,
  17. default: () => ({})
  18. },
  19. chatType: {
  20. type: String,
  21. default: msgType.chatType.SINGLE_CHAT
  22. }
  23. },
  24. methods: {
  25. openCamera() {
  26. var me = this;
  27. uni.chooseImage({
  28. count: 1,
  29. sizeType: ["original", "compressed"],
  30. sourceType: ["camera"],
  31. success(res) {
  32. me.upLoadImage(res);
  33. }
  34. });
  35. },
  36. sendImage() {
  37. var me = this;
  38. uni.chooseImage({
  39. count: 1,
  40. sizeType: ["original", "compressed"],
  41. sourceType: ["album"],
  42. success(res) {
  43. console.log('选择的图片', res)
  44. me.upLoadImage(res);
  45. }
  46. });
  47. },
  48. isGroupChat() {
  49. return this.chatType == msgType.chatType.CHAT_ROOM;
  50. },
  51. getSendToParam() {
  52. return this.isGroupChat() ? this.username.groupId : this.username.your;
  53. },
  54. upLoadImage(res) {
  55. var me = this;
  56. var tempFilePaths = res.tempFilePaths;
  57. var token = WebIM.conn.context.accessToken;
  58. uni.getImageInfo({
  59. src: res.tempFilePaths[0],
  60. success(res) {
  61. var allowType = {
  62. jpg: true,
  63. jpeg: true,
  64. gif: true,
  65. png: true,
  66. bmp: true
  67. };
  68. var str = WebIM.config.appkey.split("#");
  69. var width = res.width;
  70. var height = res.height;
  71. var index = res.path.lastIndexOf(".");
  72. console.log('index>>',index);
  73. var filetype = ~index && res.path.slice(index + 1) || "";
  74. if (!res.type) {
  75. uni.showToast({
  76. title: "H5端,uni-app暂未支持",
  77. icon: "none"
  78. })
  79. }
  80. if (filetype.toLowerCase() in allowType || res.type in allowType) {
  81. uni.uploadFile({
  82. url: "https://a1.easemob.com/" + str[0] + "/" + str[1] + "/chatfiles",
  83. filePath: tempFilePaths[0],
  84. fileType: 'image',
  85. name: "file",
  86. header: {
  87. // "Content-Type": "multipart/form-data",
  88. 'Content-Type': 'application/x-www-form-urlencoded',
  89. Authorization: "Bearer " + token
  90. },
  91. success: (res)=>{
  92. console.log('上传图片成功', res)
  93. if (res.statusCode == 400) {
  94. // 图片上传阿里云检验不合法
  95. // var errData = JSON.parse(res.data);
  96. // if (errData.error === 'content improper') {
  97. uni.showToast({
  98. title: "图片检测不合法",
  99. duration: 1000
  100. });
  101. return false
  102. // }
  103. }
  104. var data = res.data;
  105. var dataObj = JSON.parse(data);
  106. var id = WebIM.conn.getUniqueId(); // 生成本地消息 id
  107. var msg = new WebIM.message(msgType.IMAGE, id);
  108. var file = {
  109. type: msgType.IMAGE,
  110. size: {
  111. width: width,
  112. height: height
  113. },
  114. url: dataObj.uri + "/" + dataObj.entities[0].uuid,
  115. filetype: filetype,
  116. filename: tempFilePaths[0]
  117. };
  118. msg.set({
  119. apiUrl: WebIM.config.apiURL,
  120. body: file,
  121. from: me.username.myName,
  122. to: me.getSendToParam(),
  123. roomType: false,
  124. chatType: me.chatType,
  125. success: function(argument) {
  126. disp.fire('em.chat.sendSuccess', id);
  127. }
  128. });
  129. if (me.chatType == msgType.chatType.CHAT_ROOM) {
  130. msg.setGroup("groupchat");
  131. }
  132. WebIM.conn.send(msg.body);
  133. let obj = {
  134. msg: msg,
  135. type: msgType.IMAGE
  136. }
  137. me.saveSendMsg(obj);
  138. },
  139. fail: (err) => {
  140. console.log('上传失败', err)
  141. },
  142. complete: (err) => {
  143. console.log('上传完成', err)
  144. }
  145. });
  146. }
  147. }
  148. });
  149. },
  150. saveSendMsg(evt) {
  151. msgStorage.saveMsg(evt.msg, evt.type);
  152. }
  153. }
  154. };
  155. </script>