/** * 门店巡检与考核模块通用工具方法。 */ import querystring from 'querystring' /** 发送 POST 表单请求,返回 res.data */ export function post(axios, url, data, config) { return axios.post(url, querystring.stringify(data || {}), config).then(res => res.data) } /** 是否成功(rtnCode === 0) */ export function ok(res) { return !!(res && res.rtnBean && String(res.rtnBean.rtnCode) === '0') } /** 后端返回的提示信息 */ export function msg(res) { return (res && res.rtnBean && res.rtnBean.rtnMsg) || '' } /** 分页对象 */ export function pageData(res) { return (res && res.page) || {} } /** 列表 */ export function list(res) { return (res && res.list) || [] } function pad(n) { return n < 10 ? '0' + n : '' + n } /** 时间格式化:兼容时间戳数字、yyyy-MM-dd HH:mm:ss、yyyy-MM-dd */ export function fmt(value, withTime) { if (value === null || value === undefined || value === '') return '-' let d = null if (value instanceof Date) { d = value } else if (typeof value === 'number') { d = new Date(value) } else { const s = String(value) const m = s.match(/^(\d{4})-(\d{1,2})-(\d{1,2})(?:[ T](\d{1,2}):(\d{1,2})(?::(\d{1,2}))?)?/) if (m) { const base = m[1] + '-' + pad(+m[2]) + '-' + pad(+m[3]) if (withTime && m[4] !== undefined) { return base + ' ' + pad(+m[4]) + ':' + pad(+(m[5] || 0)) + ':' + pad(+(m[6] || 0)) } return base } d = new Date(s) } if (!d || isNaN(d.getTime())) return String(value) const base = d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) return withTime ? base + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds()) : base } /** 剔除空值参数:空字符串/Date 字段绑定到后端某些类型(如 Date)会触发 400,统一不发送空参数 */ export function cleanParams(obj) { const out = {} Object.keys(obj || {}).forEach(k => { if (k === 'createDate' || k === 'updateDate') return const v = obj[k] if (v === '' || v === null || v === undefined) return out[k] = v }) return out } /** 后端实体/搜索Bean中的 Date 字段默认按 "yyyy/MM/dd" 绑定,需将 "yyyy-MM-dd" 转斜杠格式 */ export function slashDate(value) { if (!value) return value const s = String(value) if (s.indexOf('-') !== -1) { return s.replace(/(\d{4})-(\d{1,2})-(\d{1,2})/, '$1/$2/$3') } return s } /** 数字格式化:去除无意义的小数位 */ export function fmtNum(value) { if (value === null || value === undefined || value === '') return '0' const n = Number(value) if (isNaN(n)) return String(value) return String(Math.round(n * 100) / 100) } /** 图片地址字符串(逗号/分号分隔)转数组 */ export function splitPictures(str) { if (!str) return [] return String(str).split(/[,;]/).map(s => s.trim()).filter(Boolean) } /** 图片数组转逗号分隔字符串 */ export function joinPictures(arr) { return (arr || []).filter(Boolean).join(',') } /** 上传图片:file 为 File 对象,返回 Promise */ export function upload(axios, file, dir) { const fd = new FormData() fd.append('file', file) if (dir) fd.append('dir', dir || '/inspect') return axios.post('/inspectManage/uploadPicture', fd, { headers: { 'Content-Type': 'multipart/form-data' } }).then(res => res.data) } /** 从 localStorage 读取当前登录用户 */ export function currentUser() { let bean = {} try { bean = JSON.parse(localStorage.getItem('userbean') || '{}') } catch (e) { /* ignore */ } return { userId: bean.userId || null, userName: bean.userName || bean.userCode || localStorage.getItem('am_username') || '' } } /* ---------------- 状态/字典文案 ---------------- */ export const PLAN_TYPE = { 1: '常规巡检', 2: '专项巡检', 3: '突击检查' } export const CYCLE_TYPE = { 1: '每日', 2: '每周', 3: '每月', 4: '单次' } export const PLAN_STATUS = { 0: { text: '草稿', type: 'info' }, 1: { text: '已发布', type: 'success' }, 2: { text: '已暂停', type: 'warning' }, 3: { text: '已结束', type: 'danger' } } export const TASK_STATUS = { 1: { text: '待执行', type: 'warning' }, 2: { text: '执行中', type: 'primary' }, 3: { text: '已提交', type: 'success' }, 4: { text: '已取消', type: 'info' } } export const RECT_STATUS = { 1: { text: '待整改', type: 'warning', color: '#E6A23C' }, 2: { text: '整改中', type: 'primary', color: '#409EFF' }, 3: { text: '待复检', type: 'primary', color: '#9B59B6' }, 4: { text: '复检通过', type: 'success', color: '#67C23A' }, 5: { text: '复检不通过', type: 'danger', color: '#F56C6C' }, 6: { text: '已超期', type: 'info', color: '#909399' }, 7: { text: '最终不通过', type: 'danger', color: '#F56C6C' } } export const ASSESS_LEVEL = { 1: { text: '优秀', type: 'success' }, 2: { text: '良好', type: 'primary' }, 3: { text: '合格', type: 'warning' }, 4: { text: '不合格', type: 'danger' } } export const INDICATOR_TYPE = { 1: '自动采集', 2: '人工巡检' } export function mapText(map, v) { const item = map[v] return item ? item.text : '-' }