participantsDetail.js 9.09 KB
import create from 'dd-store'
import pageStore from './store';
import { checkFullScren } from "../../utils/checkFullScren";
import { modifySchedule, addSchedule, getUserScheduleInTime, getHomeUserSchedule } from '../../api/request.js'

// 传给组件 hList 水平排列  所有数据 comHListData
const comHListData = {
  contactData: [
    { id: 'dd', text: '钉钉联系人', imageUrl: '../../assests/dingContact.png' },
    { id: 'outlook', text: '手动添加邮箱', imageUrl: '../../assests/emailContact.png' }
  ]
}

// 传给组件 comPopup 的数据
const comPopupData = {
  popupTitle: '添加参会人',
  popupShow: false
}
create.Page({
  store: pageStore,
  useAll: true,
  data: {
    $data: null,
    scheduleItem: '',
    conflictPeople: [],
    // 登陆当前账户的人 权限
    currentPeople: '',
    comHListData: comHListData,
    comPopupData: comPopupData,
    isIPX: false
  },
  onLoad(event) {
    this.reset()
    if (event.scheduleItem) {
      event.scheduleItem = decodeURIComponent(event.scheduleItem)
      let scheduleItem = JSON.parse(event.scheduleItem)
      this.setData({
        scheduleItem: scheduleItem
      })

      let contactPeople = []
      let contactPeopleId = []
      for (let value of this.$store.data.participatorList) {
        contactPeople.push(value)
        contactPeopleId.push(value.userId)
      }
      this.store.data.contactPeople = contactPeople
      this.store.data.contactPeopleId = contactPeopleId
      this.update()
    }
    this.setData({
      isIPX: checkFullScren(),
      currentPeople: getApp().globalData.userid
    })


  },
  onShow() {
    this.getParticipatorLength()
    this.conflictPeople()
  },
  // 添加参会人 钉钉选人  
  addParticipator() {
    const _that = this
    let require = []
    let select = []
    let outLookConcat = []
    for (let participator of _that.$store.data.participatorList) {
      if (participator.platform === 'outlook') {
        outLookConcat.push(participator)
      }
    }
    // 创建的 可选 必选
    if (!_that.data.scheduleItem) {
      select = _that.$store.data.participatorUserId
      require = [getApp().globalData.userid]
    }
    // 编辑 发起人的可选必选
    else if (this.$store.data.originalData.organizer === getApp().globalData.userid) {
      select = _that.store.data.contactPeopleId
      require = [getApp().globalData.userid]
    }
    // 编辑 参会人可选必选
    else {
      select = _that.store.data.contactPeopleId
      require = [...this.$store.data.originUsersId]
    }
    select = _that.selectedelRequired([...require], [...select]).pickedUsers

    dd.complexChoose({
      title: "选择参会人",            //标题
      multiple: true,            //是否多选
      pickedUsers: select, //已选用户
      requiredUsers: require,            //必选用户(不可取消选中状态)
      responseUserOnly: true,        //返回人,或者返回人和部门
      success: function (res) {
        let people = []
        let peopleId = []
        if (!_that.data.scheduleItem || _that.$store.data.originalData.organizer === getApp().globalData.userid) {
          people.push({ userId: getApp().globalData.userid, username: getApp().globalData.name, headUrl: getApp().globalData.avatar, platform: 'dingtalk' })
          res.users.forEach((item, index) => {
            people.push({ userId: item.userId, username: item.name, headUrl: item.avatar, platform: 'dingtalk' })
          })
        } else {
          _that.$store.data.originUsers.forEach((item, index) => {
            people.push(item)
          })
          res.users.forEach((item, index) => {
            people.push({ userId: item.userId, username: item.name, headUrl: item.avatar, platform: 'dingtalk' })
          })
        }
        people = _that.setArrary(people.concat(outLookConcat))
        for (let value of people) {
          peopleId.push(value.userId)
        }
        people = _that.setArrary(people.concat(outLookConcat))

        // 创建
        if (!_that.data.scheduleItem) {
          _that.$store.data.participatorList = people
          _that.$store.data.participatorUserId = peopleId
          _that.update()
        } else {
          _that.store.data.contactPeople = people
          _that.store.data.contactPeopleId = peopleId
          _that.update()
        }
      },
      fail: function (err) {
      }
    })
  },
  // 删除参会人
  delParticipants(event) {
    let userId = event.target.dataset.userId
    let userIdList = []
    let userList = []

    if (!!this.data.scheduleItem) {
      userList = this.store.data.contactPeople
      userIdList = this.store.data.contactPeopleId
    } else {
      userList = this.$store.data.participatorList
      userIdList = this.$store.data.participatorUserId
    }
    userList.forEach((item, index) => {
      if (userId === item.userId) {
        let people = [...userList]
        people.splice(index, 1)
        if (!!this.data.scheduleItem) {
          this.store.data.contactPeople = people
        } else {
          this.$store.data.participatorList = people
        }
        this.update()
        return
      }
    })
    userIdList.forEach((item, index) => {
      if (userId === item) {
        let peopleId = [...userIdList]
        peopleId.splice(index, 1)
        if (!!this.data.scheduleItem) {
          this.store.data.contactPeopleId = peopleId
        } else {
          this.$store.data.participatorUserId = peopleId
        }
        this.update()
        return
      }
    })
    this.getParticipatorLength()
    this.conflictPeople()
  },
  // 编辑时候的保存
  editSave() {
    if (this.$store.data.originUsers != this.store.data.contactPeople) {
      this.$store.data.updateInfo.isUpate = true
      this.$store.data.updateInfo.updateType = 'operate_user'
      this.$store.data.participatorList = this.store.data.contactPeople
      this.$store.data.participatorUserId = this.store.data.contactPeopleId
      this.update()
    }
    dd.navigateBack({
      delta: 1
    })
  },

  // 保存信息
  saveInfo() {
    this.editSave()
  },
  // 获取参会人长度
  getParticipatorLength() {
    const length = !!this.data.scheduleItem ? this.store.data.contactPeople.length : this.$store.data.participatorList.length
    dd.setNavigationBar({
      title: `参会人(${length}人)`
    })


  },
  // 重置参会人
  reset() {
    this.store.data.contactPeople = []
    this.store.data.contactPeopleId = []
    this.update()
  },
  // 展示弹窗
  showPopup() {
    this.setData({
      'comPopupData.popupShow': true
    })
  },
  // 隐藏弹窗
  closePopup() {
    this.setData({
      'comPopupData.popupShow': false
    })
  },
  // 选择添加人员的方式
  selectComHList(event) {
    switch (event.currentTarget.dataset.item.id) {
      // 选择dd加人
      case "dd":
        this.setData({
          'comPopupData.popupShow': false
        })
        this.addParticipator()
        break;

      // 选择通过outlook添加邮箱联系人
      case "outlook":
        this.setData({
          'comPopupData.popupShow': false
        })
        dd.navigateTo({ url: `./../outLookContact/outLookContact?scheduleItem=${encodeURIComponent(JSON.stringify(this.data.scheduleItem))}&toPage=participantsDetail` })
        break;
    }
  },

  // 选择弹窗
  onSelectPopup(event) {
    this.setData({
      'conSelectPopupData.showSelectPopup': false,
      'conSelectPopupData.selectPopupId': event.currentTarget.dataset.item.id,
      'centerPopup.showCenterPopup': true
    })
  },
  // 数组去重
  setArrary(arr, userId) {
    console.log(arr)
    let containt = {}
    for (let x of arr) {
      containt['id' + x.userId] = x
    }
    return Object.values(containt)
  },
  // 会议冲突
  conflictPeople() {
    let users = []
    let userIds = []
    if (!!this.data.scheduleItem) {
      users = this.store.data.contactPeople
    } else {
      users = this.$store.data.participatorList
    }
    for (let item of users) {
      userIds.push(item.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) {
          const arr = res.data.data[value].filter(function (item) {
            return item.confirmAttendance === 1
          })
          if (arr.length > 1) {
            conflictPeople.push(value)
          }
        } else {
          const arr = res.data.data[value].filter(function (item) {
            return item.confirmAttendance === 1
          })
          if (arr.length > 0) {
            conflictPeople.push(value)
          }
        }
      }
      this.setData({
        conflictPeople: conflictPeople
      })
    })
  },
  // 已选用户去中去掉必选用户
  selectedelRequired([...req], [...pic]) {
    for (let i = 0; i < req.length; i++) {
      for (let m = 0; m < pic.length; m++) {
        if (req[i] == pic[m]) {
          pic.splice(m, 1)
          m--;
        }
      }
    }
    return {
      requiredUsers: req,
      pickedUsers: pic
    }
  }

});