credit.js 3.47 KB
/**
 * Created by Tommy Huang on 18/03/21.
 */
const config = require('config-lite')({
  config_basedir: __dirname,
  config_dir: 'config'
})
const axios = require('axios')
const moment = require('moment')
const qs = require('querystring')
const uuid = require('uuid')
const Task = require('../models').Task
const User = require('../models').User
const Credit = require('../models').Credit
const TelecomReport = require('../models').TelecomReport

exports.submitTaskId = async (req, res) => {
  try {
    const id = req.user.userId
    const taskId = req.body.taskId || ''
    const userId = req.body.userId || ''
    if (!userId || !taskId) throw new Error('参数不全')
    if (id !== userId) throw new Error('token check fail')
    const task = await Task.findOne({where: {
      taskId: taskId,
      userId: userId
    }})
    if (!task) {
      const result = await Task.create({
        id: uuid(),
        taskId: taskId,
        userId: userId
      })
    }
    res.json({
      success: 0
    })
  } catch (e) {
    console.log(e)
    res.json({
      success: 0,
      msg: e.message
    })
  }
}

exports.submitBaseInfo = async (req, res) => {
  try {
    const id = req.user.userId
    const {wechatId, userId, area, address, contactAName, contactAPhone, contactARole, contactBName, contactBPhone, contactBRole} = req.body
    if (!wechatId || !userId || !area || !address || !contactAName || !contactAPhone || !contactARole || !contactBName || !contactBPhone || !contactBRole) throw new Error('参数错误')
    if (id !== userId) throw new Error('token验证失败')
    const item = {
      wechatAccount: wechatId,
      area: area,
      address: address,
      contactA: contactAName,
      contactAPhone: contactAPhone,
      contactARole: contactARole,
      contactB: contactBName,
      contactBPhone: contactBPhone,
      contactBRole: contactBRole,
      baseCreatedTime: moment().format()
    }
    const record = await Credit.findOne({where: {userId: userId}})
    if (record) {
      const update = await record.update(item)
      res.json({
        success: 1
      })
      return
    }
    const user = await User.findOne({where: {id: userId}})
    item.id = uuid()
    item.name = user.name
    item.idNo = user.idNo
    item.userId = user.id
    item.phone = user.phone
    const create = await Credit.create(item)
    res.json({
      success: 1
    })
  } catch (e) {
    console.log(e)
    res.json({
      success: 0,
      msg: e.message
    })
  }
}

exports.getStatus = async (req, res) => {
  try {
    const id = req.user.userId
    const userId = req.query.userId
    if (id !== userId) throw new Error('token验证失败')
    const [basic, telecomTask] = await Promise.all([
      Credit.findOne({where: {userId: userId}}),
      Task.findOne({
        where: {
          userId: userId,
          kind: '运营商',
          status: '成功'
        },
        order: [['created_at', 'DESC']]
      })
    ]) 
    console.log()
    let status = {
      base: {
        status: '未填写'
      },
      mobile: {
        status: '未采集'
      }
    }
    if (basic) {
      status.base.status = '已填写'
      status.base.time = moment(basic.baseCreatedTime).format('YYYY-MM-DD')
    }
    if (telecomTask) {
      status.mobile.status = '已采集'
      status.mobile.time = moment(telecomTask.updated_at).format('YYYY-MM-DD')
    }
    res.json({
      success: 1,
      status: status
    })
  } catch (e) {
    console.log(e)
    res.json({
      success: 0,
      msg: e.message
    })
  }
}