// pages/mService/cPracticaloperation/index.js import scheduleservice from '../../../service/scheduleservice'; import $f from '../../../service/baserequest'; import moment from '../../../utils/moment.min'; Component({ properties: {}, data: { icon_calendar: $f.baseIconUrl + 'res/calendar.jpg', // 实操事务 operations: [], alert: '', }, lifetimes: { attached: function () { this.getOperations(); }, }, methods: { // 获取用户实操事务 getOperations() { const that = this; scheduleservice.getOperationRecords({ success(res) { const operations = res.data; operations.forEach(o => { if (o.record) { o.state = o.record.prStateId; o.record.time = moment(o.record.prTime).format( 'YYYY年MM月DD日 HH:mm:ss', ); } else { o.state = o.operation.poStateRangeId.split(';')[0]; } }); that.setData({ operations }); that.refreshAlertTxt(); }, fail(e) { console.log(e); }, }); }, handleSwitchChange(e) { const that = this; const { index } = e.currentTarget.dataset; const s = this.data.operations[index]; const newState = this.nextState(s.operation, s.state); scheduleservice.doOperations( { operationId: s.operation.poId, stateId: newState }, { success(res) { that.setData({ [`operations[${index}].state`]: res.data.prStateId, [`operations[${index}].record.time`]: moment( res.data.prTime, ).format('YYYY年MM月DD日 HH:mm:ss'), }); that.refreshAlertTxt(); }, fail(e) {}, }, ); }, // 设备运行切换至下一个状态 nextState(operation, state) { const stateRange = operation.poStateRangeId.split(';'); let index = stateRange.indexOf(state); index++; if (index >= stateRange.length) { index = 0; } return stateRange[index]; }, refreshAlertTxt() { let alertRange; let maxState; this.data.operations.forEach(o => { if (alertRange == undefined) { alertRange = o.operation.poStateRemindRange.split(';'); } if (maxState) { if (o.state > maxState) { maxState = o.state; } } else { maxState = o.state; } }); if (alertRange && maxState) { this.setData({ alert: alertRange[maxState], }); } }, }, });