业务交流通
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. let url = encodeURI(`ws://` + app.webSocketUrl + `/webSocket/{"userno":${receiveMemberId},"messageModule":"010"}`);
  113. //建立连接
  114. wx.connectSocket({
  115. url,//本地
  116. success: function () {
  117. console.log('websocket接口调通~还没真正的成功!')
  118. },
  119. fail: function () {
  120. console.log('websocket连接失败~')
  121. },
  122. })
  123. //连接成功
  124. wx.onSocketOpen(function () {
  125. console.log('onSocketOpen', '连接成功,真正的成功');
  126. // wx.showToast({
  127. // title: '连接成功,真正的成功',
  128. // icon: 'success',
  129. // duration: 2000
  130. // })
  131. })
  132. wx.onSocketError(res=>{
  133. console.log(`打印异常`)
  134. console.log(res);
  135. })
  136. // 接收服务器的消息事件
  137. wx.onSocketMessage(function (res) {
  138. // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息
  139. let list = [];
  140. list = _this.data.newsList;
  141. let _data = JSON.parse(res.data);
  142. _data.chatRecord = JSON.parse(_data.chatRecord);
  143. let msgData = {};
  144. // type 0 自己 1 对方
  145. if (_data.chatRecord.chatRecord.sender != _this.data.receiveMemberId) {
  146. msgData.type = 1;
  147. } else {
  148. msgData.type = 0;
  149. }
  150. msgData.isRead = true;
  151. msgData.message = _data.chatRecord.chatRecord.content;
  152. msgData.messageType = _data.chatRecord.chatRecord.messageType;
  153. if (msgData.messageType == 1) {
  154. msgData.object = _data.chatRecord.chatRecord.object;
  155. }
  156. msgData.date = utils.formatTime(new Date(_data.chatRecord.chatRecord.sendTime));
  157. msgData.date = msgData.date.replaceAll('/', '-');
  158. list.push(msgData);
  159. _this.setData({
  160. newsList: list
  161. })
  162. _this.setMsgRead('newsList', _this.data.newsList, 0)
  163. _this.scrollBottom()
  164. },
  165. )
  166. // 监听连接关闭
  167. wx.onSocketClose(function () {
  168. console.log('监听 WebSocket 连接关闭事件')
  169. })
  170. },
  171. // 获取历史记录
  172. getHistory() {
  173. var { sendMemberId, businessCommunicationDemandId } = this.data;
  174. $request.get('/businessCommunicationDemand/getChatRecordById/' + sendMemberId + '.action').then(res => {
  175. if (res.status == 0) {
  176. var historyList = [...res.data.chatRecordList, ...this.data.historyList]
  177. if (historyList && historyList.length > 0) {
  178. historyList.forEach(item => {
  179. // type 0 自己 1 对方
  180. if (item.sender != sendMemberId) {
  181. item.type = 0
  182. } else {
  183. item.type = 1
  184. }
  185. item.date = utils.formatTime(new Date(item.sendTime));
  186. item.date = item.date.replaceAll('/', '-');
  187. });
  188. this.setData({
  189. historyList
  190. })
  191. }
  192. // 如果产品ID存在
  193. if (businessCommunicationDemandId) {
  194. if (historyList.length > 0) {
  195. let tempArr = [];
  196. for (let index = historyList.length - 1; index >= 0; index--) {
  197. const element = historyList[index];
  198. if (element.messageType == 1 && tempArr.length < 1) {
  199. tempArr.push(element);
  200. }
  201. }
  202. // 说明没有一条是messageType == 1 的
  203. if (tempArr.length < 1) {
  204. this.sendmsgApi("", 1);
  205. } else {
  206. if (tempArr[0].transactionId != businessCommunicationDemandId) {
  207. this.sendmsgApi("", 1);
  208. }
  209. }
  210. } else {
  211. this.sendmsgApi("", 1);
  212. }
  213. }
  214. }
  215. // 设置消息已读
  216. this.setMsgRead('historyList', this.data.historyList, 1)
  217. // 页面进入滚动到底部
  218. this.scrollBottom()
  219. }).catch(err => {
  220. console.log(err)
  221. })
  222. },
  223. // 滚动到底部
  224. scrollBottom: function () {
  225. this.setData({
  226. scrollid: "sendmessage"
  227. })
  228. },
  229. // 设置滚动区域的高度
  230. setScrollHeight: function () {
  231. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  232. var scrollHeight = (client - 136) + 'px'
  233. this.setData({
  234. scrollHeight
  235. })
  236. },
  237. // 进入详情
  238. goDetails(e) {
  239. let value = e.currentTarget.dataset.value;
  240. wx.navigateTo({
  241. url: '/pages/index/components/listDetails/Details?businessCommunicationDemandId=' + value,
  242. })
  243. },
  244. // 获取内存中聊天列表的用户信息
  245. getStorageBaseInfo: function () {
  246. //获取存储信息
  247. wx.getStorage({
  248. key: 'receivebaseInfo',
  249. success: (res) => {
  250. this.setData({
  251. receivebaseInfo: res.data
  252. })
  253. }
  254. })
  255. },
  256. // 自定义下拉刷新
  257. refresh: function () {
  258. // // 下拉的实际操作
  259. // var pageNo = this.data.pageNo + 1
  260. // this.setData({
  261. // pageNo
  262. // })
  263. // if (this.timer) {
  264. // clearTimeout(this.timer)
  265. // }
  266. // this.timer = setTimeout(() => {
  267. // this.setData({
  268. // triggered: false
  269. // })
  270. // this.getHistory()
  271. // }, 2000)
  272. },
  273. /**
  274. * 生命周期函数--监听页面显示
  275. */
  276. onShow: function () {
  277. },
  278. /**
  279. * 生命周期函数--监听页面隐藏
  280. */
  281. onHide: function () {
  282. // 如果是发送图片就不触发
  283. if (!this.data.isSendImg) {
  284. wx.closeSocket();
  285. // 检测到全局的 websocket 是连接的 关闭
  286. if (app.globalData.isOnSocketOpen) {
  287. app.globalData.isOnSocketOpen = false;
  288. }
  289. }
  290. },
  291. /**
  292. * 生命周期函数--监听页面卸载
  293. */
  294. onUnload: function () {
  295. // 如果是发送图片就不触发
  296. if (!this.data.isSendImg) {
  297. wx.closeSocket();
  298. // 检测到全局的 websocket 是连接的 关闭
  299. if (app.globalData.isOnSocketOpen) {
  300. app.globalData.isOnSocketOpen = false;
  301. }
  302. }
  303. },
  304. /**
  305. * 页面相关事件处理函数--监听用户下拉动作
  306. */
  307. onPullDownRefresh: function () {
  308. },
  309. /**
  310. * 页面上拉触底事件的处理函数
  311. */
  312. onReachBottom: function () {
  313. },
  314. send: function () {
  315. var _this = this;
  316. if (_this.data.input) {
  317. this.sendmsgApi(_this.data.input, 0);
  318. }
  319. },
  320. sendmsgApi(content, msgType) {
  321. let { sendMemberId, receiveMemberId, businessCommunicationDemandId } = this.data;
  322. let transactionId = businessCommunicationDemandId;
  323. if (!businessCommunicationDemandId) {
  324. if (this.data.historyList.length > 0) {
  325. transactionId = this.data.historyList[this.data.historyList.length - 1].transactionId;
  326. }
  327. }
  328. if (!transactionId) {
  329. transactionId = "";
  330. }
  331. let params = {
  332. sender: receiveMemberId, // 发件人Id
  333. addressee: sendMemberId, // 收件人Id
  334. transactionId, // 产品ID
  335. content,
  336. messageType: msgType,
  337. };
  338. $request.post('/businessCommunicationDemand/addChatRecord.action', params).then(res => {
  339. // 是由需求点击的聊一聊 保存完信息后需要再发送一条
  340. if (msgType == 1) {
  341. this.sendmsgApi("你好,我对这个需求很有兴趣,可以聊聊吗?", 0);
  342. }
  343. }).catch(err => {
  344. console.log(err)
  345. })
  346. var list = [];
  347. list = this.data.newsList;
  348. var temp = { 'message': content, 'date': utils.formatTime(new Date()), type: 0 };
  349. temp.date = temp.date.replaceAll('/', '-');
  350. temp.messageType = msgType;
  351. temp.transactionId = transactionId;
  352. temp.isRead = false;
  353. if (msgType == 1) {
  354. temp.object = {};
  355. temp.object.info = this.data.productInfo;
  356. }
  357. list.push(temp);
  358. this.setData({
  359. newsList: list,
  360. input: null,
  361. inputShowed: true,
  362. })
  363. this.scrollBottom()
  364. // 表情选择隐藏
  365. this.setData({
  366. emotionVisible: false,
  367. })
  368. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  369. },
  370. bindChange: function (res) {
  371. this.setData({
  372. input: res.detail.value,
  373. })
  374. },
  375. back: function () {
  376. wx.closeSocket();
  377. console.log('连接断开');
  378. },
  379. emotionChange() {
  380. this.setData({
  381. emotionVisible: !this.data.emotionVisible
  382. })
  383. },
  384. addemotion: function (e) {
  385. let { connectemoji, input } = this.data
  386. if (input) {
  387. input = input + connectemoji[e.currentTarget.dataset.index];
  388. } else {
  389. input = connectemoji[e.currentTarget.dataset.index]
  390. }
  391. this.setData({
  392. input,
  393. emotionVisible: false,
  394. inputShowed: true,
  395. })
  396. },
  397. closeEmotionVisible() {
  398. this.setData({
  399. emotionVisible: false,
  400. })
  401. },
  402. // 发送图片
  403. upImg() {
  404. this.setData({
  405. isSendImg: true
  406. })
  407. wx.chooseImage({
  408. count: 1,
  409. success: (res) => {
  410. wx.uploadFile({
  411. filePath: res.tempFilePaths[0],
  412. name: "file",
  413. url: app.mangerUrl + "/file/uploading.action?fileType=TinymceImg",
  414. success: (res2) => {
  415. let data = JSON.parse(res2.data);
  416. this.sendmsgApi(data.data, 2);
  417. }
  418. })
  419. // /file/uploading.action?fileType=TinymceImg
  420. },
  421. complete: (res) => {
  422. // 调用结束后要关闭isSendImg
  423. this.setData({
  424. isSendImg: false
  425. })
  426. }
  427. })
  428. },
  429. // 设置已读未读
  430. setMsgRead(key, data, type) {
  431. data.forEach(el => {
  432. if (el.type == type) {
  433. el.isRead = true;
  434. }
  435. })
  436. if (key == 'historyList') {
  437. this.setData({
  438. historyList: data
  439. })
  440. } else {
  441. this.setData({
  442. newsList: data
  443. })
  444. }
  445. },
  446. // 图片预览
  447. viewImg(e) {
  448. let url = e.currentTarget.dataset.url;
  449. url += "?time=" + Date.now();
  450. wx.previewImage({
  451. current: url, // 当前显示图片的http链接
  452. urls: [url] // 需要预览的图片http链接列表
  453. })
  454. },
  455. })