Quellcode durchsuchen

Merge branch 'master' of http://git.izouma.com/licailing/dingdong

licailing vor 5 Jahren
Ursprung
Commit
754ed697b2
1 geänderte Dateien mit 67 neuen und 27 gelöschten Zeilen
  1. 67 27
      src/main/map/src/views/Tim.vue

+ 67 - 27
src/main/map/src/views/Tim.vue

@@ -1,5 +1,7 @@
 <template>
-    <div></div>
+    <div>
+        <div @click="sendMessage('快点啊', '2211')">发送消息</div>
+    </div>
 </template>
 <script>
 import { mapState } from 'vuex';
@@ -14,6 +16,7 @@ export default {
             userID: 2015,
             userSig:
                 'eJwtzMsKwjAURdF-ybRSbp6SgpPiRIjgo0VwJja1l6gNMUpA-HdL2*FZB-aXVOaYf2wgBWE5kMW4sbHPiC1ODFTO-mrcxXtsSEEFgACquJgemzwGO7iUkgHApBEfo2klNWeMzRW8DVmknc7KoPbJmU11jedTysAtW9dtexN39Z0f1n1gb1fDivz*cZcwNQ__',
+            tim: null,
         };
     },
     computed: {
@@ -38,96 +41,112 @@ export default {
             let options = {
                 SDKAppID: this.SDKAppID, // 接入时需要将0替换为您的即时通信应用的 SDKAppID
             };
-            let tim = TIM.create(options); // SDK 实例通常用 tim 表示
+            this.tim = TIM.create(options); // SDK 实例通常用 tim 表示
             // 设置 SDK 日志输出级别,详细分级请参见 setLogLevel 接口的说明
-            tim.setLogLevel(0); // 普通级别,日志量较多,接入时建议使用
+            this.tim.setLogLevel(0); // 普通级别,日志量较多,接入时建议使用
             // tim.setLogLevel(1); // release级别,SDK 输出关键信息,生产环境时建议使用
 
             // 将腾讯云对象存储服务 SDK (以下简称 COS SDK)注册为插件,IM SDK 发送文件、图片等消息时,需要用到腾讯云的 COS 服务
             // HTML5 环境,注册 COS SDK
-            tim.registerPlugin({ 'cos-js-sdk': COS });
+            this.tim.registerPlugin({ 'cos-js-sdk': COS });
 
             // 监听事件,如:
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.SDK_READY, function(event) {
+            this.tim.on(TIM.EVENT.SDK_READY, (event)=>{
                 // 收到离线消息和会话列表同步完毕通知,接入侧可以调用 sendMessage 等需要鉴权的接口
                 // event.name - TIM.EVENT.SDK_READY
             });
 
-            tim.on(TIM.EVENT.MESSAGE_RECEIVED, function(event) {
+            this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, (event)=>{
                 console.log(event);
                 // 收到推送的单聊、群聊、群提示、群系统通知的新消息,可通过遍历 event.data 获取消息列表数据并渲染到页面
                 // event.name - TIM.EVENT.MESSAGE_RECEIVED
                 // event.data - 存储 Message 对象的数组 - [Message]
                 if (window.ReactNativeWebView) {
                     window.ReactNativeWebView.postMessage(
-                        JSON.stringify(event.data),
+                        JSON.stringify(event.data)
                     );
                 }
             });
 
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.MESSAGE_REVOKED, function(event) {
+            this.tim.on(TIM.EVENT.MESSAGE_REVOKED, (event)=>{
                 // 收到消息被撤回的通知。使用前需要将SDK版本升级至v2.4.0或以上。
                 // event.name - TIM.EVENT.MESSAGE_REVOKED
                 // event.data - 存储 Message 对象的数组 - [Message] - 每个 Message 对象的 isRevoked 属性值为 true
             });
 
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.MESSAGE_READ_BY_PEER, function(event) {
+            this.tim.on(TIM.EVENT.MESSAGE_READ_BY_PEER, (event)=>{
                 // SDK 收到对端已读消息的通知,即已读回执。使用前需要将SDK版本升级至v2.7.0或以上。仅支持单聊会话。
                 // event.name - TIM.EVENT.MESSAGE_READ_BY_PEER
                 // event.data - event.data - 存储 Message 对象的数组 - [Message] - 每个 Message 对象的 isPeerRead 属性值为 true
             });
 
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, function(event) {
+            this.tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, (event)=>{
                 // 收到会话列表更新通知,可通过遍历 event.data 获取会话列表数据并渲染到页面
                 // event.name - TIM.EVENT.CONVERSATION_LIST_UPDATED
                 // event.data - 存储 Conversation 对象的数组 - [Conversation]
+                if (window.ReactNativeWebView) {
+                    window.ReactNativeWebView.postMessage(
+                        JSON.stringify(event)
+                    );
+                }
             });
 
-            tim.on(TIM.EVENT.GROUP_LIST_UPDATED, function(event) {
+            this.tim.on(TIM.EVENT.GROUP_LIST_UPDATED, (event)=>{
                 // 收到群组列表更新通知,可通过遍历 event.data 获取群组列表数据并渲染到页面
                 // event.name - TIM.EVENT.GROUP_LIST_UPDATED
                 // event.data - 存储 Group 对象的数组 - [Group]
                 if (window.ReactNativeWebView) {
                     window.ReactNativeWebView.postMessage(
-                        JSON.stringify(event.data),
+                        JSON.stringify(event.data)
                     );
                 }
             });
 
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.PROFILE_UPDATED, function(event) {
+            this.tim.on(TIM.EVENT.PROFILE_UPDATED, (event)=>{
                 // 收到自己或好友的资料变更通知
                 // event.name - TIM.EVENT.PROFILE_UPDATED
                 // event.data - 存储 Profile 对象的数组 - [Profile]
             });
 
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.BLACKLIST_UPDATED, function(event) {
+            this.tim.on(TIM.EVENT.BLACKLIST_UPDATED, (event)=>{
                 // 收到黑名单列表更新通知
                 // event.name - TIM.EVENT.BLACKLIST_UPDATED
                 // event.data - 存储 userID 的数组 - [userID]
             });
 
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.ERROR, function(event) {
+            this.tim.on(TIM.EVENT.ERROR, (event)=>{
                 // 收到 SDK 发生错误通知,可以获取错误码和错误信息
                 // event.name - TIM.EVENT.ERROR
                 // event.data.code - 错误码
                 // event.data.message - 错误信息
+                if (window.ReactNativeWebView) {
+                    window.ReactNativeWebView.postMessage(
+                        JSON.stringify(event)
+                    );
+                }
             });
 
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.SDK_NOT_READY, function(event) {
+            this.tim.on(TIM.EVENT.SDK_NOT_READY, (event)=>{
                 // 收到 SDK 进入 not ready 状态通知,此时 SDK 无法正常工作
                 // event.name - TIM.EVENT.SDK_NOT_READY
+
+                if (window.ReactNativeWebView) {
+                    window.ReactNativeWebView.postMessage(
+                        JSON.stringify(event)
+                    );
+                }
             });
 
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.KICKED_OUT, function(event) {
+            this.tim.on(TIM.EVENT.KICKED_OUT, (event)=>{
                 // 收到被踢下线通知
                 // event.name - TIM.EVENT.KICKED_OUT
                 // event.data.type - 被踢下线的原因,例如 :
@@ -137,7 +156,7 @@ export default {
             });
 
             // eslint-disable-next-line no-unused-vars
-            tim.on(TIM.EVENT.NET_STATE_CHANGE, function(event) {
+            this.tim.on(TIM.EVENT.NET_STATE_CHANGE, (event)=>{
                 // 网络状态发生改变(v2.5.0 起支持)。
                 // event.name - TIM.EVENT.NET_STATE_CHANGE
                 // event.data.state 当前网络状态,枚举值及说明如下:
@@ -147,16 +166,37 @@ export default {
             });
 
             // 开始登录
-            tim.login({
-                userID: this.userID.toString(),
-                userSig: this.userSig,
+            this.tim
+                .login({
+                    userID: this.userID.toString(),
+                    userSig: this.userSig,
+                })
+                .then((imResponse)=>{
+                    console.log(imResponse.data); // 登录成功
+                    if (window.ReactNativeWebView) {
+                        window.ReactNativeWebView.postMessage(
+                            `用户${this.userID}登录成功`
+                        );
+                    }
+                })
+                .catch((imError)=> {
+										console.log(imError)
+                    if (window.ReactNativeWebView) {
+                        window.ReactNativeWebView.postMessage(
+                            JSON.stringify(imError)
+                        );
+                    }
+                });
+        },
+        sendMessage(text, toUserId) {
+            let message = this.tim.createTextMessage({
+                conversationType: TIM.TYPES.CONV_C2C,
+                payload: {
+                    text,
+                },
+                to: toUserId,
             });
-
-            if (window.ReactNativeWebView) {
-                window.ReactNativeWebView.postMessage(
-                    `用户${this.userID}登录成功`,
-                );
-            }
+            this.tim.sendMessage(message);
         },
     },
 };