Commit 389de4d5 by xmy

feat:oem

parent 2c0716aa
<?php <?php
namespace Hdll\Services\Common\Config; namespace Hdll\Services\Common\Config;
use Swoft\Redis\Redis; use Swoft\Redis\Redis;
use Swoft\App; use Swoft\App;
...@@ -20,10 +22,10 @@ class CfgCenter ...@@ -20,10 +22,10 @@ class CfgCenter
* @param string $default 默认返回值 * @param string $default 默认返回值
* @return mixed * @return mixed
*/ */
public static function get($keyStr, $default='') public static function get($keyStr, $default = '')
{ {
$data = self::_get($keyStr); $data = self::_get($keyStr);
if($data == '') { if ($data == '') {
return $default; return $default;
} }
return $data; return $data;
...@@ -43,71 +45,71 @@ class CfgCenter ...@@ -43,71 +45,71 @@ class CfgCenter
* @param string $default 默认返回值 * @param string $default 默认返回值
* @return mixed * @return mixed
*/ */
public static function getArray($keyStr, $default='') public static function getArray($keyStr, $default = '')
{ {
$data = self::_get($keyStr); $data = self::_get($keyStr);
if($data == '') { if ($data == '') {
return $default; return $default;
} }
if(is_object($data)) { if (is_object($data)) {
return json_decode(json_encode($data), true); return json_decode(json_encode($data), true);
} }
return $data; return $data;
} }
private static function _get($keyStr) private static function _get($keyStr, $oemId = 0)
{ {
$keyArr = self::parseKeyStr($keyStr); $keyArr = self::parseKeyStr($keyStr);
$rkey = implode(":", $keyArr); $rkey = implode(":", $keyArr);
$redis = App::getBean(Redis::class); $redis = App::getBean(Redis::class);
$data = $redis->get("CONFIG_CENTER:".$rkey); $data = $redis->get("CONFIG_CENTER:" . $rkey);
if($data === null) { if ($data === null) {
$res = self::dbFetch($keyStr); $res = self::dbFetch($keyStr, $oemId);
if($res) { if ($res) {
if(is_object($res[1])) { if (is_object($res[1])) {
$val = json_encode($res[1]); $val = json_encode($res[1]);
} else { } else {
$val = $res[1]; $val = $res[1];
} }
$redis->set("CONFIG_CENTER:".$res[0], $val, 3600); $redis->set("CONFIG_CENTER:" . $res[0], $val, 3600);
return $res[1]; return $res[1];
} }
} }
$obj = json_decode($data); $obj = json_decode($data);
if($obj && is_object($obj)) { if ($obj && is_object($obj)) {
return $obj; return $obj;
} }
return $data; return $data;
} }
protected static function dbFetch($keyStr) protected static function dbFetch($keyStr, $oemId)
{ {
$keyArr = self::parseKeyStr($keyStr); $keyArr = self::parseKeyStr($keyStr);
$name = array_shift($keyArr); $name = array_shift($keyArr);
$dbConn = self::dbConnect(); $dbConn = self::dbConnect();
$result = $dbConn->select("config", ['name','value'], ['name'=>$name]); $result = $dbConn->select("config", ['name', 'value'], ['name' => $name, 'oemId' => $oemId]);
if(!isset($result[0]['value'])) { if (!isset($result[0]['value'])) {
return; return;
} }
$valObj = json_decode($result[0]['value']); $valObj = json_decode($result[0]['value']);
if(!is_object($valObj)) { if (!is_object($valObj)) {
return [$name, $result[0]['value']]; return [$name, $result[0]['value']];
} }
$keys = ''; $keys = '';
foreach($keyArr as $key) { foreach ($keyArr as $key) {
if(!isset($valObj->$key)) { if (!isset($valObj->$key)) {
return; return;
} }
$keys .= $key.':'; $keys .= $key . ':';
$valObj = $valObj->$key; $valObj = $valObj->$key;
} }
$rkey = $name.':'.trim($keys,':'); $rkey = $name . ':' . trim($keys, ':');
return [trim($rkey,':'), $valObj]; return [trim($rkey, ':'), $valObj];
} }
public static function dbConnect() public static function dbConnect()
{ {
if(\env('ENVIRONMENT', '') == '') { if (\env('ENVIRONMENT', '') == '') {
// 返回线上数据库连接 // 返回线上数据库连接
return new \Medoo\Medoo([ return new \Medoo\Medoo([
'database_type' => 'mysql', 'database_type' => 'mysql',
...@@ -117,7 +119,7 @@ class CfgCenter ...@@ -117,7 +119,7 @@ class CfgCenter
'password' => 'Cfgsu#2390f*_', 'password' => 'Cfgsu#2390f*_',
'charset' => 'utf8' 'charset' => 'utf8'
]); ]);
} elseif(\env('ENVIRONMENT', '') == 'pre') { } elseif (\env('ENVIRONMENT', '') == 'pre') {
// 返回测试数据库连接 // 返回测试数据库连接
return new \Medoo\Medoo([ return new \Medoo\Medoo([
'database_type' => 'mysql', 'database_type' => 'mysql',
...@@ -143,15 +145,15 @@ class CfgCenter ...@@ -143,15 +145,15 @@ class CfgCenter
protected static function parseKeyStr($keyStr) protected static function parseKeyStr($keyStr)
{ {
$keyArr = explode(">", trim($keyStr, '>')); $keyArr = explode(">", trim($keyStr, '>'));
foreach($keyArr as $k => $v) { foreach ($keyArr as $k => $v) {
$v = trim($v); $v = trim($v);
if($v == "") { if ($v == "") {
unset($keyArr[$k]); unset($keyArr[$k]);
} else { } else {
$keyArr[$k] = $v; $keyArr[$k] = $v;
} }
} }
if(empty($keyArr)) { if (empty($keyArr)) {
throw new \Exception("[CfgCenter]未指定合法的Key"); throw new \Exception("[CfgCenter]未指定合法的Key");
} }
return $keyArr; return $keyArr;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment