chat.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. _Receive_news.push(obj); //合并历史消息对象
  189. console.log(_Receive_news, 'aaa');
  190. that.setData({
  191. Receive_news: _Receive_news, //前台数据赋值
  192. });
  193. //调用页面滚动条处理
  194. that.scrollbar(_Receive_news);
  195. break;
  196. case "images": // 发送图片
  197. console.log(' 发送图片');
  198. var obj = { //创建对象,用来合并历史消息对象
  199. content: that.data.photo, //发送消息内容
  200. type: 2, //类型 1 : 文字类型 类型 2 : 图片
  201. time: _time, //时间
  202. isMy: true, //自己
  203. send: true, // 发送成功 为true 失败false
  204. };
  205. let _Receive_img = that.data.Receive_news;
  206. _Receive_img.push(obj); //合并历史消息对象
  207. console.log(_Receive_img, '发送图片');
  208. that.setData({
  209. Receive_news: _Receive_img, //前台数据赋值
  210. });
  211. //调用页面滚动条处理
  212. that.scrollbar(_Receive_img);
  213. break;
  214. }
  215. console.log(res, '获取历史消息成功');
  216. }, fail: function (res) { //发送失败
  217. console.log('获取历史消息失败')
  218. }
  219. })
  220. },
  221. // 接收消息
  222. onSocketMessageCallback: function (e) {
  223. var that = this;
  224. let data = JSON.parse(e);
  225. console.log(data,'接受消息成功');
  226. var arrData = data.data.data;
  227. switch (data.type) {
  228. // 接收历史消息
  229. case "get_history":
  230. // console.log(arrData,111112222);
  231. var _code = data.code;
  232. if (_code == 201){
  233. // let total = arrData.length - 1;
  234. // console.log(total);
  235. // var vasd = time.TimeFormatting(1561186210);
  236. // console.log(vasd)
  237. console.log(arrData);
  238. for (var index in arrData) { //二次赋值
  239. if (arrData[index].form_id == getApp().UserToken()) { //判断 如果等于自己 true ,等于他人 false。
  240. arrData[index].isMy = true; //自己
  241. } else {
  242. arrData[index].isMy = false; //他人
  243. }
  244. if (arrData[index].type == 2){
  245. arrData[index].content = data.data.url + arrData[index].content + data.data.stylename;
  246. }
  247. if (arrData[index].create_at){
  248. var _time = time.TimeFormatting(arrData[index].create_at); // 时间格式化
  249. arrData[index].time = _time; //时间从新赋值
  250. }
  251. that.ArrMsg.push(arrData[index]);
  252. }
  253. for (var index in that.ArrMsg) { //二次赋值 把所有的数据 hooks参数 清空
  254. if(that.ArrMsg[index].hooks){
  255. that.ArrMsg[index].hooks = '';
  256. }
  257. }
  258. let Arrs1 = that.sortarr(that.ArrMsg); //最后要展示的数据 第11条 加上 hooks= 'Stop'; 参数 用来做加载更多数据,描点到最后一条信息
  259. if(Arrs1[10]){
  260. Arrs1[10].hooks = 'Stop';
  261. }
  262. console.log(Arrs1, 111112222);
  263. if (that.data.limit_page == 1) { //判断 分页 1 的时候
  264. that.setData({
  265. Receive_news: Arrs1, //前台数据赋值
  266. scrollTopVal: that.ArrMsg.length * 999, // 滚动条赋值
  267. toView: false, //关闭锚点
  268. });
  269. }else{
  270. that.setData({
  271. Receive_news: Arrs1, //前台数据赋值
  272. toView: 'Stop' //开启锚点 赋值
  273. });
  274. }
  275. that.setData({ //最后关闭锚点 方便每次进来 在开启 修复 描点会闪动
  276. toView: false,
  277. frequency: true,
  278. hiddenName: true, //隐藏加载更多图片
  279. });
  280. }else{
  281. that.setData({
  282. hiddenName: true, //隐藏加载更多图片
  283. codedata: false //code 301
  284. });
  285. }
  286. break;
  287. case "alone": // 接收他人文本
  288. var arrDatatext = data.data;
  289. console.log(arrDatatext, "aaasd");
  290. var current_time = Date.now();
  291. var _time = time.TimeFormatting(current_time); // 时间格式化
  292. // console.log(_time);
  293. var obj = { //创建对象,用来合并历史消息对象
  294. content: arrDatatext.content, //发送消息内容
  295. type: 1, //类型 1 : 文字类型 类型 2 : 图片
  296. time: _time, //时间
  297. isMy: false, //他人
  298. };
  299. let _Receive_news = that.data.Receive_news;
  300. _Receive_news.push(obj); //合并历史消息对象
  301. that.setData({
  302. Receive_news: _Receive_news, //前台数据赋值
  303. });
  304. //调用页面滚动条处理
  305. that.scrollbar(_Receive_news);
  306. break;
  307. case "images": // 接收他人图片
  308. var arrDataimg = data.data;
  309. console.log(arrDataimg, "接收图片");
  310. var current_time = Date.now();
  311. var _time = time.TimeFormatting(current_time); // 时间格式化
  312. // console.log(_time);
  313. var obj = { //创建对象,用来合并历史消息对象
  314. content: arrDataimg.content, //发送消息内容
  315. type: 2, //类型 1 : 文字类型 类型 2 : 图片
  316. time: _time, //时间
  317. isMy: false, //他人
  318. };
  319. let _Receive_img = that.data.Receive_news;
  320. _Receive_img.push(obj); //合并历史消息对象
  321. that.setData({
  322. Receive_news: _Receive_img, //前台数据赋值
  323. });
  324. //调用页面滚动条处理
  325. that.scrollbar(_Receive_img);
  326. break;
  327. }
  328. },
  329. // 处理滚动条
  330. scrollbar: function (data) {
  331. var that = this;
  332. if (data) {
  333. let lengNum = data.length;
  334. //页面滚动条处理
  335. that.setData({
  336. scrollTopVal: lengNum * 999
  337. })
  338. console.log(lengNum * 999);
  339. }
  340. },
  341. // 发送消息事件 用来获取input数据
  342. Sendout: function (e) {
  343. var that = this;
  344. // console.log(that.data.news);
  345. let msg = {
  346. // to_id: that.data.to_id,
  347. // form_id: getApp().UserId(),
  348. type: 'alone',
  349. content: that.data.news
  350. };
  351. // console.log(msg);
  352. this.SocketSend(msg);
  353. that.setData({
  354. menu: false, //隐藏底部菜单
  355. Expression: false,//隐藏表情栏
  356. inputValue: '', //清空 input
  357. news: '', //清空 input内容
  358. })
  359. that.Sendoutswitch(that.data.news) // 发送键切换
  360. // console.log(that.data.Receive_news)
  361. },
  362. //冒泡排序
  363. sortarr: function (arr) {
  364. for (let i = 0; i < arr.length - 1; i++) {
  365. for (let j = 0; j < arr.length - 1 - i; j++) {
  366. if (arr[j].create_at > arr[j + 1].create_at) {
  367. var temp = arr[j];
  368. arr[j] = arr[j + 1];
  369. arr[j + 1] = temp;
  370. }
  371. }
  372. }
  373. return arr;
  374. },
  375. // 下拉加载聊天记录
  376. upper: function (e) {
  377. var that = this;
  378. // console.log(e,'加载')
  379. if (that.data.codedata == true){
  380. that.setData({ // 显示加载更多图片
  381. hiddenName: false,
  382. })
  383. }
  384. if (that.data.frequency == true){
  385. // setTimeout(function () {
  386. that.setData({
  387. limit_page: that.data.limit_page * 1 + 1,
  388. frequency:false
  389. })
  390. console.log(that.data.limit_page * 1 + 1)
  391. that.SocketSend({ type: 'get_history', page: that.data.limit_page,limit:10})
  392. // }, 1000);
  393. }
  394. // console.log('执行')
  395. },
  396. // 点击表情
  397. Expression: function (e) {
  398. var that = this;
  399. var _news = that.data.news;
  400. var _text = _news + e.currentTarget.dataset.id;
  401. console.log(that.data.news)
  402. console.log(_text)
  403. that.setData({
  404. news: _text,
  405. inputValue: _text,
  406. })
  407. that.Sendoutswitch(that.data.news) // 发送键切换
  408. },
  409. // 点击input
  410. bindfocusclick: function (e) {
  411. var that = this;
  412. that.setData({
  413. Expression: false,
  414. menu: false,
  415. KeyboardHeight: e.detail.height,
  416. })
  417. // console.log(e.detail.height);
  418. },
  419. // 发送键切换
  420. Sendoutswitch: function (data) {
  421. var that = this;
  422. if (data == '') {
  423. that.setData({
  424. menushow: true, //菜单按钮显示隐藏
  425. Sendoutshow: false,// 发送按钮显示隐藏
  426. _width: 500, // input宽度
  427. })
  428. } else {
  429. that.setData({
  430. menushow: false, //菜单按钮显示隐藏
  431. Sendoutshow: true,// 发送按钮显示隐藏
  432. _width: 447, // input宽度
  433. })
  434. }
  435. },
  436. onUnload: function (e) {
  437. let url = 'imsocket/imchatmessage/clearunreadmsg';
  438. let datas = {
  439. to_id: this.data.to_id, form_id: getApp().UserId()
  440. };
  441. //清除未读信息
  442. getApp().postRequest(url, datas, function (res) {
  443. console.log(res, 11111);
  444. })
  445. console.log('销毁页面');
  446. },
  447. // ceshi: function (e) {
  448. // var that = this;
  449. // that.setData({
  450. // // mscrollTop: e.detail.scrollTop,
  451. // mscrollHeight: e.detail.scrollHeight
  452. // })
  453. // // console.log(e.detail.scrollTop);
  454. // // console.log(e.detail.scrollHeight);
  455. // // console.log(e.detail);
  456. // }
  457. })