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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. var utils = require("../../../utils/util.js")
  2. const app = getApp()
  3. const $request = require('../../../utils/request.js');
  4. const $faces = require('../../../utils/faces.js');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. receivebaseInfo: {
  11. avatar: "http://wx.qlogo.cn/mmopen/ajNVdqHZLLAIliaZvz5B1ibTzTehYzXdfBZ9hXTL7yuhuCUQGyzbuHeYS2yr8rO5PkbUBbHuWb5h9SibpRQkj3GTw/0",
  12. },
  13. sendAvatar: 'http://wx.qlogo.cn/mmopen/ajNVdqHZLLAIliaZvz5B1ibTzTehYzXdfBZ9hXTL7yuhuCUQGyzbuHeYS2yr8rO5PkbUBbHuWb5h9SibpRQkj3GTw/0',
  14. newsList: [
  15. // {
  16. // date: "2020.10.19",
  17. // message: '哈喽,好久不见',
  18. // type: 0
  19. // },
  20. // {
  21. // date: "2020.10.20",
  22. // message: '是呀,好久不见',
  23. // type: 1
  24. // },
  25. ],//消息列表
  26. historyList: [],
  27. input: null,
  28. openid: null,
  29. // 表情
  30. connectemoji: [],
  31. emoji_list: ['emoji1i1', 'emoji2i2', 'emoji3i3', 'emoji4i4', 'emoji5i5'],
  32. emotionVisible: false,
  33. inputShowed: false,
  34. scrollTop: 0,
  35. inputBottom: '0px',
  36. // 当前登录用户的ID
  37. receiveMemberId: null,
  38. // 对方的聊天ID
  39. sendMemberId: null,
  40. scrollid: 'scrollid',
  41. scrollHeight: '300px',
  42. // 下拉刷新
  43. triggered: true,
  44. // 历史记录当前页
  45. pageNo: 1,
  46. // 当前产品ID
  47. businessCommunicationDemandId: null,
  48. // 当前产品详情
  49. productInfo: null,
  50. // 发送框是否获取焦点
  51. inputShowed: false,
  52. // 是发送图片---用于阻止触发onhide
  53. isSendImg: false,
  54. // 发送图片url
  55. mangerUrl: "",
  56. },
  57. /**
  58. * 生命周期函数--监听页面加载
  59. */
  60. onLoad(options) {
  61. this.setData({
  62. mangerUrl: app.mangerUrl + "/file/getPhotos.action?path="
  63. })
  64. const eventChannel = this.getOpenerEventChannel()
  65. // 监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据
  66. eventChannel.on('customerid', data => {
  67. let productInfo = data.info ? data.info : null;
  68. let businessCommunicationDemandId = null;
  69. if (data.businessCommunicationDemandId) {
  70. businessCommunicationDemandId = data.businessCommunicationDemandId;
  71. }
  72. this.setData({
  73. sendMemberId: data.customerid,
  74. businessCommunicationDemandId,
  75. receivebaseInfo: {
  76. avatar: data.chatHeads,
  77. },
  78. productInfo
  79. })
  80. // 获取历史记录
  81. this.getHistory();
  82. })
  83. let customerId = app.globalData.customerId;
  84. let sendAvatar = app.globalData.businessCommunicationCustomer.chatHeads;
  85. this.setData({
  86. receiveMemberId: customerId,
  87. sendAvatar,
  88. connectemoji: $faces.getfaces(),
  89. })
  90. // 如果全局的 websocket 是连接的,需要关闭连接 调用自身的
  91. if (app.globalData.isOnSocketOpen) {
  92. wx.closeSocket();
  93. }
  94. // 获取内存中的数据
  95. this.getStorageBaseInfo()
  96. // 设置滚动区域的高度
  97. this.setScrollHeight()
  98. // 初始化websocket
  99. this.initWebSocket()
  100. // setTimeout(() => {
  101. // }, 100);
  102. },
  103. /**
  104. * 生命周期函数--监听页面初次渲染完成
  105. */
  106. onReady: function () {
  107. },
  108. // websocket初始化
  109. initWebSocket: function () {
  110. var _this = this;
  111. let { receiveMemberId, sendMemberId } = _this.data;
  112. //建立连接
  113. wx.connectSocket({
  114. url: `ws://` + app.webSocketUrl + `/webSocket/{"userno":${receiveMemberId},"messageModule":"010"}`,//本地
  115. success: function () {
  116. console.log('websocket连接成功~')
  117. },
  118. fail: function () {
  119. console.log('websocket连接失败~')
  120. },
  121. })
  122. //连接成功
  123. wx.onSocketOpen(function () {
  124. console.log('onSocketOpen', '连接成功,真正的成功');
  125. })
  126. // 接收服务器的消息事件
  127. wx.onSocketMessage(function (res) {
  128. // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息
  129. let list = [];
  130. list = _this.data.newsList;
  131. let _data = JSON.parse(res.data);
  132. _data.chatRecord = JSON.parse(_data.chatRecord);
  133. let msgData = {};
  134. // type 0 自己 1 对方
  135. if (_data.chatRecord.sender != _this.data.receiveMemberId) {
  136. msgData.type = 1;
  137. } else {
  138. msgData.type = 0;
  139. }
  140. msgData.isRead = true;
  141. msgData.message = _data.chatRecord.content;
  142. msgData.messageType = _data.chatRecord.messageType;
  143. if (msgData.messageType == 1) {
  144. msgData.object = _data.chatRecord.object;
  145. }
  146. msgData.date = utils.formatTime(new Date(_data.chatRecord.sendTime));
  147. msgData.date = msgData.date.replaceAll('/', '-');
  148. list.push(msgData);
  149. _this.setData({
  150. newsList: list
  151. })
  152. _this.setMsgRead('newsList', _this.data.newsList, 0)
  153. _this.scrollBottom()
  154. },
  155. )
  156. // 监听连接关闭
  157. wx.onSocketClose(function () {
  158. console.log('监听 WebSocket 连接关闭事件')
  159. })
  160. },
  161. // 获取历史记录
  162. getHistory() {
  163. var { sendMemberId, businessCommunicationDemandId } = this.data;
  164. $request.get('/businessCommunicationDemand/getChatRecordById/' + sendMemberId + '.action').then(res => {
  165. if (res.status == 0) {
  166. var historyList = [...res.data.chatRecordList, ...this.data.historyList]
  167. if (historyList && historyList.length > 0) {
  168. historyList.forEach(item => {
  169. // type 0 自己 1 对方
  170. if (item.sender != sendMemberId) {
  171. item.type = 0
  172. } else {
  173. item.type = 1
  174. }
  175. item.date = utils.formatTime(new Date(item.sendTime));
  176. item.date = item.date.replaceAll('/', '-');
  177. });
  178. this.setData({
  179. historyList
  180. })
  181. }
  182. // 如果产品ID存在
  183. if (businessCommunicationDemandId) {
  184. if (historyList.length > 0) {
  185. let tempArr = [];
  186. for (let index = historyList.length - 1; index >= 0; index--) {
  187. const element = historyList[index];
  188. if (element.messageType == 1 && tempArr.length < 1) {
  189. tempArr.push(element);
  190. }
  191. }
  192. // 说明没有一条是messageType == 1 的
  193. if (tempArr.length < 1) {
  194. this.sendmsgApi("", 1);
  195. } else {
  196. if (tempArr[0].transactionId != businessCommunicationDemandId) {
  197. this.sendmsgApi("", 1);
  198. }
  199. }
  200. } else {
  201. this.sendmsgApi("", 1);
  202. }
  203. }
  204. }
  205. // 设置消息已读
  206. this.setMsgRead('historyList', this.data.historyList, 1)
  207. // 页面进入滚动到底部
  208. this.scrollBottom()
  209. }).catch(err => {
  210. console.log(err)
  211. })
  212. },
  213. // 滚动到底部
  214. scrollBottom: function () {
  215. this.setData({
  216. scrollid:"sendmessage"
  217. })
  218. },
  219. // 设置滚动区域的高度
  220. setScrollHeight: function () {
  221. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  222. var scrollHeight = (client - 136) + 'px'
  223. this.setData({
  224. scrollHeight
  225. })
  226. },
  227. // 进入详情
  228. goDetails(e) {
  229. let value = e.currentTarget.dataset.value;
  230. wx.navigateTo({
  231. url: '/pages/index/components/listDetails/Details?businessCommunicationDemandId=' + value,
  232. })
  233. },
  234. // 获取内存中聊天列表的用户信息
  235. getStorageBaseInfo: function () {
  236. //获取存储信息
  237. wx.getStorage({
  238. key: 'receivebaseInfo',
  239. success: (res) => {
  240. this.setData({
  241. receivebaseInfo: res.data
  242. })
  243. }
  244. })
  245. },
  246. // 自定义下拉刷新
  247. refresh: function () {
  248. // // 下拉的实际操作
  249. // var pageNo = this.data.pageNo + 1
  250. // this.setData({
  251. // pageNo
  252. // })
  253. // if (this.timer) {
  254. // clearTimeout(this.timer)
  255. // }
  256. // this.timer = setTimeout(() => {
  257. // this.setData({
  258. // triggered: false
  259. // })
  260. // this.getHistory()
  261. // }, 2000)
  262. },
  263. /**
  264. * 生命周期函数--监听页面显示
  265. */
  266. onShow: function () {
  267. },
  268. /**
  269. * 生命周期函数--监听页面隐藏
  270. */
  271. onHide: function () {
  272. // 如果是发送图片就不触发
  273. if (!this.data.isSendImg) {
  274. wx.closeSocket();
  275. // 检测到全局的 websocket 是连接的 关闭
  276. if (app.globalData.isOnSocketOpen) {
  277. app.globalData.isOnSocketOpen = false;
  278. }
  279. }
  280. },
  281. /**
  282. * 生命周期函数--监听页面卸载
  283. */
  284. onUnload: function () {
  285. // 如果是发送图片就不触发
  286. if (!this.data.isSendImg) {
  287. wx.closeSocket();
  288. // 检测到全局的 websocket 是连接的 关闭
  289. if (app.globalData.isOnSocketOpen) {
  290. app.globalData.isOnSocketOpen = false;
  291. }
  292. }
  293. },
  294. /**
  295. * 页面相关事件处理函数--监听用户下拉动作
  296. */
  297. onPullDownRefresh: function () {
  298. },
  299. /**
  300. * 页面上拉触底事件的处理函数
  301. */
  302. onReachBottom: function () {
  303. },
  304. send: function () {
  305. var _this = this;
  306. if (_this.data.input) {
  307. this.sendmsgApi(_this.data.input, 0);
  308. }
  309. },
  310. sendmsgApi(content, msgType) {
  311. let { sendMemberId, receiveMemberId, businessCommunicationDemandId } = this.data;
  312. let transactionId = businessCommunicationDemandId;
  313. if (!businessCommunicationDemandId) {
  314. if (this.data.historyList.length > 0) {
  315. transactionId = this.data.historyList[this.data.historyList.length - 1].transactionId;
  316. }
  317. }
  318. if (!transactionId) {
  319. transactionId = "";
  320. }
  321. let params = {
  322. sender: receiveMemberId, // 发件人Id
  323. addressee: sendMemberId, // 收件人Id
  324. transactionId, // 产品ID
  325. content,
  326. messageType: msgType,
  327. };
  328. $request.post('/businessCommunicationDemand/addChatRecord.action', params).then(res => {
  329. // 是由需求点击的聊一聊 保存完信息后需要再发送一条
  330. if (msgType == 1) {
  331. this.sendmsgApi("你好,我对这个需求很有兴趣,可以聊聊吗?", 0);
  332. }
  333. }).catch(err => {
  334. console.log(err)
  335. })
  336. var list = [];
  337. list = this.data.newsList;
  338. var temp = { 'message': content, 'date': utils.formatTime(new Date()), type: 0 };
  339. temp.date = temp.date.replaceAll('/', '-');
  340. temp.messageType = msgType;
  341. temp.transactionId = transactionId;
  342. temp.isRead = false;
  343. if (msgType == 1) {
  344. temp.object = {};
  345. temp.object.info = this.data.productInfo;
  346. }
  347. list.push(temp);
  348. this.setData({
  349. newsList: list,
  350. input: null,
  351. inputShowed: true,
  352. })
  353. this.scrollBottom()
  354. // 表情选择隐藏
  355. this.setData({
  356. emotionVisible: false,
  357. })
  358. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  359. },
  360. bindChange: function (res) {
  361. this.setData({
  362. input: res.detail.value,
  363. })
  364. },
  365. back: function () {
  366. wx.closeSocket();
  367. console.log('连接断开');
  368. },
  369. emotionChange() {
  370. this.setData({
  371. emotionVisible: !this.data.emotionVisible
  372. })
  373. },
  374. addemotion: function (e) {
  375. let { connectemoji, input } = this.data
  376. if (input) {
  377. input = input + connectemoji[e.currentTarget.dataset.index];
  378. } else {
  379. input = connectemoji[e.currentTarget.dataset.index]
  380. }
  381. this.setData({
  382. input,
  383. emotionVisible: false,
  384. inputShowed: true,
  385. })
  386. },
  387. closeEmotionVisible() {
  388. this.setData({
  389. emotionVisible: false,
  390. })
  391. },
  392. // 发送图片
  393. upImg() {
  394. this.setData({
  395. isSendImg: true
  396. })
  397. wx.chooseImage({
  398. count: 1,
  399. success: (res) => {
  400. wx.uploadFile({
  401. filePath: res.tempFilePaths[0],
  402. name: "file",
  403. url: app.mangerUrl + "/file/uploading.action?fileType=TinymceImg",
  404. success: (res2) => {
  405. let data = JSON.parse(res2.data);
  406. this.sendmsgApi(data.data, 2);
  407. }
  408. })
  409. // /file/uploading.action?fileType=TinymceImg
  410. },
  411. complete: (res) => {
  412. // 调用结束后要关闭isSendImg
  413. this.setData({
  414. isSendImg: false
  415. })
  416. }
  417. })
  418. },
  419. // 设置已读未读
  420. setMsgRead(key, data, type) {
  421. data.forEach(el => {
  422. if (el.type == type) {
  423. el.isRead = true;
  424. }
  425. })
  426. if (key == 'historyList') {
  427. this.setData({
  428. historyList: data
  429. })
  430. } else {
  431. this.setData({
  432. newsList: data
  433. })
  434. }
  435. },
  436. // 图片预览
  437. viewImg(e) {
  438. let url = e.currentTarget.dataset.url;
  439. url += "?time=" + Date.now();
  440. wx.previewImage({
  441. current: url, // 当前显示图片的http链接
  442. urls: [url] // 需要预览的图片http链接列表
  443. })
  444. },
  445. })