<?php
namespace app\index\controller;

use think\Controller;

class User extends Controller
{
    // 用户首页
    public function index()
    {
        $token = input("param.token/s");
        $res = model('Accounts')->checktoken($token);
        if($res == -1){
            return $res;
        }
        $user_id = explode('/',base64_decode($token))['2'];
        $userInfo = db('accounts')->where('id', $user_id)->find();
        //print_r($userInfo);exit;
        $this->assign([
            'userInfo' => $userInfo
        ]);

        return $this->fetch();
    }

    // 修改密码
    public function uqdatePwd()
    {
        $token = input("param.token/s");
        $res = model('Accounts')->checktoken($token);
        if($res == -1){
            return $res;
        }
        $user_id = explode('/',base64_decode($token))['2'];

        if(request()->isPost()){
            $password = input("param.password/s");
            $newPassword = input("param.newPassword/s");
            $confirmPassword = input("param.confirmPassword/s");
        }

        if(empty($password)){
            return json(['code' => -1, 'data' => '', 'msg' => '原密码不能为空']);
        }

        if(empty($newPassword)){
            return json(['code' => -2, 'data' => '', 'msg' => '新密码不能为空']);
        }

        if(empty($confirmPassword)){
            return json(['code' => -3, 'data' => '', 'msg' => '确认新密码不能为空']);
        }

        if($newPassword != $confirmPassword){
            return json(['code' => -3, 'data' => '', 'msg' => '新密码不一致']);
        }

        $userInfo = db('accounts')->where('id', $user_id)->find();

        if(md5($password . config('salt')) != $userInfo['password']){
            return json(['code' => -3, 'data' => '', 'msg' => '原密码不正确']);
        }

        $param = [
            'password' => md5($newPassword . config('salt'))
        ];

        try{
            db('accounts')->where('id', $user_id)->update($param);
        }catch(\Exception $e){
            return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
        }

        return json(['code' => 1, 'data' => url('user/index'), 'msg' => '密码修改成功']);

    }

    // 用户留言
    public function LeavingMessage()
    {

        if(request()->isPost()){
            $name = input("param.name/s");
            $email = input("param.email/s");
            $content = input("param.content/s");
            $phone = input("param.phone/s");
            $qq = input("param.qq/s");
            $wechat = input("param.wechat/s");

            $image = input("param.file/s");

            if(empty($name)){
                return json(['code' => -1, 'data' => '', 'msg' => '用户名称不能为空']);
            }

//            if(empty($email)){
//                return json(['code' => -2, 'data' => '', 'msg' => '邮箱不能为空']);
//            }

//            if(empty($qq)){
//                return json(['code' => -3, 'data' => '', 'msg' => 'QQ不能为空']);
//            }

//            if(empty($wechat)){
//                return json(['code' => -4, 'data' => '', 'msg' => '微信不能为空']);
//            }

            if(empty($content)){
                return json(['code' => -5, 'data' => '', 'msg' => '内容不能为空']);
            }

            if(empty($phone)){
                return json(['code' => -6, 'data' => '', 'msg' => '电话不能为空']);
            }

//            if(empty($image)){
//                return json(['code' => -7, 'data' => '', 'msg' => '附件不能为空']);
//            }

            $param = [
                'name' => $name,
                'email' => $email,
                'content' => $content,
                'image' => $image,
                'phone' => $phone,
                'qq' => $qq,
                'wechat' => $wechat,
                'message_status' => 0,
                'add_time' => time()
            ];

            try{
                db('accountsmessage')->insertGetId($param);
            }catch(\Exception $e){
                return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => url('user/index'), 'msg' => '留言成功']);
        }

    }

    // 上传图片
    public function uplodeImg()
    {
        if(request()->isPost()){
            $file = request()->file('file');

            if(empty($file)){
                return json(['code' => -7, 'data' => '', 'msg' => '附件不能为空']);
            }
            $fileInfo = $file->getInfo();
            /*if($fileInfo['size'] > 1024 * 1024 * 2){
                // 上传失败获取错误信息
                return json( ['code' => -8, 'data' => '', 'msg' => '文件超过2M'] );
            }*/

            //检测图片格式
            $ext = explode('.', $fileInfo['name']);
            $ext = array_pop($ext);

            $extArr = explode('|', 'jpg|png|gif|jpeg');
            if(!in_array($ext, $extArr)){
                return json(['code' => -9, 'data' => '', 'msg' => '只能上传jpg|png|gif|jpeg的文件']);
            }

            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $src =  '/uploads' . '/' . date('Ymd') . '/' . $info->getFilename();
            }else{
                // 上传失败获取错误信息
                return json(['code' => -10, 'data' => '', 'msg' => $file->getError()]);
            }
        }
    }

}
