123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?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\index\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])) {
- 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, $msg);
- }
- }
- return backarr(1, 'success', $param);
- }
- /**
- * 设置请求参数验证规则
- *
- * @return void
- */
- protected function setrules()
- {
- return [];
- }
- }
|