dialogbox.js 25 KB

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