message.js 4.65 KB
// packagePersonal/pages/message/message.js
const api = require('../../restapi.js');
const dateUtil = require('../../../utils/util.js');
const app = getApp()
let timer = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    message: '',
    list: [],
    info: {},
    avatarUrl: '',
    userAvatarUrl: ''
  },
  // 发送
  bindconfirm(e) {
    console.log(e)
    if (e.detail.value) {
      api.addCustomerMessage({
        accountId: this.data.info.accountId,
        messageContent: e.detail.value,
        avatarUrl: app.globalData.userInfo.logo,
        nickName: app.globalData.userInfo.nickName,
        phone: app.globalData.userInfo.phone,
        messageContentNew: e.detail.value,
        messageStatus: 'N',
        messageType: 'TEXT',
        messageTypeNew: 'TEXT'
      }, res => {
        console.log(res)
        if (res.returnCode == 0) {
          let sendObj = {}
          sendObj.sendType = 'C'
          sendObj.messageContent = e.detail.value
          const arr = this.data.list
          arr.push(sendObj);
          console.log(arr);
          this.setData({
            message: '',
            list: arr
          })
          this.getLatestNew()
        } else {
          wx.showToast({
            icon: 'none',
            title: '发送失败,请重新发送',
          })
        }
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '发送信息不可为空',
      })
    }
  },
  // 禁调输入框的表情
  regStrFn: function (str) {
      let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g,
        indexArr = reg.exec(str);
    if (str.match(reg)) {
        str = str.replace(reg, '');
    }
    let obj = { val: str, index: indexArr }
    return obj
  },
  input(e) {
    console.log(e);
    var val = e.detail.value
    var pos = e.detail.cursor
    let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g
    if (reg.test(val)) {
      let obj = this.regStrFn(val);
      if (pos != -1 && obj.index) {
        // 计算光标的位置
        pos = obj.index.index
      }
      val = obj.val
    }
    this.setData({
      message: val
    })
    return{
      value: val,
      cursor: pos
    }
  },
  // 更新客服信息
  updateMessage() {
    api.updateMessage({
      accountId: this.data.info.accountId
    }, res => {
      console.log(res)
    })
  },
  // 获取最新未读消息信息内容
  getCustomerMessageNew() {
    var _this = this
    api.getCustomerMessageNew({
      accountId: this.data.info.accountId,
      sendType: 'B'
    }, res => {
      console.log(res)
      if (res.returnCode == '0') {
        console.log(res.data.length);
        const arr = _this.data.list
        if (res.data.length > 0){
          for(var i in res.data){
            arr.push(res.data[i]);
          }
          _this.setData({
            list: arr
          })
          _this.updateMessage();
        }
      }
    })
    timer = setTimeout(() => {
      this.getCustomerMessageNew()
    }, 3000)
  },
  // 刷新对话框
  getLatestNew() {
    setTimeout(() => {
      this.setData({
        t: 'customerMsg'
      })
    },1000/25)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData)
    var self = this;
    this.setData({
      info: app.globalData.userInfo
    })
    api.getCustomerMessageList({
      accountId: app.globalData.userInfo.accountId
    }, subRes => {
      console.log(subRes);
      if (subRes.data.length != 0) {
        this.setData({
          list: subRes.data
        })
        this.getLatestNew()
        this.updateMessage()
      }
      setTimeout(function () {
        self.getCustomerMessageNew()
      }, 3000);
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('hide');
    if(timer){
      clearTimeout(timer);
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('unLoad');
    if (timer) {
      clearTimeout(timer);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})