chat.js 14 KB

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