优化代码
parent
8caca80e98
commit
9a89816b67
|
@ -27,7 +27,7 @@ SIMPLE_JWT_PREFIX=jwt
|
|||
|
||||
# ---- 项目配置 ----
|
||||
WEB_URL=http://im.gzydong.club
|
||||
IMG_URL=http://im-img0.gzydong.club
|
||||
IMG_URL=http://127.0.0.1:9504
|
||||
UPLOAD_PATH=/www/data/lumenim
|
||||
## 管理员邮箱
|
||||
ADMIN_EMAIL=
|
||||
|
|
|
@ -44,7 +44,7 @@ class HashIdsHelper
|
|||
* @param mixed ...$numbers
|
||||
* @return string
|
||||
*/
|
||||
public static function encode(...$numbers)
|
||||
public static function encode(...$numbers): string
|
||||
{
|
||||
return self::getHashIds()->encode(...$numbers);
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ class HashIdsHelper
|
|||
*
|
||||
* @return Hashids
|
||||
*/
|
||||
private static function getHashIds()
|
||||
private static function getHashIds(): Hashids
|
||||
{
|
||||
if (!self::$hashIds instanceof Hashids) {
|
||||
self::$hashIds = new Hashids(self::$secretKey, self::$length);
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
/**
|
||||
* Json 字符串助手
|
||||
*
|
||||
* @package App\Helpers
|
||||
*/
|
||||
class JsonHelper
|
||||
{
|
||||
/**
|
||||
* Json 字符串解析
|
||||
*
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public static function decode($value)
|
||||
{
|
||||
return json_decode($value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Json 加密
|
||||
*
|
||||
* @param $value
|
||||
* @return false|string
|
||||
*/
|
||||
public static function encode($value)
|
||||
{
|
||||
return json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This is my open source code, please do not use it for commercial applications.
|
||||
* For the full copyright and license information,
|
||||
|
@ -19,13 +20,5 @@ namespace App\Helpers;
|
|||
*/
|
||||
class StringHelper
|
||||
{
|
||||
public function jsonEncode()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function jsonDecode()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||
namespace App\Listener;
|
||||
|
||||
use App\Event\TalkEvent;
|
||||
use App\Support\MessageProducer;
|
||||
use App\Support\Message;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Event\Annotation\Listener;
|
||||
|
||||
|
@ -28,6 +28,6 @@ class TalkMessageListener implements ListenerInterface
|
|||
*/
|
||||
public function process(object $event)
|
||||
{
|
||||
MessageProducer::publish(MessageProducer::create($event->event_name, $event->data));
|
||||
Message::publish(Message::create($event->event_name, $event->data));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
/**
|
||||
* Class Mail
|
||||
|
@ -20,19 +20,34 @@ class Mail
|
|||
*/
|
||||
public function send(string $address, string $subject, string $view): bool
|
||||
{
|
||||
$config = config('mail');
|
||||
try {
|
||||
$config = config('mail');
|
||||
$mail = new PHPMailer(); // PHPMailer对象
|
||||
$mail->CharSet = 'UTF-8'; // 设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
|
||||
$mail->IsSMTP(); // 设定使用SMTP服务
|
||||
$mail->SMTPDebug = 0; // 关闭SMTP调试功能
|
||||
$mail->SMTPAuth = true; // 启用 SMTP 验证功能
|
||||
$mail->SMTPSecure = 'ssl'; // 使用安全协议
|
||||
$mail->Host = $config['host']; // SMTP 服务器
|
||||
$mail->Port = $config['port']; // SMTP服务器的端口号
|
||||
$mail->Username = $config['username']; // SMTP服务器用户名
|
||||
$mail->Password = $config['password']; // SMTP服务器密码
|
||||
$mail->SetFrom($config['from'], $config['name']); // 邮箱,昵称
|
||||
// PHPMailer对象
|
||||
$mail = new PHPMailer();
|
||||
|
||||
// 设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
|
||||
$mail->CharSet = 'UTF-8';
|
||||
|
||||
// 设定使用SMTP服务
|
||||
$mail->IsSMTP();
|
||||
|
||||
// 关闭SMTP调试功能
|
||||
$mail->SMTPDebug = 0;
|
||||
|
||||
// 启用 SMTP 验证功能
|
||||
$mail->SMTPAuth = true;
|
||||
|
||||
// 使用安全协议
|
||||
$mail->SMTPSecure = 'ssl';
|
||||
|
||||
// SMTP 服务器
|
||||
$mail->Host = $config['host'];
|
||||
$mail->Port = $config['port'];
|
||||
$mail->Username = $config['username'];
|
||||
$mail->Password = $config['password'];
|
||||
|
||||
// 邮箱,昵称
|
||||
$mail->SetFrom($config['from'], $config['name']);
|
||||
$mail->Subject = $subject;
|
||||
$mail->MsgHTML($view);
|
||||
$mail->AddAddress($address); // 收件人
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace App\Support;
|
|||
|
||||
use App\Constants\RedisSubscribeChan;
|
||||
|
||||
class MessageProducer
|
||||
class Message
|
||||
{
|
||||
/**
|
||||
* @param string $event
|
|
@ -1,168 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
/**
|
||||
* Class Packet
|
||||
*/
|
||||
class Packet
|
||||
{
|
||||
/**
|
||||
* Socket.io packet type `open`.
|
||||
*/
|
||||
const OPEN = 0;
|
||||
|
||||
/**
|
||||
* Socket.io packet type `close`.
|
||||
*/
|
||||
const CLOSE = 1;
|
||||
|
||||
/**
|
||||
* Socket.io packet type `ping`.
|
||||
*/
|
||||
const PING = 2;
|
||||
|
||||
/**
|
||||
* Socket.io packet type `pong`.
|
||||
*/
|
||||
const PONG = 3;
|
||||
|
||||
/**
|
||||
* Socket.io packet type `message`.
|
||||
*/
|
||||
const MESSAGE = 4;
|
||||
|
||||
/**
|
||||
* Socket.io packet type 'upgrade'
|
||||
*/
|
||||
const UPGRADE = 5;
|
||||
|
||||
/**
|
||||
* Socket.io packet type `noop`.
|
||||
*/
|
||||
const NOOP = 6;
|
||||
|
||||
/**
|
||||
* Engine.io packet type `connect`.
|
||||
*/
|
||||
const CONNECT = 0;
|
||||
|
||||
/**
|
||||
* Engine.io packet type `disconnect`.
|
||||
*/
|
||||
const DISCONNECT = 1;
|
||||
|
||||
/**
|
||||
* Engine.io packet type `event`.
|
||||
*/
|
||||
const EVENT = 2;
|
||||
|
||||
/**
|
||||
* Engine.io packet type `ack`.
|
||||
*/
|
||||
const ACK = 3;
|
||||
|
||||
/**
|
||||
* Engine.io packet type `error`.
|
||||
*/
|
||||
const ERROR = 4;
|
||||
|
||||
/**
|
||||
* Engine.io packet type 'binary event'
|
||||
*/
|
||||
const BINARY_EVENT = 5;
|
||||
|
||||
/**
|
||||
* Engine.io packet type `binary ack`. For acks with binary arguments.
|
||||
*/
|
||||
const BINARY_ACK = 6;
|
||||
|
||||
/**
|
||||
* Socket.io packet types.
|
||||
*/
|
||||
public static $socketTypes = [
|
||||
0 => 'OPEN',
|
||||
1 => 'CLOSE',
|
||||
2 => 'PING',
|
||||
3 => 'PONG',
|
||||
4 => 'MESSAGE',
|
||||
5 => 'UPGRADE',
|
||||
6 => 'NOOP',
|
||||
];
|
||||
|
||||
/**
|
||||
* Engine.io packet types.
|
||||
*/
|
||||
public static $engineTypes = [
|
||||
0 => 'CONNECT',
|
||||
1 => 'DISCONNECT',
|
||||
2 => 'EVENT',
|
||||
3 => 'ACK',
|
||||
4 => 'ERROR',
|
||||
5 => 'BINARY_EVENT',
|
||||
6 => 'BINARY_ACK',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get socket packet type of a raw payload.
|
||||
*
|
||||
* @param string $packet
|
||||
* @return int|null
|
||||
*/
|
||||
public static function getSocketType(string $packet)
|
||||
{
|
||||
$type = $packet[0] ?? null;
|
||||
|
||||
if (!array_key_exists($type, static::$socketTypes)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int)$type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data packet from a raw payload.
|
||||
*
|
||||
* @param string $packet
|
||||
* @return array|null
|
||||
*/
|
||||
public static function getPayload(string $packet)
|
||||
{
|
||||
$packet = trim($packet);
|
||||
$start = strpos($packet, '[');
|
||||
|
||||
if ($start === false || substr($packet, -1) !== ']') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = substr($packet, $start, strlen($packet) - $start);
|
||||
$data = json_decode($data, true);
|
||||
|
||||
if (is_null($data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'event' => $data[0],
|
||||
'data' => $data[1] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if a socket packet belongs to specific type.
|
||||
*
|
||||
* @param $packet
|
||||
* @param string $typeName
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSocketType($packet, string $typeName)
|
||||
{
|
||||
$type = array_search(strtoupper($typeName), static::$socketTypes);
|
||||
|
||||
if ($type === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return static::getSocketType($packet) === $type;
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
class SocketIOParser extends Packet
|
||||
{
|
||||
/**
|
||||
* Encode output payload for websocket push.
|
||||
*
|
||||
* @param string $event
|
||||
* @param mixed $data
|
||||
* @return mixed
|
||||
*/
|
||||
public static function encode(string $event, $data)
|
||||
{
|
||||
$packet = Packet::MESSAGE . Packet::EVENT;
|
||||
$shouldEncode = is_array($data) || is_object($data);
|
||||
$data = $shouldEncode ? json_encode($data) : $data;
|
||||
$format = $shouldEncode ? '["%s",%s]' : '["%s","%s"]';
|
||||
|
||||
return $packet . sprintf($format, $event, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode message from websocket client.
|
||||
*
|
||||
* @param string $string
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($string)
|
||||
{
|
||||
$payload = Packet::getPayload($string);
|
||||
|
||||
return [
|
||||
'event' => $payload['event'] ?? null,
|
||||
'data' => $payload['data'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue