message.js 2.7 KB

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