uniapp集成腾讯IM发送图片报错(h5端),解决及新问题

**

问题:

**
使用uniapp 集成腾讯im即时通讯时,app和小程序模式可正常发送图片消息,h5端则报错。ps:我依稀记得年初我开发时h5可以正常发送图片。问题体现:
h5端发送图片提示,请检查图片格式(图片格式正常);
或者提示图片文件只能为file……但传输file时继续提示无法使用file类型;

我的原因:

我的h5端加载sdk时,如加载tim-js.js会报错,所以加载了tim-wx.js,其他功能没发现明显问题,便以为可以代替使用。
产生问题的主要原因(看了博客大佬才知道主要问题是哪里):

查看 tim-js.js 源码发现,是由于uniapp内部封装有微信小程序的 wx 对象,导致 tim-js.js 即使在浏览器环境下也错误的判断成了小程序环境,导致上传插件 cos-js-sdk-v5 出现加载失败问题;

uniapp集成腾讯IM发送图片报错(h5端),解决及新问题_第1张图片

源码:ha = "undefined" != typeof wx && "function" == typeof wx.getSystemInfoSync,
改为:ha = "undefined" == typeof wx && "function" == typeof wx.getSystemInfoSync,

此时程序可正常加载tim-js.js,并上传图片。
h5端
选择图片:

uni.chooseImage({
	sourceType:[type],
	sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
	success: (res)=>{
		console.log('imgres',res)
		// let msg1 = res;
		// this.sendMsg(msg1,'img');
		
		for(let i=0;i<res.tempFilePaths.length;i++){
			uni.getImageInfo({
				src: res.tempFilePaths[i],
				success: (image)=>{
					console.log(image.width);
					console.log(image.height);
					let msg = {url:res.tempFilePaths[i],file:res.tempFiles[i],w:image.width,h:image.height};
					// let msg = res;
					console.log('image:',msg);
					this.sendMsg(msg,'img');
				}
			});
		}
		
		
	}
});

发送图片:

let message = this.tim.createImageMessage({
 to: this.toUserId,
  conversationType: 'C2C',
  payload: {
    file: content.file,
  }
});	

现在腾讯IM客服支持的说法大概是支持uniapp,但年初刚开始研究时我询问得到的回复时否定的,emmmmm。

另外新问题

另外不知道有没有人可以看到,我在main.js中条件编译,加载不同的sdk失败,不知道有没有人解答我这个菜鸟下。我现在调试不同模式就手动改这里。,。

import Vue from 'vue'
import App from './App'
// #ifndef H5
console.log('tim-wx-sdk')
import TIM from 'tim-wx-sdk'
// #endif

// #ifdef H5
console.log('tim-js-sdk')
import TIM from 'tim-js-sdk'
// #endif

import store from './store/index.js'
……

它似乎会默认加载第一个,即h5模式运行也会加载微信sdk,继而报错。
是main.js中 //#ifndef 不生效,还是我使用的有问题,望大佬告知

你可能感兴趣的:(腾讯IM,uni-app,腾讯云)