业务交流通
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

wechat2.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. },
  53. /**
  54. * 生命周期函数--监听页面加载
  55. */
  56. onLoad(options) {
  57. const eventChannel = this.getOpenerEventChannel()
  58. // 监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据
  59. eventChannel.on('customerid', data => {
  60. let productInfo = data.info ? data.info : null;
  61. let businessCommunicationDemandId = null;
  62. if (data.businessCommunicationDemandId) {
  63. businessCommunicationDemandId = data.businessCommunicationDemandId;
  64. }
  65. this.setData({
  66. sendMemberId: data.customerid,
  67. businessCommunicationDemandId,
  68. receivebaseInfo: {
  69. avatar: data.chatHeads,
  70. },
  71. productInfo
  72. })
  73. // 获取历史记录
  74. this.getHistory();
  75. })
  76. let customerId = app.globalData.customerId;
  77. let sendAvatar = app.globalData.businessCommunicationCustomer.chatHeads;
  78. this.setData({
  79. receiveMemberId: customerId,
  80. sendAvatar,
  81. connectemoji:$faces.getfaces(),
  82. })
  83. // 获取内存中的数据
  84. this.getStorageBaseInfo()
  85. // 设置滚动区域的高度
  86. this.setScrollHeight()
  87. // 初始化websocket
  88. this.initWebSocket()
  89. // setTimeout(() => {
  90. // }, 100);
  91. },
  92. /**
  93. * 生命周期函数--监听页面初次渲染完成
  94. */
  95. onReady: function () {
  96. },
  97. // websocket初始化
  98. initWebSocket: function () {
  99. var _this = this;
  100. let { receiveMemberId, sendMemberId } = _this.data;
  101. // console.log()
  102. //建立连接
  103. wx.connectSocket({
  104. url: `ws://192.168.18.138/webSocket/{"userno":${receiveMemberId},"messageModule":"010"}`,//本地
  105. success: function () {
  106. console.log('websocket连接成功~')
  107. },
  108. fail: function () {
  109. console.log('websocket连接失败~')
  110. },
  111. })
  112. //连接成功
  113. wx.onSocketOpen(function () {
  114. console.log('onSocketOpen', '连接成功,真正的成功');
  115. })
  116. // 接收服务器的消息事件
  117. wx.onSocketMessage(function (res) {
  118. // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息
  119. let list = [];
  120. list = _this.data.newsList;
  121. let _data = JSON.parse(res.data);
  122. _data.chatRecord = JSON.parse(_data.chatRecord);
  123. let msgData = {};
  124. if (_data.chatRecord.sender != _this.data.receiveMemberId) {
  125. msgData.type = 1;
  126. } else {
  127. msgData.type = 0;
  128. }
  129. msgData.message = _data.chatRecord.content;
  130. list.push(msgData);
  131. _this.setData({
  132. newsList: list
  133. })
  134. _this.scrollBottom()
  135. },
  136. )
  137. // 监听连接关闭
  138. wx.onSocketClose(function () {
  139. console.log('监听 WebSocket 连接关闭事件')
  140. })
  141. },
  142. // 获取历史记录
  143. getHistory() {
  144. var { sendMemberId, businessCommunicationDemandId } = this.data;
  145. $request.get('/businessCommunicationDemand/getChatRecordById/' + sendMemberId + '.action').then(res => {
  146. if (res.status == 0) {
  147. var historyList = [...res.data.chatRecordList, ...this.data.historyList]
  148. if (historyList && historyList.length > 0) {
  149. historyList.forEach(item => {
  150. if (item.sender != sendMemberId) {
  151. item.type = 0
  152. } else {
  153. item.type = 1
  154. }
  155. item.date = utils.formatTime(new Date(item.sendTime));
  156. item.date = item.date.replaceAll('/', '-');
  157. });
  158. this.setData({
  159. historyList
  160. })
  161. console.log(this.data.historyList, '历史记录数据')
  162. }
  163. // 如果产品ID存在
  164. console.log(businessCommunicationDemandId, '产品ID是否存在')
  165. if (businessCommunicationDemandId) {
  166. if (historyList.length > 0) {
  167. for (let index = historyList.length - 1; index >= 0; index--) {
  168. const element = historyList[index];
  169. if (element.messageType == 1) {
  170. if (element.transactionId == businessCommunicationDemandId) {
  171. break;
  172. } else {
  173. this.sendmsgApi("", 1);
  174. }
  175. }
  176. }
  177. } else {
  178. this.sendmsgApi("", 1);
  179. }
  180. }
  181. }
  182. // 页面进入滚动到底部
  183. this.scrollBottom()
  184. }).catch(err => {
  185. console.log(err)
  186. })
  187. },
  188. // 滚动到底部
  189. scrollBottom: function () {
  190. var { newsList } = this.data
  191. var scrollid = `scrollid${newsList.length - 1}`;
  192. if (newsList.length == 0) {
  193. scrollid = "scrollid0"
  194. }
  195. this.setData({
  196. scrollid
  197. })
  198. },
  199. // 设置滚动区域的高度
  200. setScrollHeight: function () {
  201. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  202. var scrollHeight = (client - 136) + 'px'
  203. this.setData({
  204. scrollHeight
  205. })
  206. },
  207. // 进入详情
  208. goDetails(e) {
  209. let value = e.currentTarget.dataset.value;
  210. wx.reLaunch({
  211. url: '/pages/index/components/listDetails/Details?businessCommunicationDemandId=' + value,
  212. })
  213. },
  214. // 获取内存中聊天列表的用户信息
  215. getStorageBaseInfo: function () {
  216. //获取存储信息
  217. wx.getStorage({
  218. key: 'receivebaseInfo',
  219. success: (res) => {
  220. this.setData({
  221. receivebaseInfo: res.data
  222. })
  223. }
  224. })
  225. },
  226. // 自定义下拉刷新
  227. refresh: function () {
  228. // // 下拉的实际操作
  229. // var pageNo = this.data.pageNo + 1
  230. // this.setData({
  231. // pageNo
  232. // })
  233. // if (this.timer) {
  234. // clearTimeout(this.timer)
  235. // }
  236. // this.timer = setTimeout(() => {
  237. // this.setData({
  238. // triggered: false
  239. // })
  240. // this.getHistory()
  241. // }, 2000)
  242. },
  243. /**
  244. * 生命周期函数--监听页面显示
  245. */
  246. onShow: function () {
  247. },
  248. /**
  249. * 生命周期函数--监听页面隐藏
  250. */
  251. onHide: function () {
  252. wx.closeSocket();
  253. },
  254. /**
  255. * 生命周期函数--监听页面卸载
  256. */
  257. onUnload: function () {
  258. wx.closeSocket();
  259. },
  260. /**
  261. * 页面相关事件处理函数--监听用户下拉动作
  262. */
  263. onPullDownRefresh: function () {
  264. },
  265. /**
  266. * 页面上拉触底事件的处理函数
  267. */
  268. onReachBottom: function () {
  269. },
  270. /**
  271. * 用户点击右上角分享
  272. */
  273. onShareAppMessage: function () {
  274. },
  275. send: function () {
  276. var _this = this;
  277. if (_this.data.input) {
  278. this.sendmsgApi(_this.data.input, 0);
  279. }
  280. },
  281. sendmsgApi(content, msgType) {
  282. let { sendMemberId, receiveMemberId, businessCommunicationDemandId } = this.data;
  283. let transactionId = businessCommunicationDemandId;
  284. if (!businessCommunicationDemandId) {
  285. transactionId = this.data.historyList[this.data.historyList.length - 1].transactionId;
  286. }
  287. let params = {
  288. sender: receiveMemberId, // 发件人Id
  289. addressee: sendMemberId, // 收件人Id
  290. transactionId, // 产品ID
  291. content,
  292. messageType: msgType,
  293. };
  294. $request.post('/businessCommunicationDemand/addChatRecord.action', params).then(res => {
  295. console.log(`------------聊一聊保存记录-----------------`)
  296. console.log(res);
  297. // 是由需求点击的聊一聊 保存完信息后需要再发送一条
  298. if (msgType == 1) {
  299. this.sendmsgApi("你好,我对这个需求很有兴趣,可以聊聊吗?", 0);
  300. }
  301. console.log(`------------聊一聊保存记录-----------------`)
  302. }).catch(err => {
  303. console.log(err)
  304. })
  305. var list = [];
  306. list = this.data.newsList;
  307. var temp = { 'message': content, 'date': utils.formatTime(new Date()), type: 0 };
  308. temp.date = temp.date.replaceAll('/', '-');
  309. temp.messageType = msgType;
  310. temp.transactionId = transactionId;
  311. if (msgType == 1) {
  312. temp.info = this.data.productInfo;
  313. }
  314. list.push(temp);
  315. this.setData({
  316. newsList: list,
  317. input: null
  318. })
  319. console.log(this.data.newsList, 'this.data.newsList')
  320. this.scrollBottom()
  321. // 表情选择隐藏
  322. this.setData({
  323. emotionVisible: false,
  324. })
  325. const client = wx.getSystemInfoSync().windowHeight // 获取当前窗口的高度
  326. console.log(client, '当前消息高度')
  327. },
  328. bindChange: function (res) {
  329. this.setData({
  330. input: res.detail.value,
  331. })
  332. },
  333. back: function () {
  334. wx.closeSocket();
  335. console.log('连接断开');
  336. },
  337. emotionChange() {
  338. console.log(`测试打开表情`)
  339. this.setData({
  340. emotionVisible: !this.data.emotionVisible
  341. })
  342. },
  343. addemotion: function (e) {
  344. console.log(e.currentTarget.dataset.index, "点了设默默")
  345. let { connectemoji, input } = this.data
  346. if (input) {
  347. input = input + connectemoji[e.currentTarget.dataset.index];
  348. } else {
  349. input = connectemoji[e.currentTarget.dataset.index]
  350. }
  351. console.log(input, '输入框额值')
  352. this.setData({
  353. input,
  354. emotionVisible:false,
  355. inputShowed:true,
  356. })
  357. },
  358. closeEmotionVisible(){
  359. this.setData({
  360. emotionVisible:false,
  361. })
  362. },
  363. // 公共聚焦方法,方法比较笨,但是过度效果平滑流畅
  364. bottom: function () {
  365. var that = this;
  366. // 获取元素的高度
  367. let query = wx.createSelectorQuery();
  368. query.select('.news').boundingClientRect(rect => {
  369. //获取到元素
  370. let scrollTop = rect.height;
  371. this.setData({
  372. scrollTop
  373. })
  374. }).exec();
  375. console.log(this.data.scrollTop, 'hahah')
  376. wx.pageScrollTo({
  377. // scrollTop: this.data.scrollTop + 30,
  378. scrollTop: 10000,
  379. // duration: 0
  380. })
  381. },
  382. })