dialogbox.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. const app = getApp();
  2. import {
  3. ApiModel
  4. } from "../../utils/api";
  5. const _Http = new ApiModel();
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. iosX: false,
  12. sendText: "", //输入框内容
  13. headerTitle: "对话框",
  14. socketMsgQueue: [],
  15. lineCount: 1, //输入框行数
  16. toView: 'item0',
  17. keyboardHeight: 0, //键盘高度
  18. functionToChoose: "", //功能选项
  19. memePopup: false, //表情弹出
  20. pageNumber: 1,
  21. pageTotal: 1,
  22. triggered: false, //下拉状态
  23. unreadCount: 0, //对方发送未读信息
  24. joinHands: false, //是否显示合作按钮
  25. ownerid: 0, //供需id
  26. fimdialogtype: '', //对话框类型
  27. fimuserid: 0, //创建者id
  28. timsubjectid: 0, //话题id
  29. timdialogid: 0, //对话框id
  30. gambitList: [], //话题列表
  31. sendfrom: [],
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. app.initSocket(); //链接websocket 用于测试
  38. const that = this;
  39. this.setData({
  40. timdialogid: options.id,
  41. userid: wx.getStorageSync('userData').userid
  42. });
  43. //详情
  44. _Http.basic({
  45. "accesstoken": wx.getStorageSync('userData').token,
  46. "classname": "system.im.imdialog.imdialog",
  47. "method": "query_imdialogMain",
  48. "content": {
  49. "timdialogid": this.data.timdialogid
  50. }
  51. }).then(res => {
  52. console.log("详情", res)
  53. if (res.msg != '成功') return wx.showToast({
  54. title: res.data,
  55. icon: "none"
  56. });
  57. let title = res.data[0].fimdialogname.split('-'),
  58. headerTitle = (title[0] == res.data[0].fname) ? title[1] : title[0],
  59. joinHands = false;
  60. //供需类型对话框,判断对话框ID是否为当前账号,当前账号更改供需状态,非当前账号显示合作按钮
  61. if (res.data[0].fimdialogtype == '供需') {
  62. if (res.data[0].fimuserid == this.data.userid) {
  63. _Http.basic({
  64. "accesstoken": wx.getStorageSync('userData').token,
  65. "classname": "customer.supplyanddemand.supplyanddemand",
  66. "method": "updatesupplyanddemandstatus",
  67. "content": {
  68. "tsupplyanddemandid": res.data[0].ownerid,
  69. "fstatus": "对接中",
  70. "timdialogid": res.data[0].timdialogid
  71. }
  72. }, false).then(res => {
  73. console.log("对接中", res)
  74. })
  75. } else {
  76. joinHands = true
  77. }
  78. }
  79. //返回数据
  80. this.setData({
  81. headerTitle,
  82. ownerid: res.data[0].ownerid,
  83. initiator: res.data[0].fimuserid,
  84. joinHands,
  85. fimuserid: res.data[0].fimuserid,
  86. fimdialogtype: res.data[0].fimdialogtype
  87. })
  88. //判断是否为话题类型对话框
  89. if (res.data[0].fimdialogtype == '话题') {
  90. _Http.basic({
  91. "accesstoken": wx.getStorageSync('userData').token,
  92. "classname": "system.im.imdialog.imdialog",
  93. "method": "queryDialogUncloseSubject",
  94. "content": {
  95. "timdialogid": res.data[0].timdialogid
  96. }
  97. }, false).then(s => {
  98. if (s.msg != '成功') return wx.showToast({
  99. title: res.data,
  100. icon: "none"
  101. });
  102. if (s.data != '成功') {
  103. this.setData({
  104. timsubjectid: s.data[0].timsubjectid
  105. })
  106. this.endGambit(true) //存在未关闭话题,调用关闭话题,并在关闭后重新获取历史话题
  107. } else {
  108. this.query_imdialogMessage(); //不存在未关闭话题,直接获取历史话题
  109. }
  110. })
  111. } else {
  112. this.query_imdialogMessage(); //非话题类型对话框,直接获取历史记录
  113. }
  114. })
  115. //判断是否存在安全距离
  116. let iosX = (getApp().globalData.safeAreaBottom == 0) ? false : true;
  117. this.setData({
  118. iosX
  119. })
  120. //获取表情包
  121. this.getMeme()
  122. },
  123. /* 创建话题,回复话题 */
  124. createGambit() {
  125. _Http.basic({
  126. "accesstoken": wx.getStorageSync('userData').token,
  127. "classname": "system.im.imdialog.imdialog",
  128. "method": "createSubject",
  129. "content": {
  130. "timdialogid": this.data.timdialogid,
  131. "timsubjectid": this.data.timsubjectid,
  132. "fcontent": ""
  133. }
  134. }).then(res => {
  135. if (res.msg != '成功') return wx.showToast({
  136. title: res.data,
  137. icon: "none"
  138. })
  139. let gambitList = this.data.gambitList;
  140. gambitList.push(res.data[0]);
  141. this.setData({
  142. timsubjectid: res.data[0].timsubjectid,
  143. gambitList
  144. })
  145. app.globalData.SocketTask.send({
  146. data: JSON.stringify({
  147. "classname": "message.message",
  148. "method": "sendDataMessage",
  149. "content": {
  150. "timdialogid": this.data.timdialogid,
  151. "ownertable": "timsubject",
  152. "ownerid": this.data.timsubjectid,
  153. "ftype": "话题",
  154. }
  155. }),
  156. success(res) {
  157. console.log("sddf", res)
  158. }
  159. })
  160. })
  161. },
  162. /* 结束话题 */
  163. endGambit(isGetList) {
  164. _Http.basic({
  165. "accesstoken": wx.getStorageSync('userData').token,
  166. "classname": "system.im.imdialog.imdialog",
  167. "method": "closeSubject",
  168. "content": {
  169. "timsubjectid": this.data.timsubjectid,
  170. "timdialogid": this.data.timdialogid
  171. }
  172. }).then(res => {
  173. if (res.msg != '成功') return wx.showToast({
  174. title: res.data,
  175. icon: "none"
  176. });
  177. this.setData({
  178. timsubjectid: 0
  179. })
  180. if (isGetList) this.query_imdialogMessage();
  181. })
  182. },
  183. /* 发送信息 */
  184. sendMsg(type, data) {
  185. const content = this.data.sendText.trim(),
  186. that = this;
  187. if (this.data.fimdialogtype == '话题') {
  188. if (type === 'file') {
  189. console.log("附件类型", data)
  190. let list = this.data.gambitList[this.data.gambitList.length - 1].imsubjectcontent;
  191. _Http.basic({
  192. "accesstoken": wx.getStorageSync('userData').token,
  193. "classname": "system.system.docManage",
  194. "method": "changeFilesData",
  195. "content": {
  196. "files": [{
  197. "tattachmentid": data[0].tattachmentid,
  198. "fdocument": data[0].fdocument,
  199. "ownertable": "timsubjectcontent",
  200. "ownerid": list[list.length - 1].timsubjectcontenid,
  201. "ftype": data[0].ftype
  202. }]
  203. }
  204. }).then(async s => {
  205. console.log("附件位置修改", s)
  206. let id = [{
  207. message: {
  208. data: {
  209. ownerid: this.data.timsubjectid
  210. }
  211. }
  212. }],
  213. item = await this.querySubject(id)
  214. list = this.data.gambitList;
  215. item[0].closedate = item[0].createdate;
  216. item[0].sendfrom = this.data.sendfrom;
  217. list[list.length - 1] = item[0];
  218. this.setData({
  219. gambitList: list
  220. })
  221. setTimeout(() => {
  222. this.setData({
  223. toView: 'bottom'
  224. })
  225. }, 300)
  226. })
  227. } else {
  228. if (content == '') return;
  229. _Http.basic({
  230. "accesstoken": wx.getStorageSync('userData').token,
  231. "classname": "system.im.imdialog.imdialog",
  232. "method": "createSubject",
  233. "content": {
  234. "timdialogid": this.data.timdialogid,
  235. "timsubjectid": this.data.timsubjectid,
  236. "fcontent": content
  237. }
  238. }, false).then(res => {
  239. let list = this.data.gambitList,
  240. time = res.data[0].createdate.split(".");
  241. res.data[0].closedate = time[0];
  242. res.data[0].sendfrom = this.data.sendfrom;
  243. list[list.length - 1] = res.data[0];
  244. this.setData({
  245. gambitList: list,
  246. sendText: ''
  247. })
  248. setTimeout(() => {
  249. this.setData({
  250. toView: 'bottom'
  251. })
  252. }, 300)
  253. })
  254. }
  255. } else {
  256. if (type === 'file') {
  257. app.globalData.SocketTask.send({
  258. data: JSON.stringify({
  259. "classname": "message.message",
  260. "method": "sendFileMessage",
  261. "content": {
  262. "timdialogid": this.data.timdialogid,
  263. "tattachmentid": data[0].tattachmentid
  264. }
  265. }),
  266. success(res) {}
  267. })
  268. } else {
  269. if (content == '') return;
  270. app.globalData.SocketTask.send({
  271. data: JSON.stringify({
  272. "classname": "message.message",
  273. "method": "sendTextMessage",
  274. "content": {
  275. "timdialogid": that.data.timdialogid,
  276. "fmessage": content
  277. }
  278. }),
  279. success(res) {
  280. that.setData({
  281. sendText: ''
  282. })
  283. }
  284. })
  285. }
  286. }
  287. },
  288. //历史信息查询
  289. query_imdialogMessage() {
  290. _Http.basic({
  291. "accesstoken": wx.getStorageSync('userData').token,
  292. "classname": "system.im.imdialog.imdialog",
  293. "method": "query_imdialogMessage",
  294. "content": {
  295. "pageNumber": this.data.pageNumber,
  296. "pageSize": 20,
  297. "timdialogid": this.data.timdialogid
  298. }
  299. }, false).then(async res => {
  300. console.log("聊天记录", res)
  301. if (res.msg != '成功') return wx.showToast({
  302. title: res.data,
  303. icon: "none"
  304. });
  305. let list = res.data;
  306. list = list.reverse();
  307. if (res.pageNumber != 1) {
  308. if (this.data.fimdialogtype != '话题') {
  309. list = list.concat(this.data.socketMsgQueue);
  310. this.setData({
  311. socketMsgQueue: list,
  312. toView: `item${res.data.length - 1}`,
  313. triggered: false
  314. })
  315. } else {
  316. let gambitList = await this.querySubject(list);
  317. list = gambitList.concat(this.data.gambitList);
  318. this.setData({
  319. gambitList: list,
  320. toView: `item${res.data.length - 1}`,
  321. triggered: false
  322. })
  323. }
  324. } else {
  325. if (this.data.fimdialogtype != '话题') {
  326. this.setData({
  327. socketMsgQueue: list,
  328. toView: `item${res.data.length - 1}`,
  329. pageTotal: res.pageTotal
  330. })
  331. } else {
  332. let gambitList = await this.querySubject(list);
  333. this.setData({
  334. gambitList,
  335. toView: `item${res.data.length - 1}`,
  336. pageTotal: res.pageTotal
  337. })
  338. }
  339. setTimeout(() => {
  340. this.setData({
  341. toView: 'bottom'
  342. })
  343. }, 500)
  344. }
  345. })
  346. },
  347. /* 根据话题ID查询详情 */
  348. async querySubject(array) {
  349. console.log("列表", array)
  350. let list = [];
  351. for (let index = 0; index < array.length; index++) {
  352. await _Http.basic({
  353. "accesstoken": wx.getStorageSync('userData').token,
  354. "classname": "system.im.imdialog.imdialog",
  355. "method": "querySubject",
  356. "content": {
  357. "timsubjectid": array[index].message.data.ownerid
  358. }
  359. }, false).then(res => {
  360. if (res.data.length != 0 && res.data[0].imsubjectcontent.length != 0) {
  361. res.data[0].sendfrom = array[index].message.sendfrom;
  362. res.data[0].timdialogid = array[index].message.timdialogid;
  363. if (res.data[0].closedate != null) {
  364. let time = res.data[0].closedate.split('.');
  365. res.data[0].closedate = time[0];
  366. list.push(res.data[0]);
  367. } else {
  368. let time = res.data[0].createdate.split('.');
  369. res.data[0].createdate = time[0];
  370. list.push(res.data[0]);
  371. }
  372. }
  373. })
  374. }
  375. return list;
  376. },
  377. /* 加载分页 */
  378. scrolltoupper() {
  379. if (this.data.pageNumber < this.data.pageTotal) {
  380. this.setData({
  381. pageNumber: this.data.pageNumber + 1,
  382. triggered: true
  383. })
  384. this.query_imdialogMessage();
  385. } else {
  386. this.setData({
  387. triggered: true
  388. })
  389. }
  390. },
  391. //键盘弹出高度
  392. keyboardheightchange(e) {
  393. let {
  394. height
  395. } = e.detail;
  396. if (this.data.iosX && height > 150) height = height - 17;
  397. this.setData({
  398. keyboardHeight: height
  399. })
  400. },
  401. /* 确认合作 */
  402. confirmTheCooperation() {
  403. _Http.basic({
  404. "accesstoken": wx.getStorageSync('userData').token,
  405. "classname": "customer.supplyanddemand.supplyanddemand",
  406. "method": "updatesupplyanddemandstatus",
  407. "content": {
  408. "tsupplyanddemandid": this.data.ownerid,
  409. "fstatus": "已解决",
  410. "tenterprise_userid": this.data.initiator,
  411. "timdialogid": this.data.timdialogid
  412. }
  413. }).then(res => {
  414. console.log("确认合作", res)
  415. })
  416. },
  417. /* 拒绝合作 */
  418. refuseToCooperate() {
  419. _Http.basic({
  420. "accesstoken": wx.getStorageSync('userData').token,
  421. "classname": "customer.supplyanddemand.supplyanddemand",
  422. "method": "updatesupplyanddemandstatus",
  423. "content": {
  424. "tsupplyanddemandid": this.data.ownerid,
  425. "fstatus": "待对接",
  426. "timdialogid": this.data.timdialogid
  427. }
  428. }).then(res => {
  429. console.log("拒绝合作", res)
  430. })
  431. },
  432. /* 输入框数据绑定 */
  433. sendInput(e) {
  434. this.setData({
  435. sendText: e.detail.value
  436. })
  437. },
  438. /* 发送表情 */
  439. sendMeme(e) {
  440. if (this.data.fimdialogtype == '话题') return wx.showToast({
  441. title: '当前状态不可发送',
  442. icon: "none"
  443. })
  444. const {
  445. item
  446. } = e.currentTarget.dataset;
  447. let data = [];
  448. data.push(item);
  449. this.sendMsg('file', data)
  450. },
  451. //功能选择
  452. selectionFunction(e) {
  453. const {
  454. name
  455. } = e.target.dataset,
  456. that = this;
  457. if (name == undefined || name == "") return;
  458. if (name == '表情') {
  459. let keyboardHeight = (this.data.iosX == true) ? 220 : 200;
  460. this.setData({
  461. keyboardHeight,
  462. memePopup: true
  463. })
  464. that.toBotton()
  465. let time1 = setInterval(() => {
  466. this.setData({
  467. keyboardHeight
  468. })
  469. if (this.data.keyboardHeight == keyboardHeight && this.data.memePopup) clearInterval(time1);
  470. }, 100);
  471. } else if (name == '图片') {
  472. this.endMemePopup()
  473. wx.chooseMedia({
  474. count: 1,
  475. mediaType: ['image'],
  476. sourceType: ['album', 'camera'],
  477. maxDuration: 30,
  478. camera: 'back',
  479. success(res) {
  480. let data = {
  481. file: res.tempFiles[0].tempFilePath
  482. };
  483. that.afterRead(data)
  484. }
  485. })
  486. } else if (name == '历史') {
  487. wx.navigateTo({
  488. url: './history?id=' + that.data.timdialogid + '&type=' + that.data.fimdialogtype,
  489. })
  490. }
  491. },
  492. textareaFocus() {
  493. this.setData({
  494. memePopup: false, //表情弹出
  495. })
  496. this.toBotton()
  497. },
  498. //关闭表情
  499. endMemePopup(e) {
  500. this.setData({
  501. keyboardHeight: 0, //键盘高度
  502. memePopup: false, //表情弹出
  503. })
  504. },
  505. /* 文本域行数变化 */
  506. /* linechange(e) {
  507. console.log(e)
  508. const {
  509. lineCount
  510. } = e.detail;
  511. if (lineCount == this.data.lineCount) return;
  512. this.setData({
  513. lineCount
  514. })
  515. }, */
  516. //获取表情包
  517. getMeme() {
  518. _Http.basic({
  519. "accesstoken": wx.getStorageSync('userData').token,
  520. "classname": "system.system.docManage",
  521. "method": "queryDoc",
  522. "content": {
  523. "getdatafromdbanyway": true,
  524. "ownertable": "system",
  525. "ownerid": 0
  526. }
  527. }, false).then(res => {
  528. if (res.msg != '成功') return setTimeout(() => {
  529. this.getMeme();
  530. }, 3000);
  531. this.setData({
  532. memeList: res.data
  533. })
  534. })
  535. },
  536. afterRead({
  537. file
  538. }) {
  539. if (this.data.fimdialogtype == '话题') {
  540. if (this.data.timsubjectid == 0 || this.data.userid != this.data.fimuserid) {
  541. return wx.showToast({
  542. title: '当前状态不可发送',
  543. icon: "none"
  544. })
  545. } else {
  546. if (this.data.gambitList[this.data.gambitList.length - 1].imsubjectcontent.length < 1) return wx.showToast({
  547. title: '当前状态不可发送',
  548. icon: "none"
  549. })
  550. }
  551. }
  552. var that = this
  553. var index = file.lastIndexOf(".");
  554. var ext = file.substr(index + 1);
  555. var timestamp = Date.parse(new Date());
  556. wx.getFileSystemManager().readFile({
  557. filePath: file,
  558. // encoding:'utf-8',
  559. success: result => {
  560. //返回临时文件路径
  561. const fileData = result.data
  562. wx.request({
  563. url: 'https://www.buwanjia.com/bwj/rest/webclientrest/', //仅为示例,并非真实的接口地址
  564. data: {
  565. "accesstoken": wx.getStorageSync('userData').token,
  566. "classname": "system.system.docManage",
  567. "method": "getFileName",
  568. "content": {
  569. "filename": 'wx' + timestamp,
  570. "filetype": ext,
  571. "ownertable": "timdialog",
  572. "ownerid": that.data.timdialogid,
  573. "ftype": "default",
  574. "HttpMethod": 'put'
  575. }
  576. },
  577. method: 'post',
  578. header: {
  579. 'content-type': 'application/json' // 默认值
  580. },
  581. success(res) {
  582. console.log(res)
  583. that.uploadFile(res, fileData)
  584. }
  585. })
  586. },
  587. fail: console.error
  588. })
  589. },
  590. uploadFile(res, data) {
  591. var that = this
  592. wx.request({
  593. url: res.data.data.obsuploadurl,
  594. method: "PUT",
  595. data: data,
  596. header: {
  597. 'content-type': 'application/octet-stream' // 默认值
  598. },
  599. success() {
  600. wx.request({
  601. url: 'https://www.buwanjia.com/bwj/rest/webclientrest/', //仅为示例,并非真实的接口地址
  602. data: {
  603. "accesstoken": wx.getStorageSync('userData').token,
  604. "classname": "system.system.docManage",
  605. "method": "uploadSuccess",
  606. "content": {
  607. "obsfilename": res.data.data.obsfilename
  608. }
  609. },
  610. method: 'post',
  611. header: {
  612. 'content-type': 'application/json' // 默认值
  613. },
  614. success(res) {
  615. let file = res.data.data;
  616. that.sendMsg('file', file)
  617. }
  618. })
  619. }
  620. })
  621. },
  622. toBotton() {
  623. this.setData({
  624. toView: 'bottom'
  625. })
  626. },
  627. /**
  628. * 生命周期函数--监听页面初次渲染完成
  629. */
  630. onReady: function () {
  631. },
  632. /**
  633. * 生命周期函数--监听页面显示
  634. */
  635. onShow: function () {
  636. var that = this;
  637. app.globalData.callback = function (res) {
  638. //res 接收websocket onMessage事件返回的数据
  639. let objs = JSON.parse(res.data),
  640. unreadCount = that.data.unreadCount; /* triggered */
  641. that.data.socketMsgQueue.push(objs)
  642. that.setData({
  643. socketMsgQueue: that.data.socketMsgQueue
  644. })
  645. if (objs.message.sendfrom) that.setData({
  646. sendfrom: objs.message.sendfrom
  647. })
  648. that.toBotton()
  649. /* if (wx.getStorageSync('userData').userid == objs.message.sendfrom.userid) {
  650. } else {
  651. that.setData({
  652. socketMsgQueue: that.data.socketMsgQueue
  653. })
  654. } */
  655. /* ,
  656. toView: `item${that.data.socketMsgQueue.length - 1}` */
  657. }
  658. },
  659. /**
  660. * 生命周期函数--监听页面隐藏
  661. */
  662. onHide: function () {
  663. },
  664. /**
  665. * 生命周期函数--监听页面卸载
  666. */
  667. onUnload: function () {
  668. //重置未读信息
  669. _Http.basic({
  670. "accesstoken": wx.getStorageSync('userData').token,
  671. "classname": "system.im.imdialog.imdialog",
  672. "method": "restUnReadMsgCount",
  673. "content": {
  674. "timdialogid": this.data.timdialogid
  675. }
  676. }).then(res => {
  677. console.log(res)
  678. })
  679. },
  680. /**
  681. * 页面相关事件处理函数--监听用户下拉动作
  682. */
  683. onPullDownRefresh: function () {
  684. },
  685. /**
  686. * 页面上拉触底事件的处理函数
  687. */
  688. onReachBottom: function () {
  689. },
  690. /**
  691. * 用户点击右上角分享
  692. */
  693. onShareAppMessage: function () {
  694. }
  695. })