// pages/create-order/index.js import http from '../../utils/http' import util from '../../utils/util' import api from '../../utils/api' Page({ /** * 页面的初始数据 */ data: { tabs: ['维修记录填写', '维修配件添加'], tabIndex: 1, work_points: [], pointIndex: -1, id: -1, project: null, remark: '', inner_devices: [], deviceIndex: -1, parts: [], part_name: '', part_change: '', part_price: "", showAdd: false, order_id: '', // create/edit type: 'create', // create/edit dialog_type: 'create', default_dates: [], showDevice: false, money: 0 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var id = options.id ? options.id : 1 var type = options.type ? options.type : 'create' var order_id = options.order_id ? options.order_id : '' this.setData({ id, type, order_id }) api.getProject(this) api.getByName(this, 'work-points/get', 'work_points'); api.getByName(this, 'inner-devices/get', 'inner_devices', { project_id: id, status: 'using' }); }, switchChecked(e) { var name = e.currentTarget.dataset.name var items = this.data[name] console.log(items) var index = e.currentTarget.dataset.index for (var i = 0; i < items.length; ++i) { items[i].checked = false; } items[index].checked = true; this.setData({ [name]: items, deviceIndex: index }) }, submit: function (e) { // if(this.data.pointIndex < 0) { // util.error('需求工点必填'); // return false; // } if (this.data.deviceIndex < 0) { util.error('维修设备必填'); return false; } if (!this.data.reason) { util.error('维修原因必填'); return false; } if (!this.data.day) { util.error('维修天数必填'); return false; } // var work_point = this.data.work_points[this.data.pointIndex] var device = this.data.inner_devices[this.data.deviceIndex] var url = 'repair-devices/create' http({ url: url, data: { project_id: this.data.id, // work_point_id: work_point.id, inner_device_id: device.id, money: this.data.money, reason: this.data.reason, day: this.data.day, remark: this.data.remark, parts: this.data.parts }, success: function (res) { if (res.code == 0) { util.success('操作成功') setTimeout(function () { wx.navigateBack({ delta: 0, }) }, 1000) } } }) }, switchTab: function (e) { this.setData({ tabIndex: e.currentTarget.dataset.index }) }, delete: function (e) { var parts = this.data.parts var index = e.currentTarget.dataset.index this.setData({ money: (this.data.money - (parts[index].price - 0)).toFixed(2) }) parts.splice(index, 1) this.setData({ parts, }) }, add: function () { if (!this.data.part_name) { util.error('维修部位必填'); return false; } if (!this.data.part_change) { util.error('更换配件必填'); return false; } if (!this.data.part_price) { util.error('配件价格必填'); return false; } var parts = this.data.parts var part = { name: this.data.part_name, change: this.data.part_change, price: this.data.part_price } parts.push(part) this.setData({ parts, showAdd: false, money: this.data.money += (part.price - 0) }) }, cancel: function () { this.setData({ showAdd: false }) }, onChange: function (e) { var name = e.currentTarget.dataset.name var val = e.detail.value this.setData({ [name]: val }) if (name == 'part_price') { let price if (/^(\d?)+(\.\d{0,2})?$/.test(val)) { //正则验证,提现金额小数点后不能大于两位数字 price = val; } else { price = val.substring(0, val.length - 1); } this.setData({ part_price: price }) } if (name == 'pointIndex') { var work_point = this.data.work_points[val] var work_point_id = work_point ? work_point.id : '' var that = this api.getByName(this, 'inner-devices/get', 'inner_devices', { work_point_id: work_point_id }, function () { that.setData({ deviceIndex: -1 }) }); } }, switchShow(e) { var name = e.currentTarget.dataset.name var val = !this.data[name] this.setData({ [name]: val }) }, switchShowAdd: function (e) { var show = e.currentTarget.dataset.show if (show) { this.setData({ part_name: '', part_change: '', part_price: '', }) } this.setData({ showAdd: true, dialog_type: 'create' }) }, switchShowDate: function (e) { this.setData({ showDate: e.currentTarget.dataset.show }) }, confirmDate: function (e) { this.switchShowDate(e) var [start_date, end_date] = e.detail; start_date = util.formatDate(start_date) end_date = util.formatDate(end_date) this.setData({ start_date, end_date }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })