import UserApi from '../../assets/js/http/userApi' Page({ /** * 页面的初始数据 */ data: { type: '', // work工作组 draft起草组 statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'], pageTitle:'联系人', // 是否需要权限 nolimitBool: true, // 选择的用户 selectedPerson: {}, // 选择用户下标 selectedPersonIndex: null, // 当前选择的企业 selectedCompany: {}, // 企业联系人 personList: [{}, {}, {}], // 选择的邮寄联系人 selectedPostPerson: {}, // 当前页的数据 model: {}, }, // 选择联系人 selectPerson(e) { const that = this // 设置操作的下标,onLoad事件将数据放入数组 console.log('操作企业联系人下标', e.currentTarget.dataset.index); this.setData({ selectedPersonIndex: e.currentTarget.dataset.index }) if (!!this.data.selectedCompany.id) { wx.navigateTo({ url: `../personList/personList?companyId=${this.data.selectedCompany.id}&selectType=${e.currentTarget.dataset.selecttype}&nolimit=${that.data.nolimitBool}`, }) } else { wx.showToast({ title: '请先选择参会单位', icon: "none", duration: 800, }); } }, // 添加企业联系人 handleAddContacts () { if (this.data.personList.length >= 10) { wx.showToast({ title: '最多添加十个企业联系人', icon: "none", duration: 800, }); return } this.data.personList.push({}) this.setData({ personList: this.data.personList }) }, // 删除企业联系人 handleDelContacts (e) { const index = e.currentTarget.dataset.index if (this.data.personList.length === 1) { wx.showToast({ title: '最少需要一个企业联系人', icon: "none", duration: 800, }); } else { this.setData({ personList: this.data.personList.filter((_, i) => index !== i) }) } }, // 企业联系人备注 inputContactsRemarks (e) { const index = e.currentTarget.dataset.index this.data.personList[index].remarks = e.detail.value || '' this.setData({ personList: this.data.personList }) }, // 提交 formSubmit(e) { // 企业联系人1必填 if (!this.data.personList[0].contactsId) { wx.showToast({ title: '请选择企业联系人1', icon: 'none', duration: 2000 }) return } // 企业联系人不能重复 const personIds = this.data.personList.filter(item => item.contactsId).map(item => item.contactsId) if (Array.from(new Set(personIds)).length !== personIds.length) { wx.showToast({ title: '请选择不同的联系人', icon: 'none', duration: 2000 }) return } const params = Object.assign({}, this.data.model) // 总备注 params.remarks = e.detail.value.remarks || '' console.log(e, params); wx.showLoading({ title: '加载中', mask: true }) switch(this.data.type) { // 工作组 case 'work': // 联络人列表 params.payWorkingGroupSubItemContactsList = this.data.personList.filter(item => item.contactsId) UserApi.editWorkContactsInfo(params).then(res => { if (res.success) { wx.showToast({ title: res.message, icon: 'loading', duration: 1500, success: () => { this.goBack() } }) } else { wx.showToast({ title: res.message, icon: 'none', duration: 2000 }) } }).catch(e => { wx.showToast({ title: '操作失败', icon: 'error', duration: 2000 }) }) break // 起草组 case 'draft': // 联络人列表 params.payDraftingGroupSubItemContactsList = this.data.personList.filter(item => item.contactsId) UserApi.editDraftContactsInfo(params).then(res => { if (res.success) { wx.showToast({ title: res.message, icon: 'loading', duration: 1500, success: () => { this.goBack() } }) } else { wx.showToast({ title: res.message, icon: 'none', duration: 2000 }) } }).catch(e => { wx.showToast({ title: '操作失败', icon: 'error', duration: 2000 }) }) break } }, goBack(){ wx.navigateBack() }, loadData(){ const that = this wx.showToast({ title: '加载中...', icon: 'loading' }) // 根据不同入口获取不同信息 switch(that.data.type) { // 工作组获取回显信息 case 'work': UserApi.getWorkContactsInfo({ id: that.data._options.id, workingGroupId: that.data._options.workingGroupId }).then(res => { if (res.success) { that.setData({ model: res.result || {}, selectedCompany: { id: res.result.chargeCompanyId, companyName: res.result.chargeCompanyTemporaryName }, personList: res.result.payWorkingGroupSubItemContactsList || [{}, {}, {}] }) } else { wx.showToast({ title: res.message, icon: 'none', duration: 2000 }) } }).catch(err=>{ console.log(err); wx.showToast({ title: '加载失败', icon: 'none', duration: 2000 }) }) break // 起草组获取回显信息 case 'draft': UserApi.getDraftContactsInfo({ id: that.data._options.id, draftingGroupId: that.data._options.draftingGroupId }).then(res => { if (res.success) { that.setData({ model: res.result || {}, selectedCompany: { id: res.result.chargeCompanyId, companyName: res.result.chargeCompanyTemporaryName }, personList: res.result.payDraftingGroupSubItemContactsList || [{}, {}, {}] }) } else { wx.showToast({ title: res.message, icon: 'none', duration: 2000 }) } }).catch(err=>{ console.log(err); wx.showToast({ title: '加载失败', icon: 'none', duration: 2000 }) }) break } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { console.log(options); this.setData({ type: options.type }) // 保存参数 this.data._options = Object.assign({}, options) this.loadData() }, /** * 声明周期函数--监听页面显示 */ onShow: function () { console.log('选择的用户', this.data.selectedPersonIndex, this.data.selectedPerson); if (this.data.selectedPersonIndex !== null && this.data.selectedPersonIndex !== undefined) { const { id, name } = this.data.selectedPerson || {} this.data.personList[this.data.selectedPersonIndex] = { contactsId: id, contactsTemporaryName: name, remarks: '' } // 如果是选邮件联系人 if (this.data.selectedPostPerson.id) { this.setData({ model: Object.assign(this.data.model, { mailContactsId: this.data.selectedPostPerson.id, mailContactsTemporaryName: this.data.selectedPostPerson.name }) }) } this.setData({ selectedPostPerson: {}, selectedPerson: {}, selectedPersonIndex: null, personList: this.data.personList }) } } })