业务交流通
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wechat2.js 12KB

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