chat.js 18 KB

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