优化代码

pull/423/MERGE
Karson 2023-06-06 18:22:46 +08:00
parent ab7ebc3787
commit fdaf86ce92
18 changed files with 48 additions and 56 deletions

View File

@ -312,12 +312,12 @@ class Backend extends Controller
if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) { if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
continue; continue;
} }
$sym = isset($op[$k]) ? $op[$k] : '='; $sym = $op[$k] ?? '=';
if (stripos($k, ".") === false) { if (stripos($k, ".") === false) {
$k = $aliasName . $k; $k = $aliasName . $k;
} }
$v = !is_array($v) ? trim($v) : $v; $v = !is_array($v) ? trim($v) : $v;
$sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym); $sym = strtoupper($op[$k] ?? $sym);
//null和空字符串特殊处理 //null和空字符串特殊处理
if (!is_array($v)) { if (!is_array($v)) {
if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) { if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
@ -367,8 +367,8 @@ class Backend extends Controller
case 'NOT BETWEEN': case 'NOT BETWEEN':
$arr = array_slice(explode(',', $v), 0, 2); $arr = array_slice(explode(',', $v), 0, 2);
if (stripos($v, ',') === false || !array_filter($arr, function ($v) { if (stripos($v, ',') === false || !array_filter($arr, function ($v) {
return $v != '' && $v !== false && $v !== null; return $v != '' && $v !== false && $v !== null;
})) { })) {
continue 2; continue 2;
} }
//当出现一边为空时改变操作符 //当出现一边为空时改变操作符
@ -568,8 +568,8 @@ class Backend extends Controller
unset($item['password'], $item['salt']); unset($item['password'], $item['salt']);
if ($this->selectpageFields == '*') { if ($this->selectpageFields == '*') {
$result = [ $result = [
$primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '', $primarykey => $item[$primarykey] ?? '',
$field => isset($item[$field]) ? $item[$field] : '', $field => $item[$field] ?? '',
]; ];
} else { } else {
$result = array_intersect_key(($item instanceof Model ? $item->toArray() : (array)$item), array_flip($fields)); $result = array_intersect_key(($item instanceof Model ? $item->toArray() : (array)$item), array_flip($fields));

View File

@ -547,7 +547,7 @@ class Auth
} }
} }
foreach ($datalist as $k => &$v) { foreach ($datalist as $k => &$v) {
$v[$renderkey] = isset($list[$v[$fieldkey]]) ? $list[$v[$fieldkey]] : null; $v[$renderkey] = $list[$v[$fieldkey]] ?? null;
} }
unset($v); unset($v);
return $datalist; return $datalist;

View File

@ -60,7 +60,7 @@ class Email
} }
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
$secureArr = [0 => '', 1 => 'tls', 2 => 'ssl']; $secureArr = [0 => '', 1 => 'tls', 2 => 'ssl'];
$secure = isset($secureArr[$this->options['mail_verify_type']]) ? $secureArr[$this->options['mail_verify_type']] : ''; $secure = $secureArr[$this->options['mail_verify_type']] ?? '';
$logger = isset($this->options['debug']) && $this->options['debug'] ? new Log : null; $logger = isset($this->options['debug']) && $this->options['debug'] ? new Log : null;
$this->mail = new Mailer($logger); $this->mail = new Mailer($logger);
@ -217,8 +217,8 @@ class Email
$this->setError($e->getCode() . $e->getMessage()); $this->setError($e->getCode() . $e->getMessage());
} catch (CodeException $e) { } catch (CodeException $e) {
preg_match_all("/Expected: (\d+)\, Got: (\d+)( \| (.*))?\$/i", $e->getMessage(), $matches); preg_match_all("/Expected: (\d+)\, Got: (\d+)( \| (.*))?\$/i", $e->getMessage(), $matches);
$code = isset($matches[2][3]) ? $matches[2][3] : 0; $code = $matches[2][0] ?? 0;
$message = isset($matches[2][0]) ? $matches[4][0] : $e->getMessage(); $message = $matches[4][0] ?? $e->getMessage();
$message = mb_convert_encoding($message, 'UTF-8', 'GBK,GB2312,BIG5'); $message = mb_convert_encoding($message, 'UTF-8', 'GBK,GB2312,BIG5');
$this->setError($message); $this->setError($message);
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -28,16 +28,15 @@ class Ems
* *
* @param int $email 邮箱 * @param int $email 邮箱
* @param string $event 事件 * @param string $event 事件
* @return Ems * @return Ems|null
*/ */
public static function get($email, $event = 'default') public static function get($email, $event = 'default')
{ {
$ems = \app\common\model\Ems:: $ems = \app\common\model\Ems::where(['email' => $email, 'event' => $event])
where(['email' => $email, 'event' => $event])
->order('id', 'DESC') ->order('id', 'DESC')
->find(); ->find();
Hook::listen('ems_get', $ems, null, true); Hook::listen('ems_get', $ems, null, true);
return $ems ? $ems : null; return $ems ?: null;
} }
/** /**
@ -103,7 +102,7 @@ class Ems
}); });
} }
$result = Hook::listen('ems_notice', $params, null, true); $result = Hook::listen('ems_notice', $params, null, true);
return $result ? true : false; return (bool)$result;
} }
/** /**
@ -150,8 +149,7 @@ class Ems
*/ */
public static function flush($email, $event = 'default') public static function flush($email, $event = 'default')
{ {
\app\common\model\Ems:: \app\common\model\Ems::where(['email' => $email, 'event' => $event])
where(['email' => $email, 'event' => $event])
->delete(); ->delete();
Hook::listen('ems_flush'); Hook::listen('ems_flush');
return true; return true;

View File

@ -14,15 +14,13 @@ class Log extends AbstractLogger
/** /**
* Logs with an arbitrary level. * Logs with an arbitrary level.
* *
* @param mixed $level * @param mixed $level
* @param string $message * @param string $message
* @param mixed[] $context * @param array $context
* *
* @return void * @return void
*
* @throws \Psr\Log\InvalidArgumentException
*/ */
public function log($level, $message, array $context = array()) public function log($level, $message, array $context = [])
{ {
\think\Log::write($message); \think\Log::write($message);
} }

View File

@ -100,7 +100,7 @@ class Menu
if ($ids) { if ($ids) {
//旧版本的菜单需要做删除处理 //旧版本的菜单需要做删除处理
$config = Service::config($name); $config = Service::config($name);
$menus = isset($config['menus']) ? $config['menus'] : []; $menus = $config['menus'] ?? [];
$where = ['id' => ['in', $ids]]; $where = ['id' => ['in', $ids]];
if ($menus) { if ($menus) {
//必须是旧版本中的菜单,可排除用户自主创建的菜单 //必须是旧版本中的菜单,可排除用户自主创建的菜单
@ -184,10 +184,10 @@ class Menu
} }
$allow = array_flip(['file', 'name', 'title', 'url', 'icon', 'condition', 'remark', 'ismenu', 'menutype', 'extend', 'weigh']); $allow = array_flip(['file', 'name', 'title', 'url', 'icon', 'condition', 'remark', 'ismenu', 'menutype', 'extend', 'weigh']);
foreach ($newMenu as $k => $v) { foreach ($newMenu as $k => $v) {
$hasChild = isset($v['sublist']) && $v['sublist'] ? true : false; $hasChild = isset($v['sublist']) && $v['sublist'];
$data = array_intersect_key($v, $allow); $data = array_intersect_key($v, $allow);
$data['ismenu'] = isset($data['ismenu']) ? $data['ismenu'] : ($hasChild ? 1 : 0); $data['ismenu'] = $data['ismenu'] ?? ($hasChild ? 1 : 0);
$data['icon'] = isset($data['icon']) ? $data['icon'] : ($hasChild ? 'fa fa-list' : 'fa fa-circle-o'); $data['icon'] = $data['icon'] ?? ($hasChild ? 'fa fa-list' : 'fa fa-circle-o');
$data['pid'] = $pid; $data['pid'] = $pid;
$data['status'] = 'normal'; $data['status'] = 'normal';
if (!isset($oldMenu[$data['name']])) { if (!isset($oldMenu[$data['name']])) {

View File

@ -32,12 +32,11 @@ class Sms
*/ */
public static function get($mobile, $event = 'default') public static function get($mobile, $event = 'default')
{ {
$sms = \app\common\model\Sms:: $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
where(['mobile' => $mobile, 'event' => $event])
->order('id', 'DESC') ->order('id', 'DESC')
->find(); ->find();
Hook::listen('sms_get', $sms, null, true); Hook::listen('sms_get', $sms, null, true);
return $sms ? $sms : null; return $sms ?: null;
} }
/** /**
@ -78,7 +77,7 @@ class Sms
'template' => $template 'template' => $template
]; ];
$result = Hook::listen('sms_notice', $params, null, true); $result = Hook::listen('sms_notice', $params, null, true);
return $result ? true : false; return (bool)$result;
} }
/** /**
@ -125,8 +124,7 @@ class Sms
*/ */
public static function flush($mobile, $event = 'default') public static function flush($mobile, $event = 'default')
{ {
\app\common\model\Sms:: \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
where(['mobile' => $mobile, 'event' => $event])
->delete(); ->delete();
Hook::listen('sms_flush'); Hook::listen('sms_flush');
return true; return true;

View File

@ -150,7 +150,7 @@ class Token
/** /**
* 清除Token * 清除Token
* @access public * @access public
* @param int user_id 会员ID * @param int $user_id 会员ID
* @return boolean * @return boolean
*/ */
public static function clear($user_id = null) public static function clear($user_id = null)

View File

@ -15,7 +15,6 @@ use think\Hook;
*/ */
class Upload class Upload
{ {
protected $merging = false; protected $merging = false;
protected $chunkDir = null; protected $chunkDir = null;
@ -130,8 +129,8 @@ class Upload
if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) { if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) {
throw new UploadException(__('Uploaded file is not a valid image')); throw new UploadException(__('Uploaded file is not a valid image'));
} }
$this->fileInfo['imagewidth'] = isset($imgInfo[0]) ? $imgInfo[0] : 0; $this->fileInfo['imagewidth'] = $imgInfo[0] ?? 0;
$this->fileInfo['imageheight'] = isset($imgInfo[1]) ? $imgInfo[1] : 0; $this->fileInfo['imageheight'] = $imgInfo[1] ?? 0;
return true; return true;
} else { } else {
return !$force; return !$force;
@ -148,11 +147,13 @@ class Upload
$size = $matches ? $matches[1] : $this->config['maxsize']; $size = $matches ? $matches[1] : $this->config['maxsize'];
$type = $matches ? strtolower($matches[2]) : 'b'; $type = $matches ? strtolower($matches[2]) : 'b';
$typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3]; $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
$size = (int)($size * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0)); $size = (int)($size * pow(1024, $typeDict[$type] ?? 0));
if ($this->fileInfo['size'] > $size) { if ($this->fileInfo['size'] > $size) {
throw new UploadException(__('File is too big (%sMiB), Max filesize: %sMiB.', throw new UploadException(__(
'File is too big (%sMiB), Max filesize: %sMiB.',
round($this->fileInfo['size'] / pow(1024, 2), 2), round($this->fileInfo['size'] / pow(1024, 2), 2),
round($size / pow(1024, 2), 2))); round($size / pow(1024, 2), 2)
));
} }
} }
@ -316,7 +317,6 @@ class Upload
*/ */
public function chunk($chunkid, $chunkindex, $chunkcount, $chunkfilesize = null, $chunkfilename = null, $direct = false) public function chunk($chunkid, $chunkindex, $chunkcount, $chunkfilesize = null, $chunkfilename = null, $direct = false)
{ {
if ($this->fileInfo['type'] != 'application/octet-stream') { if ($this->fileInfo['type'] != 'application/octet-stream') {
throw new UploadException(__('Uploaded file format is limited')); throw new UploadException(__('Uploaded file format is limited'));
} }

View File

@ -22,8 +22,8 @@ class Area extends Model
{ {
$namearr = [1 => 'geo:province', 2 => 'geo:city', 3 => 'geo:district']; $namearr = [1 => 'geo:province', 2 => 'geo:city', 3 => 'geo:district'];
$rangearr = [1 => 15000, 2 => 1000, 3 => 200]; $rangearr = [1 => 15000, 2 => 1000, 3 => 200];
$geoname = isset($namearr[$level]) ? $namearr[$level] : $namearr[3]; $geoname = $namearr[$level] ?? $namearr[3];
$georange = isset($rangearr[$level]) ? $rangearr[$level] : $rangearr[3]; $georange = $rangearr[$level] ?? $rangearr[3];
// 读取范围内的ID // 读取范围内的ID
$redis = Cache::store('redis')->handler(); $redis = Cache::store('redis')->handler();
$georadiuslist = []; $georadiuslist = [];

View File

@ -50,7 +50,7 @@ class Category extends Model
{ {
$value = $value ? $value : $data['type']; $value = $value ? $value : $data['type'];
$list = $this->getTypeList(); $list = $this->getTypeList();
return isset($list[$value]) ? $list[$value] : ''; return $list[$value] ?? '';
} }
public function getFlagList() public function getFlagList()

View File

@ -115,8 +115,8 @@ class Config extends Model
$data = $result; $data = $result;
} }
$fieldarr = $valuearr = []; $fieldarr = $valuearr = [];
$field = isset($data['field']) ? $data['field'] : (isset($data['key']) ? $data['key'] : []); $field = $data['field'] ?? ($data['key'] ?? []);
$value = isset($data['value']) ? $data['value'] : []; $value = $data['value'] ?? [];
foreach ($field as $m => $n) { foreach ($field as $m => $n) {
if ($n != '') { if ($n != '') {
$fieldarr[] = $field[$m]; $fieldarr[] = $field[$m];
@ -219,7 +219,7 @@ class Config extends Model
} }
file_put_contents( file_put_contents(
CONF_PATH . 'extra' . DS . 'site.php', CONF_PATH . 'extra' . DS . 'site.php',
'<?php' . "\n\nreturn " . var_export_short($config) . ";\n" '<?php' . "\n\nreturn " . var_export($config, true) . ";\n"
); );
return true; return true;
} }

View File

@ -7,7 +7,7 @@ use think\Model;
/** /**
* 邮箱验证码 * 邮箱验证码
*/ */
class Ems Extends Model class Ems extends Model
{ {
// 开启自动写入时间戳字段 // 开启自动写入时间戳字段
@ -18,5 +18,4 @@ class Ems Extends Model
// 追加属性 // 追加属性
protected $append = [ protected $append = [
]; ];
} }

View File

@ -7,7 +7,7 @@ use think\Model;
/** /**
* 会员余额日志模型 * 会员余额日志模型
*/ */
class MoneyLog Extends Model class MoneyLog extends Model
{ {
// 表名 // 表名

View File

@ -7,7 +7,7 @@ use think\Model;
/** /**
* 会员积分日志模型 * 会员积分日志模型
*/ */
class ScoreLog Extends Model class ScoreLog extends Model
{ {
// 表名 // 表名

View File

@ -7,7 +7,7 @@ use think\Model;
/** /**
* 短信验证码 * 短信验证码
*/ */
class Sms Extends Model class Sms extends Model
{ {
// 开启自动写入时间戳字段 // 开启自动写入时间戳字段
@ -18,5 +18,4 @@ class Sms Extends Model
// 追加属性 // 追加属性
protected $append = [ protected $append = [
]; ];
} }

View File

@ -133,7 +133,7 @@ class Http
} }
$parts['query'] = isset($parts['query']) && $parts['query'] ? '?' . $parts['query'] : ''; $parts['query'] = isset($parts['query']) && $parts['query'] ? '?' . $parts['query'] : '';
//发送socket请求,获得连接句柄 //发送socket请求,获得连接句柄
$fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 3); $fp = fsockopen($parts['host'], $parts['port'] ?? 80, $errno, $errstr, 3);
if (!$fp) { if (!$fp) {
return false; return false;
} }

View File

@ -325,7 +325,7 @@ class Tree
'@url' => $childdata || !isset($value['@url']) ? "javascript:;" : $value['@url'], '@url' => $childdata || !isset($value['@url']) ? "javascript:;" : $value['@url'],
'@addtabs' => $childdata || !isset($value['@url']) ? "" : (stripos($value['@url'], "?") !== false ? "&" : "?") . "ref=addtabs", '@addtabs' => $childdata || !isset($value['@url']) ? "" : (stripos($value['@url'], "?") !== false ? "&" : "?") . "ref=addtabs",
'@caret' => ($childdata && (!isset($value['@badge']) || !$value['@badge']) ? '<i class="fa fa-angle-left"></i>' : ''), '@caret' => ($childdata && (!isset($value['@badge']) || !$value['@badge']) ? '<i class="fa fa-angle-left"></i>' : ''),
'@badge' => isset($value['@badge']) ? $value['@badge'] : '', '@badge' => $value['@badge'] ?? '',
'@class' => ($selected ? ' active' : '') . ($disabled ? ' disabled' : '') . ($childdata ? ' treeview' . (config('fastadmin.show_submenu') ? ' treeview-open' : '') : ''), '@class' => ($selected ? ' active' : '') . ($disabled ? ' disabled' : '') . ($childdata ? ' treeview' . (config('fastadmin.show_submenu') ? ' treeview-open' : '') : ''),
); );
$str .= strtr($nstr, $value); $str .= strtr($nstr, $value);
@ -422,7 +422,7 @@ class Tree
{ {
$arr = []; $arr = [];
foreach ($data as $k => $v) { foreach ($data as $k => $v) {
$childlist = isset($v['childlist']) ? $v['childlist'] : []; $childlist = $v['childlist'] ?? [];
unset($v['childlist']); unset($v['childlist']);
$v[$field] = $v['spacer'] . ' ' . $v[$field]; $v[$field] = $v['spacer'] . ' ' . $v[$field];
$v['haschild'] = $childlist ? 1 : 0; $v['haschild'] = $childlist ? 1 : 0;