uni-app 接入环信sdk

官方文档:

一个接入的采坑之路终于完结了,送给需要的人们
项目不需要做太多的其他处理,只做发送消息,有提示音、振动即可,多余不处理咯。给大家做个接入参考。
环信sdk :3.x.x版本
接入方式:直接引用js包
框架:uni-app
main.js里面有一个声音提示,你们自行网上下载好了,
效果:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

注意:实例化SDK对象需要把appkay加上去,加上去,加上去
文件目录一览
在这里插入图片描述
WebIMConfig.js


// for react native
let location = {
	protocol: "https"
};

let config = {
	xmppURL: "wss://im-api.easemob.com/ws/",
	apiURL: "https://a1.easemob.com",
	appkey: "---------------------#-----",//你在环信平台申请的创建的appkey
	https: false,
	isMultiLoginSessions: false,
	isWindowSDK: false,
	isSandBox: false,
	isDebug: false,
	autoReconnectNumMax: 15,
	autoReconnectInterval: 2,
	isWebRTC: false,
	isAutoLogin: true
};

export default config;

main.js

import Vue from 'vue'
import App from './App'
import SDK from "sdk/wxsdk3.4.2.js"; // 3.0sdk
import config from "sdk/WebIMConfig.js"; // 3.0sdk
import helper from 'helper/helper.js'
Vue.prototype.$helper = helper;
const WebIM = wx.WebIM = SDK;
WebIM.config = config
WebIM.conn = new WebIM.connection({
	isMultiLoginSessions: false, //是否可以登录多个,并在所有端上接收消息
	appKey:WebIM.config.appkey,//必须。
	https: false, //是否使用HTTPS 
	url: 'wss://im-api-wechat.easemob.com/websocket', // socket server (3.0 SDK)
	apiUrl: 'https://a1.easemob.com',    // rest server
	heartBeatWait: 10000, //心跳间隔
	autoReconnectNumMax: 2, //自动重连次数
	useOwnUploadFun: false ,// 是否使用自己的上传方式(如将图片文件等上传到自己的服务器,构建消息时只传url)
	open(opt) {
		this.curOpenOpt = opt;
		WebIM.conn.open(opt);
		this.closed = false;
	},
	reopen() {
		if (this.closed) {
			//this.open(this.curOpenOpt);
			WebIM.conn.open(this.curOpenOpt);
			this.closed = false;
		}
	}
});

Vue.config.productionTip = false
Vue.prototype.$WebIM = WebIM;


Vue.prototype.ScanAudio = function(){
        var music = null;
        music = uni.createInnerAudioContext(); //创建播放器对象
        music.src= "../../static/com.mp3"; //选择播放的音频
        music.play(); //执行播放
}

App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()

login.vue

<template>
	<view class="content">
		  <view class="tyb-form">
			<view class="tyb-input-li">
				<text>IM账号</text><input placeholder="请输入IM用户账号" type="text" class="tyb-inutt" v-model="form.username" value="" />
			</view>
			<view class="tyb-input-li">
				<text>IM密码</text><input placeholder="请输入IM账号密码" type="text" class="tyb-inutt" v-model="form.password" value="" />
			</view>
			<view class="tyb-input-li" >
				<view class="tyb-bt" style="" v-on:click="toLogin">
					登录
				</view>
			</view>
		  </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					username:"18314452801",
					password:"52801"
				}
			}
		},
		onLoad:function(){
			this.islogin()
		},
		methods: {
			islogin:function(){
				var user = uni.getStorageSync('userinfo');
				if(user){
					try {
						uni.navigateTo({
							url: '../index/index',
							// url:'pages/index/index?username='+user.username
						})
					} catch (e) {
					
					}
				}
				
			},
			// 登录
			toLogin:function(){
				
				// IM账号登录数据
				var options = {
					user: this.form.username,
					pwd: this.form.password,
					apiUrl:this.$WebIM.config.apiURL,
					appKey: this.$WebIM.config.appkey,
				};
				// 登录到IM
				this.$WebIM.conn.open(options)
				// 加入到缓存
				uni.setStorage({
					key:"userinfo",
					data:{ userId:this.form.username, password: this.form.password }
				})
				
				try {
					uni.navigateTo({
						url: '../index/index',
						// url:'pages/index/index?username='+user.username
					})
				} catch (e) {
				
				}
			},
		}
	}
</script>

<style>
	.tyb-form{
		padding: 20rpx;
	}
	.tyb-form text{
		display: block;
		
	}
	.tyb-form .tyb-input-li{
		padding: 10rpx;
	}
	.tyb-bt{
		background: #007AFF;color: #fff;padding:10rpx 20rpx;text-align: center;
	}
	.tyb-inutt{
		border: 1px solid #0873DE;
		padding: 10rpx;
	}
	
</style>

index.vue

<template>
	<view class="content">
		<view class="tyb-form">
			<view class="tyb-input-li">
				<text>IM账号</text><input placeholder="需要发送给谁" type="text" class="tyb-inutt" v-model="form.username" value="" />
			</view>
			<view class="tyb-input-li">
				<text>I发送内容</text><input placeholder="发送内容" type="text" class="tyb-inutt" v-model="form.text" value="" />
			</view>
			<view class="tyb-input-li" >
				<view class="tyb-bt" style="" v-on:click="sendCustomMsg">
					{{bttitle}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					username:"18314452802",
					text:"你好啊"
				},
				bttitle:'发送消息'
			}
		},
		onLoad:function(){
			
		},
		methods: {
			sendCustomMsg:function(){
				console.log("发送消息")
				var _thef = this
				var user = uni.getStorageSync('userinfo')
				var id = _thef.$WebIM.conn.getUniqueId();                 
				var msg = new _thef.$WebIM.message('txt', id);      
				var option = {
					msg: _thef.form.text,                
					to: _thef.form.username,                
					roomType: false,
					success: function (id, serverMsgId) {
						console.log('发送成功');
					},
					fail: function(e){
						console.log("发送事变",e);
					}
				};
				
				console.log(option)
				msg.set(option);
				msg.body.chatType = 'singleChat';
				console.log(_thef.$WebIM)
				_thef.$WebIM.conn.send(msg.body);
			},
			islogin:function(){
				var user = uni.getStorageSync('userinfo');
				console.log('yhu',user.data)
				
				
				// try{
				// 	var user = uni.getStorages('userinfo');
				// 	console.log('用户信息',user)
				// }catch(e){
				// 	console.log('没有用户信息')
				// }
				
			}			
		}
	}
</script>

<style>
	.tyb-form{
		padding: 20rpx;
	}
	.tyb-form text{
		display: block;
		
	}
	.tyb-form .tyb-input-li{
		padding: 10rpx;
	}
	.tyb-bt{
		background: #007AFF;color: #fff;padding:20rpx 20rpx;text-align: center;
	}
	.tyb-inutt{
		border: 1px solid #0873DE;
		padding: 10rpx;
	}
</style>

swich.vue

<template>
	<view class="content">
		接收的信息:{{msg}}
	</view>
</template>

<script>
	export default {
		data() {
			return {
				msg:''
			}
		},
		onLoad:function(){
			this.getMsg()
		},
		methods: {
			getMsg:function(){
				var message = uni.getStorageSync('message');
				this.msg = message.data
			}			
		}
	}
</script>

<style>
	
</style>

helper.js


/**
 * 页面跳转
 * @param {Object} url
 */
function to(url) {
	
	uni.navigateTo({
		url: url
	});
}

function toast(icon, text, duration, mask, position) {
	duration = duration || 1500;
	mask = mask || false;
	position = position || false;
	uni.showToast({
		icon: icon,
		title: text,
		duration: duration,
		mask: mask,
		position: position,
	})
}


export default {
	to,
	toast,
}

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_28761593

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值