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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. $request.get('/businessCommunicationDemand/getChatRecordById/' + sendMemberId + '.action').then(res => {
  136. if (res.status == 0) {
  137. var historyList = [...res.data.chatRecordList, ...this.data.historyList]
  138. if (historyList && historyList.length > 0) {
  139. historyList.forEach(item => {
  140. if (item.sender != sendMemberId) {
  141. item.type = 0
  142. } else {
  143. item.type = 1
  144. }
  145. item.date = utils.formatTime(new Date(item.sendTime));
  146. item.date = item.date.replaceAll('/', '-');
  147. });
  148. this.setData({
  149. historyList
  150. })
  151. console.log(this.data.historyList, '历史记录数据')
  152. }
  153. // 如果产品ID存在
  154. console.log(businessCommunicationDemandId, '产品ID是否存在')
  155. if (businessCommunicationDemandId) {
  156. // this.data.historyList
  157. if(historyList.length > 0){
  158. for (let index = historyList.length - 1; index >= 0; index--) {
  159. const element = historyList[index];
  160. if (element.messageType == 1) {
  161. if (element.transactionId == businessCommunicationDemandId) {
  162. break;
  163. } else {
  164. this.sendmsgApi("", 1);
  165. }
  166. }
  167. }
  168. }else{
  169. this.sendmsgApi("", 1);
  170. }
  171. }
  172. }
  173. }).catch(err => {
  174. console.log(err)
  175. })
  176. },
  177. // 滚动到底部
  178. scrollBottom: function () {
  179. var { newsList } = this.data
  180. var scrollid = `scrollid${newsList.length - 1}`
  181. this.setData({
  182. scrollid
  183. })
  184. },
  185. // 设置滚动区域的高度
  186. setScrollHeight: function () {
  187. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  188. var scrollHeight = (client - 136) + 'px'
  189. this.setData({
  190. scrollHeight
  191. })
  192. },
  193. // 获取内存中聊天列表的用户信息
  194. getStorageBaseInfo: function () {
  195. //获取存储信息
  196. wx.getStorage({
  197. key: 'receivebaseInfo',
  198. success: (res) => {
  199. this.setData({
  200. receivebaseInfo: res.data
  201. })
  202. }
  203. })
  204. },
  205. // 自定义下拉刷新
  206. refresh: function () {
  207. // 下拉的实际操作
  208. var pageNo = this.data.pageNo + 1
  209. this.setData({
  210. pageNo
  211. })
  212. if (this.timer) {
  213. clearTimeout(this.timer)
  214. }
  215. this.timer = setTimeout(() => {
  216. this.setData({
  217. triggered: false
  218. })
  219. this.getHistory()
  220. }, 2000)
  221. },
  222. /**
  223. * 生命周期函数--监听页面显示
  224. */
  225. onShow: function () {
  226. },
  227. /**
  228. * 生命周期函数--监听页面隐藏
  229. */
  230. onHide: function () {
  231. },
  232. /**
  233. * 生命周期函数--监听页面卸载
  234. */
  235. onUnload: function () {
  236. },
  237. /**
  238. * 页面相关事件处理函数--监听用户下拉动作
  239. */
  240. onPullDownRefresh: function () {
  241. },
  242. /**
  243. * 页面上拉触底事件的处理函数
  244. */
  245. onReachBottom: function () {
  246. },
  247. /**
  248. * 用户点击右上角分享
  249. */
  250. onShareAppMessage: function () {
  251. },
  252. send: function () {
  253. var _this = this;
  254. if (_this.data.input) {
  255. this.sendmsgApi(_this.data.input, 0);
  256. }
  257. },
  258. sendmsgApi(content, msgType) {
  259. let { sendMemberId, receiveMemberId, businessCommunicationDemandId } = this.data;
  260. let transactionId = businessCommunicationDemandId;
  261. if (!businessCommunicationDemandId) {
  262. transactionId = this.data.historyList[this.data.historyList.length - 1].transactionId;
  263. }
  264. let params = {
  265. sender: receiveMemberId, // 发件人Id
  266. addressee: sendMemberId, // 收件人Id
  267. transactionId, // 产品ID
  268. content,
  269. messageType: msgType,
  270. };
  271. $request.post('/businessCommunicationDemand/addChatRecord.action', params).then(res => {
  272. console.log(`------------聊一聊保存记录-----------------`)
  273. console.log(res);
  274. console.log(`------------聊一聊保存记录-----------------`)
  275. }).catch(err => {
  276. console.log(err)
  277. })
  278. // wx.sendSocketMessage({
  279. // data: _this.data.input,
  280. // success: (res) => {
  281. // },
  282. // fail: (err) => {
  283. // console.log('sendSocketMessage', '失败')
  284. // }
  285. // })
  286. var list = [];
  287. list = this.data.newsList;
  288. var temp = { 'message': this.data.input, 'date': utils.formatTime(new Date()), type: 0 };
  289. list.push(temp);
  290. this.setData({
  291. newsList: list,
  292. input: null
  293. })
  294. this.scrollBottom()
  295. // 表情选择隐藏
  296. this.setData({
  297. emotionVisible: false,
  298. })
  299. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  300. console.log(client, '当前消息高度')
  301. },
  302. bindChange: function (res) {
  303. this.setData({
  304. input: res.detail.value,
  305. })
  306. },
  307. back: function () {
  308. wx.closeSocket();
  309. console.log('连接断开');
  310. },
  311. emotionChange() {
  312. console.log(`测试打开表情`)
  313. this.setData({
  314. emotionVisible: !this.data.emotionVisible
  315. })
  316. },
  317. addemotion: function (e) {
  318. console.log(e.currentTarget.dataset.index, "点了设默默")
  319. let { connectemoji, input } = this.data
  320. if (input) {
  321. input = input + connectemoji[e.currentTarget.dataset.index];
  322. } else {
  323. input = connectemoji[e.currentTarget.dataset.index]
  324. }
  325. console.log(input, '输入框额值')
  326. this.setData({
  327. input
  328. })
  329. },
  330. // 公共聚焦方法,方法比较笨,但是过度效果平滑流畅
  331. bottom: function () {
  332. var that = this;
  333. // 获取元素的高度
  334. let query = wx.createSelectorQuery();
  335. query.select('.news').boundingClientRect(rect => {
  336. //获取到元素
  337. let scrollTop = rect.height;
  338. this.setData({
  339. scrollTop
  340. })
  341. }).exec();
  342. console.log(this.data.scrollTop, 'hahah')
  343. wx.pageScrollTo({
  344. // scrollTop: this.data.scrollTop + 30,
  345. scrollTop: 10000,
  346. // duration: 0
  347. })
  348. },
  349. })