app.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // app.js
  2. import {
  3. CbEvents
  4. } from 'open-im-sdk'
  5. import openIM from "./utils/openim";
  6. import HTTP from "./requestFn/Api"
  7. App({
  8. onLaunch() {
  9. // 展示本地存储能力
  10. const logs = wx.getStorageSync('logs') || []
  11. logs.unshift(Date.now())
  12. wx.setStorageSync('logs', logs)
  13. // 隐藏系统tabbar
  14. wx.hideTabBar();
  15. // 获取设备信息
  16. this.getSystemInfo();
  17. this.getWxCode();
  18. },
  19. onShow: function () {
  20. const openim_id = wx.getStorageSync('openim_id');
  21. const openIMToken = wx.getStorageSync('openIMToken');
  22. if (openim_id) {
  23. this.globalData.openim_id = openim_id;
  24. this.globalData.openIMToken = openIMToken;
  25. openIM.getLoginStatus().then(res => {
  26. console.log("连接成功", res);
  27. }).catch(err => {
  28. console.log("连接失败", err);
  29. this.login();
  30. })
  31. }
  32. },
  33. onHide() {
  34. openIM.logout()
  35. },
  36. getSystemInfo: function () {
  37. let menuButtonObject = wx.getMenuButtonBoundingClientRect();
  38. let t = this;
  39. wx.getSystemInfo({
  40. success: function (res) {
  41. t.globalData.systemInfo = res;
  42. let statusBarHeight = res.statusBarHeight,
  43. navTop = menuButtonObject.top,
  44. navHeight = statusBarHeight + menuButtonObject.height - (menuButtonObject.top - statusBarHeight) * 2;
  45. t.globalData.navHeight = navHeight;
  46. t.globalData.navTop = navTop; //navTop
  47. t.globalData.windowHeight = res.windowHeight;
  48. }
  49. });
  50. },
  51. onPullDownRefresh: function () {
  52. this.onRefresh();
  53. },
  54. editTabbar: function () {
  55. let tabbar = this.globalData.tabBar;
  56. let currentPages = getCurrentPages();
  57. let _this = currentPages[currentPages.length - 1];
  58. let pagePath = _this.route;
  59. (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
  60. for (let i in tabbar.list) {
  61. tabbar.list[i].selected = false;
  62. (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
  63. }
  64. _this.setData({
  65. tabbar: tabbar
  66. });
  67. },
  68. login() {
  69. const that = this;
  70. const {
  71. openim_id: userID,
  72. openIMToken: token
  73. } = that.globalData;
  74. const config = {
  75. userID,
  76. token,
  77. url: "wss://www.wxmini.getensh.com:60001",
  78. platformID: 5,
  79. };
  80. openIM.login(config).then(() => {
  81. openIM.getAllConversationList().then(res => {
  82. const str = JSON.parse(res.data);
  83. let openim_ids = "";
  84. const Session_list = str.map(item => {
  85. openim_ids = openim_ids+`${item.userID},`
  86. return {
  87. ...item,
  88. left: 0,
  89. latestMsg: JSON.parse(item.latestMsg)
  90. }
  91. })
  92. HTTP.OpenimUsers({openim_ids}).then(({list})=>{
  93. let result = [];
  94. list.map(item=>{
  95. Session_list.map(key=>{
  96. if(item.openim_id===key.userID){
  97. result.push({...key,faceURL:item.face_url})
  98. }
  99. })
  100. })
  101. console.log(result);
  102. that.globalData.Session_list = result;
  103. }).catch(err=>{
  104. that.globalData.Session_list = Session_list;
  105. })
  106. })
  107. openIM.getTotalUnreadMsgCount().then(res => {
  108. that.globalData.unreadCountHome = res.data;
  109. that.globalData.unreadCountNeig = res.data;
  110. that.globalData.unreadCountGate = res.data;
  111. that.globalData.unreadCountMes = res.data;
  112. that.globalData.unreadCountMine = res.data;
  113. });
  114. openIM.on(CbEvents.ONCONVERSATIONCHANGED, (res) => {
  115. const str = JSON.parse(res.data);
  116. const {
  117. Session_list
  118. } = that.globalData;
  119. let index = Session_list.findIndex((val) => val.conversationID === str[0].conversationID)
  120. let item = str[0];
  121. if (index > -1) {
  122. const {faceURL} = Session_list[index];
  123. Session_list.splice(index, 1);
  124. Session_list.unshift({
  125. ...item,
  126. latestMsg: JSON.parse(item.latestMsg),
  127. left: 0,
  128. faceURL
  129. })
  130. }
  131. that.globalData.Session_list = Session_list
  132. })
  133. openIM.on(CbEvents.ONNEWCONVERSATION, (res) => {
  134. const str = JSON.parse(res.data);
  135. const newChat = str.map(item => {
  136. return {
  137. ...item,
  138. left: 0,
  139. latestMsg: JSON.parse(item.latestMsg)
  140. }
  141. })
  142. const {
  143. Session_list
  144. } = that.globalData;
  145. newChat.forEach((item) => {
  146. let index = Session_list.findIndex(e =>
  147. e.conversationID == item.conversationID
  148. )
  149. if (index > -1) {
  150. Session_list[index] = {
  151. ...item,
  152. }
  153. } else {
  154. Session_list.unshift({
  155. ...item
  156. })
  157. }
  158. })
  159. that.globalData.Session_list = Session_list
  160. })
  161. openIM.on(CbEvents.ONTOTALUNREADMESSAGECOUNTCHANGED, (res) => {
  162. that.globalData.unreadCountHome = res.data;
  163. that.globalData.unreadCountNeig = res.data;
  164. that.globalData.unreadCountGate = res.data;
  165. that.globalData.unreadCountMes = res.data;
  166. that.globalData.unreadCountMine = res.data;
  167. })
  168. })
  169. },
  170. loginout() {
  171. openIM.logout().then(res => {
  172. this.globalData.userID = "";
  173. this.globalData.Session_list = [];
  174. this.globalData.unreadCountHome = 0;
  175. this.globalData.unreadCountNeig = 0;
  176. this.globalData.unreadCountGate = 0;
  177. this.globalData.unreadCountMes = 0;
  178. this.globalData.unreadCountMine = 0;
  179. })
  180. },
  181. sendMessage(val, _this) {
  182. openIM.createTextMessage(val).then(({
  183. data
  184. }) => {
  185. this.crateMessage(data, _this, 'text')
  186. })
  187. },
  188. sendCard(cardDesc, _this, res) {
  189. openIM.createCardMessage(cardDesc).then(({
  190. data
  191. }) => {
  192. this.crateMessage(data, _this, 'card', res)
  193. })
  194. },
  195. crateMessage(data, _this, type, res) {
  196. let recvID;
  197. let showname;
  198. if (res) {
  199. const {
  200. manager_list,
  201. } = _this.data;
  202. recvID = manager_list[res.tapIndex].openim_id;
  203. showname = manager_list[res.tapIndex].name;
  204. this.globalData.userID = recvID;
  205. } else {
  206. recvID = this.globalData.userID;
  207. }
  208. const options = {
  209. recvID,
  210. groupID: "",
  211. message: data,
  212. };
  213. openIM.sendMessage(options).then(({
  214. data,
  215. }) => {
  216. if (type == "text") {
  217. this.handleSendMsg(data, _this)
  218. } else {
  219. const userIDList = [recvID]
  220. openIM.getUsersInfo(userIDList).then(({
  221. data
  222. }) => {
  223. let res = JSON.parse(data);
  224. const {
  225. faceURL: faceurl
  226. } = res[0].publicInfo;
  227. this.goChat(recvID, showname, faceurl);
  228. })
  229. }
  230. })
  231. },
  232. goChat(userID, showname, faceurl) {
  233. wx.navigateTo({
  234. url: `/page/message/pages/Chat/chat?userID=${userID}&showname=${showname}&faceurl=${faceurl}`,
  235. })
  236. },
  237. handleSendMsg(data, _this) {
  238. const that = _this;
  239. const {
  240. msgList
  241. } = that.data;
  242. let newMessage = JSON.parse(data);
  243. if (msgList.length > 0) {
  244. let {
  245. sendTime
  246. } = msgList[msgList.length - 1];
  247. let minutes = (newMessage.sendTime - sendTime) / 1000 / 60;
  248. if (minutes > 5) {
  249. newMessage = {
  250. ...newMessage,
  251. showTime: true
  252. };
  253. } else {
  254. newMessage = {
  255. ...newMessage,
  256. showTime: false
  257. };
  258. }
  259. } else {
  260. newMessage = {
  261. ...newMessage,
  262. showTime: true
  263. };
  264. }
  265. if (newMessage.contentType == 102) {
  266. newMessage = {
  267. ...newMessage,
  268. content: JSON.parse(newMessage.content)
  269. };
  270. }
  271. that.setData({
  272. msgList: [...msgList, newMessage],
  273. inputVal: "",
  274. lineCount: 1,
  275. textareaHeight: 8,
  276. toView: "msg-" + msgList.length,
  277. })
  278. },
  279. getMessage(flag, _this) {
  280. const that = _this;
  281. const {
  282. userID,
  283. } = this.globalData;
  284. const {
  285. msgList
  286. } = that.data;
  287. const options = {
  288. groupID: "",
  289. startClientMsgID: flag ? msgList[0].clientMsgID : '', // 上一次拉取的最后一条消息ID或空字符串,为空字符则从最新一条开始
  290. count: 12,
  291. userID,
  292. }
  293. openIM.getHistoryMessageList(options).then(({
  294. data
  295. }) => {
  296. let result = JSON.parse(data);
  297. const newMessageList = [];
  298. if (result.length > 0) {
  299. result.forEach((item, index) => {
  300. if ([102, 108].includes(item.contentType)) {
  301. item.content = JSON.parse(item.content)
  302. }
  303. if (index == 0) {
  304. newMessageList.push({
  305. ...item,
  306. showTime: true,
  307. })
  308. } else {
  309. const current = result[index].sendTime;
  310. let minutes;
  311. const lastShowTime = result[index - 1].sendTime;
  312. minutes = (current - lastShowTime) / 1000 / 60;
  313. if (minutes > 5) {
  314. newMessageList.push({
  315. ...item,
  316. showTime: true,
  317. })
  318. } else {
  319. newMessageList.push({
  320. ...item,
  321. showTime: false,
  322. })
  323. }
  324. }
  325. })
  326. if (flag) {
  327. that.setData({
  328. msgList: [...newMessageList, ...msgList, ],
  329. inputVal: ''
  330. })
  331. that.setData({
  332. toView: 'msg-' + [newMessageList.length - 2],
  333. })
  334. } else {
  335. that.setData({
  336. msgList: newMessageList,
  337. toView: 'msg-' + [newMessageList.length - 1],
  338. })
  339. }
  340. }
  341. console.log(newMessageList);
  342. that.setData({
  343. refer: false
  344. })
  345. }).catch(err => {
  346. console.log("获取会话列表失败", err);
  347. })
  348. },
  349. createImage(url, _this) {
  350. const that = _this;
  351. const {
  352. userID: recvID,
  353. } = this.globalData;
  354. const baseInfo = {
  355. uuid: "", //图片唯一id
  356. type: "png", //图片类型
  357. size: 12465, //图片大小
  358. width: 1080, //图片宽度
  359. height: 720, //图片高度
  360. url
  361. }
  362. const data = {
  363. sourcePicture: baseInfo, //原图信息
  364. bigPicture: baseInfo, //大图信息
  365. snapshotPicture: baseInfo, //缩略图信息
  366. }
  367. openIM.createImageMessage(data).then(res => {
  368. const options = {
  369. recvID,
  370. groupID: "",
  371. message: res.data,
  372. };
  373. openIM.sendMessageNotOss(options).then(({
  374. data,
  375. }) => {
  376. this.handleSendMsg(data, that)
  377. }).catch(err => {})
  378. })
  379. },
  380. deleteChat(conversationID, index, _this) {
  381. const that = _this;
  382. const app = this;
  383. openIM.deleteConversation(conversationID).then(res => {
  384. const {
  385. Session_list
  386. } = that.data;
  387. Session_list.splice(index, 1);
  388. that.setData({
  389. Session_list
  390. })
  391. app.globalData.Session_list = Session_list;
  392. })
  393. },
  394. //获取code
  395. getWxCode() {
  396. const that = this;
  397. wx.login({
  398. success(res) {
  399. that.globalData.code = res.code;
  400. }
  401. })
  402. },
  403. onRefresh: function () {
  404. //导航条加载动画
  405. wx.showNavigationBarLoading()
  406. //loading 提示框
  407. wx.showLoading({
  408. title: 'Loading...',
  409. })
  410. setTimeout(function () {
  411. wx.hideLoading();
  412. wx.hideNavigationBarLoading();
  413. //停止下拉刷新
  414. wx.stopPullDownRefresh();
  415. }, 1000)
  416. },
  417. watch: function (variate, method) {
  418. var obj = this.globalData;
  419. let val = obj[variate]; // 单独变量来存储原来的值
  420. Object.defineProperty(obj, variate, {
  421. configurable: false,
  422. enumerable: true,
  423. set: function (value) {
  424. val = value; // 重新赋值
  425. method(variate, value); // 执行回调方法
  426. },
  427. get: function () {
  428. return val; // 返回当前值
  429. }
  430. })
  431. },
  432. onMessage(_this) {
  433. const that = _this;
  434. openIM.on(CbEvents.ONRECVNEWMESSAGE, (res) => {
  435. const {
  436. msgList
  437. } = that.data;
  438. let data = JSON.parse(res.data);
  439. const {
  440. userID
  441. } = this.globalData;
  442. if (userID == data.sendID & data.status == 2) {
  443. let current = data.sendTime;
  444. let minutes;
  445. let lastShowTime = msgList[msgList.length - 1].sendTime;
  446. minutes = (current - lastShowTime) / 1000 / 60;
  447. if (minutes > 5) {
  448. data = {
  449. ...data,
  450. showTime: true
  451. }
  452. } else {
  453. data = {
  454. ...data,
  455. showTime: false
  456. }
  457. }
  458. if (data.contentType == 102) {
  459. data = {
  460. ...data,
  461. content: JSON.parse(data.content)
  462. }
  463. }
  464. that.setData({
  465. msgList: [...msgList, data],
  466. });
  467. that.setData({
  468. toView: "msg-" + msgList.length
  469. })
  470. if (userID) {
  471. this.readMessage()
  472. }
  473. }
  474. })
  475. },
  476. readMessage() {
  477. const {
  478. userID,
  479. } = this.globalData;
  480. const options = {
  481. userID,
  482. msgIDList: []
  483. }
  484. openIM.markC2CMessageAsRead(options)
  485. },
  486. globalData: {
  487. userID: "",
  488. baseURL: "https://www.wxmini.getensh.com/api/v1",
  489. code: "",
  490. can_pay: false,
  491. house_id: null,
  492. house_name: null,
  493. garden_id: null,
  494. garden_name: null,
  495. house_list: [],
  496. tabBar: {
  497. "backgroundColor": "#ffffff",
  498. "color": "#707070",
  499. "selectedColor": "#216d2a",
  500. "list": [{
  501. "pagePath": "/page/tabBar/home/index",
  502. "iconPath": "icon/home.png",
  503. "selectedIconPath": "icon/home-select.png",
  504. "text": "首页"
  505. },
  506. {
  507. "pagePath": "/page/tabBar/neighbor/index",
  508. "iconPath": "icon/book.png",
  509. "selectedIconPath": "icon/book-select.png",
  510. "text": "邻里"
  511. },
  512. {
  513. "pagePath": "/page/tabBar/gate/index",
  514. "iconPath": "icon/phone.png",
  515. "isSpecial": true,
  516. "text": ""
  517. },
  518. {
  519. "pagePath": "/page/tabBar/message/index",
  520. "iconPath": "icon/message.png",
  521. "selectedIconPath": "icon/message-select.png",
  522. "text": "消息"
  523. },
  524. {
  525. "pagePath": "/page/tabBar/mine/index",
  526. "iconPath": "icon/my.png",
  527. "selectedIconPath": "icon/my-select.png",
  528. "text": "我的"
  529. },
  530. ]
  531. },
  532. navHeight: 0,
  533. navTop: 0,
  534. authentication: false,
  535. city_name: "",
  536. city_code: "",
  537. longitude: 0,
  538. latitude: 0,
  539. client_id: "QufW7Lmgn84ZMiYQybH5MYYq",
  540. client_secret: "yprUS1IDQGsH4saSrx3j4YKQLsacgoFn",
  541. Session_list: [],
  542. unreadCountHome: 0,
  543. unreadCountNeig: 0,
  544. unreadCountGate: 0,
  545. unreadCountMes: 0,
  546. unreadCountMine: 0,
  547. },
  548. })