common.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * 门店巡检与考核模块通用工具方法。
  3. */
  4. import querystring from 'querystring'
  5. /** 发送 POST 表单请求,返回 res.data */
  6. export function post(axios, url, data, config) {
  7. return axios.post(url, querystring.stringify(data || {}), config).then(res => res.data)
  8. }
  9. /** 是否成功(rtnCode === 0) */
  10. export function ok(res) {
  11. return !!(res && res.rtnBean && String(res.rtnBean.rtnCode) === '0')
  12. }
  13. /** 后端返回的提示信息 */
  14. export function msg(res) {
  15. return (res && res.rtnBean && res.rtnBean.rtnMsg) || ''
  16. }
  17. /** 分页对象 */
  18. export function pageData(res) {
  19. return (res && res.page) || {}
  20. }
  21. /** 列表 */
  22. export function list(res) {
  23. return (res && res.list) || []
  24. }
  25. function pad(n) {
  26. return n < 10 ? '0' + n : '' + n
  27. }
  28. /** 时间格式化:兼容时间戳数字、yyyy-MM-dd HH:mm:ss、yyyy-MM-dd */
  29. export function fmt(value, withTime) {
  30. if (value === null || value === undefined || value === '') return '-'
  31. let d = null
  32. if (value instanceof Date) {
  33. d = value
  34. } else if (typeof value === 'number') {
  35. d = new Date(value)
  36. } else {
  37. const s = String(value)
  38. const m = s.match(/^(\d{4})-(\d{1,2})-(\d{1,2})(?:[ T](\d{1,2}):(\d{1,2})(?::(\d{1,2}))?)?/)
  39. if (m) {
  40. const base = m[1] + '-' + pad(+m[2]) + '-' + pad(+m[3])
  41. if (withTime && m[4] !== undefined) {
  42. return base + ' ' + pad(+m[4]) + ':' + pad(+(m[5] || 0)) + ':' + pad(+(m[6] || 0))
  43. }
  44. return base
  45. }
  46. d = new Date(s)
  47. }
  48. if (!d || isNaN(d.getTime())) return String(value)
  49. const base = d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate())
  50. return withTime
  51. ? base + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds())
  52. : base
  53. }
  54. /** 剔除空值参数:空字符串/Date 字段绑定到后端某些类型(如 Date)会触发 400,统一不发送空参数 */
  55. export function cleanParams(obj) {
  56. const out = {}
  57. Object.keys(obj || {}).forEach(k => {
  58. if (k === 'createDate' || k === 'updateDate') return
  59. const v = obj[k]
  60. if (v === '' || v === null || v === undefined) return
  61. out[k] = v
  62. })
  63. return out
  64. }
  65. /** 后端实体/搜索Bean中的 Date 字段默认按 "yyyy/MM/dd" 绑定,需将 "yyyy-MM-dd" 转斜杠格式 */
  66. export function slashDate(value) {
  67. if (!value) return value
  68. const s = String(value)
  69. if (s.indexOf('-') !== -1) {
  70. return s.replace(/(\d{4})-(\d{1,2})-(\d{1,2})/, '$1/$2/$3')
  71. }
  72. return s
  73. }
  74. /** 数字格式化:去除无意义的小数位 */
  75. export function fmtNum(value) {
  76. if (value === null || value === undefined || value === '') return '0'
  77. const n = Number(value)
  78. if (isNaN(n)) return String(value)
  79. return String(Math.round(n * 100) / 100)
  80. }
  81. /** 图片地址字符串(逗号/分号分隔)转数组 */
  82. export function splitPictures(str) {
  83. if (!str) return []
  84. return String(str).split(/[,;]/).map(s => s.trim()).filter(Boolean)
  85. }
  86. /** 图片数组转逗号分隔字符串 */
  87. export function joinPictures(arr) {
  88. return (arr || []).filter(Boolean).join(',')
  89. }
  90. /** 上传图片:file 为 File 对象,返回 Promise<res.data> */
  91. export function upload(axios, file, dir) {
  92. const fd = new FormData()
  93. fd.append('file', file)
  94. if (dir) fd.append('dir', dir || '/inspect')
  95. return axios.post('/inspectManage/uploadPicture', fd, {
  96. headers: { 'Content-Type': 'multipart/form-data' }
  97. }).then(res => res.data)
  98. }
  99. /** 从 localStorage 读取当前登录用户 */
  100. export function currentUser() {
  101. let bean = {}
  102. try {
  103. bean = JSON.parse(localStorage.getItem('userbean') || '{}')
  104. } catch (e) { /* ignore */ }
  105. return {
  106. userId: bean.userId || null,
  107. userName: bean.userName || bean.userCode || localStorage.getItem('am_username') || ''
  108. }
  109. }
  110. /* ---------------- 状态/字典文案 ---------------- */
  111. export const PLAN_TYPE = { 1: '常规巡检', 2: '专项巡检', 3: '突击检查' }
  112. export const CYCLE_TYPE = { 1: '每日', 2: '每周', 3: '每月', 4: '单次' }
  113. export const PLAN_STATUS = {
  114. 0: { text: '草稿', type: 'info' },
  115. 1: { text: '已发布', type: 'success' },
  116. 2: { text: '已暂停', type: 'warning' },
  117. 3: { text: '已结束', type: 'danger' }
  118. }
  119. export const TASK_STATUS = {
  120. 1: { text: '待执行', type: 'warning' },
  121. 2: { text: '执行中', type: 'primary' },
  122. 3: { text: '已提交', type: 'success' },
  123. 4: { text: '已取消', type: 'info' }
  124. }
  125. export const RECT_STATUS = {
  126. 1: { text: '待整改', type: 'warning', color: '#E6A23C' },
  127. 2: { text: '整改中', type: 'primary', color: '#409EFF' },
  128. 3: { text: '待复检', type: 'primary', color: '#9B59B6' },
  129. 4: { text: '复检通过', type: 'success', color: '#67C23A' },
  130. 5: { text: '复检不通过', type: 'danger', color: '#F56C6C' },
  131. 6: { text: '已超期', type: 'info', color: '#909399' },
  132. 7: { text: '最终不通过', type: 'danger', color: '#F56C6C' }
  133. }
  134. export const ASSESS_LEVEL = {
  135. 1: { text: '优秀', type: 'success' },
  136. 2: { text: '良好', type: 'primary' },
  137. 3: { text: '合格', type: 'warning' },
  138. 4: { text: '不合格', type: 'danger' }
  139. }
  140. export const INDICATOR_TYPE = { 1: '自动采集', 2: '人工巡检' }
  141. export function mapText(map, v) {
  142. const item = map[v]
  143. return item ? item.text : '-'
  144. }