业务场景: 开单是多个销售参与开单的,但是每个人贡献不一致,所以分摊的业绩比例不一致,总业绩比为100%。
//点击按钮,弹窗
<image bindtap="handleAddsales" src="/images/add.png" style="width: 50rpx;height: 50rpx;margin-left: 10rpx;"></image>
//弹窗
<view bindtap="handleClose" wx:if="{{showMoresales}}" style="position:fixed;left:0;top:0;z-index:1000;background:#000;opacity:0.7;width:100%;height:1200px;">
</view>
<view wx:if="{{showMoresales}}" style="position:fixed;z-index:1100;left: 0;bottom: 0;">
<view style="padding:20rpx; background:#FFF;width:710rpx;border-radius:16rpx 16rpx 0 0;height: 700rpx;">
<view style="width: 100%;text-align: center;line-height: 80rpx;">添加</view>
<view style="width: 100%;height: 450rpx;overflow-y: scroll;">
<block wx:for="{{fusales}}" wx:key="index">
<view style="display: flex;margin-bottom: 20rpx;justify-content: space-between;">
<view style="display: flex;width: 300rpx;">
<view style="width: 120rpx; line-height: 60rpx;">{{item.salestitle}}:</view>
<picker style="width: 180rpx; padding: 0 0 0 10rpx;height: 60rpx;line-height: 60rpx;border: 1rpx solid #ddd;" bindchange="bindPickerChange11" data-index="{{index}}" value="{{item.salesIndex}}" range="{{item.salesType}}">
<view wx:if="{{item.salesIndex < 0}}" class="picker" style="font-size: 28rpx;color: #999;position: relative;">
请选择
<image src="/images/down.png" style="width: 30rpx;height:30rpx;position: absolute;right: 0rpx;top: 10rpx;"></image>
</view>
<view wx:else class="picker" style="color: #333;position: relative;">
{{item.salesType[item.salesIndex]}}
<image src="/images/down.png" style="width: 30rpx;height:30rpx;position: absolute;right: 0rpx;top: 10rpx;"></image>
</view>
</picker>
</view>
<view style="display: flex;width: 280rpx;">
<view style="width: 160rpx;line-height: 60rpx;">业绩占比:</view>
<input bindinput="handleValue2" data-id="{{index}}" style="border:1rpx solid #ddd; width: 150rpx;margin-left:6rpx;height: 50rpx;line-height:50rpx;text-align: center;" value="{{item.yejibi}}" type="text"></input>
<view style="line-height: 50rpx;margin-left: 10rpx;">%</view>
</view>
<view wx:if="{{index == 0}}" style="width: 80rpx;height: 60rpx;text-align: center; line-height: 60rpx;border: 1rpx solid #ddd;color: #ddd;">删除</view>
<view wx:else bindtap="handleDeletesales" data-index="{{index}}" style="width: 80rpx;height: 60rpx;text-align: center; line-height: 60rpx;border: 1rpx solid #ddd;">删除</view>
</view>
</block>
</view>
<view bindtap="handleAddline" style="text-align: center;">
<image src="/images/add.png" style="width: 50rpx;height: 50rpx;position: fixed;bottom: 120rpx;margin-left: -25rpx;"></image>
</view>
<view bindtap="handleComplete" class="click-btn" hover-class="click-btn-hover" style="width:708rpx;height: 60rpx;line-height: 60rpx;text-align: center;color: white;border-radius: 16rpx; position: fixed;bottom: 30rpx;">确定</view>
</view>
</view>
JS
Data:
fusales: [
{
salestitle:'主销售',
salesType:["公司单", "钟萍", "张生", "谢英",] ,
salelist:[ {id: "0", salename: "公司单"},{id: "88", salename: "钟萍"},{id: "125", salename: "张生"},{id: "249", salename: "谢英"}] ,
salesIndex:-1
}, {
salestitle:'辅销售',
salesType:["公司单", "钟萍", "张生", "谢英",] ,
salelist:[ {id: "0", salename: "公司单"},{id: "88", salename: "钟萍"},{id: "125", salename: "张生"},{id: "249", salename: "谢英"}] ,
salesIndex:-1
}
]
//当有超过2个销售时,点击+号按钮,添加下一个辅销售,也就是数组追加一条数据。
handleAddline() {
let arr = []
let obj = {}
obj.salesType = this.data.fusales[1].salesType
obj.salelist = this.data.fusales[1].salelist
obj.salesIndex = -1
obj.salestitle = '辅销售'
arr.push(obj)
// let newarray = this.appendSameDataToArray(arr)
// console.log(newarray)
this.setData({
fusales: this.data.fusales.concat(arr)
})
},
//下拉选择销售,(如果身份是销售,那么主销售只能选择自己,如果身份是店长,跟单,那么主销售可以是任意人。 所以onload的时候,要对fusales数据进行过滤。这部分代码依赖基础数据,就不展示了。)
bindPickerChange11(e) {
console.log(this.data.fusales, e, this.data.lotsales)
// wx.getStorageSync('userInfo').job == '销售' ?
let index = e.currentTarget.dataset.index
let item = this.data.fusales[index].salelist[e.detail.value].salename
let id = this.data.fusales[index].salelist[e.detail.value].id
this.data.fusales[index].salesIndex = e.detail.value
if (index != this.data.lotsales.length) {
this.data.lotsales[index].name = item
} else {
let arr = []
let obj = {}
obj.name = item
obj.id = id
arr.push(obj)
this.setData({
lotsales: this.data.lotsales.concat(arr)
})
}
console.log(this.data.lotsales)
this.setData({
fusales: this.data.fusales,
})
},
// 填写对应销售业绩比
handleValue2(e) {
console.log(e, this.data.lotsales)
let index = e.currentTarget.dataset.id
// this.data.lotsales[index].yejibi = e.detail.value
this.data.fusales[index].yejibi = e.detail.value
if (index != this.data.lotsales.length) {
this.data.lotsales[index].yejibi = e.detail.value
} else {
let arr = []
let obj = {}
obj.yejibi = e.detail.value
arr.push(obj)
this.setData({
lotsales: this.data.lotsales.concat(arr)
})
}
this.setData({
fusales: this.data.fusales
})
},
//保存
//判断销售名是否重复
hasDuplicateNames(objects) {
const names = new Set(); // 使用Set来存储已经出现过的名字
for (const obj of objects) {
if (names.has(obj.name)) {
// 如果名字已经在Set中存在,说明有重复,返回false
return false;
}
names.add(obj.name); // 否则,将名字添加到Set中
}
// 如果循环结束都没有返回false,说明没有重复名字,返回true
return true;
},
handleComplete() {
let sum = 0
for (let i = 0; i < this.data.lotsales.length; i++) {
sum += Number(this.data.lotsales[i].yejibi)
if (Number(this.data.lotsales[i].yejibi) == 0) {
wx.showToast({
title: '业绩占比不能为0',
duration: 3000,
icon: 'none'
})
return
}
}
console.log(sum)
if (sum != 100) {
wx.showToast({
title: '业绩总和应为100%',
duration: 3000,
icon: 'none'
})
return
} else {
for (let j = 0; j < this.data.lotsales.length; j++) {
if (!this.data.lotsales[j].name) {
wx.showToast({
title: '请选择销售',
duration: 3000,
icon: 'error'
})
return
}
}
if (this.hasDuplicateNames(this.data.lotsales)) {
let formattedArr = this.data.lotsales.map(function (item) {
// 将yejibi值转换为百分比字符串,并添加百分号
var percentage = (item.yejibi / 100) * 100 + '%'; // 假设yejibi是以100为基准的整数百分比
return item.name + percentage; // 组合名字和百分比字符串
});
// 使用join函数将格式化后的数组转换为单个字符串,元素之间用逗号分隔
var resultString = formattedArr.join(',');
this.data.fusales.splice(2)
this.setData({
fusales: this.data.fusales,
showMoresales: false,
lotvalue: resultString,
showMoresales2: true
})
} else {
wx.showToast({
title: '销售不能重复',
duration: 3000,
icon: 'none'
})
return
}
}
},
//删除辅销售
handleDeletesales(e) {
console.log(e) //要删除的辅销售下标
let index = e.currentTarget.dataset.index
this.data.fusales.splice(index, 1)
this.data.lotsales.splice(index, 1)
this.setData({
fusales: this.data.fusales,
lotsales: this.data.lotsales
})
},