outLookContact.js 3.28 KB
import { getUserScheduleInTime } from '../../api/request.js'
import { throttle } from './../../utils/utils.js'
import create from 'dd-store'
import exampleStore from '/stores/exampleStore'
create.Page({
  store: exampleStore,
  useAll: true,
  data: {
    outlookContact: [],
    value: '',
    conflictPeople: [],
    scheduleItem: ''
  },
  onLoad(event) {
    let scheduleItem = event.scheduleItem
    let outlookContact = this.data.outlookContact
    for (let value of this.store.data.participatorList) {
      if (value.platform === 'outlook') {
        outlookContact.push(value)
      }
    }
    this.setData({
      outlookContact: outlookContact,
      scheduleItem: scheduleItem
    })
    this.conflictPeople()
  },
  onShow() {
  },
  // input 的输入 事件
  onInput(event) {
    this.setData({
      value: event.detail.value
    })
  },
  // 添加email
  addEmail() {
    let reg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/
    if (reg.test(this.data.value)) {
      let outlookContact = this.data.outlookContact
      outlookContact.push({
        userId: this.data.value,
        username: this.data.value,
        headUrl: '',
        platform: 'outlook'
      })
      outlookContact = this.setArrary(outlookContact)
      this.setData({
        value: '',
        outlookContact: outlookContact
      })
      this.conflictPeople()
    } else {
      dd.alert({
        content: '请输入正确的邮箱格式',
        buttonText: '确定'
      })
    }
  },
  // 关闭
  close(event) {
    let outlookContact = this.data.outlookContact
    let index = 0
    for (let value of this.data.outlookContact) {
      if (value.userId === event.currentTarget.dataset.item.userId) {
        outlookContact.splice(index, 1)
        this.setData({
          outlookContact: outlookContact
        })
        return
      }
      index++
    }
  },
  // 数组 去重
  setArrary(arr) {
    let containt = {}
    for (let x of arr) {
      containt['id' + x.userId] = x
    }
    return Object.values(containt)
  },
  // 保存
  save() {
    // concat 拼接后生成新的数组 push 返回的是长度 
    this.store.data.participatorList.push(...this.data.outlookContact)
    let participatorList = [{ userId: getApp().globalData.userid, username: getApp().globalData.name, headUrl: getApp().globalData.avatar, platform: 'dingtalk' }].concat(this.store.data.participatorList)
    this.store.data.participatorList = this.setArrary(participatorList)
    this.update()
    dd.navigateBack({
      delta: 1
    })
  },
  // 会议冲突
  conflictPeople() {
    let userIds = []
    for (let value of this.data.outlookContact) {
      userIds.push(value.userId)
    }
    let data = {
      startTime: this.store.data.startTime.replace(/\//g, "-"),
      endTime: this.store.data.endTime.replace(/\//g, "-"),
      userIds: userIds
    }
    getUserScheduleInTime(data).then(res => {
      let conflictPeople = []
      for (let value in res.data.data) {
        if (!!this.data.scheduleItem) {
          if (res.data.data[value].length > 1) {
            conflictPeople.push(value)
          }

        } else {
          if (res.data.data[value].length > 0) {
            conflictPeople.push(value)
          }
        }

      }
      this.setData({
        conflictPeople: conflictPeople
      })
    })
  }
});