<?php
namespace app\admin\controller;

use Webman\Http\Request;

class BaseController
{
    protected $request;
    protected $validate;

    public function beforeAction(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Notes:获取分页请求参数
     * @return array
     * User: yym
     * Date: 2022/7/27
     */
    public function getPage()
    {
        $data  = $this->request->only(['page', 'limit']);
        $page  = 1;
        $limit = 10;
        if(isset($data['limit']) && !empty($data['limit']))
        {
            $limit = (int)$data['limit'];
        }
        if(isset($data['page']) && !empty($data['page']))
        {
            $page = (int)$data['page'];
        }
        return [(int)$page, (int)$limit];
    }

    /**
     * Notes:验证器简化处理
     * @param $validate
     * @param $data
     * @param string $scene
     * User: yym
     * Date: 2022/7/28
     */
    public function validateCheck($validate, $data, $scene = '')
    {
        $classname = 'app\\admin\\validate\\' . $validate;
        $model = new $classname;
        if(!empty($scene) && $model->hasScene($scene))
        {
            $model->scene($scene)->check($data);
        }else{
            $model->check($data);
        }
    }

    /**
     * Notes:密码加密随机字符串
     * @param $validate
     * @param $data
     * @param string $scene
     * User: ZQ
     * Date: 2022/9/27
     */
    public function passwordSalf($length){
        $string = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
            't', 'u', 'v', 'w', 'x', 'y','z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        // 在 $str 中随机取 $length 个数组元素键名
        $keys = array_rand($string, $length);
        $password = '';
        for($i = 0; $i < $length; $i++)
        {
            // 将 $length 个数组元素连接成字符串
            $password .= $string[$keys[$i]];
        }
        return $password;
    }

}
