buildcodes.js 11 KB
import Vue from 'vue'
class Codes {
  constructor () {
    this.allTypes = ['vehicle', 'xcycle', 'pflow', 'pedestrian']
  }
  /**
   * INIT
   * 初始化所需参数
   * 返回得到的结果
   */
  getCodeList(name){
	  console.log(JSON.parse(window.localStorage.getItem(name)))
	 return JSON.parse(window.localStorage.getItem(name))
  }
  init (allData) {
    this.cate = allData.event_cate
    this.event_type = allData.event_type
    this.allData = allData
    this.result = {}
    this.result = allData
    this.classify();
	console.log(this.result)
    return this.result
  }
  // 分类
  classify () {
    if (!this.allData) {
      return ''
    }
    if (this.cate === 'traffic') {
      this.trafficCode()
    }
    if (this.cate === 'behavior') {
      this.beheiverCode()
    }
  }
  // 安防事件处理
  beheiverCode () {
    this.publicCode()
    console.log('安防事件')
  }
  // 交通事件处理
  trafficCode () {
    this.publicCode()
    if (this.event_type === 'vehicle') {
      this.vehicle()
    }
    if (this.event_type === 'xcycle' || this.event_type === 'pedestrian') {
      this.xcycle()
    }
    if (this.event_type === 'pflow') {
      this.pflow()
    }
  }
  // *根据不同event_type做不同处理*
  // 公共字段提取
  publicCode () {
    try {
      // eslint-disable-next-line camelcase
      let shoot_dt = this.timeForm(this.result.pics[0].shoot_dt, 'local').split(' ')
      this.result.shoot_date = shoot_dt[0]
      this.result.shoot_time = shoot_dt[1]
    } catch (error) {
      if (this.curType === 1) {
        // eslint-disable-next-line camelcase
        let shoot_dt = this.timeForm(this.result.event_dt, 'local').split(' ')
        this.result.shoot_date = shoot_dt[0]
        this.result.shoot_time = shoot_dt[1]
      } else {
        this.result.shoot_dt = '未识别'
      }
    }
    // 时间
    try {
      this.result.event_dt = this.allData.event_dt
    } catch (err) {
      this.result.event_dt = ''
    }
    try {
      this.result.location_speed = this.allData.event_data.speed
    } catch (err) {
      this.result.location_speed = ''
    }
  }
  // 交通
  vehicle () {
	  try {
	    if((this.allData.event_data.vehicle.body && this.allData.event_data.vehicle.body.type.special_type) || this.allData.event_data.vehicle.body.type.special_type == 0){
	      if(this.allData.event_data.vehicle.body.type.special_type == 0){
	        this.result.special_text = '正常车辆'
	      }else if(this.allData.event_data.vehicle.body.type.special_type == 1){
	         this.result.special_text = '车辆运输车'
	      }else if(this.allData.event_data.vehicle.body.type.special_type ==2 ){
	        this.result.special_text = '危险品运输车'
	      }else if(this.allData.event_data.vehicle.body.type.special_type == 3){
	        this.result.special_text = '救护车'
	      }
	    }else{
	      	this.result.special_text  = '未知'
	    }
	  } catch (error) {
	  		this.result.special_text  = '未知'
	  }
    try {
      this.result.vehicle_plate_text = this.allData.event_data.vehicle.plate.text || '未识别'
    } catch (err) {
      this.result.vehicle_plate_text = '未识别'
    }
    // 违法类型
    try {
      this.result.illegalType = parseInt(this.allData.event_data.illegal.state) === 1 ? this.getCode('违法类型', this.allData.event_data.illegal.code) : '正常'
    } catch (err) {
      this.result.illegalType = '未识别'
    }
    try {
      this.result.location_name = this.allData.event_data.location.name === '' ? '无' : this.allData.event_data.location.name
    } catch (e) {
      this.result.location_name = '未识别'
    }
    try {
      this.result.vehicle_body_type_text = this.getCode('车辆类型', this.allData.event_data.vehicle.body.type.code)
    } catch (err) {
      this.result.vehicle_body_type_text = '未识别'
    }
    try {
      this.result.vehicle_body_logo_text = this.getCode('车辆品牌', this.allData.event_data.vehicle.body.logo.code)
    } catch (err) {
      this.result.vehicle_body_logo_text = '未识别'
    }
    try {
      this.result.plate_type_text = this.getCode('号牌类型', this.allData.event_data.vehicle.plate.type_code)
    } catch (err) {
      this.result.plate_type_text = '未识别'
    }
    try {
      this.result.vehicle_body_color_text = this.getCode('车身颜色', this.allData.event_data.vehicle.body.color.code)
    } catch (err) {
      this.result.vehicle_body_color_text = '未识别'
    }
	let ary = []
	try {
		if(this.allData.event_data.RefinedFeature.rAnnualInspection) {
			ary.push('年检标志')
		}
	} catch(e) {
		console.log('年检标志无')
	}
	try {
		if(this.allData.event_data.RefinedFeature.rDecoration) {
			ary.push('摆件')
		}
	} catch(e) {
		console.log('摆件无')
	}
	try {
		if(this.allData.event_data.RefinedFeature.rPendant) {
			ary.push('吊坠')
		}
	} catch(e) {
		console.log('吊坠无')
	}
	try {
		if(this.allData.event_data.RefinedFeature.rSunshading) {
			ary.push('遮阳板')
		}
	} catch(e) {
		console.log('遮阳板无')
	}
	if(ary.length) {
		this.result.RefinedFeature_text = ary.join('')
	} else {
		this.result.RefinedFeature_text = '未识别'
	}
  }
  // 非机动车
  xcycle () {
    // 性别
    try {
      switch (this.allData.event_data.driver.face[0].sex) {
        case '1':
          this.result.driver_face_sex = '男'
          break
        case '2':
          this.result.driver_face_sex = '女'
          break
        case '0':
          this.result.driver_face_sex = '不确定'
          break
        default:
          break
      }
    } catch (err) {
      this.result.driver_face_sex = '未识别'
    }
    // 身高
    try {
      this.result.pedestrian_height = this.allData.event_data.pedestrian.height.toFixed(2)
    } catch (err) {
      this.result.pedestrian_height = '未识别'
    }
    // 是否戴帽子
    try {
      switch (this.allData.event_data.driver.face[0].with_hats) {
        case 0:
          this.result.driver_face_with_hats = '不确定'
          break
        case 1:
          this.result.driver_face_with_hats = '戴帽子'
          break
        case 2:
          this.result.driver_face_with_hats = '未戴帽'
          break
        default:
          break
      }
    } catch (err) {
      this.result.driver_face_with_hats = '未识别'
    }
    // 车辆颜色
    try {
      this.result.xcycle_color_type = this.allData.event_data.xcycle.color.name || '未识别'
    } catch (error) {
      this.result.xcycle_color_type = '未识别'
    }
	// 违法类型
	try {
	  this.result.illegalType = parseInt(this.allData.event_data.illegal.state) === 1 ? this.getCode('违法类型', this.allData.event_data.illegal.code) : '正常'
	} catch (err) {
	  this.result.illegalType = '未识别'
	}
    // 行人上身颜色
    try {
      this.result.driver_face_upbody_text = this.getCode('车身颜色', this.allData.event_data.driver.face[0].upbody.code)
    } catch (err) {
      this.result.driver_face_upbody_text = '未识别'
    }
    // 行人下身颜色
    try {
      this.result.driver_face_lobody_text = this.getCode('车身颜色', this.allData.event_data.driver.face[0].lobody.code)
    } catch (err) {
      this.result.driver_face_lobody_text = '未识别'
    }
    // 车辆品牌
    try {
      this.result.vehicle_body_logo_text = this.getCode('车辆品牌', this.result.event_data.vehicle.body.logo.code)
    } catch (err) {
      this.result.vehicle_body_logo_text = '未识别'
    }
    // 地点名称
    try {
      this.result.location_name =this.allData.event_data.location.name === '' ? '未知' : this.allData.event_data.location.name
    } catch (e) {
      this.result.location_name = '未识别'
    }
	  // 车牌号码
	try {
	  this.result.xcycle_plate_text = this.allData.event_data.xcycle.plate.text || '未识别'
	} catch (error) {
	  this.result.xcycle_plate_text = '未识别'
	}
    // 骑车类型
    try {
      switch (this.allData.event_data.xcycle_type) {
        case 'motorcycle':
          this.result.xcycle_type_text = '摩托车'
          break
        case 'bicycle':
          this.result.xcycle_type_text = '自行车'
          break
        case 'tricycle':
          this.result.xcycle_type_text = '三轮车'
          break
        default:
          break
      }
    } catch (err) {
      this.result.xcycle_type_text = '未识别'
    }
    try {
      if (this.result.xcycle_type_text === '自行车') {
        this.result.company = this.allData.event_data.xcycle.company.name
      } else if (this.result.xcycle_type_text === '摩托车') {
        if (typeof (this.allData.event_data.xcycle.company.code) !== 'number') {
          this.result.company = '未识别'
        } else {
          this.result.company = this.getCode('外卖公司', this.allData.event_data.xcycle.company.code)
        }
      } else if (this.result.xcycle_type_text === '三轮车') {
        if (typeof (this.allData.event_data.xcycle.company.code) !== 'number') {
          this.result.company = '未识别'
        } else {
          this.result.company = this.getCode('快递公司', this.allData.event_data.xcycle.company.code)
        }
      } else {
        this.result.company = '无'
      }
    } catch (error) {
      this.result.company = '未识别'
    }
  }
  // 客流
  pflow () {

  }
  /**
   * buildError
   */
  buildError () {
    throw new Error('检测字段是否存在')
  }
  getCode (codeName, code) {
    var name = window.localStorage.getItem(codeName + '-' + code)
    name = name || ''
    return name
  }
  /**
   * timerfrom
   */
  timeForm (time, str, boo) {
    var bjTime
    if (time === undefined || time === '') {
      bjTime = new Date()
    } else if (str === 'local') {
      try {
        // 兼容
        var _dayTime = time.split(' ')
        var d = _dayTime[0].split('-')
        var mi = _dayTime[_dayTime.length - 1].split(':')
        bjTime = new Date()
        // 给date赋值年月日
        bjTime.setUTCFullYear(d[0], d[1] - 1, d[2])
        // 给date赋值时分秒
        bjTime.setUTCHours(mi[0] - 8, mi[1], mi[2])
      } catch (err) {
        // console.log('转化本地时间异常:', err.message);
        bjTime = new Date(time)
      }
      if (boo) {
        bjTime = new Date(bjTime.getTime() + 28800000 - 86400000)
      } else {
        bjTime = new Date(bjTime.getTime() + 28800000)
      }
    } else if (str === 'utc') {
      if (typeof time === 'object') {
        if (boo) {
          bjTime = new Date(time.getTime() - 28800000 + 1000)
        } else {
          bjTime = new Date(time.getTime() - 28800000)
        }
      } else {
        if (boo) {
          bjTime = new Date(new Date(time).getTime() - 28800000 + 1000)
        } else {
          bjTime = new Date(new Date(time).getTime() - 28800000)
        }
      }
    } else {
      bjTime = new Date(time)
    }
    var standardTime = [
      [bjTime.getFullYear(), bjTime.getMonth() + 1, bjTime.getDate()].join('-'), [bjTime.getHours(), bjTime.getMinutes(), bjTime.getSeconds()].join(':')
    ].join(' ').replace(/(?=\b\d\b)/g, '0')
    return standardTime
  }
}

export let buildCode = new Codes()