Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
c9454cdf
Commit
c9454cdf
authored
Apr 22, 2021
by
王源
🎧
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'hotfix' into test
parents
80a40f66
7479eeb3
Changes
21
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
94 additions
and
962 deletions
+94
-962
message.php
publish/message.php
+0
-12
MakeModelCommand.php
src/Command/MakeModelCommand.php
+77
-135
RepositoryCommand.php
src/Command/RepositoryCommand.php
+0
-48
ValidatorCommand.php
src/Command/ValidatorCommand.php
+0
-45
controller.stub
src/Command/stubs/controller.stub
+1
-0
migration.stub
src/Command/stubs/migration.stub
+6
-4
repository.stub
src/Command/stubs/repository.stub
+6
-15
repositoryInterface.stub
src/Command/stubs/repositoryInterface.stub
+1
-1
validator.stub
src/Command/stubs/validator.stub
+1
-4
ConfigProvider.php
src/ConfigProvider.php
+0
-10
FileAlreadyExistsException.php
src/Generator/FileAlreadyExistsException.php
+0
-15
Generator.php
src/Generator/Generator.php
+0
-239
RepositoryEloquentGenerator.php
src/Generator/RepositoryEloquentGenerator.php
+0
-74
RepositoryInterfaceGenerator.php
src/Generator/RepositoryInterfaceGenerator.php
+0
-22
Stub.php
src/Generator/Stubs/Stub.php
+0
-152
eloquent.stub
src/Generator/Stubs/repository/eloquent.stub
+0
-32
interface.stub
src/Generator/Stubs/repository/interface.stub
+0
-23
validator.stub
src/Generator/Stubs/validator/validator.stub
+0
-24
ValidatorGenerator.php
src/Generator/ValidatorGenerator.php
+0
-22
MessageCenter.php
src/Message/MessageCenter.php
+0
-83
functions.php
src/functions.php
+2
-2
No files found.
publish/message.php
deleted
100644 → 0
View file @
80a40f66
<?php
declare
(
strict_types
=
1
);
return
[
'center'
=>
[
'ip'
=>
env
(
'MESSAGE_CENTER_IP'
,
'127.0.0.1'
),
'domain'
=>
env
(
'MESSAGE_CENTER_DOMAIN'
,
'http://localhost'
),
'proxy'
=>
env
(
'MESSAGE_CENTER_PROXY'
),
'proxy_port'
=>
env
(
'MESSAGE_CENTER_PROXY_PORT'
,
80
),
]
];
src/Command/MakeModelCommand.php
View file @
c9454cdf
This diff is collapsed.
Click to expand it.
src/Command/RepositoryCommand.php
deleted
100644 → 0
View file @
80a40f66
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/11
* Time: 11:59
*/
namespace
Meibuyu\Micro\Command
;
use
Hyperf\Command\Annotation\Command
;
use
Hyperf\Command\Command
as
HyperfCommand
;
use
Meibuyu\Micro\Generator\FileAlreadyExistsException
;
use
Meibuyu\Micro\Generator\RepositoryEloquentGenerator
;
use
Symfony\Component\Console\Input\InputArgument
;
/**
* @Command()
*/
class
RepositoryCommand
extends
HyperfCommand
{
protected
$name
=
'gen:repository'
;
/**
* Handle the current command.
*/
public
function
handle
()
{
try
{
$name
=
$this
->
input
->
getArgument
(
'name'
);
$createValidator
=
$this
->
output
->
confirm
(
'同时创建验证器?'
,
true
);
(
new
RepositoryEloquentGenerator
([
'name'
=>
$name
,
'validator'
=>
$createValidator
,
]))
->
run
();
$this
->
info
(
"Repository created successfully."
);
}
catch
(
FileAlreadyExistsException
$e
)
{
$this
->
error
(
$e
->
getMessage
()
.
' already exists!'
);
}
}
public
function
getArguments
()
{
return
[[
'name'
,
InputArgument
::
REQUIRED
,
'名称'
]];
}
}
\ No newline at end of file
src/Command/ValidatorCommand.php
deleted
100644 → 0
View file @
80a40f66
<?php
/**
* Created by PhpStorm.
* User: wangy
* Date: 2020/2/12
* Time: 10:49
*/
namespace
Meibuyu\Micro\Command
;
use
Hyperf\Command\Annotation\Command
;
use
Hyperf\Command\Command
as
HyperfCommand
;
use
Meibuyu\Micro\Generator\FileAlreadyExistsException
;
use
Meibuyu\Micro\Generator\ValidatorGenerator
;
use
Symfony\Component\Console\Input\InputArgument
;
/**
* @Command()
*/
class
ValidatorCommand
extends
HyperfCommand
{
protected
$name
=
'gen:validator'
;
/**
* Handle the current command.
*/
public
function
handle
()
{
try
{
$name
=
$this
->
input
->
getArgument
(
'name'
);
(
new
ValidatorGenerator
([
'name'
=>
$name
]))
->
run
();
$this
->
info
(
"Validator created successfully."
);
}
catch
(
FileAlreadyExistsException
$e
)
{
$this
->
error
(
$e
->
getMessage
()
.
' already exists!'
);
}
}
public
function
getArguments
()
{
return
[[
'name'
,
InputArgument
::
REQUIRED
,
'名称'
]];
}
}
\ No newline at end of file
src/Command/stubs/controller.stub
View file @
c9454cdf
...
...
@@ -21,6 +21,7 @@ use Meibuyu\Micro\Annotation\Perm;
*/
class
%
ModelClass
%
Controller
extends
AbstractController
{
/**
* @Inject()
* @var %ModelClass%Repository
...
...
src/Command/stubs/migration.stub
View file @
c9454cdf
...
...
@@ -7,20 +7,21 @@
* Description:
*/
use
Hyperf\Database\Schema\Schema
;
use
Hyperf\Database\Schema\Blueprint
;
use
Hyperf\Database\Migrations\Migration
;
use
Hyperf\Database\Schema\Blueprint
;
use
Hyperf\Database\Schema\Schema
;
use
Hyperf\DbConnection\Db
;
class
%
ClassName
%
extends
Migration
{
/**
* Run the migrations.
*/
public
function
up
()
:
void
{
Schema
::
disableForeignKeyConstraints
();
Schema
::
create
(
'%table
n
ame%'
,
function
(
Blueprint
$table
)
{
Schema
::
create
(
'%table
N
ame%'
,
function
(
Blueprint
$table
)
{
%
attributes
%
});
%
tableComment
%
...
...
@@ -33,7 +34,8 @@ class %ClassName% extends Migration
public
function
down
()
:
void
{
Schema
::
disableForeignKeyConstraints
();
Schema
::
dropIfExists
(
'%table
n
ame%'
);
Schema
::
dropIfExists
(
'%table
N
ame%'
);
Schema
::
enableForeignKeyConstraints
();
}
}
src/Command/stubs/repository.stub
View file @
c9454cdf
...
...
@@ -25,19 +25,11 @@ use Meibuyu\Micro\Repository\Eloquent\BaseRepository;
class
%
ModelClass
%
RepositoryEloquent
extends
BaseRepository
implements
%
ModelClass
%
Repository
{
/**
* %ModelClass% 模型
* @return mixed
*/
public
function
model
()
{
return
%
ModelClass
%::
class
;
}
/**
* 数据校验器
* @return mixed
*/
public
function
validator
()
{
return
%
ModelClass
%
Validator
::
class
;
...
...
@@ -47,7 +39,7 @@ class %ModelClass%RepositoryEloquent extends BaseRepository implements %ModelCla
* 获取数据列表
* @return array
*/
public
function
list
()
:
array
public
function
list
()
{
$pageSize
=
(
int
)
$this
->
request
->
input
(
'page_size'
,
DEFAULT_PAGE_SIZE
);
%
list
%
...
...
@@ -58,7 +50,7 @@ class %ModelClass%RepositoryEloquent extends BaseRepository implements %ModelCla
* @param $id
* @return array
*/
public
function
show
(
$id
)
:
array
public
function
show
(
$id
)
{
%
show
%
}
...
...
@@ -68,7 +60,7 @@ class %ModelClass%RepositoryEloquent extends BaseRepository implements %ModelCla
* @param array $attributes
* @return bool
*/
public
function
create
(
array
$attributes
)
:
bool
public
function
create
(
array
$attributes
)
{
Db
::
transaction
(
function
()
use
(
$attributes
)
{
%
create
%
...
...
@@ -82,7 +74,7 @@ class %ModelClass%RepositoryEloquent extends BaseRepository implements %ModelCla
* @param $id
* @return bool
*/
public
function
update
(
array
$attributes
,
$id
)
:
bool
public
function
update
(
array
$attributes
,
$id
)
{
Db
::
transaction
(
function
()
use
(
$attributes
,
$id
)
{
%
update
%
...
...
@@ -96,10 +88,9 @@ class %ModelClass%RepositoryEloquent extends BaseRepository implements %ModelCla
* @return bool
* @throws HttpResponseException
*/
public
function
delete
(
$id
)
:
bool
public
function
delete
(
$id
)
{
//已用外键做级联删除 和 删除验证,不需要做逻辑验证
return
$this
->
find
(
$id
)
->
delete
();
return
parent
::
delete
(
$id
);
// TODO: Change the autogenerated stub
}
%
rs
%
}
src/Command/stubs/repositoryInterface.stub
View file @
c9454cdf
src/Command/stubs/validator.stub
View file @
c9454cdf
...
...
@@ -16,6 +16,7 @@ use Meibuyu\Micro\Validator\HyperfValidator;
class
%
ModelClass
%
Validator
extends
HyperfValidator
{
protected
$rules
=
[
ValidatorInterface
::
RULE_CREATE
=>
[
%
createRules
%
...
...
@@ -29,8 +30,4 @@ class %ModelClass%Validator extends HyperfValidator
%
attributes
%
];
protected
$messages
=
[
%
messages
%
];
}
src/ConfigProvider.php
View file @
c9454cdf
...
...
@@ -28,8 +28,6 @@ class ConfigProvider
\Hyperf\ServiceGovernance\Listener\RegisterServiceListener
::
class
=>
\Meibuyu\Micro\Listener\RegisterServiceListener
::
class
,
],
'commands'
=>
[
\Meibuyu\Micro\Command\RepositoryCommand
::
class
,
\Meibuyu\Micro\Command\ValidatorCommand
::
class
,
\Meibuyu\Micro\Command\MakeModelCommand
::
class
,
],
'annotations'
=>
[
...
...
@@ -39,14 +37,6 @@ class ConfigProvider
],
],
],
'publish'
=>
[
[
'id'
=>
'message'
,
'description'
=>
'message'
,
'source'
=>
__DIR__
.
'/../publish/message.php'
,
'destination'
=>
BASE_PATH
.
'/config/autoload/message.php'
,
],
],
];
}
}
src/Generator/FileAlreadyExistsException.php
deleted
100644 → 0
View file @
80a40f66
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/11
* Time: 15:23
*/
namespace
Meibuyu\Micro\Generator
;
class
FileAlreadyExistsException
extends
\Exception
{
}
\ No newline at end of file
src/Generator/Generator.php
deleted
100644 → 0
View file @
80a40f66
<?php
declare
(
strict_types
=
1
);
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/11
* Time: 15:16
*/
namespace
Meibuyu\Micro\Generator
;
use
Hyperf\Utils\CodeGen\Project
;
use
Hyperf\Utils\Filesystem\Filesystem
;
use
Hyperf\Utils\Str
;
use
Meibuyu\Micro\Generator\Stubs\Stub
;
abstract
class
Generator
{
/**
* The filesystem instance.
*
* @var Filesystem
*/
protected
$filesystem
;
/**
* The array of options.
*
* @var array
*/
protected
$options
;
/**
* The shortname of stub.
*
* @var string
*/
protected
$stub
;
/**
* The class-specific output paths.
*
* @var string
*/
protected
$classPath
=
''
;
/**
* The file extension.
* @var string
*/
protected
$extension
=
''
;
protected
$_name
;
protected
$_namespace
;
/**
* Create new instance of this class.
*
* @param array $options
*/
public
function
__construct
(
array
$options
=
[])
{
$this
->
filesystem
=
new
Filesystem
;
$this
->
options
=
$options
;
$this
->
makeName
();
$this
->
makeNamespace
();
}
/**
* Make name.
*/
public
function
makeName
()
{
$name
=
$this
->
name
;
if
(
Str
::
contains
(
$this
->
name
,
'\\'
))
{
$name
=
str_replace
(
'\\'
,
'/'
,
$this
->
name
);
}
if
(
Str
::
contains
(
$this
->
name
,
'/'
))
{
$name
=
str_replace
(
'/'
,
'/'
,
$this
->
name
);
}
$this
->
_name
=
Str
::
studly
(
str_replace
(
' '
,
'/'
,
ucwords
(
str_replace
(
'/'
,
' '
,
$name
))));
}
/**
* Make namespace
*/
public
function
makeNamespace
()
{
$classNamespace
=
str_replace
(
'/'
,
'\\'
,
$this
->
classPath
);
$this
->
_namespace
=
'App\\'
.
$classNamespace
.
'\\'
;
}
/**
* Get extension.
*
* @return string
*/
public
function
getExtension
()
{
return
$this
->
extension
;
}
/**
* Run the generator.
*
* @return int
* @throws FileAlreadyExistsException
*/
public
function
run
()
{
$path
=
$this
->
getPath
();
if
(
$this
->
filesystem
->
exists
(
$path
))
{
throw
new
FileAlreadyExistsException
(
$path
);
}
if
(
!
$this
->
filesystem
->
isDirectory
(
$dir
=
dirname
(
$path
)))
{
$this
->
filesystem
->
makeDirectory
(
$dir
,
0777
,
true
,
true
);
}
return
$this
->
filesystem
->
put
(
$path
,
$this
->
getStub
());
}
/**
* Get destination path for generated file.
*
* @return string
*/
public
function
getPath
()
{
$project
=
new
Project
();
return
BASE_PATH
.
'/'
.
$project
->
path
(
$this
->
_namespace
.
$this
->
_name
.
$this
->
extension
);
}
/**
* Get stub template for generated file.
*
* @return string
*/
public
function
getStub
()
{
return
(
new
Stub
(
__DIR__
.
'/Stubs/'
.
$this
->
stub
.
'.stub'
,
$this
->
getReplacements
()))
->
render
();
}
/**
* Get template replacements.
*
* @return array
*/
public
function
getReplacements
()
{
return
[
'date'
=>
$this
->
getDate
(),
'time'
=>
$this
->
getTime
(),
'class'
=>
$this
->
getClass
(),
'namespace'
=>
$this
->
getNamespace
(),
];
}
public
function
getDate
()
{
return
date
(
'Y/m/d'
,
time
());
}
public
function
getTime
()
{
return
date
(
'h:i'
,
time
());
}
/**
* Get class name.
*
* @return string
*/
public
function
getClass
()
{
return
Str
::
studly
(
class_basename
(
$this
->
_name
));
}
/**
* Get class namespace.
*
* @return string
*/
public
function
getNamespace
()
{
$segments
=
explode
(
'/'
,
$this
->
_name
);
array_pop
(
$segments
);
return
'namespace '
.
rtrim
(
$this
->
_namespace
.
implode
(
$segments
,
'\\'
),
'\\'
)
.
';'
;
}
/**
* Get value from options by given key.
*
* @param string $key
* @param string|null $default
*
* @return string
*/
public
function
getOption
(
$key
,
$default
=
null
)
{
if
(
!
array_key_exists
(
$key
,
$this
->
options
))
{
return
$default
;
}
return
$this
->
options
[
$key
]
?:
$default
;
}
/**
* Helper method for "getOption".
*
* @param string $key
* @param string|null $default
*
* @return string
*/
public
function
option
(
$key
,
$default
=
null
)
{
return
$this
->
getOption
(
$key
,
$default
);
}
/**
* Handle call to __get method.
*
* @param string $key
*
* @return string|mixed
*/
public
function
__get
(
$key
)
{
if
(
property_exists
(
$this
,
$key
))
{
return
$this
->
{
$key
};
}
return
$this
->
option
(
$key
);
}
}
\ No newline at end of file
src/Generator/RepositoryEloquentGenerator.php
deleted
100644 → 0
View file @
80a40f66
<?php
/**
* Created by PhpStorm.
* User: wangy
* Date: 2020/2/12
* Time: 9:15
*/
namespace
Meibuyu\Micro\Generator
;
class
RepositoryEloquentGenerator
extends
Generator
{
protected
$stub
=
'repository/eloquent'
;
protected
$classPath
=
'Repository/Eloquent'
;
protected
$extension
=
'RepositoryEloquent'
;
/**
* @return array
* @throws FileAlreadyExistsException
*/
public
function
getReplacements
()
{
return
array_merge
(
parent
::
getReplacements
(),
[
'interface'
=>
$this
->
getInterface
(),
'validator'
=>
$this
->
getValidator
(),
'validator_method'
=>
$this
->
getValidatorMethod
(),
]);
}
/**
* @return string
* @throws FileAlreadyExistsException
*/
public
function
getInterface
()
{
$interfaceGenerator
=
new
RepositoryInterfaceGenerator
([
'name'
=>
$this
->
name
]);
$interfaceNamespace
=
$interfaceGenerator
->
_namespace
.
$interfaceGenerator
->
_name
;
$interfaceGenerator
->
run
();
return
str_replace
([
"
\\
"
,
'/'
],
'\\'
,
$interfaceNamespace
)
.
$interfaceGenerator
->
getExtension
();
}
/**
* @return string
* @throws FileAlreadyExistsException
*/
public
function
getValidator
()
{
if
(
$this
->
validator
)
{
$validatorGenerator
=
new
ValidatorGenerator
([
'name'
=>
$this
->
name
]);
$validatorNamespace
=
$validatorGenerator
->
_namespace
.
$validatorGenerator
->
_name
;
$validatorGenerator
->
run
();
return
'use '
.
str_replace
([
"
\\
"
,
'/'
],
'\\'
,
$validatorNamespace
)
.
$validatorGenerator
->
getExtension
()
.
';'
;
}
else
{
return
''
;
}
}
public
function
getValidatorMethod
()
{
if
(
$this
->
validator
)
{
$class
=
$this
->
getClass
();
return
'public function validator()'
.
PHP_EOL
.
' {'
.
PHP_EOL
.
' return '
.
$class
.
'Validator::class;'
.
PHP_EOL
.
' }'
.
PHP_EOL
;
}
else
{
return
''
;
}
}
}
\ No newline at end of file
src/Generator/RepositoryInterfaceGenerator.php
deleted
100644 → 0
View file @
80a40f66
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/11
* Time: 15:15
*/
namespace
Meibuyu\Micro\Generator
;
class
RepositoryInterfaceGenerator
extends
Generator
{
protected
$stub
=
'repository/interface'
;
protected
$classPath
=
'Repository/Interfaces'
;
protected
$extension
=
'Repository'
;
}
\ No newline at end of file
src/Generator/Stubs/Stub.php
deleted
100644 → 0
View file @
80a40f66
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/11
* Time: 16:39
*/
namespace
Meibuyu\Micro\Generator\Stubs
;
class
Stub
{
/**
* The base path of stub file.
*
* @var null|string
*/
protected
static
$basePath
=
null
;
/**
* The stub path.
*
* @var string
*/
protected
$path
;
/**
* The replacements array.
*
* @var array
*/
protected
$replaces
=
[];
/**
* The contructor.
*
* @param string $path
* @param array $replaces
*/
public
function
__construct
(
$path
,
array
$replaces
=
[])
{
$this
->
path
=
$path
;
$this
->
replaces
=
$replaces
;
}
/**
* Create new self instance.
*
* @param string $path
* @param array $replaces
*
* @return self
*/
public
static
function
create
(
$path
,
array
$replaces
=
[])
{
return
new
static
(
$path
,
$replaces
);
}
/**
* Set base path.
*
* @param string $path
*
* @return void
*/
public
static
function
setBasePath
(
$path
)
{
static
::
$basePath
=
$path
;
}
/**
* Set replacements array.
*
* @param array $replaces
*
* @return $this
*/
public
function
replace
(
array
$replaces
=
[])
{
$this
->
replaces
=
$replaces
;
return
$this
;
}
/**
* Get replacements.
*
* @return array
*/
public
function
getReplaces
()
{
return
$this
->
replaces
;
}
/**
* Handle magic method __toString.
*
* @return string
*/
public
function
__toString
()
{
return
$this
->
render
();
}
/**
* Get stub contents.
*
* @return string
*/
public
function
render
()
{
return
$this
->
getContents
();
}
/**
* Get stub contents.
*
* @return mixed|string
*/
public
function
getContents
()
{
$contents
=
file_get_contents
(
$this
->
getPath
());
foreach
(
$this
->
replaces
as
$search
=>
$replace
)
{
$contents
=
str_replace
(
'$'
.
strtoupper
(
$search
)
.
'$'
,
$replace
,
$contents
);
}
return
$contents
;
}
/**
* Get stub path.
*
* @return string
*/
public
function
getPath
()
{
return
static
::
$basePath
.
$this
->
path
;
}
/**
* Set stub path.
*
* @param string $path
*
* @return self
*/
public
function
setPath
(
$path
)
{
$this
->
path
=
$path
;
return
$this
;
}
}
\ No newline at end of file
src/Generator/Stubs/repository/eloquent.stub
deleted
100644 → 0
View file @
80a40f66
<?php
declare
(
strict_types
=
1
);
/**
* Auto generated file.
* Date: $DATE$
* Time: $TIME$
*/
$NAMESPACE
$
use
Meibuyu\Micro\Repository\Eloquent\BaseRepository
;
use
$INTERFACE
$
;
$VALIDATOR
$
/**
* Class $CLASS$RepositoryEloquent.
*
* @package $NAMESPACE$
*/
class
$CLASS$RepositoryEloquent
extends
BaseRepository
implements
$CLASS$Repository
{
public
function
model
()
{
//
}
$VALIDATOR_METHOD
$
}
src/Generator/Stubs/repository/interface.stub
deleted
100644 → 0
View file @
80a40f66
<?php
declare
(
strict_types
=
1
);
/**
* Auto generated file.
* Date: $DATE$
* Time: $TIME$
*/
$NAMESPACE
$
use
Meibuyu\Micro\Repository\Contracts\RepositoryInterface
;
/**
* Interface $CLASS$Repository.
*
* @package $NAMESPACE$
*/
interface
$CLASS$Repository
extends
RepositoryInterface
{
//
}
src/Generator/Stubs/validator/validator.stub
deleted
100644 → 0
View file @
80a40f66
<?php
declare
(
strict_types
=
1
);
/**
* Auto generated file.
* Date: $DATE$
* Time: $TIME$
*/
$NAMESPACE
$
use
Meibuyu\Micro\Validator\Contracts\ValidatorInterface
;
use
Meibuyu\Micro\Validator\HyperfValidator
;
class
$CLASS$Validator
extends
HyperfValidator
{
protected
$rules
=
[
ValidatorInterface
::
RULE_CREATE
=>
[],
ValidatorInterface
::
RULE_UPDATE
=>
[],
];
}
src/Generator/ValidatorGenerator.php
deleted
100644 → 0
View file @
80a40f66
<?php
/**
* Created by PhpStorm.
* User: wangy
* Date: 2020/2/12
* Time: 10:05
*/
namespace
Meibuyu\Micro\Generator
;
class
ValidatorGenerator
extends
Generator
{
protected
$stub
=
'validator/validator'
;
protected
$classPath
=
'Validators'
;
protected
$extension
=
'Validator'
;
}
\ No newline at end of file
src/Message/MessageCenter.php
deleted
100644 → 0
View file @
80a40f66
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/4/15
* Time: 9:19
*/
namespace
Meibuyu\Micro\Message
;
use
GuzzleHttp\Client
;
use
GuzzleHttp\HandlerStack
;
use
Hyperf\Contract\ConfigInterface
;
use
Hyperf\Guzzle\CoroutineHandler
;
use
Hyperf\Guzzle\HandlerStackFactory
;
class
MessageCenter
{
/**
* @var Client
*/
protected
$client
;
/**
* @var ConfigInterface
*/
private
$config
;
/**
* @var HandlerStackFactory
*/
protected
$stackFactory
;
public
function
__construct
(
ConfigInterface
$config
,
HandlerStackFactory
$stackFactory
)
{
$this
->
config
=
$config
;
$this
->
stackFactory
=
$stackFactory
;
$this
->
initClient
();
}
public
function
initClient
()
{
$options
=
[
'base_uri'
=>
$this
->
config
->
get
(
'message.center.domain'
)
.
'/api/'
,
'handler'
=>
HandlerStack
::
create
(
new
CoroutineHandler
()),
'timeout'
=>
60
,
];
$proxy
=
$this
->
config
->
get
(
'message.center.proxy'
);
if
(
$proxy
)
{
$options
=
array_merge
(
$options
,
[
'proxy'
=>
$proxy
,
'swoole'
=>
[
'http_proxy_port'
=>
$this
->
config
->
get
(
'message.center.proxy_port'
),
]
]);
}
$this
->
client
=
make
(
Client
::
class
,
[
'config'
=>
$options
]);
}
public
function
request
(
$type
,
$uri
,
$data
=
[])
{
return
$this
->
client
->
request
(
$type
,
$uri
,
[
'body'
=>
json_encode
(
$data
),
'headers'
=>
[
'content-type'
=>
'application/json'
]
]);
}
public
function
syncDingUser
()
{
$response
=
$this
->
client
->
request
(
'GET'
,
'synchronizationDingUser'
);
return
json_decode
(
$response
->
getBody
()
->
getContents
(),
true
);
}
public
function
sendDing
(
$userId
,
$message
)
{
$response
=
$this
->
client
->
request
(
'POST'
,
'sendDing'
,
[
'body'
=>
json_encode
([
'userId'
=>
$userId
,
'message'
=>
$message
]),
'headers'
=>
[
'content-type'
=>
'application/json'
]
]);
return
json_decode
(
$response
->
getBody
()
->
getContents
(),
true
);
}
}
\ No newline at end of file
src/functions.php
View file @
c9454cdf
...
...
@@ -442,7 +442,7 @@ if (!function_exists('putLog')) {
* @return void
* @throws
*/
function
put_log
(
$output
=
'out-mes'
,
$filename
=
''
,
$dir
=
BASE_PATH
.
'/runtime/dev/'
)
function
put_log
(
$output
=
'out-mes'
,
$filename
=
''
,
$dir
=
BASE_PATH
.
'/runtime/
logs/
dev/'
)
{
!
is_dir
(
$dir
)
&&
!
mkdir
(
$dir
,
0777
,
true
);
// 创建一个 Channel,参数 log 即为 Channel 的名字
...
...
@@ -523,7 +523,7 @@ if (!function_exists('get_collection_values')) {
$values
[]
=
$value
;
}
}
else
{
if
(
is_array
(
$value
)
||
is_countable
(
$collection
[
$t
]
))
{
if
(
is_array
(
$value
)
||
is_countable
(
$collection
))
{
foreach
(
$value
as
$vv
)
{
if
(
isset
(
$vv
[
$ik
]))
{
$values
[]
=
$vv
[
$ik
];
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment