message.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // pages/message/message.js
  2. const app = getApp()
  3. var host = app.globalData.host;
  4. const $api = require('../../utils/api.js').API;
  5. var timer;
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. focus:false,
  12. scrollTop:0,
  13. template_id:"S7423qLZ0QBhHAI6lfqYwc6CILw5EtADmN3m_5oMnts",
  14. subscribe:false,
  15. disable:false
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. var _this = this
  22. this.getData()
  23. wx.getStorage({
  24. key: this.data.template_id,
  25. success: res=> {
  26. console.log(res,3333333333)
  27. _this.setData({
  28. subscribe: true
  29. })
  30. },
  31. })
  32. },
  33. getData(){
  34. var _this=this;
  35. f()
  36. timer=setInterval(f,10000)
  37. function f(){
  38. $api.getconsult().then(res=>{
  39. _this.setData({
  40. list:res.data.data.list
  41. })
  42. })
  43. }
  44. },
  45. inputchange(e) {
  46. this.setData({
  47. comment: e.detail.value
  48. })
  49. },
  50. subscribe(){
  51. var template_id = this.data.template_id
  52. var that = this;
  53. wx.requestSubscribeMessage({
  54. tmplIds: [template_id],
  55. success (res) {
  56. if(res[template_id]=='accept'){
  57. wx.setStorage({
  58. key: template_id,
  59. data: "accept",
  60. })
  61. that.setData({
  62. subscribe:true
  63. })
  64. that.post()
  65. }
  66. }
  67. })
  68. },
  69. post(){
  70. var data={
  71. cotent:this.data.comment
  72. }
  73. if(!data.cotent){
  74. return
  75. }
  76. this.setData({
  77. disable:true
  78. })
  79. $api.consult(data).then(res=>{
  80. this.getData()
  81. this.setData({
  82. comment:'',
  83. disable: false
  84. })
  85. })
  86. },
  87. /**
  88. * 生命周期函数--监听页面初次渲染完成
  89. */
  90. onReady: function () {
  91. },
  92. /**
  93. * 生命周期函数--监听页面显示
  94. */
  95. onShow: function () {
  96. let that = this;
  97. wx.createSelectorQuery().select("#messageScroll").boundingClientRect(function(rect){
  98. console.log(rect)
  99. wx.pageScrollTo({
  100. scrollTop:rect.height,
  101. duration:100
  102. })
  103. }).exec()
  104. },
  105. /**
  106. * 生命周期函数--监听页面隐藏
  107. */
  108. onHide: function () {
  109. },
  110. /**
  111. * 生命周期函数--监听页面卸载
  112. */
  113. onUnload: function () {
  114. clearInterval(timer)
  115. },
  116. /**
  117. * 页面相关事件处理函数--监听用户下拉动作
  118. */
  119. onPullDownRefresh: function () {
  120. },
  121. /**
  122. * 页面上拉触底事件的处理函数
  123. */
  124. onReachBottom: function () {
  125. },
  126. /**
  127. * 用户点击右上角分享
  128. */
  129. onShareAppMessage: function () {
  130. }
  131. })