chat.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. if (typeof _Receive_news == 'object'){
  301. _Receive_news.push(obj); //合并历史消息对象
  302. }
  303. that.setData({
  304. Receive_news: _Receive_news, //前台数据赋值
  305. });
  306. //调用页面滚动条处理
  307. that.scrollbar(_Receive_news);
  308. break;
  309. case "images": // 接收他人图片
  310. var arrDataimg = data.data;
  311. console.log(arrDataimg, "接收图片");
  312. var current_time = Date.now();
  313. var _time = time.TimeFormatting(current_time); // 时间格式化
  314. // console.log(_time);
  315. var obj = { //创建对象,用来合并历史消息对象
  316. content: arrDataimg.content, //发送消息内容
  317. type: 2, //类型 1 : 文字类型 类型 2 : 图片
  318. time: _time, //时间
  319. isMy: false, //他人
  320. };
  321. let _Receive_img = that.data.Receive_news;
  322. _Receive_img.push(obj); //合并历史消息对象
  323. that.setData({
  324. Receive_news: _Receive_img, //前台数据赋值
  325. });
  326. //调用页面滚动条处理
  327. that.scrollbar(_Receive_img);
  328. break;
  329. }
  330. },
  331. // 处理滚动条
  332. scrollbar: function (data) {
  333. var that = this;
  334. if (data) {
  335. let lengNum = data.length;
  336. //页面滚动条处理
  337. that.setData({
  338. scrollTopVal: lengNum * 999
  339. })
  340. console.log(lengNum * 999);
  341. }
  342. },
  343. // 发送消息事件 用来获取input数据
  344. Sendout: function (e) {
  345. var that = this;
  346. // console.log(that.data.news);
  347. let msg = {
  348. // to_id: that.data.to_id,
  349. // form_id: getApp().UserId(),
  350. type: 'alone',
  351. content: that.data.news
  352. };
  353. // console.log(msg);
  354. this.SocketSend(msg);
  355. that.setData({
  356. menu: false, //隐藏底部菜单
  357. Expression: false,//隐藏表情栏
  358. inputValue: '', //清空 input
  359. news: '', //清空 input内容
  360. })
  361. that.Sendoutswitch(that.data.news) // 发送键切换
  362. // console.log(that.data.Receive_news)
  363. },
  364. //冒泡排序
  365. sortarr: function (arr) {
  366. for (let i = 0; i < arr.length - 1; i++) {
  367. for (let j = 0; j < arr.length - 1 - i; j++) {
  368. if (arr[j].create_at > arr[j + 1].create_at) {
  369. var temp = arr[j];
  370. arr[j] = arr[j + 1];
  371. arr[j + 1] = temp;
  372. }
  373. }
  374. }
  375. return arr;
  376. },
  377. // 下拉加载聊天记录
  378. upper: function (e) {
  379. var that = this;
  380. // console.log(e,'加载')
  381. if (that.data.codedata == true){
  382. that.setData({ // 显示加载更多图片
  383. hiddenName: false,
  384. })
  385. }
  386. if (that.data.frequency == true){
  387. // setTimeout(function () {
  388. that.setData({
  389. limit_page: that.data.limit_page * 1 + 1,
  390. frequency:false
  391. })
  392. console.log(that.data.limit_page * 1 + 1)
  393. that.SocketSend({ type: 'get_history', page: that.data.limit_page,limit:10})
  394. // }, 1000);
  395. }
  396. // console.log('执行')
  397. },
  398. // 点击表情
  399. Expression: function (e) {
  400. var that = this;
  401. var _news = that.data.news;
  402. var _text = _news + e.currentTarget.dataset.id;
  403. console.log(that.data.news)
  404. console.log(_text)
  405. that.setData({
  406. news: _text,
  407. inputValue: _text,
  408. })
  409. that.Sendoutswitch(that.data.news) // 发送键切换
  410. },
  411. // 点击input
  412. bindfocusclick: function (e) {
  413. var that = this;
  414. that.setData({
  415. Expression: false,
  416. menu: false,
  417. KeyboardHeight: e.detail.height,
  418. })
  419. // console.log(e.detail.height);
  420. },
  421. // 发送键切换
  422. Sendoutswitch: function (data) {
  423. var that = this;
  424. if (data == '') {
  425. that.setData({
  426. menushow: true, //菜单按钮显示隐藏
  427. Sendoutshow: false,// 发送按钮显示隐藏
  428. _width: 500, // input宽度
  429. })
  430. } else {
  431. that.setData({
  432. menushow: false, //菜单按钮显示隐藏
  433. Sendoutshow: true,// 发送按钮显示隐藏
  434. _width: 447, // input宽度
  435. })
  436. }
  437. },
  438. onUnload: function (e) {
  439. let url = 'imsocket/imchatmessage/clearunreadmsg';
  440. let datas = {
  441. to_id: this.data.to_id, form_id: getApp().UserId()
  442. };
  443. //清除未读信息
  444. getApp().postRequest(url, datas, function (res) {
  445. console.log(res, 11111);
  446. })
  447. console.log('销毁页面');
  448. },
  449. // ceshi: function (e) {
  450. // var that = this;
  451. // that.setData({
  452. // // mscrollTop: e.detail.scrollTop,
  453. // mscrollHeight: e.detail.scrollHeight
  454. // })
  455. // // console.log(e.detail.scrollTop);
  456. // // console.log(e.detail.scrollHeight);
  457. // // console.log(e.detail);
  458. // }
  459. })