import { loginApi } from '@services'
import tools from '@tools'
import hmacSHA256 from 'crypto-js/hmac-sha256'
import Base64 from 'crypto-js/enc-base64'

export default class LoginAction {
  /**
   * 当前登录用户，检测是否该系统有权限
   */
  getInfo () { return loginApi.getInfo().then(res => res) }
  /**
   * 调用登录接口
   */
  userLogin (obj) {
    return loginApi.userLogin(obj).then(res => {
      /* 设置cookie */
      let expires = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)
      // 翻转、加盐、base64
      const salt = '0ZSGxuBkSJS5'
      let sup = res.ticket.split('').reverse().join('')
      let rad = Math.random()
      let token = salt + sup + rad
      sup = new Buffer(token).toString('base64') // eslint-disable-line
      // yii格式加密 hmac sha256
      let serialize = `a:2:{i:0;s:4:"_SUP";i:1;s:${sup.length}:"${sup}";}`
      let hamc = Base64.stringify(hmacSHA256(serialize, 'VzpR5JMDNqUsOZ0IFQARNLU9_0KLr9UC'))
      sup = encodeURIComponent(hamc + serialize)
      // 设置到cookie
      let opts = { path: '/', domain: '.ezijing.com', expires: expires }

      tools.cookies.setCookie('_SUP', sup, opts)
      // result.data.uid = result.data.sso_id
      // callback(res) // 可以不使用callback 因为使用then
      return res
    })
  }
  /**
   * 调用退出登录接口
   */
  outLogin () {
    return loginApi.outLogin().then(res => {
      if (res.status === 200) {
        tools.cookies.clearCookie('_SUP', { path: '/', domain: '.ezijing.com', expires: '' })
        return 'login-3'
      } else {
        return 'nologin-2'
      }
    })
  }
  /**
   * 调用获取验证码
   */
  sendCode (obj) { return loginApi.sendCode(obj).then(res => res) }
  /**
   * 调用验证码登录
   */
  codeLogin (obj) {
    return loginApi.codeLogin(obj).then(res => {
      /* 设置cookie */
      let expires = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)
      // 翻转、加盐、base64
      const salt = '0ZSGxuBkSJS5'
      let sup = res.ticket.split('').reverse().join('')
      let rad = Math.random()
      let token = salt + sup + rad
      sup = new Buffer(token).toString('base64') // eslint-disable-line
      // yii格式加密 hmac sha256
      let serialize = `a:2:{i:0;s:4:"_SUP";i:1;s:${sup.length}:"${sup}";}`
      let hamc = Base64.stringify(hmacSHA256(serialize, 'VzpR5JMDNqUsOZ0IFQARNLU9_0KLr9UC'))
      sup = encodeURIComponent(hamc + serialize)
      // 设置到cookie
      let opts = { path: '/', domain: '.ezijing.com', expires: expires }

      tools.cookies.setCookie('_SUP', sup, opts)
      // result.data.uid = result.data.sso_id
      // callback(res) // 可以不使用callback 因为使用then
      return res
    })
  }
  /**
   * 调用发送重置密码验证码
   */
  sendResetPwdCode (obj) { return loginApi.sendResetPwdCode(obj).then(res => res) }
  /**
   * 调用重置密码验证码确认
   */
  validateCode (obj) { return loginApi.validateCode(obj).then(res => res) }
  /**
   * 调用重置密码
   */
  resetPwd (obj) { return loginApi.resetPwd(obj).then(res => res) }
  /**
   * 调用个人信心 - 修改密码
   */
  updatePwd (obj) { return loginApi.updatePwd(obj).then(res => res) }
  /**
   * 调用个人信息 - 上传头像
   */
  updatePic (obj) { return loginApi.updatePic(obj).then(res => res) }
}
