define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) { var Controller = { index: function () { var vue = new Vue({ el: '#vue', data: { contentType: 'one', varietiesType: 'one', varietiesList: { one: ['菜籽油', '大豆油'], two: ['稻谷', '小麦', '玉米'], }, contentFields: { "one": { "tankCapacity": "", "tankType": "", "varieties": "", "nature": "", "storageNum": "", "productArea": "", "addr": "", "machiningYear": "", "enterTime": "", "level": "", "color": "", "odorAndTaste": "", "waterVolatileCompounds": "", "insolubleImpurities": "", "acidValue": "", "peroxideValue": "", "heating": "", "residualSolventContent": "", "TankDiameter": "", "oilLoadingHeight": "", "volume": "", "oilVolume": "", "managementForm": "", "custodian": "" }, "two": { "code": "", "warehouseType": "", "capacity": "", "varieties": "", "nature": "", "addr": "", "harvestYear": "", "enterTime": "", "storageNum": "", "level": "", "roughnessRate": "", "impurity": "", "water": "", "volume": "", "decVolume": "", "riceGrains": "", "managementForm": "", "length": "", "width": "", "diam": "", "height": "", "storageMethod": "", "custodian": "" }, "three": { "code": "", "warehouseType": "", "capacity": "", "varieties": "", "nature": "", "addr": "", "harvestYear": "", "enterTime": "", "storageNum": "", "level": "", "weight": "", "impurity": "", "water": "", "volume": "", "decVolume": "", "imperfect": "", "managementForm": "", "length": "", "width": "", "diam": "", "height": "", "storageMethod": "", "custodian": "" } }, contentNewFields: { "one": { "tankCapacity": "", "tankType": "", "varieties": "", "nature": "", "storageNum": "", "productArea": "", "addr": "", "machiningYear": "", "enterTime": "", "level": "", "color": "", "odorAndTaste": "", "waterVolatileCompounds": "", "insolubleImpurities": "", "acidValue": "", "peroxideValue": "", "heating": "", "residualSolventContent": "", "TankDiameter": "", "oilLoadingHeight": "", "volume": "", "oilVolume": "", "managementForm": "", "custodian": "" }, "two": { "code": "", "warehouseType": "", "capacity": "", "varieties": "", "nature": "", "addr": "", "harvestYear": "", "enterTime": "", "storageNum": "", "level": "", "roughnessRate": "", "impurity": "", "water": "", "volume": "", "decVolume": "", "riceGrains": "", "managementForm": "", "length": "", "width": "", "diam": "", "height": "", "storageMethod": "", "custodian": "" }, "three": { "code": "", "warehouseType": "", "capacity": "", "varieties": "", "nature": "", "addr": "", "harvestYear": "", "enterTime": "", "storageNum": "", "level": "", "weight": "", "impurity": "", "water": "", "volume": "", "decVolume": "", "imperfect": "", "managementForm": "", "length": "", "width": "", "diam": "", "height": "", "storageMethod": "", "custodian": "" } }, typeList: [], chinaList: [], activeName: 'one', // 表格当前页数据 tableData: [], // 多选数组 multipleSelection: [], // 搜索条件 key: '', // 默认每页数据量 pageSize: 50, // 当前页码 currentPage: 1, // 默认数据总数 totalCount: 0, // 列表加载遮罩 listLoading: false, formData: {}, addDialog: false, submitLoading: false, title: '', isCustomVariety: false, customVariety: '', // 类型:rapeseed=菜籽油、bean=大豆油、wheat=小麦、paddy=稻谷、corn=玉米 typeOptList: [{ id: '', name: '全部' }, { id: 'rapeseed', name: '菜籽油' }, { id: 'bean', name: '大豆油' }, { id: 'wheat', name: '小麦' }, { id: 'paddy', name: '稻谷' }, { id: 'corn', name: '玉米' }], // 搜索字段 search: { startTime: '', endTime: '', keyword: '', china: [], granary_type: 0, varieties: '全部', type: '', sort: 'id', }, searchOpt: [{ id: 1, value: '粮仓名称' }, { id: 2, value: '仓库储存点' }, { id: 3, value: '仓号' }], numList: [10, 11, 12, 13, 14, 15, 16, 17, 18, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43], // 表单验证规则 rules: { name: [{ required: true, message: '请您输入粮仓名称', trigger: 'blur' }], storage: [{ required: true, message: '请您输入仓库储存点', trigger: 'blur' }], num: [{ required: true, message: '请您选择仓号', trigger: 'blur' }], }, // 时间格式化 dateFormat: function (row, column) { var date = row[column.property]; if (date === undefined || date === 0) { return ''; } return moment(date * 1000).format("YYYY-MM-DD HH:mm:ss"); } }, created() { this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, methods: { // 直达中心库大屏 openCenterScreen() { window.open('/screen/index.html', '_blank'); }, // 直达习水库大屏 openXishuiScreen() { window.open('/screen/xishui.html', '_blank'); }, // 刷新页面 refreshPage() { this.search = { startTime: '', endTime: '', keyword: '', type: '', granary_type: 0, varieties: '全部', china: [], }; this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, // 仓号选择或任意输入 handleSelectNum(e) { if (!e && e !== 0) return; let num = parseInt(e, 10); if (isNaN(num)) return; this.formData.num = num; if (this.title === '添加') { this.formData.granary_type = num > 18 ? 2 : 1; this.changeGranaryType(this.formData.granary_type); } }, // 自由切换仓型(1: 圆型油罐,2: 房型平房仓) changeGranaryType(type) { this.formData.granary_type = type; if (type === 1) { this.varietiesType = 'one'; this.contentType = 'one'; if (this.formData.varieties !== '菜籽油' && this.formData.varieties !== '大豆油' && !this.isCustomVariety) { this.formData.varieties = '菜籽油'; } } else { this.varietiesType = 'two'; if (this.formData.varieties === '菜籽油' || this.formData.varieties === '大豆油') { this.formData.varieties = '稻谷'; this.contentType = 'two'; } else if (this.formData.varieties === '小麦' || this.formData.varieties === '玉米') { this.contentType = 'three'; } else if (!this.isCustomVariety) { this.contentType = 'two'; } } this.contentFields[this.contentType]['varieties'] = this.formData.varieties; }, // 品种切换 varietiesChange(e) { if (e === '自定义') { this.isCustomVariety = true; if (this.customVariety) { this.formData.varieties = this.customVariety; } } else { this.isCustomVariety = false; this.customVariety = ''; this.formData.varieties = e; if (e === '菜籽油' || e === '大豆油') { this.contentType = 'one'; this.formData.granary_type = 1; this.varietiesType = 'one'; } else if (e === '小麦' || e === '玉米') { this.contentType = 'three'; this.formData.granary_type = 2; this.varietiesType = 'two'; } else { this.contentType = 'two'; this.formData.granary_type = 2; this.varietiesType = 'two'; } } this.contentFields[this.contentType]['varieties'] = this.formData.varieties; }, // 自定义品种输入变更 handleCustomVarietyChange(val) { this.formData.varieties = val; if (val && val.indexOf('油') !== -1) { this.contentType = 'one'; this.formData.granary_type = 1; this.varietiesType = 'one'; } else { this.formData.granary_type = 2; this.varietiesType = 'two'; } this.contentFields[this.contentType]['varieties'] = val; }, // 选择图片 selectImage() { Fast.api.open("general/attachment/select?multiple=false", '选择图片', { callback: res => { this.$set(this.formData, 'image', res.url); } }); }, handleClearDate(field) { if (!this.search[field]) { // 重新加载 this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); } }, handleChinaChange(e) { if (e.length === 0) { // 重新加载 this.$set(this.search, 'china', ''); this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); } }, handleTableClick(tab, event) { }, // 添加/编辑 openAddDialog(type, item) { this.addDialog = true; if (type === 'add') { this.title = '添加'; this.isCustomVariety = false; this.customVariety = ''; this.contentFields = JSON.parse(JSON.stringify(this.contentNewFields)); this.formData = { id: 0, name: '', image: '', storage: '成都直属库有限公司本部', num: '', screenPosition: '', granary_type: 1, varieties: '菜籽油', content: [] }; this.contentType = 'one'; this.varietiesType = 'one'; } else { this.title = '编辑'; let isPreset = ['菜籽油', '大豆油', '稻谷', '小麦', '玉米'].indexOf(item.varieties) !== -1; this.isCustomVariety = !isPreset; this.customVariety = isPreset ? '' : item.varieties; this.formData = { id: item.id, name: item.name, image: item.image, storage: item.storage, num: item.num, screenPosition: (item.content || {}).screenPosition || '', old_num: item.num, granary_type: item.granary_type || (item.num > 18 ? 2 : 1), varieties: item.varieties, content: [] }; if (item.varieties === '菜籽油' || item.varieties === '大豆油' || (item.varieties && item.varieties.indexOf('油') !== -1)) { this.contentType = 'one'; } else if (item.varieties === '小麦' || item.varieties === '玉米') { this.contentType = 'three'; } else { this.contentType = 'two'; } if (this.formData.granary_type == 1) { this.varietiesType = 'one'; } else { this.varietiesType = 'two'; } this.contentFields[this.contentType] = JSON.parse(JSON.stringify(item.content || {})); } }, // 查看详情 openDetail(item) { layer.open({ type: 2, title: item.name, shadeClose: false, scrollbar: false, maxmin: true, shade: 0.2, area: ['80%', '60%'], content: 'detail?id=' + item.id }); }, handelTypeRadio(e) { this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, // 从服务器读取数据 loadData(key, currentPage, pageSize, sort) { this.listLoading = true; $.getJSON('granary/index', { keyword: key, type: this.search.type, start_time: this.search.startTime, end_time: this.search.endTime, granary_type: this.search.granary_type, varieties: this.search.varieties, page: currentPage, rows: pageSize, sort: sort, china: this.search.china }, data => { this.totalCount = data.total; this.tableData = data.list; this.chinaList = data.chinaList; this.typeList = data.typeList; this.contentFields = data.contentFields; this.listLoading = false; }); }, submit() { this.$refs.form.validate(valid => { if (!valid) { this._sweetMsg({ type: 'warning', content: '请您完善仓库信息' }); return false; } if (!this.formData.varieties || this.formData.varieties === '自定义') { this._sweetMsg({ type: 'warning', content: '请您输入或选择存储品种' }); return false; } let fields = this.contentFields[this.contentType] || {}; // 智能缺省填充:对未填写的非关键参数自动补全默认占位符 '-',避免粗暴拦截阻断创建 for (let key in fields) { if (fields[key] === '' || fields[key] === null || fields[key] === undefined) { fields[key] = '-'; } } fields['varieties'] = this.formData.varieties; fields['screenPosition'] = this.formData.screenPosition || ''; // 提交 let loading = this.$loading({ lock: true, text: '正在提交中...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.3)' }); const payload = Object.assign({}, this.formData, {content: fields}); delete payload.screenPosition; $.ajax({ url: 'granary/add', type: 'POST', data: payload, success: res => { loading.close(); if (res.code === 200) { this.$message.success(res.msg); this.addDialog = false; this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); } else { this.$message.warning(res.msg); } } }) }); }, // 排序 sortChange(e) { }, // 搜索 searchData() { this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, // 每页显示数据量变更 handleSizeChange(val) { this.pageSize = val; this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, // 页码变更 handleCurrentChange(val) { this.currentPage = val; this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, // 多选响应 handleSelectionChange(val) { this.multipleSelection = val; }, // 批量删除 del() { if (this.multipleSelection.length === 0) { this.$message.warning('请选择至少一条粮仓进行删除!'); return; } var array = []; this.multipleSelection.forEach((row) => { array.push(row.id); }); this.$confirm('您确定要删除所选的粮仓吗?, 是否继续?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { $.ajax({ url: 'granary/dell', data: "id=" + array, type: "POST", dataType: 'json', success: res => { if (res.code === 200) { this.$message.success(res.msg); this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); } else { this.$message.warning(res.msg); } }, error: function (er) { } }); }).catch(() => { }); }, // 单条删除 dell(id) { this.$confirm('您确定要删除此粮仓吗?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { $.ajax({ url: 'granary/dell', data: { id }, type: "POST", dataType: 'json', success: res => { if (res.code === 200) { this.$message.success(res.msg); this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); } else { this.$message.warning(res.msg); } }, error: function (er) { } }); }).catch(() => { }); }, handleClearSelect(field) { // 重新加载 this.$set(this.search, field, ''); this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, handleClearSearchInp() { if (this.search.type) { // 重新加载 this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); } }, // 弹窗提示框 _sweetMsg(obj) { swal({ title: obj.title || '温馨提示', text: obj.content, type: obj.type || 'success', confirmButtonColor: "#7367f0", confirmButtonText: '确定' }); }, // 询问框 _sweetConfirm(obj, confirmCallBack = () => { }, cancelCallBck = () => { }) { swal({ title: obj.title, text: obj.content || '温馨提示', type: 'info', showCancelButton: true, confirmButtonColor: '#7367f0', confirmButtonText: '', cancelButtonText: '取消', closeOnConfirm: false }, function (confirm) { if (confirm) { confirmCallBack(); } else { cancelCallBck(); } }); } } }); }, detail: function () { }, edit: function () { Controller.api.bindevent(); }, api: { bindevent: function () { Form.api.bindevent($("form[role=form]")); } } }; return Controller; });