dialogbox.js 15 KB

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