message.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. list:[],
  17. width:0,
  18. height:0,
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. var _this = this
  25. this.getData()
  26. wx.getStorage({
  27. key: this.data.template_id,
  28. success: res=> {
  29. console.log(res,3333333333)
  30. _this.setData({
  31. subscribe: true
  32. })
  33. },
  34. })
  35. },
  36. getData(){
  37. var _this=this;
  38. f()
  39. timer=setInterval(f,10000)
  40. function f(){
  41. $api.getconsult().then(res=>{
  42. var nlist = _this.data.list
  43. _this.setData({
  44. list:res.data.data.list
  45. })
  46. if(_this.data.list.length > nlist.length){
  47. _this.scroll()
  48. }
  49. })
  50. }
  51. },
  52. inputchange(e) {
  53. this.setData({
  54. comment: e.detail.value
  55. })
  56. },
  57. subscribe(){
  58. var template_id = this.data.template_id
  59. var that = this;
  60. wx.requestSubscribeMessage({
  61. tmplIds: [template_id],
  62. success (res) {
  63. if(res[template_id]=='accept'){
  64. wx.setStorage({
  65. key: template_id,
  66. data: "accept",
  67. })
  68. that.setData({
  69. subscribe:true
  70. })
  71. that.post()
  72. }
  73. }
  74. })
  75. },
  76. post(){
  77. var data={
  78. cotent:this.data.comment
  79. }
  80. if(!data.cotent){
  81. return
  82. }
  83. this.setData({
  84. disable:true
  85. })
  86. $api.consult(data).then(res=>{
  87. this.getData()
  88. this.setData({
  89. comment:'',
  90. disable: false
  91. })
  92. })
  93. },
  94. sendImage(url,width,height){
  95. var data={
  96. cotent:url,
  97. width:width,
  98. height:height,
  99. }
  100. if(!data.cotent){
  101. return
  102. }
  103. this.setData({
  104. disable:true
  105. })
  106. $api.consult(data).then(res=>{
  107. this.getData()
  108. this.setData({
  109. comment:'',
  110. disable: false
  111. })
  112. })
  113. },
  114. //预览
  115. preview(e){
  116. console.log(e)
  117. var images = []
  118. images[0] = e.target.dataset.urls
  119. wx.previewImage({
  120. urls: images,
  121. current: 0
  122. })
  123. },
  124. scroll(){
  125. let that = this;
  126. wx.createSelectorQuery().select("#messageScroll").boundingClientRect(function(rect){
  127. wx.pageScrollTo({
  128. scrollTop:rect.height,
  129. duration:100
  130. })
  131. }).exec()
  132. },
  133. uploadImg(){
  134. let _this = this;
  135. wx.chooseImage({
  136. success: res => {
  137. wx.showLoading({
  138. title: '正在上传',
  139. })
  140. const tempFilePaths = res.tempFiles
  141. for (let i = 0; i < tempFilePaths.length; i++) {
  142. $api.uploadAvatar(tempFilePaths[i].path,'file').then(res=>{
  143. wx.hideLoading()
  144. if(res.code == 0){
  145. var url = res.data.url
  146. wx.getImageInfo({
  147. src: tempFilePaths[i].path,
  148. success:function(res){
  149. _this.sendImage(url,res.width,res.height)
  150. }
  151. })
  152. }else{
  153. wx.showToast({
  154. title: '上传失败',
  155. })
  156. }
  157. })
  158. .catch(err=>{
  159. wx.hideLoading()
  160. })
  161. }
  162. }
  163. })
  164. },
  165. /**
  166. * 生命周期函数--监听页面初次渲染完成
  167. */
  168. onReady: function () {
  169. },
  170. /**
  171. * 生命周期函数--监听页面显示
  172. */
  173. onShow: function () {
  174. // let that = this;
  175. // wx.createSelectorQuery().select("#messageScroll").boundingClientRect(function(rect){
  176. // wx.pageScrollTo({
  177. // scrollTop:rect.height,
  178. // duration:100
  179. // })
  180. // }).exec()
  181. },
  182. /**
  183. * 生命周期函数--监听页面隐藏
  184. */
  185. onHide: function () {
  186. },
  187. /**
  188. * 生命周期函数--监听页面卸载
  189. */
  190. onUnload: function () {
  191. clearInterval(timer)
  192. },
  193. /**
  194. * 页面相关事件处理函数--监听用户下拉动作
  195. */
  196. onPullDownRefresh: function () {
  197. },
  198. /**
  199. * 页面上拉触底事件的处理函数
  200. */
  201. onReachBottom: function () {
  202. },
  203. /**
  204. * 用户点击右上角分享
  205. */
  206. onShareAppMessage: function () {
  207. }
  208. })