App.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <script>
  2. let WebIM = (wx.WebIM = require("./utils/WebIM")["default"]);
  3. let msgStorage = require("./components/chat/msgstorage");
  4. let msgType = require("./components/chat/msgtype");
  5. let disp = require("./utils/broadcast");
  6. let logout = false;
  7. // let emedia = uni.emedia = require("./emediaSDK/webrtc/src/entry")
  8. // let emedia = (uni.emedia = require("./emediaSDK/emedia_for_miniProgram"));
  9. // emedia.config({ useUniappPlugin: true });
  10. function ack(receiveMsg) {
  11. // 处理未读消息回执
  12. var bodyId = receiveMsg.id; // 需要发送已读回执的消息id
  13. var ackMsg = new WebIM.message("read", WebIM.conn.getUniqueId());
  14. ackMsg.set({
  15. id: bodyId,
  16. to: receiveMsg.from,
  17. });
  18. WebIM.conn.send(ackMsg.body);
  19. }
  20. function onMessageError(err) {
  21. if (err.type === "error") {
  22. uni.showToast({
  23. title: err.errorText,
  24. });
  25. return false;
  26. }
  27. return true;
  28. }
  29. function getCurrentRoute() {
  30. let pages = getCurrentPages();
  31. if (pages.length > 0) {
  32. let currentPage = pages[pages.length - 1];
  33. return currentPage.route;
  34. }
  35. return "/";
  36. }
  37. // 包含陌生人版本
  38. function calcUnReadSpot(message) {
  39. let myName = uni.getStorageSync("myUsername");
  40. uni.getStorageInfo({
  41. success: function (res) {
  42. let storageKeys = res.keys;
  43. let newChatMsgKeys = [];
  44. let historyChatMsgKeys = [];
  45. storageKeys.forEach((item) => {
  46. if (item.indexOf(myName) > -1 && item.indexOf("rendered_") == -1) {
  47. newChatMsgKeys.push(item);
  48. }
  49. });
  50. let count = newChatMsgKeys.reduce(function (result, curMember, idx) {
  51. let chatMsgs;
  52. chatMsgs = uni.getStorageSync(curMember) || [];
  53. return result + chatMsgs.length;
  54. }, 0);
  55. getApp().globalData.unReadMessageNum = count;
  56. disp.fire("em.unreadspot", message);
  57. },
  58. });
  59. }
  60. function saveGroups() {
  61. var me = this;
  62. return WebIM.conn.getGroup({
  63. limit: 50,
  64. success: function (res) {
  65. uni.setStorage({
  66. key: "listGroup",
  67. data: res.data,
  68. });
  69. },
  70. error: function (err) {
  71. console.log(err);
  72. },
  73. });
  74. }
  75. // var jpushModule = uni.requireNativePlugin("JG-JPush");
  76. import webIM from './utils/WebIM.js'
  77. var that = ''
  78. export default {
  79. globalData: {
  80. unReadMessageNum: 0,
  81. userInfo: null,
  82. saveFriendList: [],
  83. saveGroupInvitedList: [],
  84. isIPX: false, //是否为iphone X
  85. conn: {
  86. closed: false,
  87. curOpenOpt: {},
  88. open(opt) {
  89. uni.showLoading({
  90. title: "正在初始化客户端..",
  91. mask: true,
  92. });
  93. this.curOpenOpt = opt;
  94. WebIM.conn.open(opt);
  95. this.closed = false;
  96. },
  97. reopen() {
  98. if (this.closed) {
  99. //this.open(this.curOpenOpt);
  100. WebIM.conn.open(this.curOpenOpt);
  101. this.closed = false;
  102. }
  103. },
  104. },
  105. onLoginSuccess: function(myName) {
  106. uni.hideLoading();
  107. uni.redirectTo({
  108. url: "../chat/chat?myName=" + myName,
  109. });
  110. },
  111. getUserInfo(cb) {
  112. var me = this;
  113. if (this.userInfo) {
  114. typeof cb == "function" && cb(this.userInfo);
  115. } else {
  116. // 调用登录接口
  117. uni.login({
  118. success() {
  119. uni.getUserInfo({
  120. success(res) {
  121. me.userInfo = res.userInfo;
  122. typeof cb == "function" && cb(me.userInfo);
  123. },
  124. });
  125. },
  126. });
  127. }
  128. },
  129. checkIsIPhoneX: function() {
  130. const me = this;
  131. uni.getSystemInfo({
  132. success: function(res) {
  133. // 根据 model 进行判断
  134. if (res.model && res.model.search("iPhone X") != -1) {
  135. me.isIPX = true;
  136. }
  137. },
  138. });
  139. },
  140. },
  141. onLaunch: function() {
  142. this.HXlogin()
  143. console.log('App Launch')
  144. var me = this;
  145. var logs = uni.getStorageSync("logs") || [];
  146. logs.unshift(Date.now());
  147. uni.setStorageSync("logs", logs); //
  148. disp.on("em.main.ready", function() {
  149. calcUnReadSpot();
  150. });
  151. disp.on("em.chatroom.leave", function() {
  152. calcUnReadSpot();
  153. });
  154. disp.on("em.chat.session.remove", function() {
  155. calcUnReadSpot();
  156. });
  157. disp.on("em.chat.audio.fileLoaded", function() {
  158. calcUnReadSpot();
  159. });
  160. disp.on("em.main.deleteFriend", function() {
  161. calcUnReadSpot();
  162. });
  163. disp.on("em.chat.audio.fileLoaded", function() {
  164. calcUnReadSpot();
  165. }); //
  166. WebIM.conn.listen({
  167. onOpened(message) {
  168. console.log('>>>>>>环信登录成功啦')
  169. },
  170. onReconnect() {
  171. uni.showToast({
  172. title: "重连中...",
  173. duration: 2000,
  174. });
  175. },
  176. onSocketConnected() {
  177. uni.showToast({
  178. title: "socket连接成功",
  179. duration: 2000,
  180. });
  181. },
  182. onClosed() {
  183. /* uni.showToast({
  184. title: "网络已断开",
  185. icon: "none",
  186. duration: 2000,
  187. });
  188. uni.redirectTo({
  189. url: "/pages/login/codeLogin/codeLogin",
  190. }); */
  191. me.globalData.conn.closed = true;
  192. WebIM.conn.close();
  193. },
  194. onInviteMessage(message) {
  195. me.globalData.saveGroupInvitedList.push(message);
  196. disp.fire("em.invite.joingroup", message); // uni.showModal({
  197. // title: message.from + " 已邀你入群 " + message.roomid,
  198. // success(){
  199. // disp.fire("em.invite.joingroup", message);
  200. // },
  201. // error(){
  202. // disp.fire("em.invite.joingroup", message);
  203. // }
  204. // });
  205. },
  206. onReadMessage(message) {
  207. //console.log('已读', message)
  208. },
  209. //onPresence为旧版 ,建议参考最新增删好友api文档 :http://docs-im.easemob.com/im/web/basics/buddy
  210. onPresence(message) {
  211. switch (message.type) {
  212. case "unsubscribe":
  213. break;
  214. // 好友邀请列表
  215. case "subscribe":
  216. for (let i = 0; i < me.globalData.saveFriendList.length; i++) {
  217. if (me.globalData.saveFriendList[i].from === message.from) {
  218. me.globalData.saveFriendList[i] = message;
  219. disp.fire("em.subscribe");
  220. return;
  221. }
  222. }
  223. msgStorage.saveReceiveMsg(message, 'INFORM'); //存添加好友消息,方便展示通知
  224. me.globalData.saveFriendList.push(message);
  225. disp.fire("em.subscribe");
  226. break;
  227. case "subscribed":
  228. uni.showToast({
  229. title: "添加成功",
  230. duration: 1000,
  231. });
  232. disp.fire("em.subscribed");
  233. break;
  234. case "unsubscribed":
  235. disp.fire("em.unsubscribed");
  236. break;
  237. case "direct_joined":
  238. saveGroups();
  239. uni.showToast({
  240. title: "已进群",
  241. duration: 1000,
  242. });
  243. break;
  244. case "memberJoinPublicGroupSuccess":
  245. saveGroups();
  246. uni.showToast({
  247. title: "已进群",
  248. duration: 1000,
  249. });
  250. break;
  251. case "invite":
  252. // 防止重复添加
  253. for (let i = 0; i < me.globalData.saveGroupInvitedList.length; i++) {
  254. if (me.globalData.saveGroupInvitedList[i].from === message.from) {
  255. me.globalData.saveGroupInvitedList[i] = message;
  256. disp.fire("em.invite.joingroup")
  257. return;
  258. }
  259. }
  260. me.globalData.saveGroupInvitedList.push(message);
  261. disp.fire("em.invite.joingroup");
  262. msgStorage.saveReceiveMsg(message, 'INFORM'); //存添加好友消息,方便展示通知
  263. break;
  264. case "unavailable":
  265. disp.fire("em.contacts.remove");
  266. disp.fire("em.group.leaveGroup", message);
  267. break;
  268. case "deleteGroupChat":
  269. disp.fire("em.invite.deleteGroup", message);
  270. break;
  271. case "leaveGroup":
  272. disp.fire("em.group.leaveGroup", message);
  273. break;
  274. case "removedFromGroup":
  275. disp.fire("em.group.leaveGroup", message);
  276. break;
  277. default:
  278. break;
  279. }
  280. },
  281. onRoster(message) {
  282. // let pages = getCurrentPages();
  283. // if(pages[0]){
  284. // pages[0].onShow();
  285. // }
  286. },
  287. onVideoMessage(message) {
  288. console.log("onVideoMessage: ", message);
  289. if (message) {
  290. msgStorage.saveReceiveMsg(message, msgType.VIDEO);
  291. }
  292. calcUnReadSpot(message);
  293. ack(message);
  294. },
  295. onAudioMessage(message) {
  296. console.log("onAudioMessage", message);
  297. if (message) {
  298. if (onMessageError(message)) {
  299. msgStorage.saveReceiveMsg(message, msgType.AUDIO);
  300. }
  301. calcUnReadSpot(message);
  302. ack(message);
  303. }
  304. },
  305. onCmdMessage(message) {
  306. console.log("onCmdMessage", message);
  307. if (message) {
  308. if (onMessageError(message)) {
  309. msgStorage.saveReceiveMsg(message, msgType.CMD);
  310. }
  311. calcUnReadSpot(message);
  312. ack(message);
  313. }
  314. },
  315. // onLocationMessage(message){
  316. // console.log("Location message: ", message);
  317. // if(message){
  318. // msgStorage.saveReceiveMsg(message, msgType.LOCATION);
  319. // }
  320. // },
  321. onTextMessage(message) {
  322. console.log("收到消息", message);
  323. if (message) {
  324. if (onMessageError(message)) {
  325. msgStorage.saveReceiveMsg(message, msgType.TEXT);
  326. }
  327. calcUnReadSpot(message);
  328. ack(message);
  329. }
  330. },
  331. onEmojiMessage(message) {
  332. console.log("onEmojiMessage", message);
  333. if (message) {
  334. if (onMessageError(message)) {
  335. msgStorage.saveReceiveMsg(message, msgType.EMOJI);
  336. }
  337. calcUnReadSpot(message);
  338. ack(message);
  339. }
  340. },
  341. onPictureMessage(message) {
  342. console.log("onPictureMessage", message);
  343. if (message) {
  344. if (onMessageError(message)) {
  345. msgStorage.saveReceiveMsg(message, msgType.IMAGE);
  346. }
  347. calcUnReadSpot(message);
  348. ack(message);
  349. }
  350. },
  351. onFileMessage(message) {
  352. console.log("onFileMessage", message);
  353. if (message) {
  354. if (onMessageError(message)) {
  355. msgStorage.saveReceiveMsg(message, msgType.FILE);
  356. }
  357. calcUnReadSpot(message);
  358. ack(message);
  359. }
  360. },
  361. // 各种异常
  362. onError(error) {
  363. console.log(error); // 16: server-side close the websocket connection
  364. // if (error.type == WebIM.statusCode.WEBIM_CONNCTION_DISCONNECTED) {
  365. // // if(error.type == WebIM.statusCode.WEBIM_CONNCTION_DISCONNECTED && !logout){
  366. // // if(WebIM.conn.autoReconnectNumTotal < WebIM.conn.autoReconnectNumMax){
  367. // // return;
  368. // // }
  369. // uni.showToast({
  370. // title: "websocket 断开",
  371. // duration: 1000
  372. // });
  373. // uni.redirectTo({
  374. // url: "../login/login"
  375. // });
  376. // logout = true;
  377. // return;
  378. // } // 8: offline by multi login
  379. // if (error.type == WebIM.statusCode.WEBIM_CONNCTION_SERVER_ERROR) {
  380. // uni.showToast({
  381. // title: "offline by multi login",
  382. // duration: 1000
  383. // });
  384. // uni.redirectTo({
  385. // url: "../login/login"
  386. // });
  387. // }
  388. if (error.type == WebIM.statusCode.WEBIM_CONNCTION_OPEN_ERROR) {
  389. uni.hideLoading();
  390. disp.fire("em.error.passwordErr"); // uni.showModal({
  391. // title: "用户名或密码错误",
  392. // confirmText: "OK",
  393. // showCancel: false
  394. // });
  395. }
  396. if (error.type == WebIM.statusCode.WEBIM_CONNCTION_AUTH_ERROR) {
  397. uni.hideLoading();
  398. disp.fire("em.error.tokenErr");
  399. }
  400. if (error.type == "socket_error") {
  401. ///sendMsgError
  402. console.log("socket_errorsocket_error", error);
  403. /* uni.showToast({
  404. title: "网络已断开",
  405. icon: "none",
  406. duration: 2000,
  407. }); */
  408. disp.fire("em.error.sendMsgErr", error);
  409. }
  410. },
  411. });
  412. this.globalData.checkIsIPhoneX();
  413. },
  414. onShow: function() {
  415. console.log('App Show')
  416. },
  417. onHide: function() {
  418. console.log('App Hide')
  419. },
  420. methods: {
  421. HXlogin() {
  422. var userId = uni.getStorageSync('myUsername')
  423. var options = {
  424. user: userId,
  425. pwd : '12345678',
  426. appKey: uni.WebIM.config.appkey,
  427. success: function() {
  428. // var lianjie = uni.WebIM.conn.isOpened()
  429. var my = uni.getStorageSync("myUsername");
  430. console.log('登录成功');
  431. // uni.setStorageSync('myUsername',my)
  432. },
  433. error: function() {
  434. }
  435. }
  436. uni.WebIM.conn.open(options);
  437. }
  438. }
  439. }
  440. </script>
  441. <style lang="scss">
  442. /*每个页面公共css */
  443. @import "colorui/main.css";
  444. @import "colorui/icon.css";
  445. @import "uview-ui/index.scss";
  446. .content {
  447. background-color: #ffffff;
  448. width: 100%;
  449. min-height: 100vh;
  450. display: flex;
  451. flex-direction: column;
  452. font-size: 28rpx;
  453. color: #333;
  454. box-sizing: border-box;
  455. }
  456. .hflex {
  457. display: flex;
  458. flex-direction: row;
  459. flex-wrap: nowrap;
  460. }
  461. .vflex {
  462. display: flex;
  463. flex-direction: column;
  464. flex-wrap: nowrap;
  465. }
  466. .jbetween {
  467. justify-content: space-between;
  468. }
  469. .jaround {
  470. justify-content: space-around;
  471. }
  472. .jend {
  473. justify-content: flex-end;
  474. }
  475. .jcenter {
  476. justify-content: center;
  477. }
  478. .acenter {
  479. align-items: center;
  480. }
  481. .abetween {
  482. align-content: space-between;
  483. }
  484. .astart {
  485. align-items: flex-start;
  486. }
  487. .aend {
  488. align-items: flex-end;
  489. }
  490. .fwrap {
  491. flex-wrap: wrap;
  492. }
  493. .text_hide {
  494. overflow: hidden;
  495. white-space: nowrap;
  496. text-overflow: ellipsis;
  497. }
  498. .text_hide2 {
  499. text-overflow: -o-ellipsis-lastline;
  500. overflow: hidden; //溢出内容隐藏
  501. text-overflow: ellipsis; //文本溢出部分用省略号表示
  502. display: -webkit-box; //特别显示模式
  503. -webkit-line-clamp: 2; //行数
  504. line-clamp: 2;
  505. -webkit-box-orient: vertical; //盒子中内容竖直排列
  506. }
  507. </style>