Skip to content

Commit

Permalink
规范Session配置参数
Browse files Browse the repository at this point in the history
  • Loading branch information
kyour-cn committed Sep 14, 2021
1 parent cec044f commit 93f02a6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
34 changes: 23 additions & 11 deletions App/WebSocket/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ class Base extends Controller
/**
* Session操作
* @param string $key
* @param string $data
* @param mixed $data
* @return bool|mixed|null
* @throws \EasySwoole\Redis\Exception\RedisException
*/
public function session(string $key = SWORD_NULL, $data = SWORD_NULL)
{
$args = $this->caller()->getArgs();

$sname = config('session.sessionName');
if(empty($args[$sname])){
$name = config('session.session_name');
if(empty($args[$name])){
return false;
}

if(empty($args[$sname])) return false;
if(empty($args[$name])) return false;

$value = cache($args[$sname]);
$token = $args[$name];

$value = cache($token);
if(!$value) return false;

if($key == SWORD_NULL){
Expand All @@ -40,10 +42,10 @@ public function session(string $key = SWORD_NULL, $data = SWORD_NULL)
return $value[$key] ?? null;
}elseif($data == null){
unset($value[$key]);
cache($args[$sname], $value);
cache($token, $value);
}else{
$value[$key] = $data;
cache($args[$sname], $value);
cache($token, $value);
}
return true;
}
Expand All @@ -56,11 +58,11 @@ public function sessionId()
{
$args = $this->caller()->getArgs();

$sname = config('session.sessionName');
if(empty($args[$sname])){
$name = config('session.session_name');
if(empty($args[$name])){
return false;
}
return $args[$sname];
return $args[$name];
}

/**
Expand Down Expand Up @@ -88,8 +90,18 @@ public function withData(int $code = 0, string $msg = '', $result = [], int $cou
$ret['ES_TOKEN'] = $args['ES_TOKEN'];
}

$this->response()->setMessage(json_encode($ret));
$this->responseImmediately(json_encode($ret));
return true;
}

/**
* 获取上传数据
* @return array
*/
public function input(): array
{
$args = $this->caller()->getArgs();
return $args['data']??[];
}

}
2 changes: 1 addition & 1 deletion Config/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
return [
'enable' => true, //启用Session
'type' => 'file', //储存驱动 redis、file
'sessionName' => 'sessionId', //Session的CookieName
'session_name' => 'sessionId', //Session的CookieName
'expire' => 86400 * 7 //过期时间 s
];
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ php sword server stop

## 项目结构
```
PATH 部署目录
PATH 项目根目录
├─App 应用目录
│ ├─HttpController Http控制器
│ ├─WebSocket WebSocket控制器
Expand Down

0 comments on commit 93f02a6

Please sign in to comment.