123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965 |
- import packetCode from './PacketCodeC.js'
- import store from './store/index.js'
- import SKIMSDK from './websocket_sdk.js'
- import { queryData, upData, initData } from './util/dbUtil.js'
- const EventDispatcher = function() {
- this.listeners = {}
- }
- let eventDispatcher
- const WEBIM = {
- resource: "web",
- /*单聊标识*/
- CHAT: "chat",
- /*群聊标识*/
- GROUPCHAT: "groupchat",
- token: null,
- userId: null,
- nickName: "",
- isReadDel: 0,
- /*用户 jid 10004541/web */
- userIdStr: null,
- /*服务器连接地址 ws://localhost:5260 */
- serverUrl: null,
- server: null,
- /*消息超时 时间 默认 15 秒*/
- sendTimeOut: 15,
- /*等待消息回执的 消息Id 数组*/
- waitReceiptMessageIds: {},
- heartCheck: false,
- isReconnection: false,
- options: null,
- showLoginDialog:false,
- /*初始化*/
- initWebIM: function(serverUrl, heartCheck, isReconnection) {
- console.log('【initWebIM】初始化')
- WEBIM.heartCheck = heartCheck;
- WEBIM.isReconnection = isReconnection;
- WEBIM.serverUrl = serverUrl;
- WEBIM.options = {
- url: serverUrl,
- success(res) {},
- fail() {}
- }
- eventDispatcher = new EventDispatcher();
- WEBIM.server = new SKIMSDK({
- heartCheck: heartCheck,
- isReconnection: isReconnection,
- });
- WEBIM.server.initWebSocket(WEBIM.options);
- WEBIM.server.onReceivedMsg(event => {
- let packet = packetCode.decode(event.data);
- let command = packet.command;
- eventDispatcher.dispatchEvent(command, toJSON(packet))
- eventDispatcher.removeListener(command, toJSON(packet))
- let name = 'pushRes';
- let value = packet;
- if (command === -10) {
- store.commit('$uStore', {
- name,
- value
- });
- }
- if(packet&&!packet.success){
- // this.liangxinLogin()
- }
- });
- WEBIM.server.onNetworkChange(WEBIM.options);
- WEBIM.server.onSocketClosed(WEBIM.options)
- },
- liangxinLogin(){
- const that = this
- var userInfoTmp = uni.getStorageSync("userInfo")
- if(userInfoTmp.phone){
- that.login(userInfoTmp.phone, "123456", null, res=>{
- console.log('粮信登录',res)
- if (res.success) {
- that.showLoginDialog = false
- // 缓存用户
- that.userData = res.response.data
- console.log('userData',that.userData)
- let name = 'userData';
- let value = that.userData;
- store.commit('$uStore', {
- name,
- value
- });
- // 缓存通讯录
- that.listGuests(that.userData.user.operId, res => {
- // #ifdef APP-PLUS
- createFSQL(that.userData.user.operId).then();
- let contact = res.response.data;
- contact.forEach(c=>{
- c.members.forEach(m=>{
- m.name = c.name;
- addFSQL(m, that.userData.user.operId).then();
- })
- })
- // #endif
- let name = 'firendItem';
- let value = res.response.data;
- store.commit('$uStore', {
- name,
- value
- });
- });
-
- // 缓存消息列表
- that.queryOnlineMessage(that.userData.user.operId,q =>{
- let data = q.response.data;
- for(var i in data){
- initData(data[i].groupMsg.list, data[i].groupInfo.chatId);
- }
- })
-
- // 缓存链接
- that.getLinks(that.userData.user.operId, res=>{
- let name = 'linkItem';
- let value = res.response.data;
- store.commit('$uStore', {
- name,
- value
- });
- });
-
-
- // // 跳转到消息列表
- // that.$u.route({
- // url: 'pages/home/home',
- // type: 'switchTab'
- // });
- } else {
- uni.showModal({
- title:res.reason + ",请稍后再试",
- showCancel:false
- })
- }
- });
- }
- else if(!WEBIM.showLoginDialog){
- let pages = getCurrentPages();
- let curPage = pages[pages.length-1];
- if(curPage.$page.fullPath != '/pages/public/login'
- && curPage.$page.fullPath != '/pages/public/login_account_number'
- && curPage.$page.fullPath != '/pages/public/register'
- && curPage.$page.fullPath != '/pages/public/code'
- && curPage.$page.fullPath != '/pages/public/reset'
- && curPage.$page.fullPath != '/pages/index/index'
- && curPage.$page.fullPath != '/pages/sale/index'
- && curPage.$page.fullPath != '/pages/sale/information'
- && curPage.$page.fullPath.indexOf('/pages/sale/webview') == -1 )
- {
- // uni.clearStorageSync();
- let name = 'hasLogin';
- let value = false;
- store.commit('$uStore', {
- name,
- value
- });
- WEBIM.showLoginDialog = true
- uni.navigateTo({
- url:'/pages/public/login'
- })
- }
- }
-
- },
- disconnect: function(e) {
- WEBIM.server.closeWebSocket()
- },
- isConnect: function() {
- return WEBIM.server._isLogin;
- },
- login: (username, password, code, func) => {
- let requestPacket = {
- username,
- password,
- code: null,
- version: 1,
- command: 1
- }
- send(requestPacket)
- eventDispatcher.addListener('2', func)
- },
- register: (phone, password, nickname, func) => {
- let p = {
- phone,
- password,
- nickname,
- version:1,
- command:49
- }
- send(p)
- eventDispatcher.addListener('50', func)
- },
- heartTest: (userId, func) => {
- let packet = {
- userId,
- version: 1,
- command: 17
- }
- send(packet)
- eventDispatcher.addListener('18', func)
- },
- send2Group: (toGroupId, userId, message, msgType, func) => {
- let requestPacket = {
- toGroupId,
- userId,
- msgType,
- message,
- version: 1,
- command: 15
- }
- send(requestPacket)
- eventDispatcher.addListener('16', func)
- },
- send2Friend: (toUserId, userId, message, msgType, func) => {
- let requestPacket = {
- userId,
- toUserId,
- msgType,
- message,
- version: 1,
- command: 3
- }
- send(requestPacket)
- eventDispatcher.addListener('4', func)
- },
- logout: (userId, func) => {
- let requestPacket = {
- userId,
- version: 1,
- command: 5
- }
- send(requestPacket)
- eventDispatcher.addListener('6', func)
- },
- createGroup: (userIdList, defaultGroupName, userId, func) => {
- let requestPacket = {
- userId,
- userIdList,
- defaultGroupName,
- version: 1,
- command: 7
- }
- send(requestPacket)
- eventDispatcher.addListener('8', func)
- },
- queryMembers: (groupId, userId, func) => {
- let requestPacket = {
- groupId,
- userId,
- version: 1,
- command: 9
- }
- send(requestPacket)
- eventDispatcher.addListener('10', func)
- },
- getLastMessage: (id, chatId, userId, chatType, func) => {
- let requestPacket = {
- id: id,
- chatId,
- userId,
- chatType,
- version: 1,
- command: 45
- }
- send(requestPacket)
- eventDispatcher.addListener('46', func)
- },
- joinGroup: (groupId, ids, userName, func) => {
- let requestPacket = {
- groupId,
- userIds: ids,
- currentUsername: userName,
- version: 1,
- command: 11
- }
- send(requestPacket)
- eventDispatcher.addListener('12', func)
- },
- delGroupMember: (groupId, ids, func) => {
- let requestPacket = {
- groupId,
- userIds: ids,
- version: 1,
- command: 13
- }
- send(requestPacket)
- eventDispatcher.addListener('14', func)
- },
- transferGroup: (groupId, userId, func) => {
- let requestPacket = {
- groupId,
- userId,
- version: 1,
- command: 53
- }
- send(requestPacket)
- eventDispatcher.addListener('54', func)
- },
- getGroups: (condition, userId, func) => {
- let requestPacket = {
- condition,
- userId,
- version: 1,
- command: 25
- }
- send(requestPacket)
- eventDispatcher.addListener('26', func)
- },
- listGuests: (userId, func) => {
- let packet = {
- userId,
- version: 1,
- command: 47
- }
- send(packet)
- eventDispatcher.addListener('48', func)
- },
- queryFriendMessages: (toUserId, userId, pageNum, func) => {
- let requestPacket = {
- userId,
- pageNum,
- toUserId,
- pageSize: 10,
- version: 1,
- command: 27
- }
- send(requestPacket)
- eventDispatcher.addListener('28', func)
- },
-
- getFriendMessageByCondition: (toUserId, userId, pageNum, condition, func) => {
- let requestPacket = {
- userId,
- condition,
- pageNum,
- toUserId,
- pageSize: 5,
- version: 1,
- command: 27
- }
- send(requestPacket)
- eventDispatcher.addListener('28', func)
- },
- queryGroupMessages: (toGroupId, userId, pageNum, func) => {
- let requestPacket = {
- toGroupId,
- pageNum,
- pageSize: 10,
- userId,
- version: 1,
- command: 23
- }
- send(requestPacket)
- eventDispatcher.addListener('24', func)
- },
- getGroupMessageByCondition: (toGroupId, userId, pageNum, condition, func) => {
- let requestPacket = {
- toGroupId,
- pageNum,
- condition,
- pageSize: 5,
- userId,
- version: 1,
- command: 23
- }
- send(requestPacket)
- eventDispatcher.addListener('24', func)
- },
- queryChats: (condition, userId, func) => {
- let requestPacket = {
- condition,
- userId,
- pageNum: -1,
- pageSize: -1,
- version: 1,
- command: 19
- }
- send(requestPacket)
- eventDispatcher.addListener('20', func)
- },
- updateNickName: (userId, nickName, func) => {
- let requestPacket = {
- userId,
- nickName,
- version: 1,
- command: 79
- }
- send(requestPacket)
- eventDispatcher.addListener('80', func)
- },
- getBusinessCard: (userId, func) => {
- let requestPacket = {
- userId,
- version: 1,
- command: 65
- }
- send(requestPacket)
- eventDispatcher.addListener('66', func)
- },
- createChatList: (userId, groupId, message, msgType, func) => {
- let requestPacket = {
- userId,
- groupId,
- msgType,
- message,
- version: 1,
- command: 61
- }
- send(requestPacket)
- eventDispatcher.addListener('62', func)
- },
- randomSmsCode: (phone, func) => {
- let req = {
- userId: '5f6d9d98',
- phone,
- version: 1,
- command: 63
- }
- send(req)
- eventDispatcher.addListener('64', func)
- },
- getCustomerServiceList: (userId, func) => {
- let requestPacket = {
- userId,
- version: 1,
- command: 93
- }
- send(requestPacket)
- eventDispatcher.addListener('94', func)
- },
- deleteEmontion: (userId, motionId, func) => {
- let requestPacket = {
- userId,
- motionId,
- version: 1,
- command: 95
- }
- send(requestPacket)
- eventDispatcher.addListener('96', func)
- },
- stopAudio: (userId, chatId, chatType, func) => {
- let requestPacket = {
- userId,
- chatId,
- chatType,
- method: 21,
- version: 1,
- command: 21
- }
- send(requestPacket)
- eventDispatcher.addListener('22', func)
- },
- queryGroupUser: (userId, groupId, func) => {
- let requestPacket = {
- userId,
- groupId,
- method: 17,
- version: 1,
- command: 21
- }
- send(requestPacket)
- eventDispatcher.addListener('22', func)
- },
- getUserRemark: (userId, friendUserId, func) => {
- let requestPacket = {
- friendUserId,
- userId,
- method: 10,
- version: 1,
- command: 21
- }
- send(requestPacket)
- eventDispatcher.addListener('22', func)
- },
- updateAvatar: (userId, avatar, func) => {
- let requestPacket = {
- userId,
- avatar,
- version: 1,
- command: 97
- }
- send(requestPacket)
- eventDispatcher.addListener('98', func)
- },
- getUserById: (userId, func) => {
- let requestPacket = {
- userId,
- version: 1,
- command: 65
- }
- send(requestPacket)
- eventDispatcher.addListener('66', func)
- },
- deleteGroupMsg: (userId, msgId, groupId, func) => {
- let requestPacket = {
- userId,
- msgId,
- groupId,
- method: 5,
- version: 1,
- command: 81
- }
- send(requestPacket)
- eventDispatcher.addListener('82', func)
- },
- deleteFriendMsg: (userId, msgId, friendId, func) => {
- let requestPacket = {
- userId,
- msgId,
- friendId,
- version: 1,
- command: 73
- }
- send(requestPacket)
- eventDispatcher.addListener('74', func)
- },
- updateRemarkName: (userId, friendUserId, remarkName, func) => {
- let requestPacket = {
- userId,
- friendUserId,
- remarkName,
- method: 7,
- version: 1,
- command: 21
- }
- send(requestPacket)
- eventDispatcher.addListener('22', func)
- },
- updatePassword: (userId, username, password, func) => {
- let requestPacket = {
- userId,
- username,
- password,
- version: 1,
- command: 77
- }
- send(requestPacket)
- eventDispatcher.addListener('78', func)
- },
- uploadContact: (name, phone, userId, func) => {
- let requestPacket = {
- name,
- phone,
- userId,
- method: 9,
- version: 1,
- command: 21
- }
- send(requestPacket)
- eventDispatcher.addListener('22', func)
- },
- freshCode: (func) => {
- let requestPacket = {
- method: 4,
- version: 1,
- command: 21
- }
- send(requestPacket)
- eventDispatcher.addListener('22', func)
- },
- delChat: (userId, chatId, func) => {
- let requestPacket = {
- userId,
- chatId,
- version: 1,
- command: 83
- }
- send(requestPacket)
- eventDispatcher.addListener('84', func)
- },
- updateGroupNick: (userId, groupId, nickName, func) => {
- let requestPacket = {
- userId,
- groupId,
- nickName,
- version: 1,
- command: 79
- }
- send(requestPacket)
- eventDispatcher.addListener('80', func)
- },
- attend: (chat, userId, func) => {
- let requestPacket = {
- userId,
- chat,
- version: 1,
- command: 75
- }
- send(requestPacket)
- eventDispatcher.addListener('76', func)
- },
- queryMsgByChatId: (msgId, groupId, userId, func) => {
- let requestPacket = {
- userId,
- msgId,
- groupId,
- version: 1,
- command: 69
- }
- send(requestPacket)
- eventDispatcher.addListener('70', func)
- },
- addEmoticon(userId, avatar, func) {
- let requestPacket = {
- userId,
- avatar,
- version: 1,
- command: 71
- }
- send(requestPacket)
- eventDispatcher.addListener('72', func)
- },
- getEmoticons(userId, func) {
- let requestPacket = {
- userId,
- version: 1,
- command: 85
- }
- send(requestPacket)
- eventDispatcher.addListener('86', func)
- },
- updateGroupName: (userId, groupId, groupName, func) => {
- let requestPacket = {
- userId,
- groupId,
- groupName,
- version: 1,
- command: 91
- }
- send(requestPacket)
- eventDispatcher.addListener('92', func)
- },
- clearGroupMsg: (userId, groupId, func) => {
- let requestPacket = {
- userId,
- groupId,
- version: 1,
- command: 87
- }
- send(requestPacket)
- eventDispatcher.addListener('88', func)
- },
- openChat: (chatId, userId, chatType, func) => {
- let requestPacket = {
- userId,
- chatId,
- chatType,
- version: 1,
- command: 39
- }
- send(requestPacket)
- eventDispatcher.addListener('40', func)
- },
- getMoneys(userId, pageNum, func) {
- let requestPacket = {
- pageNum,
- pageSiz: 5,
- userId,
- version: 1,
- command: 41
- }
- send(requestPacket)
- eventDispatcher.addListener('42', func)
- },
- queryNotice(userId, groupId, func) {
- let requestPacket = {
- userId,
- groupId,
- action: 'query',
- version: 1,
- command: 43
- }
- send(requestPacket)
- eventDispatcher.addListener('44', func)
- },
- updateNotice(userId, groupId, context, func) {
- let requestPacket = {
- userId,
- groupId,
- action: 'update',
- context,
- version: 1,
- command: 43
- }
- send(requestPacket)
- eventDispatcher.addListener('44', func)
- },
- removeGroupUser(userIds, groupId, func) {
- let requestPacket = {
- userIds,
- groupId,
- version: 1,
- command: 51
- }
- send(requestPacket)
- eventDispatcher.addListener('52', func)
- },
-
- insertFriend(userId, friendId, func) {
- let requestPacket = {
- userId,
- friendId,
- version: 1,
- command: 55
- }
- send(requestPacket)
- eventDispatcher.addListener('56', func)
- },
- // 查找用户
- findFriendRequestList: (nickName, func) => {
- let requestPacket = {
- nickName,
- version: 1,
- command: 89
- }
- send(requestPacket)
- eventDispatcher.addListener('90', func)
- },
- // 查看新朋友列表
- queryFriendRequestList(userId, func) {
- let requestPacket = {
- userId,
- version: 1,
- command: 57
- }
- send(requestPacket)
- eventDispatcher.addListener('58', func)
- },
- // 同意好友请求
- AcceptFriendRequest(friendId, userId, func) {
- let requestPacket = {
- friendId,
- userId,
- version: 1,
- command: 59
- }
- send(requestPacket)
- eventDispatcher.addListener('60', func)
- },
-
- queryPostsReq: (userId, pageNum, func) => {
- let requestPacket = {
- pageNum,
- pageSize: 10,
- userId,
- version: 1,
- command: 106
- }
- send(requestPacket)
- eventDispatcher.addListener('107', func)
- },
-
- createPostReq: (userId, postContext, urls, func) => {
- let requestPacket = {
- postContext,
- urls,
- pageSize: 10,
- userId,
- version: 1,
- command: 102
- }
- send(requestPacket)
- eventDispatcher.addListener('103', func)
- },
-
- toFabulousRes: (id, userId, postId, func) => {
- let requestPacket = {
- id,
- postId,
- pageSize: 10,
- userId,
- version: 1,
- command: 108
- }
- send(requestPacket)
- eventDispatcher.addListener('109', func)
- },
-
- toCommentReqPacket: (id, userId, postId, comment, func) => {
- let requestPacket = {
- id,
- postId,
- comment,
- pageSize: 10,
- userId,
- version: 1,
- command: 104
- }
- send(requestPacket)
- eventDispatcher.addListener('105', func)
- },
- getLinks: (userId, func) => {
- let packet = {
- userId,
- link: {
- title:'看一看'
- },
- version: 1,
- command: 110
- }
- send(packet)
- eventDispatcher.addListener('111', func)
- },
- createMessage: (message, msgType, toGroupId, func) => {
- let p ={
- toGroupId,
- msgType,
- message,
- version: 1,
- command: 120
- }
- send(p)
- eventDispatcher.addListener('121', func)
- },
- robRedPacket: (func) =>{
- let p = {
- version: 1,
- command:118
- }
- send(p)
- eventDispatcher.addListener('119', func)
- },
- createRedPacket: (func) =>{
- let p ={
- version: 1,
- command:116
- }
- send(p)
- eventDispatcher.addListener('117', func)
- },
- addFaceUser: (userId, faceId, func)=> {
- let p = {
- userId,
- faceId,
- version: 1,
- command: 121
- }
- send(p)
- eventDispatcher.addListener('122', func)
- },
- listFaces: (keyword, func)=> {
- let p = {
- keyword,
- version: 1,
- command: 123
- }
- send(p)
- eventDispatcher.addListener('124', func)
- },
- listBanner:(func)=>{
- let p = {
- version: 1,
- command: 125
- }
- send(p)
- eventDispatcher.addListener('126', func)
- },
- joinRoom:(groupIds, func) =>{
- let p={
- groupIds,
- version: 1,
- command: -1
- }
- send(p)
- eventDispatcher.addListener('-2', func)
- },
- quitRoom:(groupIds, func) =>{
- let p={
- groupIds,
- version: 1,
- command: -3
- }
- send(p)
- eventDispatcher.addListener('-4', func)
- },
- queryOnlineMessage(userId, func){
- let p={
- userId,
- version: 1,
- command: -7
- }
- send(p)
- eventDispatcher.addListener('-6', func)
- }
- }
- function toJSON(packet) {
- return JSON.parse(JSON.stringify(packet))
- }
- EventDispatcher.prototype.addListener = function(eventKey, fun, context) {
- let list = []
- this.listeners[eventKey] = list
- let listener = {
- func: fun,
- context: context
- }
- list.push(listener)
- return listener
- }
- EventDispatcher.prototype.removeListener = function(eventKey, fun, context) {
- let list = this.listeners[eventKey]
- if (list !== undefined) {
- let size = list.length
- for (let i = 0; i < size; i++) {
- let listener = list[i]
- if (listener.func === fun && listener.context === context) {
- list.splice(i, 1)
- return
- }
- }
- }
- }
- EventDispatcher.prototype.dispatchEvent = function(eventKey, event) {
- let list = this.listeners[eventKey]
- if (list !== undefined) {
- let size = list.length
- for (let i = 0; i < size; i++) {
- let listener = list[i]
- let fun = listener.func
- let context = listener.context
- if (context != null) {
- fun.call(context, event)
- } else {
- fun(event)
- }
- }
- }
- }
- let send = (p) => {
- p.token = store.state.userData.token;
- console.log('【websocket】send ' + p.token)
- console.log(p)
- WEBIM.server.sendWebSocketMsg({
- data: p,
- success(res) {
- console.log('【websocket】发送成功')
-
- uni.hideLoading()
- },
- fail(err) {
- console.log('【websocket】发送失败')
- console.log(err)
-
- uni.hideLoading()
- }
- });
- }
- export default WEBIM
|