dialogbox.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function (options) {
  31. app.initSocket(); //链接websocket 用于测试
  32. const that = this;
  33. this.setData({
  34. timdialogid: options.id,
  35. userid: wx.getStorageSync('userData').userid
  36. });
  37. //详情
  38. _Http.basic({
  39. "accesstoken": wx.getStorageSync('userData').token,
  40. "classname": "system.im.imdialog.imdialog",
  41. "method": "query_imdialogMain",
  42. "content": {
  43. "timdialogid": this.data.timdialogid
  44. }
  45. }).then(res => {
  46. console.log("详情", res)
  47. if (res.msg != '成功') return wx.showToast({
  48. title: res.data,
  49. icon: "none"
  50. });
  51. let title = res.data[0].fimdialogname.split('-'),
  52. headerTitle = (title[0] == res.data[0].fname) ? title[1] : title[0],
  53. joinHands = false;
  54. if (res.data[0].fimdialogtype == '供需' && res.data[0].fimuserid == this.data.userid) {
  55. _Http.basic({
  56. "accesstoken": wx.getStorageSync('userData').token,
  57. "classname": "customer.supplyanddemand.supplyanddemand",
  58. "method": "updatesupplyanddemandstatus",
  59. "content": {
  60. "tsupplyanddemandid": res.data[0].ownerid,
  61. "fstatus": "对接中",
  62. "timdialogid": res.data[0].timdialogid
  63. }
  64. }, false).then(res => {
  65. console.log("对接中", res)
  66. })
  67. } else {
  68. joinHands = true
  69. }
  70. this.setData({
  71. headerTitle,
  72. ownerid: res.data[0].ownerid,
  73. initiator: res.data[0].fimuserid,
  74. joinHands
  75. })
  76. })
  77. //群历史信息
  78. this.query_imdialogMessage();
  79. let iosX = (getApp().globalData.safeAreaBottom == 0) ? false : true;
  80. this.setData({
  81. iosX
  82. })
  83. //获取表情包
  84. this.getMeme()
  85. },
  86. /* 确认合作 */
  87. confirmTheCooperation() {
  88. _Http.basic({
  89. "accesstoken": wx.getStorageSync('userData').token,
  90. "classname": "customer.supplyanddemand.supplyanddemand",
  91. "method": "updatesupplyanddemandstatus",
  92. "content": {
  93. "tsupplyanddemandid": this.data.ownerid,
  94. "fstatus": "已解决",
  95. "tenterprise_userid": this.data.initiator,
  96. "timdialogid": this.data.timdialogid
  97. }
  98. }).then(res => {
  99. console.log("确认合作", res)
  100. })
  101. },
  102. /* 拒绝合作 */
  103. refuseToCooperate() {
  104. _Http.basic({
  105. "accesstoken": wx.getStorageSync('userData').token,
  106. "classname": "customer.supplyanddemand.supplyanddemand",
  107. "method": "updatesupplyanddemandstatus",
  108. "content": {
  109. "tsupplyanddemandid": this.data.ownerid,
  110. "fstatus": "待对接",
  111. "timdialogid": this.data.timdialogid
  112. }
  113. }).then(res => {
  114. console.log("拒绝合作", res)
  115. })
  116. },
  117. //历史信息查询
  118. query_imdialogMessage() {
  119. _Http.basic({
  120. "accesstoken": wx.getStorageSync('userData').token,
  121. "classname": "system.im.imdialog.imdialog",
  122. "method": "query_imdialogMessage",
  123. "content": {
  124. "pageNumber": this.data.pageNumber,
  125. "pageSize": 20,
  126. "timdialogid": this.data.timdialogid
  127. }
  128. }, false).then(res => {
  129. if (res.msg != '成功') return wx.showToast({
  130. title: res.data,
  131. icon: "none"
  132. })
  133. if (res.pageNumber != 1) {
  134. let list = res.data;
  135. list = list.reverse();
  136. list = list.concat(this.data.socketMsgQueue);
  137. this.setData({
  138. socketMsgQueue: list,
  139. toView: `item${res.data.length - 1}`,
  140. triggered: false
  141. })
  142. } else {
  143. let list = res.data;
  144. list = list.reverse();
  145. this.setData({
  146. socketMsgQueue: list,
  147. toView: `item${res.data.length - 1}`,
  148. pageTotal: res.pageTotal
  149. })
  150. setTimeout(() => {
  151. this.setData({
  152. toView: 'bottom'
  153. })
  154. }, 500)
  155. }
  156. })
  157. },
  158. scrolltoupper() {
  159. if (this.data.pageNumber < this.data.pageTotal) {
  160. this.setData({
  161. pageNumber: this.data.pageNumber + 1,
  162. triggered: true
  163. })
  164. this.query_imdialogMessage();
  165. } else {
  166. this.setData({
  167. triggered: true
  168. })
  169. }
  170. },
  171. //键盘弹出高度
  172. keyboardheightchange(e) {
  173. let {
  174. height
  175. } = e.detail;
  176. if (this.data.iosX && height > 150) height = height - 17;
  177. this.setData({
  178. keyboardHeight: height
  179. })
  180. },
  181. /* 输入框数据绑定 */
  182. sendInput(e) {
  183. this.setData({
  184. sendText: e.detail.value
  185. })
  186. },
  187. /* 发送信息 */
  188. sendMsg(type, data) {
  189. const content = this.data.sendText.trim(),
  190. that = this;
  191. if (type === 'file') {
  192. console.log("发送", data)
  193. app.globalData.SocketTask.send({
  194. data: JSON.stringify({
  195. "classname": "message.message",
  196. "method": "sendFileMessage",
  197. "content": {
  198. "timdialogid": this.data.timdialogid,
  199. "tattachmentid": data[0].tattachmentid
  200. }
  201. }),
  202. success(res) {}
  203. })
  204. } else {
  205. if (content == '') return;
  206. app.globalData.SocketTask.send({
  207. data: JSON.stringify({
  208. "classname": "message.message",
  209. "method": "sendTextMessage",
  210. "content": {
  211. "timdialogid": that.data.timdialogid,
  212. "fmessage": content
  213. }
  214. }),
  215. success(res) {
  216. that.setData({
  217. sendText: ''
  218. })
  219. }
  220. })
  221. }
  222. },
  223. sendMeme(e) {
  224. const {
  225. item
  226. } = e.currentTarget.dataset;
  227. let data = [];
  228. data.push(item);
  229. this.sendMsg('file', data)
  230. },
  231. //功能选择
  232. selectionFunction(e) {
  233. const {
  234. name
  235. } = e.target.dataset,
  236. that = this;
  237. if (name == undefined || name == "") return;
  238. if (name == '表情') {
  239. let keyboardHeight = (this.data.iosX == true) ? 220 : 200;
  240. this.setData({
  241. keyboardHeight,
  242. memePopup: true
  243. })
  244. that.toBotton()
  245. let time1 = setInterval(() => {
  246. this.setData({
  247. keyboardHeight
  248. })
  249. if (this.data.keyboardHeight == keyboardHeight && this.data.memePopup) clearInterval(time1);
  250. }, 100);
  251. } else if (name == '图片') {
  252. this.endMemePopup()
  253. wx.chooseMedia({
  254. count: 1,
  255. mediaType: ['image'],
  256. sourceType: ['album', 'camera'],
  257. maxDuration: 30,
  258. camera: 'back',
  259. success(res) {
  260. let data = {
  261. file: res.tempFiles[0].tempFilePath
  262. };
  263. that.afterRead(data)
  264. }
  265. })
  266. } else if (name == '历史') {
  267. wx.navigateTo({
  268. url: './history?id=' + that.data.timdialogid,
  269. })
  270. }
  271. },
  272. textareaFocus() {
  273. this.setData({
  274. memePopup: false, //表情弹出
  275. })
  276. this.toBotton()
  277. },
  278. //关闭表情
  279. endMemePopup(e) {
  280. this.setData({
  281. keyboardHeight: 0, //键盘高度
  282. memePopup: false, //表情弹出
  283. })
  284. },
  285. /* 文本域行数变化 */
  286. /* linechange(e) {
  287. console.log(e)
  288. const {
  289. lineCount
  290. } = e.detail;
  291. if (lineCount == this.data.lineCount) return;
  292. this.setData({
  293. lineCount
  294. })
  295. }, */
  296. //获取表情包
  297. getMeme() {
  298. _Http.basic({
  299. "accesstoken": wx.getStorageSync('userData').token,
  300. "classname": "system.system.docManage",
  301. "method": "queryDoc",
  302. "content": {
  303. "getdatafromdbanyway": true,
  304. "ownertable": "system",
  305. "ownerid": 0
  306. }
  307. }, false).then(res => {
  308. if (res.msg != '成功') return setTimeout(() => {
  309. this.getMeme();
  310. }, 3000);
  311. this.setData({
  312. memeList: res.data
  313. })
  314. })
  315. },
  316. afterRead({
  317. file
  318. }) {
  319. var that = this
  320. var index = file.lastIndexOf(".");
  321. var ext = file.substr(index + 1);
  322. var timestamp = Date.parse(new Date());
  323. wx.getFileSystemManager().readFile({
  324. filePath: file,
  325. // encoding:'utf-8',
  326. success: result => {
  327. //返回临时文件路径
  328. const fileData = result.data
  329. wx.request({
  330. url: 'https://www.buwanjia.com/bwj/rest/webclientrest/', //仅为示例,并非真实的接口地址
  331. data: {
  332. "accesstoken": wx.getStorageSync('userData').token,
  333. "classname": "system.system.docManage",
  334. "method": "getFileName",
  335. "content": {
  336. "filename": 'wx' + timestamp,
  337. "filetype": ext,
  338. "ownertable": "timdialog",
  339. "ownerid": that.data.timdialogid,
  340. "ftype": "default",
  341. "HttpMethod": 'put'
  342. }
  343. },
  344. method: 'post',
  345. header: {
  346. 'content-type': 'application/json' // 默认值
  347. },
  348. success(res) {
  349. console.log(res)
  350. that.uploadFile(res, fileData)
  351. }
  352. })
  353. },
  354. fail: console.error
  355. })
  356. },
  357. uploadFile(res, data) {
  358. var that = this
  359. wx.request({
  360. url: res.data.data.obsuploadurl,
  361. method: "PUT",
  362. data: data,
  363. header: {
  364. 'content-type': 'application/octet-stream' // 默认值
  365. },
  366. success() {
  367. wx.request({
  368. url: 'https://www.buwanjia.com/bwj/rest/webclientrest/', //仅为示例,并非真实的接口地址
  369. data: {
  370. "accesstoken": wx.getStorageSync('userData').token,
  371. "classname": "system.system.docManage",
  372. "method": "uploadSuccess",
  373. "content": {
  374. "obsfilename": res.data.data.obsfilename
  375. }
  376. },
  377. method: 'post',
  378. header: {
  379. 'content-type': 'application/json' // 默认值
  380. },
  381. success(res) {
  382. let file = res.data.data;
  383. console.log(res)
  384. that.sendMsg('file', file)
  385. }
  386. })
  387. }
  388. })
  389. },
  390. toBotton() {
  391. this.setData({
  392. toView: 'bottom'
  393. })
  394. },
  395. /**
  396. * 生命周期函数--监听页面初次渲染完成
  397. */
  398. onReady: function () {
  399. },
  400. /**
  401. * 生命周期函数--监听页面显示
  402. */
  403. onShow: function () {
  404. var that = this
  405. app.globalData.callback = function (res) {
  406. //res 接收websocket onMessage事件返回的数据
  407. let objs = JSON.parse(res.data),
  408. unreadCount = that.data.unreadCount; /* triggered */
  409. that.data.socketMsgQueue.push(objs)
  410. console.log("发送消息", objs)
  411. that.setData({
  412. socketMsgQueue: that.data.socketMsgQueue
  413. })
  414. that.toBotton()
  415. /* if (wx.getStorageSync('userData').userid == objs.message.sendfrom.userid) {
  416. } else {
  417. that.setData({
  418. socketMsgQueue: that.data.socketMsgQueue
  419. })
  420. } */
  421. /* ,
  422. toView: `item${that.data.socketMsgQueue.length - 1}` */
  423. }
  424. },
  425. /**
  426. * 生命周期函数--监听页面隐藏
  427. */
  428. onHide: function () {
  429. },
  430. /**
  431. * 生命周期函数--监听页面卸载
  432. */
  433. onUnload: function () {
  434. //重置未读信息
  435. _Http.basic({
  436. "accesstoken": wx.getStorageSync('userData').token,
  437. "classname": "system.im.imdialog.imdialog",
  438. "method": "restUnReadMsgCount",
  439. "content": {
  440. "timdialogid": this.data.timdialogid
  441. }
  442. }).then(res => {
  443. console.log(res)
  444. })
  445. },
  446. /**
  447. * 页面相关事件处理函数--监听用户下拉动作
  448. */
  449. onPullDownRefresh: function () {
  450. },
  451. /**
  452. * 页面上拉触底事件的处理函数
  453. */
  454. onReachBottom: function () {
  455. },
  456. /**
  457. * 用户点击右上角分享
  458. */
  459. onShareAppMessage: function () {
  460. }
  461. })