业务交流通
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

wechat2.js 15KB

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