define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) { var Controller = { index: function () { let vue = new Vue({ el: '#vue', data: { typeName: '', detail: { type: '', granary: { name: '' } }, detailDialog: false, granaryList: [], typeList: { rapeseed: { name: '菜籽油', content: [] } }, newTypeList: [], // 类型:rapeseed=菜籽油、bean=大豆油、wheat=小麦、paddy=稻谷、corn=玉米 typeOptList: [{ id: '', name: '全部' }, { id: 'rapeseed', name: '菜籽油' }, { id: 'bean', name: '大豆油' }, { id: 'wheat', name: '小麦' }, { id: 'paddy', name: '稻谷' }, { id: 'corn', name: '玉米' }], loadingText: '拼命加载中...', formLoading: false, // 添加编辑 title: '', addDialog: false, // 表格当前页数据 tableData: [], // 多选数组 multipleSelection: [], // 搜索条件 key: '', // 默认每页数据量 pageSize: 20, // 当前页码 currentPage: 1, // 默认数据总数 totalCount: 0, // 列表加载遮罩 listLoading: false, // 搜索字段 search: { startTime: '', endTime: '', keyword: '', type: '', gid: '', date: '', season: 0, sort: 'id', }, // 字段信息 formData: { id: 0, type: 'rapeseed', record_date: '', season: '', content: [] }, searchOpt: [{ id: 1, value: '老师姓名' }, { id: 2, value: '手机号码' }, { id: 3, value: '用户名' }], // 表单验证规则 rules: { gid: [{ required: true, message: '请您选择所属仓库', trigger: 'blur' }], record_date: [{ 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: { isNumericRule(item) { return [item.warning, item.alert].every(value => value !== null && value !== undefined && String(value).trim() !== '' && Number.isFinite(Number(value))); }, ruleDirection(item) { return item.direction === 'upper' || item.direction === 'lower' ? item.direction : (this.isNumericRule(item) && Number(item.warning) > Number(item.alert) ? 'lower' : 'upper'); }, rulePreference(item) { return this.ruleDirection(item) === 'lower' ? 'higher' : 'lower'; }, setRuleDirection(item, direction) { if (direction !== 'upper' && direction !== 'lower') return; this.$set(item, 'direction', direction); }, toggleRule(item) { this.setRuleDirection(item, this.ruleDirection(item) === 'upper' ? 'lower' : 'upper'); }, ruleRanges(item) { const w = item.warning, a = item.alert; return this.ruleDirection(item) === 'upper' ? [`当前值 ≤ ${w}`, `${w} < 当前值 ≤ ${a}`, `当前值 > ${a}`] : [`当前值 ≥ ${w}`, `${a} ≤ 当前值 < ${w}`, `当前值 < ${a}`]; }, ruleError(item) { if (!this.isNumericRule(item)) return ''; const upper = this.ruleDirection(item) === 'upper'; return (upper && Number(item.warning) > Number(item.alert)) || (!upper && Number(item.warning) < Number(item.alert)) ? (upper ? '越小越好指标要求:预警值 ≤ 警告值(高值预警)' : '越大越好指标要求:预警值 ≥ 警告值(低值预警)') : ''; }, // 刷新页面 refreshPage() { this.search = { startTime: '', endTime: '', keyword: '', type: '', gid: '', date: '', season: 0, sort: 'id', }; this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, handleGranaryChange(e) { let list = this.granaryList; let item = list.find(v => v.id === e); this.typeName = item.varieties; this.formData.type = item.type; }, handelIsNumber(index, val) { this.typeList[this.formData.type]['content'][index].is_number = val; }, openDialogDetail(detail) { this.detail = detail; this.detailDialog = true; }, handleClearSelect(field) { // 重新加载 this.$set(this.search, field, ''); this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, handleClearDate(field) { if (!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); } }, // 提交 submit() { this.$refs.form.validate(valid => { if (!valid) { return false; } let formData = this.formData; let content = this.typeList[formData.type]['content']; let name = ''; // 参数验证 for (let item of content) { name = item.name; const error = this.ruleError(item); if (error) { this._sweetMsg({ type: 'warning', content: `${name}:${error}` }); return false; } this.$set(item, 'direction', this.ruleDirection(item)); if (item.val === '' || item.val === null || item.val === undefined) { this._sweetMsg({ type: 'warning', content: `请您输入${name}值` }); return false; } if (item.warning === '') { this._sweetMsg({ type: 'warning', content: `请您输入${name}预警值` }); return false; } if (item.alert === '') { this._sweetMsg({ type: 'warning', content: `请您输入${name}警告值` }); return false; } } formData.content = content; // 提交 let loading = this.$loading({ lock: true, text: '正在提交中...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.3)' }); $.ajax({ url: 'testing/add', type: 'POST', data: formData, 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); } } }) }); }, // 添加/编辑 openAddDialog(type, item) { this.addDialog = true; this.typeList = JSON.parse(JSON.stringify(this.newTypeList)); if (type === 'add') { this.title = '添加'; this.formData = { id: 0, gid: '', type: 'rapeseed', record_date: '', season: 1, sort: '', content: [] }; } else { this.title = '编辑'; this.formData = { id: item.id, gid: item.gid, type: item.type, season: item.season, record_date: item.year + '-' + item.month, sort: item.sort, content: [] }; this.typeName = item.granary.varieties; this.typeList[item.type]['content'] = JSON.parse(JSON.stringify(item.testing_params)); } }, handleSort(item) { // 提交 let loading = this.$loading({ lock: true, text: '正在更改中...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.3)' }); $.ajax({ url: 'testing/sort', type: 'POST', data: { id: item.id, sort: item.sort }, success: res => { loading.close(); 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); } } }) }, // 从服务器读取数据 loadData(key, currentPage, pageSize, sort) { this.listLoading = true; $.getJSON('testing/index', { keyword: key, type: this.search.type, gid: this.search.gid, date: this.search.date, start_time: this.search.startTime, end_time: this.search.endTime, season: this.search.season, page: currentPage, rows: pageSize, sort: sort, }, data => { this.totalCount = data.total; this.tableData = data.list; this.granaryList = data.granaryList; this.typeList = data.typeList; this.newTypeList = data.typeList; this.listLoading = false; }); }, // 排序 sortChange(e) { }, // 搜索 searchData() { this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, handelTypeRadio(e) { this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort); }, handelSeasonRadio(e) { 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(() => { let loading = this.$loading({ lock: true, text: '正在删除中..', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.3)' }); $.ajax({ url: 'testing/dell', data: "id=" + array, type: "POST", dataType: 'json', success: res => { loading.close(); 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(() => { let loading = this.$loading({ lock: true, text: '正在删除中..', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.3)' }); $.ajax({ url: 'testing/dell', data: { id }, type: "POST", dataType: 'json', success: res => { loading.close(); 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(() => { }); }, // 弹窗提示框 _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(); } }); } } }); }, add: function () { Controller.api.bindevent(); }, edit: function () { Controller.api.bindevent(); }, api: { bindevent: function () { Form.api.bindevent($("form[role=form]")); } } }; return Controller; });