<?php
// +----------------------------------------------------------------------
// | 修改个人密码
// +----------------------------------------------------------------------
// | Copyright (c) 2018 https://blog.junphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小黄牛 <1731223728@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\admin\auth;
use app\controller\SuperClass\Admin;

class Admin_pwd extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/auth/admin_pwd", method="GET|POST", title="修改个人密码")
    */
    public function run() {
        if (\x\Request::is_post()) {
            return $this->update();
        }
        return $this->display();
    }

    // 修改密码
    private function update() {
        $param = \x\Request::post();

        $vif = $this->_where($param);
        if ($vif !== true) return $vif;

        $Db = new \x\Db();
        $res = $Db->name('auth_admin')->where('id', $this->admin['id'])->update([
            'pwd' => md5($param['pwd2'].$this->admin['create_time'])
        ]);
        $Db->return();
        if ($res != false) {
            return $this->returnJson('00', '修改成功！');
        }

        return $this->returnJson('01', '修改失败！');
    }

    // 新增条件过滤
    private function _where($param=[]) {

        if (empty($param['pwd1'])) return $this->returnJson('01', '请输入当前密码');
        if (empty($param['pwd2'])) return $this->returnJson('01', '请输入新密码');
        if (empty($param['pwd3'])) return $this->returnJson('01', '请输入二次密码');
        if ($param['pwd3'] != $param['pwd2']) return $this->returnJson('01', '二次密码不正确！');

        if (md5($param['pwd1'].$this->admin['create_time']) != $this->admin['pwd']) {
            return $this->returnJson('01', '当前密码错误！');
        }
        
        return true;
    }
}

