import { getInviteList, addMember } from '../../utils/api' import convertPY from '../../utils/convertPY' const app = getApp() Page({ data: { chooseAllValue: false, dialogVisible: false, searchValue: '', groupId: '', inviteFriendList: [], initInviteFriendList: [], couldInvite: false, groupInfo: { groupAvatar: '', groupLeader: '', groupName:'', groupId: '' }, filterOptionVisible: false, selectedOption: 'lastName', loadMoreVisible:true, loadMoreType:'loading', checkedList:[] }, onLoad (options) { this.setData({ groupId: options.id, groupInfo: app.globalData.groupInfo }) this.getInviteList() }, // 清除选择 chooseAll (e) { this.setData({ checkedList:[] }) if (this.data.inviteFriendList.length === 0) { wx.showToast({ title: '暂无可邀请好友', icon: 'none', duration: 2000 }) this.setData({ chooseAllValue: false }) } else { const inviteFriendList = [...this.data.inviteFriendList] for(let i = 0; i < inviteFriendList.length; i ++) { for(let j = 0; j < inviteFriendList[i].memberList.length; j ++) { // if (e.detail.value.length > 0) { // inviteFriendList[i].memberList[j].checked = true // } else { inviteFriendList[i].memberList[j].checked = false // } } } this.setData({ inviteFriendList, couldInvite: e.currentTarget.dataset.value.length > 0 }) } }, inviteFriend () { this.setData({ dialogVisible: true }) }, parentVisibleValue (e) { this.setData({ dialogVisible: e.detail.visibleValue }) }, getInviteList (e = {}) { let value = '' if ('detail' in e) { value = e.detail.value } const para = { groupId: this.data.groupId, mobile: this.data.selectedOption === 'mobile' ? value : '', road: this.data.selectedOption === 'road' ? value : '', lastName: this.data.selectedOption === 'lastName' ? value : '' } getInviteList(para).then(res => { this.setData({ initInviteFriendList: res.data }) let inviteFriendList = [] res.data.forEach(item => { item.checked = false item.disabled=false const alphabet = convertPY.convertPYs(item.nickname) const index = inviteFriendList.findIndex(item => item.alphabet === alphabet) if (index > -1) { inviteFriendList[index].memberList.push(item) } else { inviteFriendList.push({ alphabet: alphabet, memberList: [item] }) } }) inviteFriendList.sort((a,b) => a.alphabet.charCodeAt() - b.alphabet.charCodeAt()) this.setData({ inviteFriendList, loadMoreVisible:false }) if(res.data.length==0){ this.setData({ loadMoreVisible:true, loadMoreType:'none' }) } }).catch(err => { console.log(err) this.setData({ loadMoreVisible:false }) }) }, chooseFriend (e) { const index = this.data.inviteFriendList.findIndex(item => item.alphabet === e.detail.alphabet) this.data.inviteFriendList[index].memberList.forEach(item => { if (item.userId === e.detail.userId) { item.checked = !item.checked } }) this.setData({ inviteFriendList:this.data.inviteFriendList }) let chosen = false let chooseAll = true for(let i = 0; i < this.data.inviteFriendList.length; i ++) { for(let j = 0; j < this.data.inviteFriendList[i].memberList.length; j ++) { if (this.data.inviteFriendList[i].memberList[j].checked) { chosen = true break } } } for(let i = 0; i < this.data.inviteFriendList.length; i ++) { for(let j = 0; j < this.data.inviteFriendList[i].memberList.length; j ++) { if (!this.data.inviteFriendList[i].memberList[j].checked) { chooseAll = false } } } let checkedList = [] this.data.inviteFriendList.forEach((element,index)=>{ element.memberList.forEach((item,itemIndex)=>{ if(item.checked){ checkedList.push(item) } }) }) this.setData({ checkedList, chooseAllValue: chooseAll, couldInvite: chosen }) if(this.data.checkedList.length>10){ const Index = this.data.inviteFriendList.findIndex(item => item.alphabet === e.detail.alphabet) wx.showModal({ title: '温馨提示', content: '一次最多只能选择10个好友', }) this.data.inviteFriendList[Index].memberList.forEach((element,index) => { if (element.userId === e.detail.userId) { this.setData({ [`inviteFriendList[${Index}].memberList[${index}].checked`]:false }) } }) } }, // 邀请好友 确认弹窗 inviteFriendComfirm () { const members = [] for(let i = 0; i < this.data.inviteFriendList.length; i ++) { for(let j = 0; j < this.data.inviteFriendList[i].memberList.length; j ++) { if (this.data.inviteFriendList[i].memberList[j].checked) { members.push({ userId: this.data.inviteFriendList[i].memberList[j].userId, userAvatar: this.data.inviteFriendList[i].memberList[j].userAvatar, nickname: this.data.inviteFriendList[i].memberList[j].nickname, partyMember: this.data.inviteFriendList[i].memberList[j].partyMember, mobile: this.data.inviteFriendList[i].memberList[j].mobile }) } } } const para = { groupId: this.data.groupId, members } wx.showLoading({ title: '加载中' }) addMember(para).then(res => { wx.hideLoading() console.log(res) wx.showToast({ title: '邀请好友成功', icon :'none', duration: 2000 }) const pages = getCurrentPages() const page = pages[pages.length - 2] page.getAssociationDetail() this.getInviteList() this.setData({ chooseAllValue: false, couldInvite: false }) setTimeout(() => { wx.navigateBack() },1000) }).catch(err => { console.log(err) }) }, // 筛选过滤条件 filterOptions () { this.setData({ filterOptionVisible: !this.data.filterOptionVisible }) }, chooseOption (e) { this.setData({ selectedOption: e.currentTarget.dataset.option, filterOptionVisible: false }) } })