p2 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.8 KiB

<?php
namespace app\admin\controller;
use app\admin\service\ConfigService;
use app\utility\MongoConnection;
use MongoDB\BSON\ObjectId;
use phpDocumentor\Reflection\Types\Object_;
use think\facade\Cache;
use think\facade\Filesystem;
use app\model\ConfigModel;
class Config extends AdminBaseController
{
public function setConfig()
{
try {
$param = $this->request->param();
if (empty($param['name'])) return json(['code' => '1', 'message' => '参数错误', 'data' => []]);
$name = $param['name'];
$type = $param['type'] ?? 'string';
$value = $param['value'] ?? '';
$tip = $param['tip'] ?? '';
$insertData = [
'name' => $name,
'group' => 'config',
'title' => $name,
'tip' => $tip,
'type' => $type,
'value' => $value,
];
$config = ConfigModel::where('name', $name)->find();
if (empty($config)) {
$bool = ConfigModel::insert($insertData);
if (!$bool) return json(['code' => '100500', 'message' => '设置失败', 'data' => []]);
} else {
$bool = ConfigModel::where('name', $name)->update($insertData);
if (!$bool) return json(['code' => '100500', 'message' => '设置失败', 'data' => []]);
}
return json(['code' => '0', 'message' => 'Request successful', 'data' => []]);
} catch (\Exception $exception) {
return json(['code' => '100500', 'message' => '系统繁忙', 'data' => [$exception->getMessage()]]);
}
}
public function getConfig()
{
try {
$param = $this->request->param();
if (empty($param['name'])) return json(['code' => '1', 'message' => '参数错误', 'data' => []]);
$name = $param['name'];
$value = ConfigModel::where('name', $name)->value('value');
return json(['code' => '0', 'message' => 'Request successful', 'data' => ['value' => $value]]);
} catch (\Exception $exception) {
return json(['code' => '100500', 'message' => '系统繁忙', 'data' => [$exception->getMessage()]]);
}
}
public function getConfigList()
{
$returnData = (new ConfigService())->getConfigList($this->request->param());
return json($returnData);
}
// 获取邮箱模板列表
public function emailTemplateList()
{
$returnData = (new ConfigService())->emailTemplateList($this->request->param());
return json($returnData);
}
// 编辑邮箱模板列表
public function editEmailTemplate()
{
$returnData = (new ConfigService())->editEmailTemplate($this->request->param());
return json($returnData);
}
// 短信模板列表
public function smsTemplateList()
{
$returnData = (new ConfigService())->smsTemplateList($this->request->param());
return json($returnData);
}
// 编辑邮短信板列表
public function editSmsTemplate()
{
$returnData = (new ConfigService())->editSmsTemplate($this->request->param());
return json($returnData);
}
// 获取行情数据列表
public function quoteList()
{
$params = $this->request->param();
if (!isset($params['page']) || !isset($params['limit']) || !isset($params['market_type'])) {
return json([
'code' => 400,
'message' => '缺少参数',
'data' => []
]);
}
// 检查市场类型是否有效
if ($params['market_type'] < 0) {
return json([
'code' => 400,
'message' => '市场类型无效',
'data' => []
]);
}
// 根据市场类型获取数据在mongo中对应的集合名称
if (!isset(MongoConnection::COLLECTION_ARR[$params['market_type']])) {
return json([
'code' => 400,
'message' => '没有找到对应的行情数据',
'data' => []
]);
}
$collectionName = MongoConnection::COLLECTION_ARR[$params['market_type']];
// 分页计算
$page = (int)$params['page'];
$pageSize = (int)$params['limit'];
$options = [
'skip' => ($page - 1) * $pageSize,
'limit' => $pageSize,
'sort' => ['_id' => -1], // 按创建时间倒序
];
// 构造查询条件
$filter = [];
if (!empty($params['name'])) {
// 使用正则表达式进行模糊匹配(i 表示不区分大小写)
$filter['Name'] = [
'$regex' => $params['name'],
'$options' => 'i'
];
}
if (!empty($params['code'])) {
$filter['Code'] = trim($params['code']);
}
if (!empty($params['country'])) {
$filter['Country'] = time($params['country']);
}
// 查询数据
$client = MongoConnection::getClient();
$collection = $client->selectCollection(MongoConnection::QUOTE_DATA_BASE_NAME, $collectionName);
$cursor = $collection->find($filter, $options);
$results = iterator_to_array($cursor); // 将 BSON 文档转换为数组
$total = $collection->countDocuments($filter);
$list = [];
foreach ($results as $item) {
$arr = (array)$item;
$list[] = $arr;
}
return json([
'code' => 0,
'message' => 'ok',
'data' => [
'total' => $total,
'page' => $page,
'pageSize' => $pageSize,
'list' => $list,
]
]);
}
// 行情数据置顶
public function quoteTopData()
{
$params = $this->request->param();
if (empty($params['id']) || empty($params['sort']) || empty($params['market_type'])) {
return json([
'code' => 400,
'message' => '缺少参数',
'data' => []
]);
}
// 根据市场类型获取数据在mongo中对应的集合名称
if (!isset(MongoConnection::COLLECTION_ARR[$params['market_type']])) {
return json([
'code' => 400,
'message' => '没有找到对应的行情数据',
'data' => []
]);
}
$collectionName = MongoConnection::COLLECTION_ARR[$params['market_type']];
// 根据市场类型获取置顶数据将要存储的Redis Key
if (!isset(MongoConnection::QUOTE_TOP_DATA_HASH_ARR[$params['market_type']])) {
return json([
'code' => 400,
'message' => '没有找到对应的行情数据存储KEY',
'data' => []
]);
}
$quoteTopDataKey = MongoConnection::QUOTE_TOP_DATA_HASH_ARR[$params['market_type']];
// 获取行情数据
$client = MongoConnection::getClient();
$collection = $client->selectCollection(MongoConnection::QUOTE_DATA_BASE_NAME, $collectionName);
$doc = $collection->find(['_id'=>new ObjectId($params['id'])]);
$results = iterator_to_array($doc);
// 构建缓存数据
$buildArr = [];
switch ($params['market_type']) {
case 3:
$buildArr = [
'Code' => $results[0]['Code'],
'Type' => $results[0]['Country'],
'Exchange' => $results[0]['Exchange'],
'Sort' => $params['sort'],
];
break;
case 19:
$buildArr = [
'Code' => $results[0]['symbol'], // 外汇行情用symbol字段值作为Code
'Type' => $results[0]['category'], // 外汇行情用category作为标识
'Sort' => $params['sort'],
];
break;
default:
return json([
'code' => 500,
'message' => '构建数据失败',
'data' => []
]);
}
// 检测构建数据
if (empty($buildArr)) {
return json([
'code' => 500,
'message' => '构建数据为空',
'data' => []
]);
}
$jsonStr = json_encode($buildArr);
// 根据市场类型获取Redis Key
$res = Cache::store('redis')->hSet($quoteTopDataKey, $buildArr['Code'], $jsonStr);
return json([
'code' => 0,
'message' => 'ok',
'data' => [
'cache_key' => $quoteTopDataKey,
'cache_val' => $buildArr,
'isOk' => $res
]
]);
}
}