<?php /** * Created by PhpStorm. * User: zhoutao * Date: 16/8/9 * Time: 上午10:44 */ namespace Api\Controller; use Common\Common\Cache; use \Common\Controller\Api; abstract class AbstractController extends Api\AbstractController { /** 数据含义:开启 */ const OPEN = 1; /** 数据含义:关闭 */ const CLOSE = 2; /** 定义数据含义: 是 */ const MEAN_TRUE = 1; /** 定义数据含义: 否 */ const MEAN_FALSE = 2; /** * 提交的字段 [判断不断完善中... 2016-12-09 11:17:40] * 'require' => true, // 是否必填 * 'default' => '', // 默认数据 (没有则 '') * 'verify' => 'strval', // 验证方法 (没有则 null) * 'cn' => '名称', // 字段名 (比如为空时的报错提示里用) * 'maxLength' => PrizeModel::MAX_NAME_COUNT // 最长限制 * 'area' => [1, 2] // in_array 数据范围约束 * 'regexp' => '' // 正则匹配 */ protected $field = []; // 提交的数据 protected $data = []; // 使用自动获取验证方法 protected $autoGetData = false; /** * 积分配置 * @var array */ protected $integralCache = []; /** * 学分配置 * @var array */ protected $creditCache = []; /** 策略类型 mi_type0: 积分策略 mi_type1: 学分策略 */ const MI_TYPE_IS_INTEGRAL = 'mi_type0'; const MI_TYPE_IS_CREDIT = 'mi_type1'; public function before_action($action = '') { $beforeAction = parent::before_action($action); if ($this->autoGetData) { // 自动获取提交值 $this->getData(); // 校验 $this->verifyData(); } $cache = Cache::instance(); $this->integralCache = $cache->get('Common.StrategySetting'); $this->creditCache = $cache->get('Common.StrategySettingCredit'); // 灯塔系统没有是否开启积分的逻辑 zs_anything 2017-06-06 // if ($this->integralCache['eirsEnable'] != self::OPEN) { // E('_ERR_INTEGRAL_IS_NOT_OPEN'); // return false; // } return $beforeAction; } public function after_action($action = '') { return parent::after_action(); } /** * 验证数据 * @return bool */ protected function verifyData() { $error = []; foreach ($this->field as $name => $todo) { // 判断长度限制 if (isset($todo['maxLength']) && !empty($this->data[$name]) && (is_array($this->data[$name]) ? count($this->data[$name]) : mb_strlen($this->data[$name], 'utf-8')) > $todo['maxLength']) { $error['maxLength'][] = $todo['cn'] . '(' . $todo['maxLength'] . ')'; continue; } // 判断数据范围 if (isset($todo['area']) && !empty($this->data[$name]) && !in_array($this->data[$name], $todo['area'])) { $error['area'][] = $todo['cn']; continue; } // 正则验证 if (isset($todo['regexp']) && !empty($this->data[$name]) && !preg_match($todo['regexp'], $this->data[$name])) { $error['regexp'][] = $todo['cn']; continue; } } // 为了合并报错 if (!empty($error['maxLength'])) { E(L('_ERR_OVER_MAX_COUNT', ['name' => implode(',', $error['maxLength'])])); return false; } if (!empty($error['area'])) { E(L('_ERR_OVER_DATA_AREA', ['name' => implode(',', $error['area'])])); return false; } if (!empty($error['regexp'])) { E(L('_ERR_OVER_DATA_REGEXP', ['name' => implode(',', $error['regexp'])])); return false; } return true; } /** * 获取提交数据 * @return array|bool */ protected function getData() { $this->data = []; $emptyKey = []; foreach ($this->field as $name => $todo) { // 获取提交数据 $data = I('post.' . $name, !empty($todo['default']) ? $todo['default'] : '', !empty($todo['verify']) ? $todo['verify'] : null); // 验证必填项 if (empty($data) && !isset($todo['default'])) { if ($todo['require']) { $emptyKey[] = $todo['cn']; } continue; } $this->data[$name] = $data; } if (!empty($emptyKey)) { E(L('_ERR_CANNOT_EMPTY', ['name' => implode('|', $emptyKey)])); return false; } return true; } /** * 获取分页数据 * @param String $pageKey 当前页的键值名 * @param String $limitKey 每页数量的键值名 * @return array * + 开始数 * + 每页数量 */ protected function getPageOption($pageKey, $limitKey) { list($start, $perpage, $page) = page_limit($this->data[$pageKey], $this->data[$limitKey]); unset($this->data[$pageKey]); unset($this->data[$limitKey]); return [$start, $perpage, $page]; } }