业务交流通
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

wechat2.js 13KB

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