业务交流通
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

wechat2.js 12KB

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