微信小程序自定义输入仿咸鱼发布
效果图
效果图2
主要代码
- .js
// pages/user/release/release.js
import Toast from 'vant-weapp/toast/toast';
Page({
/**
* 页面的初始数据
*/
data: {
category:'品类',
categoryCope:'品类',
value: '',
price:'¥0.00',
show1: false,
show2: false,
freightWay: '0',
freightWayName: '',
priceNumber:"",//价格
startingPriceNumber: "",//入手价
freightNumber: "",//运费
flag1:-1,
flag2: -1,
flag3: -1,
line:1,
columns: ['青春','小说','文学','艺术','动漫/幽默','娱乐时尚','旅游','地图/地理','生活','婚恋/性','育儿/成长','保健/心理健康','体育','自我实现','管理','经济','法律','政治/军事','哲学/宗教','社会科学','文化','历史','传记','少儿','中小学教辅','外语','考试','教材','科普读物','计算机/网络','医学','其他'],
},
//运费计算方法
chooseWay(e) {
if (e.detail==1){
this.setData({
freightWay: e.detail,
freightWayName: '包邮'
})
}
if (e.detail == 2) {
this.setData({
freightWay: e.detail,
freightWayName: '按距离计算'
})
}
},
//选择书籍品类弹出层
choiceCategory(){
this.setData({ show1: true });
},
onClose1() {
this.setData({ show1: false });
},
//点击确定后选择该品类
choiceIt(){
this.setData({
category: this.data.categoryCope,
show1: false
})
},
onChange(e) {
const { picker, value, index } = e.detail;
this.setData({
categoryCope:value
})
},
// 改变价格触发弹出层
changePrice() {
this.setData({ show2: true });
},
onClose2() {
this.setData({ show2: false });
},
// 定义关于自定义键盘输入的计算
InputCalculation:function(line,flag,data,index){
if (index == 11) {
// 关闭弹出层
this.onClose2();
}
else {
if (index <= 8) { index += 1 }
else if (index == 9) { index = "." }
else if (index == 10) { index = "0" }
let length = data.length
data = data.substring(0, flag + 1) + index + data.substring(flag + 1, data.length)
if(line==1){
this.setData({
priceNumber: data,
flag1: flag + 1
})
}
if (line == 2) {
this.setData({
startingPriceNumber: data,
flag2: flag + 1
})
}
if (line == 3) {
this.setData({
freightNumber: data,
flag3: flag + 1
})
}
}
},
//自定义键盘输入
inputNumber(e){
// 价格使用自定义键盘输入并且显示出来
if(this.data.line==1)
{
let index = e.currentTarget.dataset.index
let flag = this.data.flag1
let data = this.data.priceNumber
if (parseInt(data) > 10000) {
Toast('价格最高不超过10万元');
}
else { this.InputCalculation(1, flag, data, index); }
}
// 入手价使用自定义键盘输入并且显示出来
if (this.data.line == 2)
{
let index = e.currentTarget.dataset.index
let flag = this.data.flag2
let data = this.data.startingPriceNumber
if (parseInt(data) > 10000) {
Toast('价格最高不超过10万元');
}
else { this.InputCalculation(2, flag, data, index); }
}
if (this.data.line == 3)
{
// 运费使用自定义键盘输入并且显示出来
let index = e.currentTarget.dataset.index
let flag = this.data.flag3