<template>
  <div class="login">
    <div class="login-hd">
      <div class="logo" title="紫荆教育"></div>
      <h2 class="login-title">欢迎进入紫荆校友会</h2>
    </div>
    <div class="login-bd">
      <div class="login-form">
        <div class="login-form__item">
          <input
            type="text"
            class="login-input"
            placeholder="手机/邮箱/用户名"
            v-model="ruleForm.account"
          />
        </div>
        <div class="login-form__item">
          <input
            type="password"
            class="login-input"
            placeholder="密码"
            v-model="ruleForm.password"
            @keyup.enter="onSubmit"
          />
        </div>
        <div class="login-form__item">
          <van-checkbox v-model="ruleForm.RememberMe" shape="square"
            >下次自动登录</van-checkbox
          >
          <!-- <div class="login-type">验证码登录</div> -->
        </div>
        <div class="login-form__item">
          <div class="login-button" @click="onSubmit">登录</div>
        </div>
      </div>
    </div>
    <div class="login-ft">
      <p class="login-tips">
        登录遇到困难？请点击
        <span class="forget-password" @click="passwordVisible = true"
          >找回密码</span
        >
      </p>
    </div>
    <password v-model="passwordVisible" />
  </div>
</template>
<script>
import Cookies from 'js-cookie'

import * as api from '@/api/account'
// import AccountLogin from './AccountLogin.vue'
// import CodeLogin from './CodeLogin.vue'
import Password from './Password.vue'

export default {
  components: { Password },
  data() {
    const UA = navigator.userAgent.toLowerCase()
    return {
      ruleForm: {
        type: 1,
        account: '',
        password: '',
        RememberMe: false
      },
      passwordVisible: false,
      isWechat: /micromessenger/.test(UA),
      unionid: Cookies.get('wechat_login_no_phone_error')
    }
  },
  computed: {
    // 重定向地址
    redirectURI() {
      const { query } = this.$route
      return query.redirect_uri ? decodeURIComponent(query.redirect_uri) : ''
    }
  },
  methods: {
    // 提交
    onSubmit() {
      if (!this.ruleForm.account) {
        this.$notify('请输入手机/邮箱/用户名')
      } else if (!this.ruleForm.password) {
        this.$notify('请输入密码')
      } else {
        this.unionid ? this.bindWechat() : this.loginRequest()
      }
    },
    // 登录
    loginRequest() {
      let data = Object.assign({}, this.ruleForm, {
        service: 'https://h5.ezijing.com'
      })
      api
        .login(data)
        .then(response => {
          this.loginSuccess(response)
        })
        .catch(error => {
          error.response && this.$notify(error.response.data.message)
        })
    },
    // 绑定微信并登录
    bindWechat() {
      let data = Object.assign({}, this.ruleForm, { unionid: this.unionid })
      api
        .bindWechat(data)
        .then(response => {
          this.loginSuccess(response)
        })
        .catch(error => {
          error.response && this.$notify(error.response.data.message)
        })
    },
    // 登录成功
    loginSuccess(response) {
      if (response.code === 0) {
        Cookies.remove('wechat_login_error', { domain: '.ezijing.com' })
        Cookies.remove('wechat_login_no_phone_error', {
          domain: '.ezijing.com'
        })
        if (this.redirectURI) {
          window.location.href = this.redirectURI
        } else {
          this.$router.replace('/')
        }
        // this.$router.replace('/')
      } else {
        this.$notify(response.msg)
      }
    },
    wechatLogin() {
      const appId = 'wx451c01d40d090d7a'
      // 回调地址
      const redirectURI = `${
        process.env.VUE_APP_WECHAT_REDIRECT_URL
      }?needCheck=false&identity=friend&redirectUrl=${this.redirectURI ||
        location.origin}`
      // 微信的地址
      const wechatUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${encodeURIComponent(
        redirectURI
      )}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
      // 打开地址
      location.href = wechatUrl
    },
    checkWechatLogin() {
      return !!(
        Cookies.get('wechat_login_error') ||
        Cookies.get('wechat_login_no_phone_error')
      )
    }
  },
  created() {
    if (this.isWechat && !this.checkWechatLogin()) {
      this.wechatLogin()
    }
  }
}
</script>
<style lang="scss">
.login {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.logo {
  width: 136px;
  height: 44px;
  background: url('~@/assets/img/logo.png') no-repeat;
  background-size: contain;
}
.login-hd {
  min-height: 170px;
  padding: 30px 0 0 30px;
  background: url('~@/assets/img/login_bg.png') no-repeat right top;
  background-size: 152px 170px;
}
.login-title {
  margin-top: 6px;
  font-size: 30px;
  color: #333;
}
.login-bd {
  flex: 1;
}
.login-form {
  padding: 40px 0 90px;
  overflow: hidden;
}
.login-form__item {
  padding: 0 30px;
}
.login-input {
  padding: 0 20px;
  width: 100%;
  height: 40px;
  font-size: 15px;
  color: #999;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0px 5px 15px 0px rgba(0, 0, 0, 0.06);
  border-radius: 20px;
  box-sizing: border-box;
  margin-bottom: 30px;
}
.login-checkbox {
  width: 15px;
  height: 15px;
  border: 1px solid rgba(153, 153, 153, 1);
}
.login-button {
  float: right;
  height: 60px;
  padding-right: 90px;
  font-size: 15px;
  line-height: 60px;
  background: url('~@/assets/img/login_button.png') no-repeat right center;
  background-size: contain;
  cursor: pointer;
}
.login-ft {
  padding: 40px 0;
}
.login-tips {
  text-align: center;
  font-size: 12px;
  color: #999;
  .forget-password {
    color: #1989fa;
  }
}
</style>
