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" }, onLoad (options) { this.setData({ groupId: options.id, groupInfo: app.globalData.groupInfo }) this.getInviteList() }, chooseAll (e) { 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.detail.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 => { console.log("邀请好友列表", res) this.setData({ initInviteFriendList: res.data }) let inviteFriendList = [] res.data.forEach(item => { item.checked = 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 }) }).catch(err => { console.log(err) }) }, chooseFriend (e) { console.log(e.detail.alphabet, e.detail.userId) 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 } }) 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 } } } this.setData({ chooseAllValue: chooseAll, couldInvite: chosen }) }, // 邀请好友 确认弹窗 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 }) }).catch(err => { console.log(err) }) }, // 筛选过滤条件 filterOptions () { this.setData({ filterOptionVisible: !this.data.filterOptionVisible }) }, chooseOption (e) { this.setData({ selectedOption: e.currentTarget.dataset.option, filterOptionVisible: false }) } })