<?php

namespace app\api\controller;

use app\api\service\TokenService;
use app\api\validate\UserValidate;
use app\BaseController;
use app\Request;
use app\model\project\User as userModel;

class User extends BaseController
{

    public function login(Request $request)
    {

        $vo = (new UserValidate())->goCheck(['mobile','password']);
        if ($vo !== true) {
            return $vo;
        }
        $data = $request->param();
        $user = userModel::where(['mobile'=>$data['mobile']])->find();
        if (!$user)
        {
            return $this->returnMsg('用户不存在',0);
        }

        $data['password'] = md5($data['password'].$user['salt']);
        if ($data['password']!=$user['password'])
        {
            return $this->returnMsg('密码不正确',0);
        }

        $token = TokenService::generateToken($user['id']);

        $update['token'] = $token;
        $update['last_login_time'] = time();
        userModel::where(['id'=>$user['id']])->update($update);

        return $this->returnMsg('操作成功',1,['token' => $token, 'expires_in' => TokenService::EXPIRE]);


    }

    public function getUserInfo(Request $request)
    {
        $vo = (new UserValidate())->goCheck(['token']);
        if ($vo !== true) {
            return $vo;
        }
        $data = $request->header();

        $is_expires = TokenService::verifyToken($data['token']);
        if ($is_expires===false)
        {
            return $this->returnMsg('token无效',0);
        }

        $user = userModel::where(['token'=>$data['token']])->find();
        if (!$user)
        {
            return $this->returnMsg('token无效',0);
        }

        return $this->returnMsg('操作成功',1,$user);
    }


    public function register(Request $request)
    {
        $vo = (new UserValidate())->goCheck();
        if ($vo !== true) {
            return $vo;
        }
        $data = $request->param();

        $is_exit = userModel::where(['mobile' => $data['mobile']])->whereOr(['username'=>$data['name']])->find();
        if ($is_exit)
        {
            return $this->returnMsg('用户已存在',0);
        }

        $user['username'] = $data['name'];
        $user['mobile'] = $data['mobile'];
        $user['salt'] = random(4);
        $user['password'] = md5($data['password'].$user['salt']);
        $user['reg_time'] = time();
        $res = Usermodel::insert($user);
        if (!$res)
        {
            return $this->returnMsg('注册失败',0);
        }


        return $this->returnMsg('注册成功',1);
    }

    public function restUserPasswordStep1(Request $request)
    {
        $vo = (new UserValidate())->goCheck(['str','code']);
        if ($vo !== true) {
            return $vo;
        }
        $data = $request->param();

        $user = userModel::where(['mobile' => $data['str']])->whereOr(['username'=>$data['str']])->find();

        if (!$user)
        {
            return $this->returnMsg('用户不存在',0);
        }

        //sms
        return $this->returnMsg('success',1);

    }

    public function restUserPasswordStep2(Request $request)
    {
        $vo = (new UserValidate())->goCheck(['str','confirm_password','password']);
        if ($vo !== true) {
            return $vo;
        }
        $data = $request->param();

        $user = userModel::where(['mobile' => $data['str']])->whereOr(['username'=>$data['str']])->find();

        if (!$user)
        {
            return $this->returnMsg('用户不存在',0);
        }

        $update['password'] = md5($data['password'].$user['salt']);
        userModel::where(['id'=>$user['id']])->update($update);

        //销毁token
        TokenService::deleteToken($user['token']);

        return $this->returnMsg('success',1);

    }



}