业务交流通
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

wechat2.js 9.5KB

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