// pages/create-order/index.js import http from '../../utils/http' import util from '../../utils/util' import api from '../../utils/api' Page({ /** * 页面的初始数据 */ data: { tabs: ['内部设备调用', '调用设备添加'], tabIndex: 0, work_points: [], pointIndex: -1, id: -1, project: null, remark: '', devices: [], // create/edit type: 'create', order_id: '', loadedOrder: false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // options = { // id: 1, // type: 'edit', // order_id: 194 // } var id = options.id ? options.id : 1 var type = options.type ? options.type : 'create' var order_id = options.order_id ? options.order_id : 195 this.setData({ id, type, order_id }) api.getProject(this) api.getByName(this, 'work-points/get', 'work_points'); if(order_id) { var that = this api.getByName(this, 'orders/detail', 'order', {id: order_id}, function(res) { that.initData() }); wx.setNavigationBarTitle({ title: '修订订单', }) } }, navigate: function(e) { wx.navigateTo({ url: e.currentTarget.dataset.url, }) }, initData: function() { var order = this.data.order, work_points = this.data.work_points, pointIndex = this.data.pointIndex for(var i = 0; i < work_points.length; ++i) { if(work_points[i].id == order.work_point_id) { pointIndex = i; break; } } this.setData({ pointIndex, remark: order.remark, devices: order.inner_devices, loadedOrder: true }) }, submit: function(e) { var type = e.currentTarget.dataset.type var is_draft = type == 'draft' ? 1 : 2 var submit_type = this.data.type if(this.data.pointIndex < 0) { util.error('需求工点必填'); return false; } if(this.data.devices.length <= 0) { util.error('请选择调用设备'); return false; } var work_point = this.data.work_points[this.data.pointIndex] var url = (submit_type == 'create' || type == 're-rent') ? 'orders/createInner' : 'orders/updateInner' wx.setStorageSync('sg-added-devices', []) http({ url: url, data: { id: this.data.order_id, project_id: this.data.id, work_point_id: work_point.id, remark: this.data.remark, devices: this.data.devices, is_draft: is_draft, type: type }, success: function(res) { if(res.code == 0) { util.success('操作成功') } } }) }, switchTab: function(e) { this.setData({ tabIndex: e.currentTarget.dataset.index }) }, onChange: function(e) { var name = e.currentTarget.dataset.name this.setData({ [name]: e.detail.value }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { if(this.data.type == 'create' || this.data.loadedOrder) { var devices = wx.getStorageSync('sg-added-devices') devices = devices ? devices : [] this.setData({ devices }) } }, goAdd: function(e) { var devices = this.data.devices wx.setStorageSync('sg-added-devices', devices) this.navigate(e) }, deleteDevice: function(e) { var index = e.currentTarget.dataset.index var devices = this.data.devices devices.splice(index, 1) this.setData({ devices }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })