const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const formatPassword = (pwd , currentPwd)  => {
  if (pwd === '') {
    wx.showToast({
      title: '密码不能为空',
      icon: 'error'
    })
    return false;
  }
  if (pwd.length < 6 || pwd.length > 10) {
    wx.showToast({
      title: '密码长度必须在6-10位',
      icon: 'none'
    })
    return false;
  }
  if (currentPwd === '') {
    wx.showToast({
      title: '请输入二次确认密码',
      icon: 'error'
    })
    return false;
  }
  if (currentPwd !== pwd) {
    wx.showToast({
      title: '两次密码不一致',
      icon: 'error'
    })
    return false;
  }
  // 纪文昊写的  密码必须包含字母和数字
  const isValid = /^[A-Za-z0-9]+$/.test(pwd); // 只能是数字和字母
  if (!isValid) {
    wx.showToast({
      title: '密码只能包含字母和数字',
      icon: 'none'
    });
    return false;
  }
  return true;
}

const notLogin = (msg) => {
  if (msg === '无权限' || msg === 'token为空' || msg === 'token已过期' || msg === '用户不存在') {
    wx.showToast({
      title: '未登录',
      icon: 'none'
    })
    setTimeout(() => {
      wx.redirectTo({
        url: '/pages/welcome/homePage/homePage',
      })
    }, 1000)
  } else {
    wx.showToast({
      title: msg,
      icon: 'none'
    });
  }
}

module.exports = {
  formatTime,
  formatPassword,
  notLogin
}
