chat.js 13 KB

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