业务交流通
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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. receivebaseInfo:{
  58. avatar:data.chatHeads
  59. }
  60. })
  61. // 获取历史记录
  62. this.getHistory()
  63. })
  64. let customerId = app.globalData.customerId;
  65. let sendAvatar = app.globalData.businessCommunicationCustomer.chatHeads;
  66. console.log(customerId, 'customerId');
  67. var _this = this;
  68. _this.setData({
  69. receiveMemberId: customerId,
  70. sendAvatar
  71. })
  72. console.log(this.data.sendAvatar, 'hahha')
  73. // 获取内存中的数据
  74. this.getStorageBaseInfo()
  75. // 设置滚动区域的高度
  76. this.setScrollHeight()
  77. // 初始化websocket
  78. this.initWebSocket()
  79. // 页面进入滚动到底部
  80. this.scrollBottom()
  81. },
  82. /**
  83. * 生命周期函数--监听页面初次渲染完成
  84. */
  85. onReady: function () {
  86. },
  87. // websocket初始化
  88. initWebSocket: function () {
  89. var _this = this;
  90. let { receiveMemberId, sendMemberId } = this.data;
  91. // console.log()
  92. //建立连接
  93. wx.connectSocket({
  94. url: `ws://192.168.18.138/webSocket/{"userno":${receiveMemberId},"messageModule":"010"}`,//本地
  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. let list = [];
  110. list = _this.data.newsList;
  111. let _data = JSON.parse(res.data);
  112. _data.chatRecord = JSON.parse(_data.chatRecord);
  113. let msgData = {};
  114. if(_data.chatRecord.sender != _this.data.receiveMemberId){
  115. msgData.type = 1;
  116. }else{
  117. msgData.type = 0;
  118. }
  119. msgData.message = _data.chatRecord.content;
  120. list.push(msgData);
  121. _this.setData({
  122. newsList: list
  123. })
  124. _this.scrollBottom()
  125. },
  126. )
  127. // 监听连接关闭
  128. wx.onSocketClose(function () {
  129. console.log('监听 WebSocket 连接关闭事件')
  130. })
  131. },
  132. // 获取历史记录
  133. getHistory() {
  134. var { sendMemberId,businessCommunicationDemandId } = this.data;
  135. let url = "";
  136. if(businessCommunicationDemandId){
  137. url = '/businessCommunicationDemand/getChatRecordById/' + businessCommunicationDemandId + '/' + sendMemberId + '.action'
  138. }else{
  139. url = '/businessCommunicationDemand/getChatRecordById/' + sendMemberId + '.action'
  140. }
  141. $request.get(url).then(res => {
  142. console.log(`------------聊一聊获取历史记录-----------------`)
  143. console.log(res);
  144. console.log(`------------聊一聊获取历史记录-----------------`)
  145. if (res.status == 0) {
  146. var historyList = [...res.data.chatRecordList, ...this.data.historyList]
  147. if (historyList && historyList.length > 0) {
  148. historyList.forEach(item => {
  149. if (item.sender != sendMemberId) {
  150. item.type = 0
  151. } else {
  152. item.type = 1
  153. }
  154. item.date = utils.formatTime(new Date(item.sendTime))
  155. });
  156. this.setData({
  157. historyList
  158. })
  159. console.log(this.data.historyList, '历史记录数据')
  160. }
  161. }
  162. }).catch(err => {
  163. console.log(err)
  164. })
  165. },
  166. // 滚动到底部
  167. scrollBottom: function () {
  168. var { newsList } = this.data
  169. var scrollid = `scrollid${newsList.length - 1}`
  170. this.setData({
  171. scrollid
  172. })
  173. },
  174. // 设置滚动区域的高度
  175. setScrollHeight: function () {
  176. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  177. var scrollHeight = (client - 136) + 'px'
  178. this.setData({
  179. scrollHeight
  180. })
  181. },
  182. // 获取内存中聊天列表的用户信息
  183. getStorageBaseInfo: function () {
  184. //获取存储信息
  185. wx.getStorage({
  186. key: 'receivebaseInfo',
  187. success: (res) => {
  188. this.setData({
  189. receivebaseInfo: res.data
  190. })
  191. }
  192. })
  193. },
  194. // 自定义下拉刷新
  195. refresh: function () {
  196. // 下拉的实际操作
  197. var pageNo = this.data.pageNo + 1
  198. this.setData({
  199. pageNo
  200. })
  201. if (this.timer) {
  202. clearTimeout(this.timer)
  203. }
  204. this.timer = setTimeout(() => {
  205. this.setData({
  206. triggered: false
  207. })
  208. this.getHistory()
  209. }, 2000)
  210. },
  211. /**
  212. * 生命周期函数--监听页面显示
  213. */
  214. onShow: function () {
  215. },
  216. /**
  217. * 生命周期函数--监听页面隐藏
  218. */
  219. onHide: function () {
  220. },
  221. /**
  222. * 生命周期函数--监听页面卸载
  223. */
  224. onUnload: function () {
  225. },
  226. /**
  227. * 页面相关事件处理函数--监听用户下拉动作
  228. */
  229. onPullDownRefresh: function () {
  230. },
  231. /**
  232. * 页面上拉触底事件的处理函数
  233. */
  234. onReachBottom: function () {
  235. },
  236. /**
  237. * 用户点击右上角分享
  238. */
  239. onShareAppMessage: function () {
  240. },
  241. send: function () {
  242. var _this = this;
  243. if (_this.data.input) {
  244. let { sendMemberId ,receiveMemberId ,businessCommunicationDemandId} = _this.data;
  245. let params = {
  246. sender: receiveMemberId, // 发件人Id
  247. addressee: sendMemberId, // 收件人Id
  248. transactionId: businessCommunicationDemandId, // 产品ID
  249. content: _this.data.input
  250. };
  251. $request.post('/businessCommunicationDemand/addChatRecord.action',params).then(res => {
  252. console.log(`------------聊一聊保存记录-----------------`)
  253. console.log(res);
  254. console.log(`------------聊一聊保存记录-----------------`)
  255. }).catch(err => {
  256. console.log(err)
  257. })
  258. // wx.sendSocketMessage({
  259. // data: _this.data.input,
  260. // success: (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. })