chat.js 12 KB

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