// pages/calculator/index.js // import { imageUrl } from '../../utils/config' // import { calculatorOption, calculatorSave, decodePhone, bindPhone } from '../../api/index' // import Ls from '../../utils/storage' // let ls = new Ls Page({ /** * 页面的初始数据 */ data: { // background: imageUrl + 'calculator/background.jpg', userPhone: '', type: '', info: '', area: '', money: '', typeArray: ['精装', '毛坯'], infoArray: [ '一室一厅', '两室一厅', '两室两厅', '三室两厅', '四室两厅', '五室两厅及以上' ], calculatorOptions: [], showPopup: false, activePicker: '', pickerIndex: 0, // 展示计算结果 showResultMoney: false, containerContentStyle: 'hidden;' }, // 返回上一页 navigateBack () { wx.navigateBack({ delta: 1, }) }, // picker 选择 onChange (e) { this.setData({ pickerIndex: e.detail.index }) }, // input 输入 inputTyping (e) { this.setData({ [e.currentTarget.dataset.index]: e.detail.value }) }, // 选择房屋类型、户型信息 toSelectType (e) { let index = e.currentTarget.dataset.index let { activePicker } = this.data if ( activePicker === index) { this.setData({ showPopup: true }) } else { this.setData({ pickerIndex: 0, activePicker: index, showPopup: true }) } }, // 取消选择 cancelPicker () { this.setData({ showPopup: false }) }, // 确认选择 confirmPicker () { let { activePicker, pickerIndex } = this.data this.setData({ [activePicker]: pickerIndex, showPopup: false }) }, // 获取用户手机号 getPhoneNumber (e) { // console.log(e) let that = this decodePhone({ code: e.detail.code }) .then(res => { if (res.phone === undefined || res.phone.length === null || !res.phone) { wx.showToast({ title: res.msg, icon: 'none' }) } else { bindPhone({ phone: res.phone }) .then(newInfo => { ls.set('userInfo', newInfo.member) that.setData({ userPhone: res.phone }) that.submit() }) } }) }, // 立即计算 submit () { // console.log('立即计算') let { userPhone } = this.data ls.get('userInfo').then(res => { // console.log(res) if (userPhone) { let { type, info, area, money, typeArray, infoArray, calculatorOptions: options } = this.data let submitData = {} submitData.name = res.name submitData.phone = res.phone if (type === '') { wx.showToast({ title: '请选择房屋类型', icon: 'none' }) return false } if (info === '') { wx.showToast({ title: '请选择户型信息', icon: 'none' }) return false } if (area === '') { wx.showToast({ title: '请输入房屋面积', icon: 'none' }) return false } if (money === '') { wx.showToast({ title: '请输入装修预算', icon: 'none' }) return false } submitData = ['房屋类型:' + typeArray[type], '户型信息:' + infoArray[info], '房屋面积:' + area + '㎡', '装修预算:' + money + '万元'].join('-') for (let i in options) { options[i].money = (money * options[i].rate / 100).toFixed(2) let childList = options[i].childList for(let j in childList) { childList[j].money = (money * childList[j].rate / 100).toFixed(2) } } this.setData({ calculatorOptions: options, showResultMoney: true, containerContentStyle: 'auto' }) this.submitCalculatorResult(submitData) } }) }, // 提交计算结果 submitCalculatorResult (data) { calculatorSave({ info: data }).then(res => { // console.log(res) if (res.success) { wx.showToast({ title: '计算成功', icon: 'none' }) } else { console.log(res.msg) } }) }, // 获取计算器配置 getCalculatorOption () { calculatorOption().then(res => { this.setData({ calculatorOptions: res.data }) }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { let that = this // this.getCalculatorOption() // ls.get('userInfo').then(res => { // if (res.phone) { // that.setData({ userPhone: res.phone }) // } // }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })