<?php

namespace app\user\controller;

use think\Controller;
use think\Lang;
use think\Validate;

class Updatepwd extends UserControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/login.lang.php');
    }

    /**
     * 修改密码
     * @return mixed
     */
    public function index()
    {
        $user_info = $this->getAdminInfo();
        $user = db('user')->where('user_id',$user_info['user_id'])->find();
        if (request()->isPost()) {
            $password = input('post.password');
            $new_password = input('post.new_password');
            $confirm_password = input('post.confirm_password');

            // 原密码 验证
            if(md5($password) != $user['user_password']){
                //验证失败
                $this->error('原密码输入不正确');
            }

            // 新密码 验证
            if($new_password != $confirm_password){
                //验证失败
                $this->error('新密码不一致');
            }

            $update_info = array(
                'user_password' => md5($new_password)
            );
            $res = db('user')->where('user_id', $user_info['user_id'])->update($update_info);
            if($res == 1){
                return $this->redirect('User/Member/index');
            }

        }else{
            $user_info = $this->getAdminInfo();
            $user = db('user')->where('user_id',$user_info['user_id'])->find();
            return $this->fetch();
        }
    }

    /**
     * 修改用户名
     * @return mixed
     */
    public function username()
    {
        return $this->fetch();
    }

    /**
     * 修改电话
     * @return mixed
     */
    public function phone()
    {
        $user_info = $this->getAdminInfo();
        $user = db('user')->where('user_id',$user_info['user_id'])->find();
        $this->assign('user', $user);
        return $this->fetch();
    }

}


?>
