chat.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. const app = getApp();
  2. const time = require('../../../utils/time.js');
  3. const ImageType = 'images';
  4. const TextType = 'text';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. codedata: true, //用来判断code
  11. frequency:true, //多次加载次数限制
  12. hiddenName:true, // 加载更多显示隐藏
  13. toView:'Stop', //锚点
  14. scrollTopVal: 1, //滚动条
  15. menu: false, //菜单显示隐藏
  16. Expression: false, // 表情显示隐藏
  17. Sendoutshow: false, // 发送按钮显示隐藏
  18. menushow: true, //菜单按钮显示隐藏
  19. _width: 500, //input高度
  20. // uploadImages: ["/image/smyz2.png","/image/smyz1.png"], //相册放大图集
  21. news: '',//input消息
  22. KeyboardHeight: 366.66666666666674, //键盘高度
  23. ExpressionImg: '', //表情
  24. // Loadmore: true, //用来判断加载聊天记录。。每次只能执行一次
  25. inputValue: '', //input 的 value
  26. photo: '', //相片
  27. Receive_news: '', //历史消息
  28. UserToken: '', //区分自己
  29. limit_page: 1,
  30. myheadurl: '', //我的头像
  31. headUrl: '', //他人头像
  32. to_id: '', //他人
  33. form_id: '', //自己
  34. // scrollTop:'' //下拉更多时的位置
  35. connectemojiO: ['😊', '😅', '😲', '😭', '😂', '😄', '😩', '😞', '😵', '😒', '😍', '😤', '😜', '😝', '😋', '😘', '😚', '😷', '😳', '😃', '😆', '😁', '😢', '😨', '😠', '😣', '😌', '😖', '😔', '😰', '😱', '😪', '😏', '😓'],
  36. },
  37. onLoad: function (options) {
  38. var that = this;
  39. that.ArrMsg = [];
  40. // console.log(that.data.UserToken);
  41. // console.log(options.headUrl);
  42. // console.log(options.myheadurl);
  43. // console.log(getApp().UserId(),'token');
  44. // console.log(options);
  45. const friend = options;
  46. that.setData({
  47. to_id: friend.to_id, // 他人
  48. UserToken: getApp().UserToken(), //自己
  49. myheadurl: options.myheadurl, //我的头像
  50. headUrl: options.headUrl, //他人头像
  51. })
  52. this.SocketSend({
  53. // to_id: friend.to_id, // 他人
  54. // form_id: getApp().UserId(), //自己
  55. type: 'get_history',
  56. page: this.data.limit_page,
  57. limit:10
  58. });
  59. getApp().Imsocket().onSocketMessageCallback = this.onSocketMessageCallback;
  60. },
  61. // 点击scroll-view 关闭底部菜单
  62. resetInputStatus: function () {
  63. var that = this;
  64. that.setData({
  65. menu: false, //关闭底部菜单
  66. Expression: false, //关闭表情
  67. })
  68. },
  69. // 底部菜单显示隐藏切换
  70. chatInputExtraClickEvent: function () {
  71. var that = this;
  72. that.setData({
  73. menu: !that.data.menu,
  74. Expression: false, //关闭表情
  75. })
  76. },
  77. // 关闭表情显示隐藏切换
  78. ExpressionClickEvent: function () {
  79. var that = this;
  80. that.setData({
  81. Expression: !that.data.Expression,
  82. menu: false, //关闭底部菜单
  83. })
  84. },
  85. // 相册点击放大
  86. enlarge: function (e) {
  87. var that = this;
  88. // console.log(that.data.uploadImages)
  89. console.log(e.currentTarget.dataset.src,)
  90. wx.previewImage({
  91. current: e.currentTarget.dataset.src, // 当前显示图片的http链接
  92. urls: [e.currentTarget.dataset.src], // 需要预览的图片http链接列表
  93. })
  94. },
  95. //input输入框
  96. Telephone: function (e) {
  97. var that = this;
  98. // console.log(e.detail.value);
  99. that.setData({
  100. news: e.detail.value, //获取输入文字
  101. })
  102. that.Sendoutswitch(e.detail.value); // 发送键切换
  103. },
  104. // 上传图片
  105. chooseImageTap: function () {
  106. var that = this;
  107. wx.chooseImage({
  108. count: 1,
  109. sizeType: ['original', 'compressed'],
  110. sourceType: ['album', 'camera'],
  111. success(res) {
  112. wx.uploadFile({
  113. url: getApp().globalData.url + 'imsocket/images/chatimg',
  114. filePath: res.tempFilePaths[0],
  115. name: 'photo',
  116. formData: {
  117. key: wx.getStorageSync('access_key'),
  118. token: wx.getStorageSync('access_token')
  119. },
  120. success: function (res) {
  121. var data = JSON.parse(res.data);
  122. console.log(data, '图片上传成功');
  123. if (data.code == 201) {
  124. let con = data.data;
  125. //显示图片
  126. console.log(con, 1);
  127. // console.log(ImgRes.tempFilePaths[0], 1222222);
  128. that.SocketSend({ type: ImageType, content: con.img_name });
  129. // that.msgManager.sendMsg({ type: IMOperator.ImageType, content: con })
  130. } else if (data.code == 301) {
  131. wx.showModal({
  132. title: '提示',
  133. content: data.msg,
  134. showCancel: false,
  135. })
  136. }
  137. }, fail: function (d) {
  138. }
  139. })
  140. console.log(res.tempFilePaths[0]);
  141. // tempFilePath可以作为img标签的src属性显示图片
  142. that.setData({
  143. photo: res.tempFilePaths[0],
  144. menu: false, //菜单显示隐藏
  145. });
  146. }
  147. })
  148. },
  149. // 重发消息
  150. retransmission: function () {
  151. wx.showModal({
  152. content: '重发该消息?',
  153. success(res) {
  154. if (res.confirm) {
  155. console.log('用户点击确定')
  156. } else if (res.cancel) {
  157. console.log('用户点击取消')
  158. }
  159. }
  160. })
  161. },
  162. // 发送消息
  163. SocketSend: function (msg) {
  164. var that = this;
  165. msg.form_id = getApp().UserId();
  166. msg.to_id = that.data.to_id;
  167. // console.log(that.data.Receive_news);
  168. // { to_id: friend.to_id, form_id: getApp().UserId(), type: 'get_history' }
  169. // msg.type = 'get_history';
  170. getApp().Imsocket().sendSocketMessage({
  171. msg: JSON.stringify(msg),
  172. success: function (res) { //发送成功
  173. var current_time = Date.now();
  174. var _time = time.TimeFormatting(current_time); // 时间格式化
  175. // console.log(_time);
  176. // 我跟他人说话
  177. switch (msg.type) {
  178. case "alone": // 发送文本
  179. console.log(' 发送文本');
  180. var obj = { //创建对象,用来合并历史消息对象
  181. content: that.data.news, //发送消息内容
  182. type: 1, //类型 1 : 文字类型 类型 2 : 图片
  183. time: _time, //时间
  184. isMy: true, //自己
  185. send: true, // 发送成功 为true 失败false
  186. };
  187. let _Receive_news = that.data.Receive_news;
  188. if (typeof _Receive_news == 'object') {
  189. _Receive_news.push(obj); //合并历史消息对象
  190. }
  191. // console.log(_Receive_news, 'aaa');
  192. that.setData({
  193. Receive_news: _Receive_news, //前台数据赋值
  194. });
  195. //调用页面滚动条处理
  196. that.scrollbar(_Receive_news);
  197. break;
  198. case "images": // 发送图片
  199. console.log(' 发送图片');
  200. var obj = { //创建对象,用来合并历史消息对象
  201. content: that.data.photo, //发送消息内容
  202. type: 2, //类型 1 : 文字类型 类型 2 : 图片
  203. time: _time, //时间
  204. isMy: true, //自己
  205. send: true, // 发送成功 为true 失败false
  206. };
  207. let _Receive_img = that.data.Receive_news;
  208. _Receive_img.push(obj); //合并历史消息对象
  209. console.log(_Receive_img, '发送图片');
  210. that.setData({
  211. Receive_news: _Receive_img, //前台数据赋值
  212. });
  213. //调用页面滚动条处理
  214. that.scrollbar(_Receive_img);
  215. break;
  216. }
  217. console.log(res, '获取历史消息成功');
  218. }, fail: function (res) { //发送失败
  219. console.log('获取历史消息失败')
  220. }
  221. })
  222. },
  223. // 接收消息
  224. onSocketMessageCallback: function (e) {
  225. var that = this;
  226. let data = JSON.parse(e);
  227. console.log(data,'接受消息成功');
  228. var arrData = data.data.data;
  229. switch (data.type) {
  230. // 接收历史消息
  231. case "get_history":
  232. // console.log(arrData,111112222);
  233. var _code = data.code;
  234. if (_code == 201){
  235. // let total = arrData.length - 1;
  236. // console.log(total);
  237. // var vasd = time.TimeFormatting(1561186210);
  238. // console.log(vasd)
  239. console.log(arrData);
  240. for (var index in arrData) { //二次赋值
  241. if (arrData[index].form_id == getApp().UserToken()) { //判断 如果等于自己 true ,等于他人 false。
  242. arrData[index].isMy = true; //自己
  243. } else {
  244. arrData[index].isMy = false; //他人
  245. }
  246. if (arrData[index].type == 2){
  247. arrData[index].content = data.data.url + arrData[index].content + data.data.stylename;
  248. }
  249. if (arrData[index].create_at){
  250. var _time = time.TimeFormatting(arrData[index].create_at); // 时间格式化
  251. arrData[index].time = _time; //时间从新赋值
  252. }
  253. that.ArrMsg.push(arrData[index]);
  254. }
  255. for (var index in that.ArrMsg) { //二次赋值 把所有的数据 hooks参数 清空
  256. if(that.ArrMsg[index].hooks){
  257. that.ArrMsg[index].hooks = '';
  258. }
  259. }
  260. let Arrs1 = that.sortarr(that.ArrMsg); //最后要展示的数据 第11条 加上 hooks= 'Stop'; 参数 用来做加载更多数据,描点到最后一条信息
  261. if(Arrs1[10]){
  262. Arrs1[10].hooks = 'Stop';
  263. }
  264. console.log(Arrs1, 111112222);
  265. if (that.data.limit_page == 1) { //判断 分页 1 的时候
  266. that.setData({
  267. Receive_news: Arrs1, //前台数据赋值
  268. scrollTopVal: that.ArrMsg.length * 999, // 滚动条赋值
  269. toView: false, //关闭锚点
  270. });
  271. }else{
  272. that.setData({
  273. Receive_news: Arrs1, //前台数据赋值
  274. toView: 'Stop' //开启锚点 赋值
  275. });
  276. }
  277. that.setData({ //最后关闭锚点 方便每次进来 在开启 修复 描点会闪动
  278. toView: false,
  279. frequency: true,
  280. hiddenName: true, //隐藏加载更多图片
  281. });
  282. }else{
  283. that.setData({
  284. hiddenName: true, //隐藏加载更多图片
  285. codedata: false //code 301
  286. });
  287. }
  288. break;
  289. case "alone": // 接收他人文本
  290. var arrDatatext = data.data;
  291. console.log(arrDatatext, "aaasd");
  292. var current_time = Date.now();
  293. var _time = time.TimeFormatting(current_time); // 时间格式化
  294. // console.log(_time);
  295. var obj = { //创建对象,用来合并历史消息对象
  296. content: arrDatatext.content, //发送消息内容
  297. type: 1, //类型 1 : 文字类型 类型 2 : 图片
  298. time: _time, //时间
  299. isMy: false, //他人
  300. };
  301. let _Receive_news = that.data.Receive_news;
  302. if (typeof _Receive_news == 'object'){
  303. _Receive_news.push(obj); //合并历史消息对象
  304. }
  305. that.setData({
  306. Receive_news: _Receive_news, //前台数据赋值
  307. });
  308. //调用页面滚动条处理
  309. that.scrollbar(_Receive_news);
  310. break;
  311. case "images": // 接收他人图片
  312. var arrDataimg = data.data;
  313. console.log(arrDataimg, "接收图片");
  314. var current_time = Date.now();
  315. var _time = time.TimeFormatting(current_time); // 时间格式化
  316. // console.log(_time);
  317. var obj = { //创建对象,用来合并历史消息对象
  318. content: arrDataimg.content, //发送消息内容
  319. type: 2, //类型 1 : 文字类型 类型 2 : 图片
  320. time: _time, //时间
  321. isMy: false, //他人
  322. };
  323. let _Receive_img = that.data.Receive_news;
  324. _Receive_img.push(obj); //合并历史消息对象
  325. that.setData({
  326. Receive_news: _Receive_img, //前台数据赋值
  327. });
  328. //调用页面滚动条处理
  329. that.scrollbar(_Receive_img);
  330. break;
  331. }
  332. },
  333. // 发送消息事件 用来获取input数据
  334. Sendout: function (e) {
  335. var that = this;
  336. // console.log(that.data.news);
  337. let msg = {
  338. // to_id: that.data.to_id,
  339. // form_id: getApp().UserId(),
  340. type: 'alone',
  341. content: that.data.news
  342. };
  343. // console.log(msg);
  344. this.SocketSend(msg);
  345. that.setData({
  346. menu: false, //隐藏底部菜单
  347. Expression: false,//隐藏表情栏
  348. inputValue: '', //清空 input
  349. news: '', //清空 input内容
  350. })
  351. that.Sendoutswitch(that.data.news) // 发送键切换
  352. // console.log(that.data.Receive_news)
  353. },
  354. //冒泡排序
  355. sortarr: function (arr) {
  356. for (let i = 0; i < arr.length - 1; i++) {
  357. for (let j = 0; j < arr.length - 1 - i; j++) {
  358. if (arr[j].create_at > arr[j + 1].create_at) {
  359. var temp = arr[j];
  360. arr[j] = arr[j + 1];
  361. arr[j + 1] = temp;
  362. }
  363. }
  364. }
  365. return arr;
  366. },
  367. // 下拉加载聊天记录
  368. upper: function (e) {
  369. var that = this;
  370. // console.log(e,'加载')
  371. if (that.data.codedata == true){
  372. that.setData({ // 显示加载更多图片
  373. hiddenName: false,
  374. })
  375. }
  376. if (that.data.frequency == true){
  377. // setTimeout(function () {
  378. that.setData({
  379. limit_page: that.data.limit_page * 1 + 1,
  380. frequency:false
  381. })
  382. console.log(that.data.limit_page * 1 + 1)
  383. that.SocketSend({ type: 'get_history', page: that.data.limit_page,limit:10})
  384. // }, 1000);
  385. }
  386. // console.log('执行')
  387. },
  388. // 点击表情
  389. Expression: function (e) {
  390. var that = this;
  391. var _news = that.data.news;
  392. var _text = _news + e.currentTarget.dataset.id;
  393. console.log(that.data.news)
  394. console.log(_text)
  395. that.setData({
  396. news: _text,
  397. inputValue: _text,
  398. })
  399. that.Sendoutswitch(that.data.news) // 发送键切换
  400. },
  401. // 点击input
  402. bindfocusclick: function (e) {
  403. var that = this;
  404. that.setData({
  405. Expression: false,
  406. menu: false,
  407. KeyboardHeight: e.detail.height,
  408. })
  409. // console.log(e.detail.height);
  410. },
  411. // 发送键切换
  412. Sendoutswitch: function (data) {
  413. var that = this;
  414. if (data == '') {
  415. that.setData({
  416. menushow: true, //菜单按钮显示隐藏
  417. Sendoutshow: false,// 发送按钮显示隐藏
  418. _width: 500, // input宽度
  419. })
  420. } else {
  421. that.setData({
  422. menushow: false, //菜单按钮显示隐藏
  423. Sendoutshow: true,// 发送按钮显示隐藏
  424. _width: 447, // input宽度
  425. })
  426. }
  427. },
  428. onUnload: function (e) {
  429. let _that = this;
  430. _that.SocketSend({
  431. type: 'clearChatTotal'
  432. });
  433. // let url = 'imsocket/imchatmessage/clearunreadmsg';
  434. // let datas = {
  435. // to_id: this.data.to_id, form_id: getApp().UserId()
  436. // };
  437. // //清除未读信息
  438. // getApp().postRequest(url, datas, function (res) {
  439. // console.log(res, 11111);
  440. // })
  441. console.log('销毁页面');
  442. },
  443. // ceshi: function (e) {
  444. // var that = this;
  445. // that.setData({
  446. // // mscrollTop: e.detail.scrollTop,
  447. // mscrollHeight: e.detail.scrollHeight
  448. // })
  449. // // console.log(e.detail.scrollTop);
  450. // // console.log(e.detail.scrollHeight);
  451. // // console.log(e.detail);
  452. // }
  453. // 处理滚动条
  454. scrollbar: function (data) {
  455. var that = this;
  456. if (data.length > 1) {
  457. let lengNum = data.length;
  458. //页面滚动条处理
  459. that.setData({
  460. scrollTopVal: lengNum * 999
  461. })
  462. console.log(lengNum * 999);
  463. }
  464. },
  465. })