业务交流通
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. var utils = require("../../../utils/util.js")
  2. const app = getApp()
  3. const api = require('../../../utils/request.js'); //相对路径
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. receivebaseInfo: {
  10. avatar:"http://wx.qlogo.cn/mmopen/ajNVdqHZLLAIliaZvz5B1ibTzTehYzXdfBZ9hXTL7yuhuCUQGyzbuHeYS2yr8rO5PkbUBbHuWb5h9SibpRQkj3GTw/0",
  11. },
  12. sendAvatar: 'http://wx.qlogo.cn/mmopen/ajNVdqHZLLAIliaZvz5B1ibTzTehYzXdfBZ9hXTL7yuhuCUQGyzbuHeYS2yr8rO5PkbUBbHuWb5h9SibpRQkj3GTw/0',
  13. newsList: [
  14. {
  15. date: "2020.10.19",
  16. message: '哈喽,好久不见',
  17. type: 0
  18. },
  19. {
  20. date: "2020.10.20",
  21. message: '是呀,好久不见',
  22. type: 1
  23. },
  24. {
  25. date: "2020.10.20",
  26. message: '是呀,好久不见',
  27. type: 1
  28. },
  29. {
  30. date: "2020.10.20",
  31. message: 'sad加撒恐龙当家撒了框架到拉喀什就科利达杀伤力肯定会萨克雷号地块了撒接口连接达萨罗凯撒将到拉萨就来的就是sad就卡了😘',
  32. type: 1
  33. },
  34. {
  35. date: "2020.10.19",
  36. message: '自行车金坷垃上架了靠的就是卡旅行者沉默现在你沙雕萨比许昌办是撒娇登记卡数据库大数据库写注册表你现在从现在是阿斯顿撒多😄',
  37. type: 0
  38. },
  39. ],//消息列表
  40. historyList: [],
  41. input: null,
  42. openid: null,
  43. connectemoji: ["😘", "😡", "😔", "😄", "❤"],
  44. emoji_list: ['emoji1i1', 'emoji2i2', 'emoji3i3', 'emoji4i4', 'emoji5i5'],
  45. emotionVisible: false,
  46. inputShowed: false,
  47. scrollTop: 0,
  48. inputBottom: '0px',
  49. receiveMemberId: null,
  50. sendMemberId: null,
  51. scrollid: 'scrollid',
  52. scrollHeight: '300px',
  53. // 下拉刷新
  54. triggered: true,
  55. // 历史记录当前页
  56. pageNo: 1,
  57. },
  58. /**
  59. * 生命周期函数--监听页面加载
  60. */
  61. onLoad: function (options) {
  62. var receiveMemberId = options.receiveMemberId
  63. var sendMemberId = app.globalData.open_id
  64. var sendAvatar = app.globalData.sendAvatar
  65. var _this = this;
  66. _this.setData({
  67. receiveMemberId,
  68. sendMemberId,
  69. sendAvatar
  70. })
  71. console.log(app.globalData.sendAvatar, 'hahha')
  72. // 获取内存中的数据
  73. this.getStorageBaseInfo()
  74. // 设置滚动区域的高度
  75. this.setScrollHeight()
  76. // 获取历史记录
  77. this.getHistory()
  78. // 初始化websocket
  79. this.initWebSocket()
  80. // 页面进入滚动到底部
  81. this.scrollBottom()
  82. },
  83. /**
  84. * 生命周期函数--监听页面初次渲染完成
  85. */
  86. onReady: function () {
  87. },
  88. // websocket初始化
  89. initWebSocket: function () {
  90. var _this = this;
  91. var { receiveMemberId, sendMemberId } = this.data
  92. //建立连接
  93. wx.connectSocket({
  94. url: `ws://192.168.18.156/webSocket/{"userno":"666678","messageModule":"007"}`,//本地
  95. success: function () {
  96. console.log('websocket连接成功~')
  97. },
  98. fail: function () {
  99. console.log('websocket连接失败~')
  100. },
  101. })
  102. //连接成功
  103. wx.onSocketOpen(function () {
  104. console.log('onSocketOpen', '连接成功,真正的成功');
  105. })
  106. // 接收服务器的消息事件
  107. wx.onSocketMessage(function (res) {
  108. // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息
  109. var list = [];
  110. list = _this.data.newsList;
  111. var _data = JSON.parse(res.data);
  112. list.push(_data);
  113. console.log(list)
  114. _this.setData({
  115. newsList: list
  116. })
  117. _this.scrollBottom()
  118. },
  119. )
  120. // 监听连接关闭
  121. wx.onSocketClose(function () {
  122. console.log('监听 WebSocket 连接关闭事件')
  123. })
  124. },
  125. // 获取历史记录
  126. getHistory: function () {
  127. var { receiveMemberId, sendMemberId, pageNo } = this.data
  128. var params = {
  129. receiveMemberId,
  130. sendMemberId,
  131. pageNo,
  132. pageSize: 5,
  133. }
  134. api.get("/zxxt/chat/msg/list", params, (res) => {
  135. if (res.code == 'success') {
  136. // var historyList = res.data.data
  137. var historyList = [...res.data.data, ...this.data.historyList]
  138. if (historyList && historyList.length > 0) {
  139. historyList.forEach(item => {
  140. if (item.send_member_id == sendMemberId) {
  141. item.type = 0
  142. } else {
  143. item.type = 1
  144. }
  145. });
  146. this.setData({
  147. historyList
  148. })
  149. console.log(this.data.historyList, '历史记录数据')
  150. } else {
  151. // 判断是否是第一次进入查看历史记录:是(不显示弹框,不是则显示弹框)
  152. if (this.data.pageNo > 1) {
  153. wx.showToast({
  154. title: "没有更多历史记录了",
  155. icon: 'none',
  156. duration: 2000
  157. })
  158. }
  159. }
  160. } else {
  161. if (res.message) {
  162. wx.showToast({
  163. title: res.message,
  164. icon: 'none',
  165. duration: 2000
  166. })
  167. }
  168. }
  169. }, (res) => {
  170. if (res.message) {
  171. wx.showToast({
  172. title: res.message,
  173. icon: 'none',
  174. duration: 2000
  175. })
  176. }
  177. })
  178. },
  179. // 滚动到底部
  180. scrollBottom: function () {
  181. var { newsList } = this.data
  182. var scrollid = `scrollid${newsList.length - 1}`
  183. this.setData({
  184. scrollid
  185. })
  186. },
  187. // 设置滚动区域的高度
  188. setScrollHeight: function () {
  189. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  190. var scrollHeight = (client - 236) + 'px'
  191. this.setData({
  192. scrollHeight
  193. })
  194. },
  195. // 获取内存中聊天列表的用户信息
  196. getStorageBaseInfo: function () {
  197. //获取存储信息
  198. wx.getStorage({
  199. key: 'receivebaseInfo',
  200. success: (res) => {
  201. this.setData({
  202. receivebaseInfo: res.data
  203. })
  204. }
  205. })
  206. },
  207. // 自定义下拉刷新
  208. refresh: function () {
  209. // 下拉的实际操作
  210. var pageNo = this.data.pageNo + 1
  211. this.setData({
  212. pageNo
  213. })
  214. if (this.timer) {
  215. clearTimeout(this.timer)
  216. }
  217. this.timer = setTimeout(() => {
  218. this.setData({
  219. triggered: false
  220. })
  221. this.getHistory()
  222. }, 2000)
  223. },
  224. /**
  225. * 生命周期函数--监听页面显示
  226. */
  227. onShow: function () {
  228. },
  229. /**
  230. * 生命周期函数--监听页面隐藏
  231. */
  232. onHide: function () {
  233. },
  234. /**
  235. * 生命周期函数--监听页面卸载
  236. */
  237. onUnload: function () {
  238. },
  239. /**
  240. * 页面相关事件处理函数--监听用户下拉动作
  241. */
  242. onPullDownRefresh: function () {
  243. },
  244. /**
  245. * 页面上拉触底事件的处理函数
  246. */
  247. onReachBottom: function () {
  248. },
  249. /**
  250. * 用户点击右上角分享
  251. */
  252. onShareAppMessage: function () {
  253. },
  254. send: function () {
  255. var _this = this;
  256. if (_this.data.input) {
  257. wx.sendSocketMessage({
  258. data: _this.data.input,
  259. success: (res) => {
  260. console.log(res)
  261. },
  262. fail: (err) => {
  263. console.log('sendSocketMessage', '失败')
  264. }
  265. })
  266. var list = [];
  267. list = this.data.newsList;
  268. var temp = { 'message': _this.data.input, 'date': utils.formatTime(new Date()), type: 0 };
  269. list.push(temp);
  270. this.setData({
  271. newsList: list,
  272. input: null
  273. })
  274. this.scrollBottom()
  275. // 表情选择隐藏
  276. this.setData({
  277. emotionVisible: false,
  278. })
  279. }
  280. // this.bottom()
  281. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  282. console.log(client, 'shurugaodu')
  283. },
  284. bindChange: function (res) {
  285. this.setData({
  286. input: res.detail.value,
  287. })
  288. },
  289. back: function () {
  290. wx.closeSocket();
  291. console.log('连接断开');
  292. },
  293. emotionChange() {
  294. console.log(`测试打开表情`)
  295. this.setData({
  296. emotionVisible: !this.data.emotionVisible
  297. })
  298. },
  299. addemotion: function (e) {
  300. console.log(e.currentTarget.dataset.index, "点了设默默")
  301. let { connectemoji, input } = this.data
  302. if (input) {
  303. input = input + connectemoji[e.currentTarget.dataset.index];
  304. } else {
  305. input = connectemoji[e.currentTarget.dataset.index]
  306. }
  307. console.log(input, '输入框额值')
  308. this.setData({
  309. input
  310. })
  311. },
  312. // 公共聚焦方法,方法比较笨,但是过度效果平滑流畅
  313. bottom: function () {
  314. var that = this;
  315. // 获取元素的高度
  316. let query = wx.createSelectorQuery();
  317. query.select('.news').boundingClientRect(rect => {
  318. //获取到元素
  319. let scrollTop = rect.height;
  320. this.setData({
  321. scrollTop
  322. })
  323. }).exec();
  324. console.log(this.data.scrollTop, 'hahah')
  325. wx.pageScrollTo({
  326. // scrollTop: this.data.scrollTop + 30,
  327. scrollTop: 10000,
  328. // duration: 0
  329. })
  330. },
  331. })