chat.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. import action from "./action.js"
  2. class chat {
  3. push(res, fromway) {
  4. if (fromway != uni.getStorageSync('storeykey')) return false;
  5. var data = {
  6. id: res.data.cache_key,
  7. msg: res.data
  8. }
  9. let key = prefix(data.id);
  10. var msg_list_key = msglist_key();
  11. var statesource = cache(key) ? cache(key) : [];
  12. // console.log(statesource[statesource.length-1].msg_id,res.data.msg_id)
  13. if (statesource.length > 0) {
  14. if (statesource[statesource.length - 1].msg_id == res.data.msg_id) return false;
  15. }
  16. if (data.msg.sender_id != uni.getStorageSync('access_token')) data.msg.unread = 1;
  17. else data.msg.unread = 0;
  18. var statelist = [];
  19. var msglist = cache(msg_list_key) || [];
  20. statesource.push(data.msg);
  21. // console.log(data.msg.message);
  22. if (data.msg.message.type != 'tips' || data.msg.message.msg_type == 'show') {
  23. //更新消息列表
  24. if (data.msg.isloading == 0 || data.msg.message.msg_type == 'show') {
  25. let msgindex = msglist.findIndex(v => v.key == key);
  26. let unreadCount = 0;
  27. var readtime = 0;
  28. if (msgindex >= 0) {
  29. let tmp = msglist.splice(msgindex, 1);
  30. unreadCount = tmp[0].msg.unread || 0;
  31. readtime = tmp[0].msg.readtime || 0;
  32. }
  33. // console.log(data.msg);
  34. // console.log(data.msg.cache_key , uni.getStorageSync('cache_key'))
  35. if (data.msg.self != 1 && data.msg.cache_key != uni.getStorageSync('cache_key')) {
  36. var no_tip = 0;
  37. var msgnotip = uni.getStorageSync('msgnotip');
  38. if (!msgnotip) msgnotip = [];
  39. msgnotip.map(item => {
  40. if (item == data.msg.cache_key) {
  41. no_tip = 1;
  42. }
  43. })
  44. try {
  45. data.msg.message.content = JSON.parse(data.msg.message.content);
  46. } catch (e) {
  47. }
  48. if (data.msg.message.content.type == 'remind' && data.msg.message.content.remind.id == uni
  49. .getStorageSync('access_token')) {
  50. no_tip = 0;
  51. data.msg.message.content.content = data.msg.message.content.content.replace('@{' + data.msg
  52. .message.content.remind.id + '}', '有人@我');
  53. cache('remid_' + key, data.msg);
  54. }
  55. if (no_tip == 0) {
  56. try {
  57. let userinfo = cache('userInfo');
  58. if (parseInt(data.msg.group_id) > 0) {
  59. if (parseInt(userinfo.note1) != 0) action.playVoice('/static/voice/chat.mp3');
  60. if (parseInt(userinfo.note2) != 0) uni.vibrateLong();
  61. } else {
  62. if (parseInt(userinfo.note3) != 0) action.playVoice('/static/voice/friend.mp3');
  63. if (parseInt(userinfo.note4) != 0) uni.vibrateLong();
  64. }
  65. } catch (e) {
  66. //TODO handle the exception
  67. }
  68. }
  69. }
  70. //console.log(data.msg);
  71. if (cache('remid_' + key)) var datamsg = cache('remid_' + key)
  72. else
  73. var datamsg = data.msg
  74. delete datamsg['receiver']
  75. msglist.push({
  76. key,
  77. msg: {
  78. ...datamsg,
  79. unread: data.msg.self ? unreadCount : unreadCount + 1,
  80. readtime: 0,
  81. istop: 0
  82. }
  83. });
  84. uni.setStorageSync(msg_list_key, msglist);
  85. }
  86. // console.log(data.msg);
  87. } else {
  88. let list1 = statesource;
  89. //测回消息
  90. if (data.msg.message.msg_type == 'chat_back') {
  91. // #ifdef APP-PLUS
  92. /* plus.push.clear(); */
  93. // #endif
  94. try {
  95. var msg_id = data.msg.message.data.msg_id;
  96. } catch (e) {
  97. //TODO handle the exception
  98. var msg_id = data.msg.msg_id;
  99. }
  100. var list = [];
  101. if (msg_id > 0) {
  102. var unread = 0;
  103. var tempdata = '';
  104. var num = 0;
  105. for (var i = 0; i < statesource.length; i++) {
  106. var item = statesource[i];
  107. if (parseInt(item.msg_id) == parseInt(msg_id)) {
  108. } else {
  109. list.push(item);
  110. if (item.message.type != 'tips') {
  111. tempdata = item;
  112. unread = unread + item.unread;
  113. }
  114. }
  115. }
  116. statesource = list;
  117. let msgindex = msglist.findIndex(v => v.key == key);
  118. let unreadCount = 0;
  119. if (msgindex >= 0) {
  120. let tmp = msglist.splice(msgindex, 1);
  121. unreadCount = tmp[0].msg.unread || 0;
  122. }
  123. msglist.push({
  124. key,
  125. msg: {
  126. ...tempdata,
  127. unread: unread,
  128. istop: set_msgtop(data.msg.cache_key)
  129. }
  130. });
  131. cache(msg_list_key, msglist);
  132. }
  133. } else {
  134. // console.log(data);
  135. }
  136. }
  137. uni.setStorageSync(key, statesource);
  138. return {
  139. 'source': statesource,
  140. 'msglist': msglist
  141. };
  142. }
  143. send(data) {
  144. console.log(data);
  145. }
  146. //最近聊天记录
  147. msglist() {
  148. let tmp = [];
  149. var msg_list_key = msglist_key();
  150. let lists11 = cache(msg_list_key) ? cache(msg_list_key) : [];
  151. let lists = [];
  152. var userid = uni.getStorageSync('access_token');
  153. for (var i = 0; i < lists11.length; i++) {
  154. try {
  155. // console.log(lists11[i]['key'],uni.getStorageSync('access_token'));
  156. if (lists11[i]['key'].indexOf(userid + '_chat_') > -1) lists.push(lists11[i]);
  157. } catch (e) {
  158. }
  159. }
  160. lists.forEach(v => {
  161. v.msg = Object.assign(v.msg, (+v.msg.group_id > 1) ? getGroupInfoById(v.msg.group_id) :
  162. getUserInfo(v.msg));
  163. try {
  164. v.msg.message.content = JSON.parse(v.msg.message.content);
  165. } catch (e) {
  166. }
  167. if (v.msg.message.content.type == 'remind') {
  168. if (v.msg.message.content.remind.id == uni.getStorageSync('access_token')) {
  169. v.msg.message.content.content = v.msg.message.content.content.replace('@{' + v.msg
  170. .message.content.remind.id + '}', "有人@我");
  171. } else {
  172. var mem = getGroupmemebrs(v.msg.group_id, v.msg.message.content.remind.id);
  173. if (mem.id == v.msg.message.content.remind.id) {
  174. var atname = mem.nickname;
  175. } else var atname = v.msg.message.content.remind.nickname;
  176. v.msg.message.content.content = v.msg.message.content.content.replace('@{' + v.msg
  177. .message.content.remind.id + '}', '@' + atname);
  178. }
  179. }
  180. // if (!v.msg.group_id) console.log(getUserInfoById(v.msg.id))
  181. if (!v.msg.none) tmp.push(v.msg);
  182. });
  183. let temp1 = [];
  184. let temp2 = [];
  185. tmp.map(item => {
  186. if (item.istop > 0) temp1.push(item)
  187. else temp2.push(item)
  188. })
  189. temp1.sort((a, b) => b.istop - a.istop);
  190. temp2.sort((a, b) => b.timestamp - a.timestamp);
  191. // tmp.sort((a, b) => b.timestamp - a.timestamp);
  192. action.setStatusTips();
  193. var res = temp1.concat(temp2);
  194. return res;
  195. }
  196. }
  197. export default new chat();
  198. const cache = function(key, value) {
  199. try {
  200. if (arguments.length == 1) {
  201. // console.log(key);
  202. return uni.getStorageSync(key)
  203. }
  204. if (arguments.length > 1) {
  205. if (value === null) {
  206. uni.removeStorageSync(key);
  207. } else {
  208. uni.setStorageSync(key, value);
  209. }
  210. }
  211. } catch (e) {
  212. uni.showToast({
  213. icon: 'none',
  214. title: '程序发生错误'
  215. });
  216. }
  217. };
  218. const parseDate = (str) => new Date(str.replace(/(-|年|月)/g, '/').replace('日', ''));
  219. const dateToTime = (date) => date.getTime() / 1000;
  220. const cutTimeStr = (str) => str.substr(0, 11);
  221. function timestampToTime(timestamp) {
  222. var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  223. var Y = date.getFullYear();
  224. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  225. var D = date.getDate();
  226. var h = date.getHours();
  227. var m = date.getMinutes();
  228. var s = date.getSeconds();
  229. if (D < 10) D = '0' + D;
  230. if (h < 10) h = '0' + h;
  231. if (m < 10) m = '0' + m;
  232. if (s < 10) s = '0' + s;
  233. return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
  234. }
  235. function setTimeShow(timestamp) {
  236. var time = timestampToTime(timestamp);
  237. var now = new Date();
  238. var Year = now.getFullYear();
  239. var month = now.getMonth() + 1;
  240. if (month < 10) month = '0' + month;
  241. var day = now.getDate();
  242. if (day < 10) day = '0' + day;
  243. var today = Year + '-' + month + '-' + day;
  244. if (time.substr(0, 10) == today) return time.substr(11, 8);
  245. if (time.substr(0, 4) == Year) return time.substr(5, 14);
  246. return time;
  247. }
  248. function prefix(id) {
  249. return uni.getStorageSync('access_token') + '_chat_' + id;
  250. }
  251. function prefixIndex(id) {
  252. return uni.getStorageSync('access_token') + prefix(id) + '_index';
  253. }
  254. function msglist_key() {
  255. return prefix('msglist');
  256. }
  257. function set_msgtop(key) {
  258. var msgtop = cache('msgtop');
  259. var istop = 0;
  260. for (var i = 0; i < msgtop.length; i++) {
  261. if (msgtop[i] == key) {
  262. return true;
  263. }
  264. }
  265. return false;
  266. }
  267. function set_senderlist(sender) {
  268. var list = uni.getStorageSync(prefix('senderlist'));
  269. var isin = 0;
  270. if (list.length > 0) {
  271. for (var i = 0; i < list.length; i++) {
  272. if (parseInt(list[i].id) == parseInt(sender.id) && isin == 0) {
  273. list[i].nickname = sender.nickname;
  274. list[i].avatar = sender.avatar;
  275. isin = 1;
  276. }
  277. }
  278. } else var list = Array();
  279. if (isin == 0) list.push(sender);
  280. uni.setStorageSync(prefix('senderlist'), list);
  281. }
  282. function getUserInfoById(id) {
  283. if (id > 0) {
  284. var result = {
  285. id,
  286. avatar: config.imgUri + '/uploads/avatar.jpg',
  287. nickname: '未知用户',
  288. };
  289. let list = uni.getStorageSync(uni.getStorageSync('access_token') + '_frienddata');
  290. if (!list) list = [];
  291. list.forEach(v => {
  292. if (v.id == id) {
  293. result.nickname = v.nickname;
  294. if (v.avatar.indexOf('http') > -1) {
  295. } else v.avatar = config.imgUri + v.avatar
  296. result.avatar = v.avatar;
  297. }
  298. })
  299. } else {
  300. var system = uni.getStorageSync('system');
  301. var result = {
  302. id,
  303. avatar: config.imgUri + system['admin_logo'],
  304. nickname: system['admin_nickname'],
  305. };
  306. }
  307. return result;
  308. }
  309. function getGroupInfoById(id) {
  310. var groups = uni.getStorageSync(uni.getStorageSync('access_token') + '_groups');
  311. for (var i = 0; i < groups.length; i++) {
  312. if (groups[i].id == id) {
  313. var avatar = groups[i].avatar;
  314. if (avatar !== null && avatar.indexOf('http') > -1) {
  315. } else avatar = config.imgUri + avatar
  316. var result = {
  317. id: id,
  318. avatar: avatar,
  319. nickname: groups[i].nickname,
  320. none: false
  321. };
  322. return result;
  323. }
  324. }
  325. return {
  326. id,
  327. avatar: config.imgUri + '/uploads/group.jpg',
  328. nickname: '未知群组',
  329. none: false
  330. };
  331. }
  332. function getGroupmemebrs(group_id, userid) {
  333. var members = uni.getStorageSync(group_id + '_group_members');
  334. if (!members) members = [];
  335. let result = [];
  336. members.forEach(item => {
  337. if (item.id == userid) result = item;
  338. })
  339. return result;
  340. }
  341. function get_chat_list(id) {
  342. try {
  343. var temp = cache(prefix(id)) ? cache(prefix(id)) : [];
  344. return temp;
  345. } catch (e) {
  346. //TODO handle the exception
  347. return [];
  348. }
  349. }
  350. function get_chatold_list(id) {
  351. try {
  352. var temp = cache(prefix('old' + id)) ? cache(prefix('old' + id)) : [];
  353. return lists;
  354. } catch (e) {
  355. //TODO handle the exception
  356. return [];
  357. }
  358. }
  359. function msg_vocice(data) {
  360. if (data.msg.self != 1 && data.msg.cache_key != uni.getStorageSync('cache_key')) {
  361. var no_tip = 0;
  362. var msgnotip = uni.getStorageSync('msgnotip');
  363. if (!msgnotip) msgnotip = [];
  364. msgnotip.map(item => {
  365. if (item.storekey == data.msg.cache_key) {
  366. no_tip = 1;
  367. }
  368. })
  369. if (data.msg.message.content.type == 'remind' && data.msg.message.content.remind.id == uni.getStorageSync(
  370. 'access_token')) {
  371. no_tip = 0;
  372. data.msg.message.content.content = data.msg.message.content.content.replace('@{' + data.msg.message.content
  373. .remind.id + '}', '有人@我');
  374. cache('remid_' + key, data.msg);
  375. }
  376. if (no_tip == 0) {
  377. let userinfo = cache('userInfo');
  378. // console.log(userinfo);
  379. if (parseInt(userinfo.note2) != 0) uni.vibrateLong();
  380. if (parseInt(userinfo.note1) != 0) action.playVoice('/static/voice/chat.mp3');
  381. }
  382. }
  383. return data;
  384. }
  385. function httpString(str) {
  386. var reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
  387. // console.log(str);
  388. try {
  389. var s = str.match(reg);
  390. if (s != null) {
  391. return s;
  392. } else {
  393. return false;
  394. }
  395. } catch (e) {
  396. return false;
  397. }
  398. }
  399. function get_senderinfo(v) {
  400. try {
  401. if (v.group_id > 0) {
  402. var mem = getGroupmemebrs(v.group_id, v.sender_id);
  403. if (mem.id > 0) v.sender = mem;
  404. // console.log(mem);
  405. }
  406. var avatar = v.sender.avatar;
  407. if (avatar.indexOf('http') > -1) {
  408. } else avatar = config.imgUri + avatar
  409. var obj = {
  410. id: v.sender_id,
  411. avatar: avatar,
  412. nickname: v.sender.nickname,
  413. };
  414. } catch (e) {
  415. // console.log(v);
  416. var obj = {
  417. id: v.sender_id,
  418. avatar: '',
  419. nickname: v.nickname,
  420. };
  421. }
  422. return obj;
  423. }
  424. function message_format(v) {
  425. try {
  426. v.message.content = JSON.parse(v.message.content);
  427. var type = v.message.content.type;
  428. } catch (e) {
  429. //TODO handle the exception
  430. var type = v.message.content.type;
  431. }
  432. if (type == 'remind') {
  433. var mem = getGroupmemebrs(v.group_id, v.message.content.remind.id);
  434. if (mem.id == v.message.content.remind.id) {
  435. var atname = mem.nickname;
  436. } else
  437. var atname = v.message.content.remind.nickname;
  438. v.message.content.content = v.message.content.content.replace('@{' + v.message.content.remind.id + '}', '@' +
  439. atname);
  440. }
  441. if (v.message.type == 'text' && v.message.content.type != 'emotion') {
  442. if (type == 'remind')
  443. var content = v.message.content.content;
  444. else var content = v.message.content;
  445. var res = httpString(content);
  446. if (res != false) {
  447. v.message.type = 'url';
  448. v.message.content = {};
  449. v.message.content.content = content;
  450. v.message.content.urls = res;
  451. }
  452. }
  453. return v;
  454. }