dialogbox.js 14 KB

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