<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/11/7
 * Time: 上午11:10
 */

namespace backend\controllers;


use backend\helpers\DateTimeHelper;
use backend\helpers\MailHelper;
use backend\helpers\RandomHelper;
use backend\models\Config;
use backend\models\Member;
use backend\models\Signin;

class LoginController extends BaseController
{
    /**
     * 登录验证方法
     * @return \yii\web\Response
     */
    public function actionLoginPost()
    {
        $data = \Yii::$app->getRequest()->post();
        $checkData = $this->checkLoginInfo($data['username'], $data['password'], $data['type'], $data['ip']);
        if ($checkData->data['code'] == 1) {
            return $this->outJson(1, $checkData->data['data'], 'LOGIN SUCCESS');
        } else {
            return $this->outJson(0, false, 'LOGIN FAILED');
        }
    }

    /**
     * @title 校验登录信息
     * @param string $username
     * @param string $password
     * @param int $type
     * @param string $ip
     * @return \yii\web\Response
     */
    private function checkLoginInfo($username = '', $password = '', $type, $ip)
    {
        if (empty($username) || empty($password)) {
            return $this->outJson(0, [], '用户名或秘密不能为空');
        } else {
            $member = new Member();
            $model = $member->findByUserName($username, $type);
            if ($type == Member::MEMBER_TYPE_IB && $model == null) {
                $model = $member->findByIbOldLoginName($username);
            }

            if (!empty($model) && ($model->password == md5($password))
                && intval($model->type) == $type && intval($model->is_enable) == 1
            ) {
                $signin = new Signin();
                $signin->member_id = $model->id;
                $signin->ip = $ip;
                $signin->in_time = DateTimeHelper::microtime_float();
                $signin->save();
                
                $model->ip = $ip;
                if ($model->save()) {
                    return $this->outJson(1, $model->getAttributes(null, ['password']), "LOGIN SUCCESS");
                }
            } else {
                return $this->outJson(0, false, 'LOGIN FAILED');
            }
        }
    }

    /**
     * 找回密码-修改密码发送验证码
     */
    public function actionPwd1()
    {
        $request = \Yii::$app->getRequest()->post();
        $username = $request['username'];
        $type = isset($request['type']) ? intval($request['type']) : 1;

        $member = new Member();
        $m = $member->findByUserName($username, $type);
        if ($m == null) {
            return $this->outJson(0, [], '用户不存在');
        }

        $code = RandomHelper::getRandomNo(6);
        $paramArray = ['code' => $code];
        $config = Config::findOne(1);
        $t = MailHelper::sendMail("找回密码验证码", $username, $paramArray, 'do.not.reply', $config->mail_code);

        if (!$t) {
            return $this->outJson(0, [], '邮件发送失败');
        }

        $m->random_code = $code;
        $m->random_code_time = DateTimeHelper::microtime_float();
        $rs = $m->save();
        if ($rs) {
            return $this->outJson(1, [$rs], '操作成功');
        }
    }

    /**
     * 找回密码-修改密码
     */
    public function actionModifyPwd1()
    {
        $request = \Yii::$app->getRequest()->post();
        $username = $request['username'];
        $password = $request['password'];
        $rePassword = $request['rePassword'];
        $code = $request['code'];
        $type = isset($request['type']) ? intval($request['type']) : 1;

        if (empty($username)) {
            return $this->outJson(0, [], '电子邮箱格式错误');
        }

        if (empty($code)) {
            return $this->outJson(0, [], '请输入邮箱验证码');
        }

        if (empty($password)) {
            return $this->outJson(0, [], '请输入新密码');
        }

        if (empty($rePassword)) {
            return $this->outJson(0, [], '请输入重复密码');
        }

        if ($password != $rePassword) {
            return $this->outJson(0, [], '2次密码不一致');
        }

        $member = new Member();
        $m = $member->findByUserName($username, $type);

        if ($m == null) {
            return $this->outJson(0, [], '用户不存在');
        }

        if ($m->random_code == null || $m->random_code_time == null) {
            return $this->outJson(0, [], '验证码错误');
        }

        if ($m->random_code != $code) {
            return $this->outJson(0, [], '验证码错误');
        }

        if ($m->random_code_time + 1800000 < DateTimeHelper::microtime_float()) {
            return $this->outJson(0, [], '验证码已过期');
        }

        $m->password = md5($password);
        $m->random_code = null;
        $m->random_code_time = null;
        $rs = $m->save();
        if ($rs) {
            return $this->outJson(1, [$rs], '修改成功');
        }
    }

    public function actionLoginByIdPassword()
    {
        $id = trim(\Yii::$app->getRequest()->post('id'));
        $password = trim(\Yii::$app->getRequest()->post('password'));
        if (empty($id)) {
            return $this->outJson(0, [], 'id不能为空');
        }
        if (empty($password)) {
            return $this->outJson(0, [], '密码不能为空');
        }
        /** @var Member $member */
        $member = Member::find()->where(['id' => $id])->limit(1)->one();
        if ($member == null) {
            return $this->outJson(0, [], '用户不存在');
        }
        if ($member['password'] !== $password) {
            return $this->outJson(0, [], '密码不正确');
        }
        return $this->outJson(1, $member->getAttributes(null, ['password']), 'OK');
    }
}
