51 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
import moment from "moment";
 | 
						|
const isDev = process.env.NODE_ENV === 'development'
 | 
						|
// 打印日志
 | 
						|
export const httpLog = function (logName, { method, url, params, data }, response, error) {
 | 
						|
  const logColor = response ? '#0ad554' : '#FF0000'
 | 
						|
  const logLevel = parseInt(localStorage.getItem('loglevel') || '-1')
 | 
						|
  if (logLevel >= 0) {
 | 
						|
    const time = moment().format('YYYY-MM-DD HH:mm:ss')
 | 
						|
    console.groupCollapsed(`%c [DEBUG] HTTP: ${time}-${method.toUpperCase()}@${url}@${logName || ''}`,
 | 
						|
      `color:  #fff;background-color: ${logColor};`
 | 
						|
    )
 | 
						|
    if (data) {
 | 
						|
      console.log('   query: %o', data)
 | 
						|
    }
 | 
						|
    if (params) {
 | 
						|
      console.log('   query: %o', params)
 | 
						|
    }
 | 
						|
    if (response) {
 | 
						|
      console.log('response: %o', response)
 | 
						|
    }
 | 
						|
    if (error) {
 | 
						|
      console.log('   error: %o', error)
 | 
						|
    }
 | 
						|
    console.groupEnd()
 | 
						|
  }
 | 
						|
  if (error) throw error
 | 
						|
  if (response) return response
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * 为服务支持日志功能
 | 
						|
 * @param constructor
 | 
						|
 * @return {{new(): {httpLog: *}, prototype: {httpLog: *}}}
 | 
						|
 * @constructor
 | 
						|
 */
 | 
						|
export const SupportLog = function (constructor) {
 | 
						|
  return class extends constructor {
 | 
						|
    log = httpLog
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
export const Log = function (logName) {
 | 
						|
  return function (target, propertyKey, descriptor) {
 | 
						|
    const method = descriptor.value || target[propertyKey]
 | 
						|
    descriptor.value = function (params = {}, options = {}) {
 | 
						|
      return method && method.apply(this, [params, options, logName])
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 |