业务交流通
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

wechat2.js 11KB

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