import WxValidate from '../../utils/WxValidate'; var netUtil = require("../../utils/requestUtil.js"); //require引入 const chooseLocation = requirePlugin('chooseLocation'); const app = getApp(); Page({ /** * 页面的初始数据 */ data: { data: [], areaName: '', longitude: '', latitude: '', address: '', id: '', StatusBar: app.globalData.StatusBar, CustomBar: app.globalData.CustomBar, startTime: '2020-01-01', endTime: '2021-01-01', index: null, multiIndex: [0, 0, 0], time: '12:01', region: ['安徽省', '合肥市', '蜀山区'], status: ['未出租', '出租中'], floornum_array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33], floor_num: 0, apartment_array: ['一室一厅一卫', '两室一厅一卫', '三室一厅一卫', '三室两厅一卫', '三室两厅两卫', '四室一厅一卫', '四室两厅一卫', '四室两厅两卫', '五室一厅一卫', '五室两厅一卫', '五室两厅两卫'], apartment: '三室一厅一卫', imgList: [], photo: '', modalName: null, textareaAValue: '', textareaBValue: '', housingAllocation: [] }, onShow: function() { const location = chooseLocation.getLocation(); // 如果点击确认选点按钮,则返回选点结果对象,否则返回null console.log(location); if (location != null) { this.setData({ areaName: location.name, latitude: location.latitude, longitude: location.longitude, address: location.address }) } }, onReady: function() { const _this = this; wx.getLocation({ type: 'wgs84', success(res) { console.log(res) _this.setData({ latitude: res.latitude, longitude: res.longitude }) } }) }, /** * 获取指定位置 */ getLocation: function() { const key = 'RRXBZ-DW33W-SPER5-R6WUB-GGYRZ-GWBIL'; //使用在腾讯位置服务申请的key const referer = '租户掌柜-微信小程序'; //调用插件的app的名称 const location = JSON.stringify({ latitude: this.data.latitude, longitude: this.data.longitude }); const category = '生活服务,娱乐休闲'; wx.navigateTo({ url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&location=' + location + '&category' + category }); }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { var global_config = wx.getStorageSync('global_config'); //读取后端房源配置信息 this.setData({ housingAllocation: global_config.housingAllocation }) var id = options.id; //判断是否含有id if (id) { this.setData({ id: id }) this.getDetail(id); } //验证 this.initValidate() }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { }, formSubmit: function(e) { const params = e.detail.value // 传入表单数据,调用验证方法 if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0] this.showModal(error) return false } //执行入库 this.insertData(params); }, //获取数据详情 getDetail: function(id) { var self = this; var url = app.globalData.requestUrl + 'index/house/view/' + id; netUtil.sendGet(url) .then((res) => { var result = res.data.data //判断已选中的房源配置项 if (result.house.housing_allocation) { for (let i in self.data.housingAllocation) { if (result.house.housing_allocation.indexOf(i) >= 0) { self.data.housingAllocation[i].checked = true } } } //判断地址选择 var region = [ result.house.province, result.house.city, result.house.area, ] self.setData({ housingAllocation: self.data.housingAllocation, region: region, imgList: result.house.imgList, photo: result.house.photo, startTime: result.house.start_time, endTime: result.house.end_time, floor_num: result.house.floor_num - 1, apartment: result.house.apartment }) this.setData({ data: result }) }) }, //插入用户数据 insertData: function(data) { var self = this; var method_url = self.data.id > 0 ? 'index/house/edit' : 'index/house/add'; var url = app.globalData.requestUrl + method_url; netUtil.sendPost(url, data) .then((res) => { if (res.statusCode == 200 && res.data.code == 200) { wx.showToast({ title: '提交成功', icon: 'success', duration: 2000, success: function() { var global_config = wx.getStorageSync('global_config'); self.setData({ id: '', data: [], startTime: '2020-01-01', endTime: '2021-01-01', imgList: [], photo: '', longitude: '', latitude: '', address: '', housingAllocation: global_config.housingAllocation }) } }) } else { wx.showToast({ title: res.data.msg, icon: 'none', duration: 3000 }) } }) }, showModal: function(e) { console.log(e); wx.showToast({ title: e.msg, icon: 'none', duration: 2000 }) }, //选择楼层 bindfloorNum(e) { this.setData({ floor_num: e.detail.value }) }, //选择户型 bindapartment(e) { this.setData({ apartment: this.data.apartment_array[e.detail.value] }) }, //开始日期 startTimeChange(e) { this.setData({ startTime: e.detail.value }) }, //结束日期 endTimeChange(e) { this.setData({ endTime: e.detail.value }) }, //地域选择 RegionChange: function(e) { this.setData({ region: e.detail.value }) }, //图片选择 ChooseImage() { var self = this; var url = app.globalData.requestUrl + 'index/upload/plupload'; wx.chooseImage({ count: 10, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], //从相册选择 success: (res) => { //上传到服务器 wx.showToast({ title: '正在上传...', icon: 'loading', mask: true, duration: 30000 }) let tempFilePaths = res.tempFilePaths; for (var i in tempFilePaths) { wx.uploadFile({ url: url, filePath: tempFilePaths[i], name: 'file', success: function(res) { //赋值操作 var data = JSON.parse(res.data); self.setData({ imgList: self.data.imgList.length != 0 ? self.data.imgList.concat([data.url]) : [data.url] }) //赋值图片隐藏域 self.setData({ photo: self.data.imgList.join(',') }) }, fail: function(res) { wx.showModal({ title: '错误提示', content: '上传图片失败', showCancel: false, success: function(res) {} }) } }) } wx.hideToast(); } }); }, ViewImage(e) { wx.previewImage({ urls: this.data.imgList, current: e.currentTarget.dataset.url }); }, DelImg(e) { wx.showModal({ title: '图片删除', content: '确定要删除这张图片吗?', cancelText: '再看看', confirmText: '确认', success: res => { if (res.confirm) { this.data.imgList.splice(e.currentTarget.dataset.index, 1); this.setData({ imgList: this.data.imgList, photo: this.data.imgList.join(',') }) } } }) }, initValidate() { // 验证字段的规则 const rules = { name: { required: true, }, door_number: { required: true, }, rent: { required: true, }, apartment: { required: true, }, measure: { required: true, }, water_money: { required: true, }, gas_money: { required: true, }, floor_num: { required: true, }, } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { name: { required: "请先获取小区位置名称", }, door_number: { required: "请输入房屋门牌号", }, rent: { required: "请输入租金", }, apartment: { required: "请输入房屋户型", }, measure: { required: "请输入房屋面积", }, water_money: { required: "请输入水费", }, gas_money: { required: "请输入燃气费", }, floor_num: { required: "请输入小区楼层", }, } // 创建实例对象 this.WxValidate = new WxValidate(rules, messages) }, })