<?php
/*
 * @Author: wang jun
 * @Date: 2022-01-18 11:12:23
 * @Last Modified by: wang jun
 * @Last Modified time: 2022-01-19 15:58:46
 * 微信类
 */
namespace app\app\logic;

use think\facade\Log;

class baselogic
{
    /**
     * 根据配置验证param
     * wj
     * 20220119
     * [[name=>'',title=>'',type=>'',regex=>"",require=>booler]]
     */
    protected function checkparam($functionname, $arr)
    {
        $param = $arr;
        $rules = $this->setrules();
        if (isset($rules[$functionname])) {
            try {
                $list = $rules[$functionname];
                $namelist = array_column($list, 'name');
                if (count($namelist) != count($list) || count($list) != count(array_filter($namelist))) {
                    throw new \Exception("规则name设置错误");
                }
                $titlelist = array_column($list, 'title');
                if (count($titlelist) != count($list) || count($list) != count(array_filter($titlelist))) {
                    throw new \Exception("规则title设置错误");
                }
                foreach ($list as $key => $value) {
                    $name = $value['name'];
                    $title = $value['title'];
                    //必填
                    if (isset($value['require']) && $value['require']) {
                        if (!isset($param[$name]) || empty($param[$name])) {
                            throw new \Exception($title . '未填');
                        }
                    }
                    if (!$value['require']) {
                        if (!isset($param[$name])) {
                            if (isset($value['default'])) {
                                $param[$name] = $value['default'];
                            }
                        }
                        continue;
                    }

                    $paramvalue = $param[$name];
                    //类型
                    if (isset($value['type'])) {
                        $tpe = $value['type'];
                        switch ($tpe) {
                            case 'string':
                                if (!is_string($paramvalue) || empty($paramvalue)) {
                                    throw new \Exception($title . '格式错误');
                                }
                                break;
                            case 'numeric':
                                if (!is_numeric($paramvalue)) {
                                    throw new \Exception($title . '格式错误');
                                }
                                break;
                            case 'array':
                                if (!is_array($paramvalue)) {
                                    throw new \Exception($title . '格式错误');
                                }
                                break;
                        }
                    }
                    //正则
                    if (isset($value['regex'])) {
                        $regex = $value['regex'];
                        if (!preg_match($regex, $paramvalue)) {
                            throw new \Exception($title . '正则格式错误');
                        }
                    }
                }
                return backarr(1, 'success', $param);
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                Log::error($msg);
                return backarr(0, "请求错误");
            }
        }
        return backarr(1, 'success', $param);
    }
    /**
     * 设置请求参数验证规则
     *
     * @return void
     */
    protected function setrules()
    {
        return [];
    }
}
